Files
bbps-backend/src/main/java/com/billpaymentservice/bbps/controller/PaymentController.java
T
2025-12-01 16:57:48 +05:30

80 lines
3.8 KiB
Java

package com.billpaymentservice.bbps.controller;
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.BillPaymentRequest;
import com.billpaymentservice.bbps.dto.request.TransactionStatusRequest;
import com.billpaymentservice.bbps.dto.request.TrasactionStatusByClientReqIdRequest;
import com.billpaymentservice.bbps.dto.response.TransactionStatusResponse;
import com.billpaymentservice.bbps.service.PaymentService;
import com.billpaymentservice.bbps.service.PaymentStartService;
import com.billpaymentservice.bbps.dto.request.PaymentStartUserRequest;
@RestController
@RequestMapping("/agent/payment")
public class PaymentController {
/**
* Fetches bill details from external BBPS service
*/
private final PaymentService paymentService;
private final PaymentStartService paymentStartService;
// New constructor used by Spring to inject both services
public PaymentController(PaymentService paymentService, PaymentStartService paymentStartService) {
this.paymentService = paymentService;
this.paymentStartService = paymentStartService;
}
@PostMapping(value = "/intiatePayment", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<ResponseModel> requestBillFetch(
@Validated @RequestBody BillPaymentRequest billPaymentRequest) {
ResponseModel response = paymentService.initiatePayment(billPaymentRequest);
return ResponseEntity.ok(response);
}
@PostMapping(value = "/checkTransactionStatus", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<ResponseModel> checkTransactionStatus(
@Validated @RequestBody TransactionStatusRequest transactionStatusRequest) {
ResponseModel response = paymentService.checkTrasactionStatus(transactionStatusRequest);
return ResponseEntity.ok(response);
}
@PostMapping(value = "/checkTransactionStatusByClientReqId", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<ResponseModel> checkTransactionStatusByClientReqId(
@Validated @RequestBody TrasactionStatusByClientReqIdRequest transactionStatusRequest) {
ResponseModel response = paymentService.checkTrasactionStatusByClientReqId(transactionStatusRequest);
return ResponseEntity.ok(response);
}
@PostMapping(value="/callback/paymentIntiate", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<ResponseModel> handleBillValidateCallback(@RequestBody TransactionStatusResponse transactionStatusResponse) {
ResponseModel genralResp=paymentService.handlePaymentIntiateCallback(transactionStatusResponse);
// Return a response indicating successful receipt
return ResponseEntity.ok(genralResp);
}
@PostMapping(value = "/startPayment", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<ResponseModel> startPayment(@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.startPayment(paymentStartUserRequest);
return ResponseEntity.ok(response);
}
}