Files
bbps-backend/src/main/java/com/billpaymentservice/bbps/controller/PaymentStartController.java
T

53 lines
2.6 KiB
Java

package com.billpaymentservice.bbps.controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.billpaymentservice.bbps.dto.ResponseModel;
import com.billpaymentservice.bbps.dto.request.PaymentStartUserRequest;
import com.billpaymentservice.bbps.dto.request.CallbackRequest;
import com.billpaymentservice.bbps.service.PaymentStartService;
@RestController
@RequestMapping("/payment")
public class PaymentStartController {
private final PaymentStartService paymentStartService;
public PaymentStartController(PaymentStartService paymentStartService) {
this.paymentStartService = paymentStartService;
}
@PostMapping(value = "/startWebPayment", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<ResponseModel> startWebPayment(@Validated @RequestBody PaymentStartUserRequest paymentStartUserRequest) {
ResponseModel response = paymentStartService.startWebPayment(paymentStartUserRequest);
return ResponseEntity.ok(response);
}
@PostMapping(value = "/callback/startPayment", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Map<String, String>> handleCallback(@RequestBody CallbackRequest callbackRequest) {
ResponseModel resp = paymentStartService.handleStartCallback(callbackRequest);
Map<String, String> result = new HashMap<>();
// Use the status field from ResponseModel as merchant status code string
result.put("status", resp.getStatus());
return ResponseEntity.ok(result);
}
@PostMapping(value = "/startIntentPayment", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<ResponseModel> startIntentPayment(@Validated @RequestBody PaymentStartUserRequest paymentStartUserRequest) {
if (this.paymentStartService == null) {
// Defensive: if bean not injected, return failure
return ResponseEntity.badRequest().body(new ResponseModel("PaymentStartService not available", "FAILED", 400, null));
}
ResponseModel response = paymentStartService.startIntentPayment(paymentStartUserRequest);
return ResponseEntity.ok(response);
}
}