| 1 |
package com.paymentlink.controller.api; |
| 2 |
|
| 3 |
import com.paymentlink.model.dto.CartItemDto; |
| 4 |
import com.paymentlink.model.dto.CustomerInfoDto; |
| 5 |
import com.paymentlink.model.dto.ShippingInfoDto; |
| 6 |
import com.paymentlink.model.entity.Order; |
| 7 |
import com.paymentlink.model.entity.OrderItem; |
| 8 |
import com.paymentlink.service.OrderService; |
| 9 |
import org.springframework.http.HttpStatus; |
| 10 |
import org.springframework.http.ResponseEntity; |
| 11 |
import org.springframework.web.bind.annotation.*; |
| 12 |
|
| 13 |
import java.util.HashMap; |
| 14 |
import java.util.List; |
| 15 |
import java.util.Map; |
| 16 |
|
| 17 |
@RestController |
| 18 |
@RequestMapping("/api/orders") |
| 19 |
public class OrderApiController { |
| 20 |
|
| 21 |
private final OrderService orderService; |
| 22 |
|
| 23 |
public OrderApiController(OrderService orderService) { |
| 24 |
this.orderService = orderService; |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
* GET /api/orders - Get all orders (admin) |
| 29 |
*/ |
| 30 |
@GetMapping |
| 31 |
public ResponseEntity<Map<String, Object>> getAllOrders() { |
| 32 |
try { |
| 33 |
List<Order> orders = orderService.getAllOrders(); |
| 34 |
|
| 35 |
Map<String, Object> response = new HashMap<>(); |
| 36 |
response.put("success", true); |
| 37 |
response.put("orders", orders); |
| 38 |
return ResponseEntity.ok(response); |
| 39 |
|
| 40 |
} catch (Exception e) { |
| 41 |
Map<String, Object> error = new HashMap<>(); |
| 42 |
error.put("success", false); |
| 43 |
error.put("error", e.getMessage()); |
| 44 |
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
* POST /api/orders - Create order |
| 50 |
*/ |
| 51 |
@PostMapping |
| 52 |
public ResponseEntity<Map<String, Object>> createOrder( |
| 53 |
@RequestHeader(value = "x-session-id", required = false) String sessionId, |
| 54 |
@RequestBody Map<String, Object> request) { |
| 55 |
|
| 56 |
try { |
| 57 |
|
| 58 |
@SuppressWarnings("unchecked") |
| 59 |
List<Map<String, Object>> itemsData = (List<Map<String, Object>>) request.get("items"); |
| 60 |
@SuppressWarnings("unchecked") |
| 61 |
Map<String, String> customerData = (Map<String, String>) request.get("customerInfo"); |
| 62 |
@SuppressWarnings("unchecked") |
| 63 |
Map<String, String> shippingData = (Map<String, String>) request.get("shippingInfo"); |
| 64 |
|
| 65 |
|
| 66 |
List<OrderItem> items = itemsData.stream().map(item -> { |
| 67 |
OrderItem orderItem = new OrderItem(); |
| 68 |
orderItem.setProductId(toLong(item.get("productId"))); |
| 69 |
orderItem.setQuantity(toInteger(item.get("quantity"))); |
| 70 |
return orderItem; |
| 71 |
}).toList(); |
| 72 |
|
| 73 |
|
| 74 |
Order order = orderService.createOrder( |
| 75 |
items, |
| 76 |
customerData.get("email"), |
| 77 |
customerData.get("name"), |
| 78 |
customerData.get("phone"), |
| 79 |
customerData.get("contactPreference"), |
| 80 |
shippingData.get("address"), |
| 81 |
shippingData.get("city"), |
| 82 |
shippingData.get("state"), |
| 83 |
shippingData.get("zip"), |
| 84 |
shippingData.get("country"), |
| 85 |
shippingData.get("shippingMethod"), |
| 86 |
sessionId |
| 87 |
); |
| 88 |
|
| 89 |
|
| 90 |
orderService.completeOrder(order.getOrderId(), sessionId); |
| 91 |
|
| 92 |
Map<String, Object> response = new HashMap<>(); |
| 93 |
response.put("success", true); |
| 94 |
response.put("order", order); |
| 95 |
return ResponseEntity.status(HttpStatus.CREATED).body(response); |
| 96 |
|
| 97 |
} catch (IllegalStateException | IllegalArgumentException e) { |
| 98 |
Map<String, Object> error = new HashMap<>(); |
| 99 |
error.put("success", false); |
| 100 |
error.put("error", e.getMessage()); |
| 101 |
return ResponseEntity.badRequest().body(error); |
| 102 |
|
| 103 |
} catch (Exception e) { |
| 104 |
Map<String, Object> error = new HashMap<>(); |
| 105 |
error.put("success", false); |
| 106 |
error.put("error", e.getMessage()); |
| 107 |
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
* GET /api/orders/{orderId} - Get order details |
| 113 |
*/ |
| 114 |
@GetMapping("/{orderId}") |
| 115 |
public ResponseEntity<Map<String, Object>> getOrder(@PathVariable String orderId) { |
| 116 |
try { |
| 117 |
return orderService.getOrderById(orderId) |
| 118 |
.map(order -> { |
| 119 |
Map<String, Object> response = new HashMap<>(); |
| 120 |
response.put("success", true); |
| 121 |
response.put("order", order); |
| 122 |
return ResponseEntity.ok(response); |
| 123 |
}) |
| 124 |
.orElseGet(() -> { |
| 125 |
Map<String, Object> error = new HashMap<>(); |
| 126 |
error.put("success", false); |
| 127 |
error.put("error", "Order not found"); |
| 128 |
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(error); |
| 129 |
}); |
| 130 |
|
| 131 |
} catch (Exception e) { |
| 132 |
Map<String, Object> error = new HashMap<>(); |
| 133 |
error.put("success", false); |
| 134 |
error.put("error", e.getMessage()); |
| 135 |
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
* POST /api/orders/{orderId}/verify-email - Verify email to view tracking |
| 141 |
*/ |
| 142 |
@PostMapping("/{orderId}/verify-email") |
| 143 |
public ResponseEntity<Map<String, Object>> verifyEmail( |
| 144 |
@PathVariable String orderId, |
| 145 |
@RequestBody Map<String, String> request) { |
| 146 |
|
| 147 |
try { |
| 148 |
String email = request.get("email"); |
| 149 |
|
| 150 |
if (email == null || email.isEmpty()) { |
| 151 |
Map<String, Object> error = new HashMap<>(); |
| 152 |
error.put("success", false); |
| 153 |
error.put("error", "Email is required"); |
| 154 |
return ResponseEntity.badRequest().body(error); |
| 155 |
} |
| 156 |
|
| 157 |
boolean isValid = orderService.verifyOrderEmail(orderId, email); |
| 158 |
|
| 159 |
if (!isValid) { |
| 160 |
Map<String, Object> error = new HashMap<>(); |
| 161 |
error.put("success", false); |
| 162 |
error.put("error", "Email does not match order"); |
| 163 |
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(error); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
return orderService.getOrderById(orderId) |
| 168 |
.map(order -> { |
| 169 |
Map<String, Object> response = new HashMap<>(); |
| 170 |
response.put("success", true); |
| 171 |
response.put("order", order); |
| 172 |
return ResponseEntity.ok(response); |
| 173 |
}) |
| 174 |
.orElseGet(() -> { |
| 175 |
Map<String, Object> error = new HashMap<>(); |
| 176 |
error.put("success", false); |
| 177 |
error.put("error", "Order not found"); |
| 178 |
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(error); |
| 179 |
}); |
| 180 |
|
| 181 |
} catch (Exception e) { |
| 182 |
Map<String, Object> error = new HashMap<>(); |
| 183 |
error.put("success", false); |
| 184 |
error.put("error", e.getMessage()); |
| 185 |
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
private Long toLong(Object value) { |
| 191 |
if (value == null) return null; |
| 192 |
if (value instanceof Number) { |
| 193 |
return ((Number) value).longValue(); |
| 194 |
} |
| 195 |
if (value instanceof String) { |
| 196 |
return Long.parseLong((String) value); |
| 197 |
} |
| 198 |
throw new IllegalArgumentException("Cannot convert " + value.getClass() + " to Long"); |
| 199 |
} |
| 200 |
|
| 201 |
private Integer toInteger(Object value) { |
| 202 |
if (value == null) return null; |
| 203 |
if (value instanceof Number) { |
| 204 |
return ((Number) value).intValue(); |
| 205 |
} |
| 206 |
if (value instanceof String) { |
| 207 |
return Integer.parseInt((String) value); |
| 208 |
} |
| 209 |
throw new IllegalArgumentException("Cannot convert " + value.getClass() + " to Integer"); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
|