Files
bbps-backend/src/main/java/com/billpaymentservice/bbps/controller/PaymentStartController.java
T
2025-12-09 18:30:22 +05:30

73 lines
3.7 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.CallbackRequest;
import com.billpaymentservice.bbps.dto.request.PaymentStartUserRequest;
import com.billpaymentservice.bbps.service.PaymentStartService;
import com.billpaymentservice.bbps.util.ApiDocCommonResponses;
import com.billpaymentservice.bbps.util.BBPSConstant;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
@RestController
@RequestMapping("/payment")
public class PaymentStartController {
private final PaymentStartService paymentStartService;
public PaymentStartController(PaymentStartService paymentStartService) {
this.paymentStartService = paymentStartService;
}
@PostMapping(value = "/startWebPayment")
@Operation(summary = "Start web payment process", description = "This API initiates the payment process for a web-based transaction using user and bill details.")
@ApiResponses(value = {
@ApiResponse(responseCode = BBPSConstant.OK_STR, description = "Payment process started successfully", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ResponseModel.class))) })
@ApiDocCommonResponses
public ResponseEntity<ResponseModel> startWebPayment(
@Validated @RequestBody PaymentStartUserRequest paymentStartUserRequest) {
ResponseModel response = paymentStartService.startWebPayment(paymentStartUserRequest);
return ResponseEntity.ok(response);
}
@PostMapping(value = "/callback/startPayment")
@Operation(summary = "Handle start payment callback", description = "This callback API is invoked by the payment gateway/BBPS to confirm the payment initiation status.")
@ApiResponses(value = {
@ApiResponse(responseCode = BBPSConstant.OK_STR, description = "Start Payment Callback handled successfully", content = @Content(mediaType = "application/json")) })
@ApiDocCommonResponses
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")
@Operation(summary = "Start intent payment process", description = "This API initiates the intent-based payment process for a user.")
@ApiResponses(value = {
@ApiResponse(responseCode = BBPSConstant.OK_STR, description = "Intent payment started successfully", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ResponseModel.class))), })
@ApiDocCommonResponses
public ResponseEntity<ResponseModel> startIntentPayment(
@Validated @RequestBody PaymentStartUserRequest paymentStartUserRequest) {
ResponseEntity.badRequest().body(new ResponseModel("PaymentStartService not available", "FAILED", 400, null));
ResponseModel response = paymentStartService.startIntentPayment(paymentStartUserRequest);
return ResponseEntity.ok(response);
}
}