springboot3整合支付宝沙箱网页版

1.maven依赖:


    org.springframework.boot
    spring-boot-starter-web



    org.springframework.boot
    spring-boot-starter-test
    test


    com.alipay.sdk
    alipay-sdk-java
    4.35.79.ALL


    cn.hutool
    hutool-all
    5.8.0

2. application.yml 包括支付宝沙箱appid, 私钥,支付宝公钥等,因为需要回调,先要配置外网接口,这里使用了natapp, 配置如下:

alipay:
  appId: 
  appPrivateKey: 
  alipayPublicKey: 
  notifyUrl: http://vwe3ye.natappfree.cc/alipay/notify
  serverUrl: https://openapi-sandbox.dl.alipaydev.com/gateway.do
  format: json
  charset: UTF-8
  signType: RSA2
  returnUrl: http://localhost:8080/orders

3.config文件:

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

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

    private String notifyUrl;

    private String  serverUrl;

    private String format;
    private String charset;
    private String signType;

    private String returnUrl;

...  setters()、 getters()

}

4.编写controller:

 @RequestMapping("/pay")
    public void pay4(HttpServletResponse response) throws AlipayApiException, IOException {
        AlipayClient client = new DefaultAlipayClient(aliPayConfig.getServerUrl(),
                aliPayConfig.getAppId(),
                aliPayConfig.getAppPrivateKey(),
                aliPayConfig.getFormat(),
                aliPayConfig.getCharset()
                , aliPayConfig.getAlipayPublicKey(),
                aliPayConfig.getSignType());
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        AlipayTradePagePayModel model = new AlipayTradePagePayModel();
        model.setOutTradeNo("20240129"+new Random().nextInt(100000));
        model.setTotalAmount("88.88");
        model.setSubject("Iphone6 16G");
        model.setProductCode("FAST_INSTANT_TRADE_PAY");
        request.setBizModel(model);
//        request.setNotifyUrl("http://9w2mpf.natappfree.cc/alipay/notify");
        request.setNotifyUrl(aliPayConfig.getNotifyUrl());
        request.setReturnUrl(aliPayConfig.getReturnUrl());
        String form =client.pageExecute(request, "GET").getBody();

        response.sendRedirect(form);

    }
@PostMapping("/notify")
    public String notify(HttpServletRequest request) throws AlipayApiException {
//        System.out.println(request);
        System.out.println("经过notify");
        if(request.getParameter("trade_status").equals("TRADE_SUCCESS")){
            HashMap params = new HashMap<>();
            Map parameterMap = request.getParameterMap();
            for(String name:parameterMap.keySet()){
                params.put(name, request.getParameter(name));
            }
            String sign = params.get("sign");
            String content = AlipaySignature.getSignCheckContentV1(params);
            boolean checkSignatrue =AlipaySignature.rsa256CheckContent(content
                                   , sign, aliPayConfig.getAlipayPublicKey(), CHARSET);
            if(checkSignatrue){
                System.out.println("交易名称 : "+params.get("subject"));
                System.out.println("支付宝订单号:" + params.get("trade_no"));
                System.out.println("本系统交易号: "+ params.get("out_trade_no"));
            }

        }
        return "支付成功";
    }
@RequestMapping("/orders")
public String orders(){
    return "支付完成";
}

你可能感兴趣的:(java,开发语言)