PaymentGatewayController.java
Bridget/payment_solutions/src/main/java/com/checkout/payment/gateway/controller/PaymentGatewayController.java
package com.checkout.payment.gateway.controller;
import com.checkout.payment.gateway.model.GetPaymentResponse;
import com.checkout.payment.gateway.model.PostPaymentRequest;
import com.checkout.payment.gateway.model.PostPaymentResponse;
import com.checkout.payment.gateway.model.ProcessPaymentResult;
import com.checkout.payment.gateway.service.PaymentGatewayService;
import java.util.UUID;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/v1")
public class PaymentGatewayController {
private final PaymentGatewayService paymentGatewayService;
public PaymentGatewayController(PaymentGatewayService paymentGatewayService) {
this.paymentGatewayService = paymentGatewayService;
}
@GetMapping("/payments/{id}")
@PreAuthorize("hasAnyRole('MERCHANT','ADMIN')")
public ResponseEntity<GetPaymentResponse> getPaymentById(@PathVariable UUID id) {
return new ResponseEntity<>(paymentGatewayService.getPaymentById(id), HttpStatus.OK);
}
@PostMapping("/payments")
@PreAuthorize("hasAnyRole('MERCHANT','ADMIN')")
public ResponseEntity<PostPaymentResponse> processPayment(
@RequestHeader(value = "Idempotency-Key", required = false) String idempotencyKey,
@RequestBody PostPaymentRequest request) {
ProcessPaymentResult result = paymentGatewayService.processPayment(idempotencyKey, request);
return new ResponseEntity<>(
result.response(),
HttpStatus.valueOf(result.httpStatusCode()));
}
}
相關文章
PaymentGatewayApplication.java
PaymentGatewayApplication.java — java source code from the Bridget learning materials (Bridget/payment-gateway-challenge-java/src/main/java/com/checkout/payment/gateway/PaymentGatewayApplication.java).
閱讀文章 →ApplicationConfiguration.java
ApplicationConfiguration.java — java source code from the Bridget learning materials (Bridget/payment-gateway-challenge-java/src/main/java/com/checkout/payment/gateway/configuration/ApplicationConfiguration.java).
閱讀文章 →RequestResponseLogger.java
RequestResponseLogger.java — java source code from the Bridget learning materials (Bridget/payment-gateway-challenge-java/src/main/java/com/checkout/payment/gateway/configuration/RequestResponseLogger.java).
閱讀文章 →PaymentGatewayController.java
PaymentGatewayController.java — java source code from the Bridget learning materials (Bridget/payment-gateway-challenge-java/src/main/java/com/checkout/payment/gateway/controller/PaymentGatewayController.java).
閱讀文章 →PaymentStatus.java
PaymentStatus.java — java source code from the Bridget learning materials (Bridget/payment-gateway-challenge-java/src/main/java/com/checkout/payment/gateway/enums/PaymentStatus.java).
閱讀文章 →BankErrorException.java
BankErrorException.java — java source code from the Bridget learning materials (Bridget/payment-gateway-challenge-java/src/main/java/com/checkout/payment/gateway/exception/BankErrorException.java).
閱讀文章 →