准备

支付宝新版SDK AlipayEasy 2.0.2
SpringBoot 2.3.3.RELEASE
Vue2

进入支付宝开发平台,使用支付宝公钥证书方式,6个文件参考支付宝文档弄下来
在这里插入图片描述

pom.xml

<!--pom.xml  支付宝sdk -->
<dependency>
    <groupId>com.alipay.sdk</groupId>
    <artifactId>alipay-easysdk</artifactId>
    <version>2.0.2</version>
</dependency>

yml

在这里插入图片描述
controller就不写了

AliPaySvrvice


@Slf4j
@Service
public class Service_AilPay {

    @Resource
    private OmsOrderServiceImpl orderService;

    @Value("${alipay.ali_return_url}")
    private String ali_return_url; //支付成功同步接口
    @Value("${alipay.ali_notify_url}")
    private String ali_notify_url; //支付成功异步接口


    //支付
    public String Alipay(FinalParma finalParma, int isMobile) {
        try {
            if (isMobile == 1) {
                // quitUrl参数是手机网站支付用于在H5页面收银台中添加返回按钮的, 我没测试所以置空
                AlipayTradeWapPayResponse response = Factory.Payment.Wap()
                        .pay(finalParma.getSubject(), finalParma.getOut_trade_no(), finalParma.getTotal_amount(), "", ali_return_url);
                if (ResponseChecker.success(response)) {
                    System.out.println("调用手机支付成功");
                    return response.body;
                } else {
                    System.out.println("调用手机支付失败");
                }
            } else {
                //optional是添加穿透域回传参数
                AlipayTradePagePayResponse response = Factory.Payment.Page()
                        .optional("passback_params",finalPar.getPassback_params())
                        .pay(finalPar.getSubject(), finalPar.getOut_trade_no(), finalPar.getTotal_amount(), ali_return_url);
                        
                if (ResponseChecker.success(response)) {
                    System.out.println("调用电脑支付成功");
                    return response.body;
                } else {
                    System.out.println("调用电脑支付失败");
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    //支付成功异步通知
    public String ali_notify(HttpServletRequest request) {
        try {
            Map<String, String> params = getAllRequestParam(request);
            if (!Factory.Payment.Common().verifyNotify(params)) {
                System.out.println("校验失败");
                return "fail";
            }
            String trade_status = params.get("trade_status");

            if (trade_status.equals("TRADE_SUCCESS")) {
                String order_ids = URLDecoder.decode(params.get("passback_params"), StandardCharsets.UTF_8);
                System.out.println("支付成功:" + trade_status);
                String[] idArr = order_ids.split(",");
                List<OmsOrder> orders = orderService.GetOrder(idArr, "", "",-1,-1,"");
                for (OmsOrder order : orders) {
                    if (order.getOrder_status() == 0) {
                        //TODO
                        order.setPayment_time(TimeUtil.StringDataToLocalDateTime(params.get("gmt_payment")));
                        orderService.UpdateOrder(order);
                    }
                }
                return "success";
            }
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return "fail";
    }

    //支付宝退款
    public String ali_refund(OmsOrder order) {
        try {
            AlipayTradeRefundResponse response = Factory.Payment.Common()
                    .refund(order.getOut_trade_no(), order.getPay_amount().toString());
            if (ResponseChecker.success(response)) {
                System.out.println("调用退款成功");
                //TODO
                return response.httpBody;
            } else {
                System.out.println("调用退款失败");
            }
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    //得到请求的参数
    private Map<String, String> getAllRequestParam(final HttpServletRequest request) {
        Map<String, String> res = new HashMap<>();
        Enumeration<?> temp = request.getParameterNames();
        while (temp.hasMoreElements()) {
            String en = (String) temp.nextElement();
            String value = request.getParameter(en);
            res.put(en, value);
        }
        return res;
    }
}

在这里插入图片描述

AliConfig

/**
 * 支付宝新版sdk配置
 */
@Configuration
public class Config_Ali {

    @Value("${alipay.protocol}")
    private String protocol;
    
    @Value("${alipay.gatewayHost}")
    private String gatewayHost;
    
    @Value("${alipay.appId}")
    private String appId;
    
    @Value("${alipay.app_private_key}")
    private String app_private_key; //应用私钥
    @Value("${alipay.mer_cert_path}")
    private String mer_cert_path; //应用公钥证书
    @Value("${alipay.ali_cert_path}")
    private String ali_cert_path; //支付宝公钥证书
    @Value("${alipay.ali_root_cert_path}")
    private String ali_root_cert_path; //支付宝根证书
    @Value("${alipay.ali_notify_url}")
    private String ali_notify_url; //支付成功异步接口


    @Bean
    public Config ConfigAli() throws Exception {
        Config config = new Config();
        config.protocol = protocol;
        config.gatewayHost = gatewayHost;
        config.signType = "RSA2";
        config.appId = appId;

        config.merchantPrivateKey = ReadTXTUtil.readTxt(new ClassPathResource(app_private_key).getInputStream());
        config.merchantCertPath = new ClassPathResource(mer_cert_path).getPath();
        config.alipayCertPath = new ClassPathResource(ali_cert_path).getPath();
        config.alipayRootCertPath = new ClassPathResource(ali_root_cert_path).getPath();
        config.notifyUrl = ali_notify_url;
        Factory.setOptions(config);
        return config;
    }
}


//=============== controller 支付成功同步接口 ======================================//
    @ApiOperation("支付宝付款后同步跳转返回")
    @RequestMapping("/ali_return_url")
    public String ali_return_url(HttpServletRequest request) {
        return "<html>\n" +
              "<head>\n" +
              "</head>\n" +
              "<body>\n" +
              "<div style=\"display: flex\">\n" +
              "    <b style=\"margin: 100px auto;color: red;font-size: 50px;\"> 支付成功:"+request.getParameter("total_amount") +"元 </b>\n" +
              "</div>\n" +
              "</body>\n" +
              "</html>";
    }
进支付宝开放平台,红框就是设置那个支付成功异步回调接口,下面的用不到
保证外网能直接访问你这个接口;可以用内网穿透工具

在这里插入图片描述

Vue,请求支付方法

6.

Vue调用支付宝返回的表单去支付

在这里插入图片描述
只测试了支付方法

Logo

前往低代码交流专区

更多推荐