123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- package com.fdkankan.pay.util.stripe;
- import com.fdkankan.pay.common.ResultCode;
- import com.fdkankan.pay.config.PayConfig;
- import com.fdkankan.pay.entity.*;
- import com.fdkankan.pay.exception.BusinessException;
- import com.fdkankan.pay.response.OpenPayResponse;
- import com.fdkankan.pay.service.*;
- import com.fdkankan.pay.util.CacheUtil;
- import com.paypal.api.payments.Payment;
- import com.stripe.model.Price;
- import com.stripe.model.Product;
- import com.stripe.model.Subscription;
- import com.stripe.model.checkout.Session;
- import com.stripe.param.SubscriptionCancelParams;
- import com.stripe.param.checkout.SessionCreateParams;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import javax.print.attribute.standard.NumberOfInterveningJobs;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.math.BigDecimal;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.UUID;
- @Service
- @Slf4j
- public class StripeService {
- @Autowired
- IStripeProductService stripeProductService;
- @Autowired
- IStripePriceService stripePriceService;
- @Autowired
- IStripeCustomerService stripeCustomerService;
- @Autowired
- IAutopayOrderService autopayOrderService;
- @Autowired
- IStripeConfigService stripeConfigService;
- public Object openPay(Order param, String ipAddr,String lang) {
- StripeConfig stripeConfig = stripeConfigService.getByServeId(param.getServeId());
- if(stripeConfig == null){
- throw new BusinessException(ResultCode.PAYPAL_CONFIG_ERROR);
- }
- stripeConfig.setSuccessUrl( CacheUtil.mainUrl + stripeConfig.getCallBackUrl() +"/"+param.getOrderSn()+"/"+param.getPayType()+"/success");
- stripeConfig.setCancelUrl( CacheUtil.mainUrl + stripeConfig.getCallBackUrl() +"/"+param.getOrderSn()+"/"+param.getPayType()+"/cancel");
- //stripe支付
- if(param.getPayType() == 6 ){
- return this.createPay(param,stripeConfig,lang);
- }
- throw new BusinessException(ResultCode.WX_ORDER_PAY_TYPE_ERROR);
- }
- public Object createPay(Order param,StripeConfig stripeConfig,String lang){
- try {
- OpenPayResponse openPayResponse = new OpenPayResponse();
- openPayResponse.setOrderSn(param.getOrderSn());
- openPayResponse.setPayType(6);
- if(param.getAutoPay() == 1){
- AutopayOrder autopayOrder = autopayOrderService.getByOrderSn(param.getOrderSn(),param.getPayType());
- openPayResponse.setAutoPay(1);
- if(autopayOrder !=null){
- openPayResponse.setH5Url(autopayOrder.getSubscriptionHref());
- openPayResponse.setQrCodeUrl(autopayOrder.getSubscriptionHref());
- return openPayResponse;
- }
- }
- String productName = StringUtils.isBlank(param.getProductName()) ? "product" :param.getProductName();
- String productId = stripeProductService.getByName(productName);
- if(StringUtils.isBlank(productId)){
- log.info("创建stripe支付失败--productId{}",productId);
- throw new BusinessException(ResultCode.STRIPE_ERROR);
- }
- String priceId = stripePriceService.getByParam(param.getOrderMoney(),stripeConfig.getCurrency(),productId,param.getAutoPay(),param.getAutoPayTime());
- if(StringUtils.isBlank(priceId)){
- log.info("创建stripe支付失败--priceId{}",productId);
- throw new BusinessException(ResultCode.STRIPE_ERROR);
- }
- StripeCustomer stripeCustomer = stripeCustomerService.getByUserName(param.getUserName());
- SessionCreateParams.Mode mode = SessionCreateParams.Mode.PAYMENT;
- if(param.getAutoPay() == 1){
- mode = SessionCreateParams.Mode.SUBSCRIPTION;
- }
- SessionCreateParams.Locale locale = SessionCreateParams.Locale.AUTO;
- if(StringUtils.isNotBlank(lang) ){
- if("kr".equalsIgnoreCase(lang)){
- locale = SessionCreateParams.Locale.KO;
- }else {
- try {
- locale = SessionCreateParams.Locale.valueOf(lang.toUpperCase());
- }catch (Exception e){
- log.info("stripe中不包含该语言:{}",lang);
- locale = SessionCreateParams.Locale.EN;
- }
- }
- }
- SessionCreateParams params4 = SessionCreateParams.builder()
- .setMode(mode)
- .setSuccessUrl(stripeConfig.getSuccessUrl())
- .setCancelUrl(stripeConfig.getCancelUrl())
- .setLocale(locale)
- .setCustomerEmail(stripeCustomer!=null ?null :param.getUserName())
- .setCustomer(stripeCustomer!=null ?stripeCustomer.getCustomer():null)
- .addLineItem(
- SessionCreateParams.LineItem.builder()
- .setQuantity(1L)
- .setPrice(priceId)
- .build()).putMetadata("orderId", param.getOrderSn())
- .build();
- Session session = Session.create(params4);
- openPayResponse.setH5Url(session.getUrl());
- openPayResponse.setQrCodeUrl(session.getUrl());
- if(param.getAutoPay() == 1){
- autopayOrderService.saveAutoOrder(param.getOrderSn(),productId,priceId,null,session.getUrl(),param.getPayType());
- }
- return openPayResponse;
- }catch (Exception e){
- log.info("创建stripe支付失败--{}",e);
- throw new BusinessException(ResultCode.STRIPE_ERROR);
- }
- }
- public Boolean callBack(HttpServletRequest request, HttpServletResponse response, Order order, String result) {
- log.info("stripe-callback:{},{}",order,request);
- Boolean payFlag = false;
- String trade_no = null;
- String openId = null;
- if("cancel".equals(result)){
- return false;
- }
- try {
- Map<String, String[]> parameterMap = request.getParameterMap();
- for (String key : parameterMap.keySet()) {
- log.info("stripe-callBack--request:{},{}",key,request.getParameter(key));
- }
- }catch (Exception e) {
- log.error("stripe支付回调异常,errorMsg:{}", e.getMessage());
- return false;
- }finally {
- //orderService.payResult(order,payFlag,trade_no,openId);
- }
- return true;
- }
- public void cancelSubscriptions(String subscriptionId) {
- try {
- Subscription resource = Subscription.retrieve(subscriptionId);
- SubscriptionCancelParams params = SubscriptionCancelParams.builder().build();
- Subscription subscription = resource.cancel(params);
- }catch (Exception e){
- log.info("取消订阅失败:{}",e);
- throw new BusinessException(ResultCode.CANCEL_SUBSCRIPTIONS_ERROR);
- }
- }
- }
|