服务器之家

服务器之家 > 正文

Java中Spring Boot支付宝扫码支付及支付回调的实现代码

时间:2021-08-11 12:20     来源/作者:Yy夜雨

前言:最近开发支付宝支付功能,总结一下做个分享

官方文档:https://opendocs.alipay.com/apis
支付宝沙箱地址: https://openhome.alipay.com/platform/appDaily.htm?tab=info
支付宝支付流程:

准备工作:获取支付宝沙箱数据(appid,支付宝网关,rsa2秘,沙箱支付账号等)
集成springboot,使用java代码发起支付请求
支付宝收到支付请求后,返回html代码片段,用于前端展示二维码
扫码支付成功后,支付宝发送同步、异步通知请求,同步、异步通知路径可在配置文件中进行配置
收到异步通知结果后,进行验签,验签通过,返回成功信息通知支付宝不在进行异步通知
此时支付宝支付流程完成,调用支付宝查询接口,确认支付成功

一、获取支付宝沙箱数据

打开上述沙箱地址,获取沙箱配置,查看下图信息

Java中Spring Boot支付宝扫码支付及支付回调的实现代码

二、集成springboot,使用java代码发起支付请求

1、在pom.xml文件添加支付宝依赖

?
1
2
3
4
5
6
<!-- 支付宝支付 -->
        <dependency>
            <groupid>com.alipay.sdk</groupid>
            <artifactid>alipay-sdk-java</artifactid>
            <version>3.7.26.all</version>
        </dependency>

2、在项目中新建一个支付宝工具类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
public class alipayconfig {
    // ↓↓↓↓↓↓↓↓↓↓请在这里配置您的基本信息↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
 
    // 应用id,您的appid,收款账号既是您的appid对应支付宝账号
    public static string app_id = "";
 
    // 商户私钥,您的pkcs8格式rsa2私钥
    public static string merchant_private_key = "";
 
    // 支付宝公钥,查看地址:https://openhome.alipay.com/platform/keymanage.htm 对应appid下的支付宝公钥。
    public static string alipay_public_key = "";
 
    // 服务器异步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
    
    // 这里需要配置支付宝回调的后端路径,必须要外网可以访问
    public static string notify_url = "http://localhost:8080/alipay.trade.page.pay-java-utf-8/notify_url.jsp";
    
    // 页面跳转同步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
    
    // todo 这里需要配置支付宝回调的前端路径,必须要外网可以访问
    public static string return_url = "http://localhost:8080/alipay.trade.page.pay-java-utf-8/return_url.jsp"
 
    // 签名方式
    public static string sign_type = "rsa2";
 
    // 字符编码格式
    public static string charset = "utf-8";
 
    // 支付宝网关
    public static string gatewayurl = "https://openapi.alipaydev.com/gateway.do";
 
    // 日志路径
    public static string log_path = "c:\\";
 
    // ↑↑↑↑↑↑↑↑↑↑请在这里配置您的基本信息↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
 
    /**
     * 写日志,方便测试(看网站需求,也可以改成把记录存入数据库)
     *
     * @param sword 要写入日志里的文本内容
     */
    public static void logresult(string sword) {
        filewriter writer = null;
        try {
            writer = new filewriter(log_path + "alipay_log_" + system.currenttimemillis() + ".txt");
            writer.write(sword);
        } catch (exception e) {
            e.printstacktrace();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (ioexception e) {
                    e.printstacktrace();
                }
            }
        }
    }
}

3、定义service

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public interface alipayservice {
 
  /**
   * 发起支付
   * @param outtradeno  订单编号(唯一)
   * @param totalamount  订单价格
   * @param subject    商品名称
   */
  string gopay(string outtradeno,bigdecimal totalamount,string subject) throws exception;
  
  /**
   * 交易查询
   * @param outtradeno 订单编号(唯一)
   */
  string query(string outtradeno) throws alipayapiexception;
  
  /**
   * 交易关闭
   * @param outtradeno订单编号(唯一)
   */
  string close(string outtradeno) throws alipayapiexception;
}

4、实现serviceimpl,发起支付请求,交易查询等

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
public class alipayserviceimpl implements alipayservice {
 
    @autowired
    private rechargedetailservice rechargedetailservice;
 
