|
|
@@ -1,17 +1,14 @@
|
|
|
package com.cyksj.service.stripe;
|
|
|
|
|
|
-import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.cyksj.common.exception.BusinessRuntimeException;
|
|
|
import com.cyksj.common.task.GlobalThreadPoolTaskExecutor;
|
|
|
import com.cyksj.common.util.Jsons;
|
|
|
import com.cyksj.enums.GatewayApiCode;
|
|
|
import com.cyksj.mapper.StripePayConfigMapper;
|
|
|
import com.cyksj.model.entity.OrderDon;
|
|
|
-import com.cyksj.model.entity.OrderMasterDon;
|
|
|
import com.cyksj.model.entity.StripePayConfig;
|
|
|
import com.cyksj.model.request.OrderRefundReq;
|
|
|
import com.cyksj.service.order.OrderDonService;
|
|
|
-import com.cyksj.service.order.OrderMasterDonService;
|
|
|
import com.stripe.Stripe;
|
|
|
import com.stripe.exception.StripeException;
|
|
|
import com.stripe.model.*;
|
|
|
@@ -26,7 +23,6 @@ import java.math.BigDecimal;
|
|
|
import java.math.RoundingMode;
|
|
|
import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
|
|
|
/**
|
|
|
* @author zwhui
|
|
|
@@ -45,85 +41,78 @@ public class StripeService {
|
|
|
|
|
|
private final StripePayConfigMapper stripePayConfigMapper;
|
|
|
|
|
|
- private final OrderMasterDonService orderMasterDonService;
|
|
|
-
|
|
|
|
|
|
|
|
|
public String checkOutStripe(Long orderId,String successUrl) throws Exception{
|
|
|
- OrderDon orderDon = orderDonService.getById(orderId);
|
|
|
- BigDecimal money = orderDon.getMoney();
|
|
|
- orderDon.setType(OrderDon.Type.STRIPE);
|
|
|
- orderDonService.updateById(orderDon);
|
|
|
- HashMap<String, String> responseData = new HashMap<String, String>();
|
|
|
- if (OrderDon.Status.hasPayment.equals(orderDon.getStatus())) {
|
|
|
- throw new BusinessRuntimeException(GatewayApiCode.METADATA_SYSTEM_ERROR.getCode(),"The donation has been paid");
|
|
|
- }
|
|
|
- StripePayConfig stripePayConfig = stripePayConfigMapper.selectById(1);
|
|
|
- try {
|
|
|
- Stripe.apiKey = stripePayConfig.getApiKey();
|
|
|
- SessionCreateParams params =
|
|
|
- SessionCreateParams.builder()
|
|
|
- .setMode(SessionCreateParams.Mode.PAYMENT)
|
|
|
- .setSuccessUrl(successUrl)
|
|
|
- .setCancelUrl(stripePayConfig.getCancelUrl())
|
|
|
- .setClientReferenceId(orderDon.getId().toString())
|
|
|
- .addLineItem(
|
|
|
- SessionCreateParams.LineItem.builder()
|
|
|
- .setQuantity(1L)
|
|
|
- .setPriceData(
|
|
|
- SessionCreateParams.LineItem.PriceData.builder()
|
|
|
- .setCurrency("USD")
|
|
|
- .setUnitAmount(money.longValue())
|
|
|
- .setProductData(
|
|
|
- SessionCreateParams.LineItem.PriceData.ProductData.builder()
|
|
|
- .setName("donation")
|
|
|
- .setDescription("donation")
|
|
|
- .build()).build()).build()).build();
|
|
|
- Session session = Session.create(params);
|
|
|
- responseData.put("id", session.getId());
|
|
|
- } catch (StripeException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- return Jsons.toJson(responseData);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- public void fulfillOrder(Session session) throws Exception{
|
|
|
- if (null == session){
|
|
|
- log.error("支付回调失败");
|
|
|
- return;
|
|
|
- }
|
|
|
- String mOid = session.getClientReferenceId();
|
|
|
- if (!"paid".equals(session.getPaymentStatus())){
|
|
|
- return;
|
|
|
- }
|
|
|
- OrderMasterDon masterDon = orderMasterDonService.getById(mOid);
|
|
|
-
|
|
|
- if (masterDon == null){
|
|
|
- return;
|
|
|
- }
|
|
|
- if (!masterDon.getStatus().equals(OrderMasterDon.Status.noPayment) && !masterDon.getStatus().equals(OrderMasterDon.Status.close)) {
|
|
|
- log.info("支付宝已回调,重复修改状态 id:{}", masterDon.getId());
|
|
|
- return;
|
|
|
- }
|
|
|
- String paymentIntentId = session.getPaymentIntent();
|
|
|
- PaymentIntent paymentIntent = PaymentIntent.retrieve(paymentIntentId);
|
|
|
- String chargeId = paymentIntent.getLatestCharge();
|
|
|
- BalanceTransaction balanceTransaction = BalanceTransaction.retrieve(Charge.retrieve(chargeId).getBalanceTransaction());
|
|
|
- //更新订单支付状态
|
|
|
- masterDon.setTransactionId(chargeId);
|
|
|
- masterDon.setAbroadTransactionFee(BigDecimal.valueOf(balanceTransaction.getFee()));
|
|
|
- masterDon.setPayTime(new Date());
|
|
|
- log.info("支付成功订单:"+mOid+",支付交易号:"+chargeId);
|
|
|
- log.info("支付成功订单:"+mOid+",返回参数:"+Jsons.toJson(session));
|
|
|
-
|
|
|
- //子订单
|
|
|
- List<OrderDon> orderDonList = orderDonService.list(Wrappers.lambdaQuery(OrderDon.class)
|
|
|
- .eq(OrderDon::getMOid, mOid));
|
|
|
- orderDonList.forEach(orderDon->{
|
|
|
- orderDonService.orderNotify(orderDon);
|
|
|
- });
|
|
|
- }
|
|
|
+ OrderDon orderDon = orderDonService.getById(orderId);
|
|
|
+ BigDecimal money = orderDon.getMoney();
|
|
|
+ orderDon.setType(OrderDon.Type.STRIPE);
|
|
|
+ orderDonService.updateById(orderDon);
|
|
|
+ HashMap<String, String> responseData = new HashMap<String, String>();
|
|
|
+ if (OrderDon.Status.hasPayment.equals(orderDon.getStatus())) {
|
|
|
+ throw new BusinessRuntimeException(GatewayApiCode.METADATA_SYSTEM_ERROR.getCode(),"The donation has been paid");
|
|
|
+ }
|
|
|
+ StripePayConfig stripePayConfig = stripePayConfigMapper.selectById(1);
|
|
|
+ try {
|
|
|
+ Stripe.apiKey = stripePayConfig.getApiKey();
|
|
|
+ SessionCreateParams params =
|
|
|
+ SessionCreateParams.builder()
|
|
|
+ .setMode(SessionCreateParams.Mode.PAYMENT)
|
|
|
+ .setSuccessUrl(successUrl)
|
|
|
+ .setCancelUrl(stripePayConfig.getCancelUrl())
|
|
|
+ .setClientReferenceId(orderDon.getId().toString())
|
|
|
+ .addLineItem(
|
|
|
+ SessionCreateParams.LineItem.builder()
|
|
|
+ .setQuantity(1L)
|
|
|
+ .setPriceData(
|
|
|
+ SessionCreateParams.LineItem.PriceData.builder()
|
|
|
+ .setCurrency("USD")
|
|
|
+ .setUnitAmount(money.longValue())
|
|
|
+ .setProductData(
|
|
|
+ SessionCreateParams.LineItem.PriceData.ProductData.builder()
|
|
|
+ .setName("donation")
|
|
|
+ .setDescription("donation")
|
|
|
+ .build()).build()).build()).build();
|
|
|
+ Session session = Session.create(params);
|
|
|
+ responseData.put("id", session.getId());
|
|
|
+ } catch (StripeException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return Jsons.toJson(responseData);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public void fulfillOrder(Session session) throws Exception{
|
|
|
+ if (null == session){
|
|
|
+ log.error("支付回调失败");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ String orderId = session.getClientReferenceId();
|
|
|
+ if (!"paid".equals(session.getPaymentStatus())){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ OrderDon orderDon = orderDonService.getById(orderId);
|
|
|
+ if (orderDon == null){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (orderDon.getStatus().equals(OrderDon.Status.hasPayment)) {
|
|
|
+ log.info("已回调,重复修改状态 id:{}", orderDon.getId());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ String paymentIntentId = session.getPaymentIntent();
|
|
|
+ PaymentIntent paymentIntent = PaymentIntent.retrieve(paymentIntentId);
|
|
|
+ String chargeId = paymentIntent.getLatestCharge();
|
|
|
+ BalanceTransaction balanceTransaction = BalanceTransaction.retrieve(Charge.retrieve(chargeId).getBalanceTransaction());
|
|
|
+ //更新订单支付状态
|
|
|
+
|
|
|
+ orderDon.setTransactionId(chargeId);
|
|
|
+ orderDon.setAbroadTransactionFee(BigDecimal.valueOf(balanceTransaction.getFee()));
|
|
|
+ orderDon.setPayTime(new Date());
|
|
|
+ orderDonService.orderNotify(orderDon);
|
|
|
+ log.info("支付成功订单:"+orderId+",支付交易号:"+chargeId);
|
|
|
+ log.info("支付成功订单:"+orderId+",返回参数:"+Jsons.toJson(session));
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
|
|
|
public Boolean refund(OrderRefundReq refundReq, OrderDon orderDon) throws StripeException {
|