S SmartDocs
Chuỗi bài: Bridget java 58 dòng · Cập nhật 2026-05-08

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);
    }
}

Bài viết liên quan

Bridget java Cập nhật 2026-03-30

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).

Đọc bài viết →
Bridget java Cập nhật 2026-03-20

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).

Đọc bài viết →
Bridget java Cập nhật 2026-03-30

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).

Đọc bài viết →
Bridget java Cập nhật 2026-04-14

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).

Đọc bài viết →
Bridget java Cập nhật 2026-03-20

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).

Đọc bài viết →
Bridget java Cập nhật 2026-03-29

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).

Đọc bài viết →