Stripe API

2025. 3. 24. 18:35TIL

결제 시스템을 구현하기 위해서 Stripe API를 이용

 

stripe 최신 sdk 버전 확인

https://docs.stripe.com/api?lang=java

 

Stripe API Reference

The Stripe API is organized around REST. Our API has predictable resource-oriented URLs, accepts form-encoded request bodies, returns JSON-encoded responses, and uses standard HTTP response codes, authentication, and verbs. You can use the Stripe API in te

docs.stripe.com

 

API 의 최신 버전은 2025-02-24 버전이므로 최신 버전 사용

    implementation 'com.stripe:stripe-java:28.4.0'

 

해당 샘플 예시를 참고로 service를 작성

https://www.baeldung.com/java-stripe-api

 

package hjp.shoppingmall.domain.shopping.service;

import com.stripe.Stripe;
import com.stripe.model.PaymentIntent;
import com.stripe.param.PaymentIntentCreateParams;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class StripeService{

    @Value("${stripe.secret-key}")
    private String secretKey;

    @PostConstruct
    public void init() {
        Stripe.apiKey = secretKey;
    }
    
    public PaymentIntent createPaymentIntent(long amount) {
        PaymentIntentCreateParams params = PaymentIntentCreateParams.builder()
                .setAmount(amount * 100)
                .setCurrency("usd")
                .build();

        try {
            return PaymentIntent.create(params);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}

 

타입 관련 에러가 발생

 

id, amount, currency 부분만 명시하여 반환하여 해결

@PostMapping("/create")
public ResponseEntity<Map<String, Object>> createPaymentIntent(@RequestParam long amount) {
    PaymentIntent paymentIntent = stripeService.createPaymentIntent(amount);

    if (paymentIntent != null) {
        Map<String, Object> response = new HashMap<>();
        response.put("id", paymentIntent.getId());
        response.put("amount", paymentIntent.getAmount());
        response.put("currency", paymentIntent.getCurrency());
        return ResponseEntity.ok(response);
    } else {
        return ResponseEntity.status(500).body(Collections.singletonMap("error", "Failed to create payment intent"));
    }
}

 

테스트 결과: