VUE+SpringBoot项目支付宝支付(超详细超简单)

首先此文章是转载了青戈老师的视频讲解 青戈老师

1、导入支付宝的SDK pom文件


        
        
            com.alipay.sdk
            alipay-easysdk
            2.2.1
        

2、application.yml配置  直达链接

alipay:
  appId: 你自己的appID
  appPrivateKey: 自己的私钥
  alipayPublicKey: 支付宝的公钥
  notifyUrl: 

3、Controller

@GetMapping("/pay")
public CommonResult pay(AliPay aliPay) {
    System.out.println(aliPay);
    AlipayTradePagePayResponse response;
    try {
        //  发起API调用(以创建当面付收款二维码为例)
        response = Factory.Payment.Page()
                .pay(URLEncoder.encode(aliPay.getSubject(), "UTF-8"), aliPay.getTraceNo(), aliPay.getTotalAmount(), "");
    } catch (Exception e) {
        System.err.println("调用遭遇异常,原因:" + e.getMessage());
        throw new RuntimeException(e.getMessage(), e);
    }
    System.out.println(response.body);
    return CommonResult.ok(response.body);
}

4、实体类

private String traceNo;
private String totalAmount;
private String subject;
private String alipayTraceNo;

5、aliPayConfig包


@Data
@Component
@ConfigurationProperties(prefix = "alipay")
public class AplipayConfig {

    private String appId;
    private String appPrivateKey;
    private String alipayPublicKey;
    private String notifyUrl;

    @PostConstruct
    public void init() {
        // 设参(只需设置一次)
        Config options = getOptions();
        options.appId = this.appId;
        options.merchantPrivateKey = this.appPrivateKey;
        options.alipayPublicKey = this.alipayPublicKey;
        options.notifyUrl = this.notifyUrl;
        Factory.setOptions(options);
        System.out.println("!!!!!!支付宝SDK初始化成功!!!!!!");
    }

    private Config getOptions() {
        Config config = new Config();
        config.protocol = "https";
        config.gatewayHost = "openapi.alipaydev.com";
        config.signType = "RSA2";
        return config;
    }

}

到这里就结束了 请求格式是http://localhost:自己的端口/aliPayController/pay?&subject=商品名称&traceNo=订单号&totalAmount=价格

VUE+SpringBoot项目支付宝支付(超详细超简单)_第1张图片

在前端使用新建窗口打开就ok拉

 

你可能感兴趣的:(spring,boot,vue.js,java)