From e1b08a9979eca23b2db231f1db992742a46d7f0f Mon Sep 17 00:00:00 2001 From: xupei Date: Wed, 17 Jul 2024 15:26:11 +0800 Subject: [PATCH] modify:Stripe --- .../com/ai/da/common/config/PayPalClient.java | 7 +----- .../ai/da/service/impl/StripeServiceImpl.java | 23 +++++++++++-------- ...-sandbox.properties => payment.properties} | 18 ++++++++++++++- 3 files changed, 32 insertions(+), 16 deletions(-) rename src/main/resources/{paypal-sandbox.properties => payment.properties} (60%) diff --git a/src/main/java/com/ai/da/common/config/PayPalClient.java b/src/main/java/com/ai/da/common/config/PayPalClient.java index bcdef0a3..89d4d8db 100644 --- a/src/main/java/com/ai/da/common/config/PayPalClient.java +++ b/src/main/java/com/ai/da/common/config/PayPalClient.java @@ -3,17 +3,12 @@ package com.ai.da.common.config; import com.paypal.core.PayPalEnvironment; import com.paypal.core.PayPalHttpClient; import lombok.extern.slf4j.Slf4j; -import org.apache.commons.lang3.StringUtils; -import org.json.JSONArray; -import org.json.JSONObject; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; -import java.util.Iterator; - @Configuration @Slf4j -@PropertySource("classpath:paypal-sandbox.properties") +@PropertySource("classpath:payment.properties") public class PayPalClient { public PayPalHttpClient client(String mode, String clientId, String clientSecret) { diff --git a/src/main/java/com/ai/da/service/impl/StripeServiceImpl.java b/src/main/java/com/ai/da/service/impl/StripeServiceImpl.java index fd6b3295..1f9933d5 100644 --- a/src/main/java/com/ai/da/service/impl/StripeServiceImpl.java +++ b/src/main/java/com/ai/da/service/impl/StripeServiceImpl.java @@ -20,6 +20,7 @@ import com.stripe.net.Webhook; import com.stripe.param.*; import com.stripe.param.checkout.SessionCreateParams; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -36,9 +37,6 @@ import java.util.Objects; @Slf4j public class StripeServiceImpl implements StripeService { - private static final String privateKey = "sk_test_51P4ZZL02n1TEydyN8qQHjOA9imsFU7Oxs2HMHGy2urHnnQgSHnZuu5vVP6pKhEACwUpsKNyrbZpdcg5TJWJLRHcY008dEO1fn2"; -// private static final String privateKey = "pk_test_51P4ZZL02n1TEydyNluht6VDxnBMoBw8S524otzlXV3CMCZI8HmkcUr3CKy8Z0eQ2WssSJ9sAaZnIQJI04fARHHJi00pcIB4sKU"; - @Resource private OrderInfoService orderInfoService; @Resource @@ -50,6 +48,12 @@ public class StripeServiceImpl implements StripeService { @Resource private RefundInfoService refundInfoService; + @Value("${stripe.private-key}") + private String privateKey; + + @Value("${stripe.webhook-sign-secret}") + private String signSecret; + @Override @Transactional(rollbackFor = Exception.class) public String pay(Integer quantity, String returnUrl) { @@ -79,7 +83,8 @@ public class StripeServiceImpl implements StripeService { //创建支付信息得到url SessionCreateParams params3 = SessionCreateParams.builder() .setMode(SessionCreateParams.Mode.PAYMENT) - .setSuccessUrl(returnUrl) //可自定义成功页面 + .setSuccessUrl(returnUrl) + .setPaymentIntentData(SessionCreateParams.PaymentIntentData.builder().setDescription("AiDA - " + orderId).build())//可自定义成功页面 .addLineItem( SessionCreateParams.LineItem.builder() .setQuantity(Long.valueOf(quantity)) @@ -107,7 +112,7 @@ public class StripeServiceImpl implements StripeService { log.info("stripe异步通知进行中"); String payload = null; String sigHeader = null; - String endpointSecret = "whsec_e0dBiJngx6qqgJj6yPyJ2A9ouh1Cjv5w";//webhook秘钥签名 + String endpointSecret = signSecret; try { sigHeader = request.getHeader("Stripe-Signature"); payload = payPalCheckoutService.getBody(request); @@ -296,7 +301,7 @@ public class StripeServiceImpl implements StripeService { // 3、创建支付 paymentIntent // 4、确认订单 // 5、捕获金额(执行扣款操作) - public static String createCustomer() throws StripeException { + public String createCustomer() throws StripeException { Stripe.apiKey = privateKey; // Customer允许重复使用 @@ -311,7 +316,7 @@ public class StripeServiceImpl implements StripeService { return customer.getId(); } - public static String createPaymentMethod(String customerId) throws StripeException { + public String createPaymentMethod(String customerId) throws StripeException { Stripe.apiKey = privateKey; @@ -334,7 +339,7 @@ public class StripeServiceImpl implements StripeService { return paymentMethod.getId(); } - public static String createPaymentIntent(String paymentMethodId, String customerId) throws StripeException { + public String createPaymentIntent(String paymentMethodId, String customerId) throws StripeException { Stripe.apiKey = privateKey; Long amount = 600L; @@ -355,7 +360,7 @@ public class StripeServiceImpl implements StripeService { return paymentIntent.getId(); } - public static String confirmPaymentIntent(String clientSecret) throws StripeException { + public String confirmPaymentIntent(String clientSecret) throws StripeException { Stripe.apiKey = privateKey; PaymentIntent resource = PaymentIntent.retrieve(clientSecret); diff --git a/src/main/resources/paypal-sandbox.properties b/src/main/resources/payment.properties similarity index 60% rename from src/main/resources/paypal-sandbox.properties rename to src/main/resources/payment.properties index 5036149c..0f1d2b4e 100644 --- a/src/main/resources/paypal-sandbox.properties +++ b/src/main/resources/payment.properties @@ -1,3 +1,5 @@ +##### PayPal + # developer-sandbox-xp paypal.client-id=ATbaebYi7-GXWRWJqwRLYMzKEbwjh4BFRqD4Y13i4lZq0rplWIM_IpPrtPKpdkAt_KrPXd6IJTwsDqa5 paypal.client-secret=EHWWJqGmmbfjLXqCUpGrvxRYBPPtWvA3hR5ZaAyHlGSVJiHoQPS8skbNaJ9h39VObnchUbgiY2pPu__s @@ -21,4 +23,18 @@ paypal.webhook_id=31797347YC028794L #paypal.receiver.email=kimwong@code-create.com.hk #paypal.mode=live #paypal.webhook_id=41L14847MC833625B -#paypal.webhook_id=1D107312EX592781K \ No newline at end of file +#paypal.webhook_id=1D107312EX592781K + +##### Stripe + +# developer +#stripe.private-key=sk_test_51P4ZZL02n1TEydyN8qQHjOA9imsFU7Oxs2HMHGy2urHnnQgSHnZuu5vVP6pKhEACwUpsKNyrbZpdcg5TJWJLRHcY008dEO1fn2 +#stripe.webhook-sign-secret=whsec_e0dBiJngx6qqgJj6yPyJ2A9ouh1Cjv5w + +# kim - test +stripe.private-key=sk_test_51LwPrxH7nPZ8bkrNj67TFD7sxucaTANs1lf0KGSu1QSJfxYXcnigq2wTaZyZzST7y0fMbhhvaJZ4LjjFhr95M83a00eXrmOTL0 +stripe.webhook-sign-secret=whsec_GoyVEAaBtuGD5Rt55z83JnPnLDAZTN3u + +# kim - live +#stripe.private-key=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 +#stripe.webhook-sign-secret=whsec_hhGDgdelQRHSg4LmChtQe41crj41eb11 \ No newline at end of file