    @override
    public string gopay(string outtradeno, bigdecimal totalamount, string subject) throws exception {
 
        alipayclient alipayclient = new defaultalipayclient(alipayconfig.gatewayurl, alipayconfig.app_id,
                alipayconfig.merchant_private_key, "json", alipayconfig.charset, alipayconfig.alipay_public_key,
                alipayconfig.sign_type);
 
        alipaytradepagepayrequest alipayrequest = new alipaytradepagepayrequest();
 
        /** 同步通知,支付完成后,支付成功页面 */
        alipayrequest.setreturnurl(alipayconfig.return_url);
        /** 异步通知,支付完成后,需要进行的异步处理 */
        alipayrequest.setnotifyurl(alipayconfig.notify_url);
 
        alipayrequest.setbizcontent("{\"out_trade_no\":\"" + outtradeno + "\"," + "\"total_amount\":\"" + totalamount
                + "\"," + "\"subject\":\"" + subject + "\"," + "\"body\":\"付款\"," + "\"timeout_express\":\"15m\","
                + "\"product_code\":\"fast_instant_trade_pay\"}");
 
        /** 转换格式 */
        string form = "";
 
        form = alipayclient.pageexecute(alipayrequest).getbody();
 
        return form;
 
    }
    @override
    public string query(string outtradeno) throws alipayapiexception {
        alipayclient alipayclient = new defaultalipayclient(alipayconfig.gatewayurl, alipayconfig.app_id,
                alipayconfig.merchant_private_key, "json", alipayconfig.charset, alipayconfig.alipay_public_key,
                alipayconfig.sign_type);
        alipaytradequeryrequest alipayrequest = new alipaytradequeryrequest();
        /** 请求接口 */
        alipayrequest.setbizcontent("{\"out_trade_no\":\"" + outtradeno + "\"," + "\"trade_no\":\"" + "" + "\"}");
        /** 转换格式 */
        string result = alipayclient.execute(alipayrequest).getbody();
        return result;
    }
 
    @override
    public string close(string outtradeno) throws alipayapiexception {
        alipaytradecloserequest alipayrequest = new alipaytradecloserequest();
        alipayclient alipayclient = new defaultalipayclient(alipayconfig.gatewayurl, alipayconfig.app_id,
                alipayconfig.merchant_private_key, "json", alipayconfig.charset, alipayconfig.alipay_public_key,
                alipayconfig.sign_type);
        alipayrequest.setbizcontent("{\"out_trade_no\":\"" + outtradeno + "\"," + "\"trade_no\":\"" + "" + "\"}");
 
        string result = alipayclient.execute(alipayrequest).getbody();
 
        return result;
    }
}

5、创建支付宝controller

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
public class alipaycontroller {
 
  @autowired
  private alipayservice alipayservice;
  
  /**
   * 订单支付
   */
  @getmapping("gopay")
  public map<object, object> gopay() throws exception{
    /** 模仿数据库,从后台调数据*/
    string outtradeno = "202101010001";
    bigdecimaltotalamount = new bigdecimal(10000);
    string subject = "苹果12";
    
    string pay = alipayservice.gopay(outtradeno, totalamount, subject);
    
    map<object, object> pays = new hashmap<>();
    pays.put("pay", pay);
    
    return pays;
  }
  
  /**
   * 交易查询
   */
  @postmapping("aipayquery")
  public result<object> alipayquery() throws exception{
    /**调取支付订单号*/
    string outtradeno = "13123";
    
    string query = alipayservice.query(outtradeno);
    
    object json = jsonobject.tojson(query);
    
    /*jsonobject jobject = new jsonobject();
    jobject.get(query);*/
    return result.success(json);
  }
  
  /**
   * 交易关闭
   * @throws alipayapiexception
   */
  @postmapping("alipayclose")
  public result<object> alipaycolse() throws alipayapiexception{
    
    /** 调取数据*/
    string outtradeno = "13123";
    
    string close = alipayservice.close(outtradeno);
    
    return result.success(close);
  }
  /**
     * 异步通知支付结果
     *
     * @param request
     * @return string
     * @throws alipayapiexception
     * @throws parseexception
     */
    @postmapping("/callback")
    public string alipaynotify(httpservletrequest request) throws exception {
 
        // 获取支付宝的请求信息
        map<string, string> map = new hashmap<>();
        map<string, string[]> requestparams = request.getparametermap();
        if(requestparams.isempty()) {
            return "failure";
        }
        // 将 map<string,string[]> 转为 map<string,string>
        for (iterator<string> iter = requestparams.keyset().iterator(); iter.hasnext();) {
            string name = iter.next();
            string[] values = requestparams.get(name);
            string valuestr = "";
            for (int i = 0; i < values.length; i++) {
                valuestr = (i == values.length - 1) ? valuestr + values[i] : valuestr + values[i] + ",";
            }
            map.put(name, valuestr);
        }
        // 验签
        boolean signverified = alipaysignature.rsacheckv1(map, alipayconfig.alipay_public_key, alipayconfig.charset,
                alipayconfig.sign_type);
        // 验签通过
        if (signverified) {
            //支付成功后进行操作
        }
        return "failure";
    }
}

到此springboot整合支付宝扫码支付,就完成了

到此这篇关于java中spring boot支付宝扫码支付及支付回调的实现代码的文章就介绍到这了,更多相关spring boot支付宝扫码支付内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/qq954724583/article/details/113860419

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部