PaymentController.java
Bridget/payment-gateway-microservices/services/payment-service/src/main/java/com/checkout/paymentgw/payment/api/PaymentController.java
package com.checkout.paymentgw.payment.api;
import com.checkout.paymentgw.payment.application.CreatePaymentUseCase;
import com.checkout.paymentgw.payment.api.dto.PostPaymentRequest;
import com.checkout.paymentgw.payment.api.dto.PostPaymentResponse;
import jakarta.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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;
import java.net.URI;
/**
* Public API of the payment-service. Only mutation endpoints live here;
* reads go to {@code payment-query-service} via the API gateway.
*/
@RestController
@RequestMapping("/api/payments")
public class PaymentController {
private static final Logger LOG = LoggerFactory.getLogger(PaymentController.class);
private final CreatePaymentUseCase useCase;
public PaymentController(CreatePaymentUseCase useCase) {
this.useCase = useCase;
}
/**
* Create a payment.
*
* <p>Mandatory header: {@code Idempotency-Key}. The same key for the same merchant
* returns the same response, even if invoked many times.
*
* <p>Returns {@code 201 Created} with a {@code Location} header pointing at
* {@code GET /api/payments/{id}} on the read side. Note that due to eventual
* consistency the read side may lag the write side by a few hundred ms.
*/
@PostMapping
public ResponseEntity<PostPaymentResponse> create(
@RequestHeader(value = "Idempotency-Key") String idempotencyKey,
@RequestHeader(value = "X-Merchant-Id", defaultValue = "anonymous") String merchantId,
@Valid @RequestBody PostPaymentRequest request) {
LOG.info("create payment merchant={} amount={} {}", merchantId, request.getAmount(), request.getCurrency());
PostPaymentResponse response = useCase.execute(idempotencyKey, merchantId, request);
return ResponseEntity
.status(HttpStatus.CREATED)
.location(URI.create("/api/payments/" + response.getId()))
.body(response);
}
}
관련 글
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).
글 읽기 →