Java对接支付宝支付功能(一)官方Demo调试

支付宝当面付功能官方Demo调试的相关内容

支付宝当面付官方Demo调试

配置沙箱环境

官方文档

点击“开放平台-开发者中心-沙箱环境”。进入沙箱环境页面,系统已经自动为你创建一个应用,在基础信息中可以看到应用信息。

  1. 生成并上传RSA2(SHA256)的应用公钥,详见生成RSA密钥;配置RSA2(SHA256)的应用公钥后,不需要配置RSA(SHA1)密钥,RSA和RSA2签名算法区别可以参考此处

  2. 编写代码时,请将

​ a.请求网关修改为:https://openapi.alipaydev.com/gateway.do

​ b.appid切换为沙箱的appid

​ c.签名方式使用RSA2

​ d.应用私钥使用第1步生成的RSA2(SHA256)的私钥(请根据开发语言进行选择原始或pkcs8格式)

​ e.支付宝公钥切换为第1步配置后应用公钥后,点击查看支付宝公钥看到的公钥

当面付demo演示

  1. 下载demo,地址:https://docs.open.alipay.com/194/105201/

  2. 将demo导入idea

  3. 配置支付相关的属性alipayinfo.properties

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
# 支付宝网关名、partnerId和appId
open_api_domain = https://openapi.alipaydev.com/gateway.do
mcloud_api_domain = http://mcloudmonitor.com/gateway.do
pid = 你的pid
appid = 你的appid

# RSA秘钥生成具体见官方文档
# 应用私钥公钥
private_key = 应用公钥
public_key = 应用私钥

#SHA1withRsa对应支付宝公钥
#alipay_public_key = 使用RSA2时,不用配置此项

#SHA256withRsa对应支付宝公钥
alipay_public_key = 支付宝公钥

# 签名类型: RSA->SHA1withRsa,RSA2->SHA256withRsa
sign_type = RSA2
# 当面付最大查询次数和查询间隔(毫秒)
max_query_retry = 5
query_duration = 5000
# 当面付最大撤销次数和撤销间隔(毫秒)
max_cancel_retry = 3
cancel_duration = 2000
# 交易保障线程第一次调度延迟和调度间隔(秒)
heartbeat_delay = 5
heartbeat_duration = 900
  1. 启动main方法

运行结果

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
一月 22, 2019 10:58:54 下午 com.alipay.demo.trade.config.Configs init
信息: 配置文件名: alipayinfo.properties
一月 22, 2019 10:58:54 下午 com.alipay.demo.trade.config.Configs init
信息: Configs{支付宝openapi网关: https://openapi.alipaydev.com/gateway.do
, 支付宝mcloudapi网关域名: http://mcloudmonitor.com/gateway.do
, pid: 2088102177123264
, appid: 2016092400588794
, 商户RSA私钥: MIIEvg******g1jt2A
, 商户RSA公钥: MIIBIj******IDAQAB
, 支付宝RSA公钥: MIIBIj******IDAQAB
, 签名类型: RSA2
, 查询重试次数: 5
, 查询间隔(毫秒): 5000
, 撤销尝试次数: 3
, 撤销重试间隔(毫秒): 2000
, 交易保障调度延迟(秒): 5
, 交易保障调度间隔(秒): 900
}
一月 22, 2019 10:58:54 下午 com.alipay.demo.trade.service.impl.AbsAlipayTradeService tradePrecreate
信息: trade.precreate bizContent:{"out_trade_no":"tradeprecreate15481691348134324306","seller_id":"","total_amount":"0.01","undiscountable_amount":"0","subject":"xxx品牌xxx门店当面付扫码消费","body":"购买商品3件共20.00元","goods_detail":[{"goods_id":"goods_id001","goods_name":"xxx小面包","quantity":1,"price":"10"},{"goods_id":"goods_id002","goods_name":"xxx牙刷","quantity":2,"price":"5"}],"operator_id":"test_operator_id","store_id":"test_store_id","extend_params":{"sys_service_provider_id":"2088100200300400500"},"timeout_express":"120m"}
一月 22, 2019 10:58:56 下午 com.alipay.api.internal.util.AlipayLogger logBizSummary
信息: Summary^_^10000^_^null^_^ProtocalMustParams:charset=utf-8&method=alipay.trade.precreate&sign=eT6bgbDSF33TeESmRaqWCzfKZ5FB23e4ul6IOdPR9y1Lk6fKEWAhIWW/H0PWt4ttzsyhazy/HnoP4rSNBA11MW6FkK7EA7bwvU+V/kuDEMer4mbq9bPZ44zq5XHUW2wWxrGEIXWGa0O2UwFg1ahWDJCeXGUYqndOFtgMLeuKsShEZ7l/6t9xfMEQSUyfzutucqgGGBApA3YNRpxa7Dh0zD1PZw0yqe0qDpEp2mbNvOEYWilaLS1wtHwOaOzNYKXJ+9xfrz1YMjh6R0ikbIRGdKxPuTLiRB7F8pLim/QwvZYC/n0O9tmIF1X7CrN44Vimhy23IY3VEWbjuGKD4jvM8Q==&version=1.0&app_id=2016092400588794&sign_type=RSA2&timestamp=2019-01-22 22:58:54^_^ProtocalOptParams:alipay_sdk=alipay-sdk-java-3.3.0&format=json^_^ApplicationParams:biz_content={"out_trade_no":"tradeprecreate15481691348134324306","seller_id":"","total_amount":"0.01","undiscountable_amount":"0","subject":"xxx品牌xxx门店当面付扫码消费","body":"购买商品3件共20.00元","goods_detail":[{"goods_id":"goods_id001","goods_name":"xxx小面包","quantity":1,"price":"10"},{"goods_id":"goods_id002","goods_name":"xxx牙刷","quantity":2,"price":"5"}],"operator_id":"test_operator_id","store_id":"test_store_id","extend_params":{"sys_service_provider_id":"2088100200300400500"},"timeout_express":"120m"}^_^567ms,524ms,22ms
一月 22, 2019 10:58:56 下午 com.alipay.demo.trade.service.impl.AbsAlipayService getResponse
信息: {"alipay_trade_precreate_response":{"code":"10000","msg":"Success","out_trade_no":"tradeprecreate15481691348134324306","qr_code":"https:\/\/qr.alipay.com\/bax06491yqcl41q5rnzb00e8"},"sign":"K68FrpzgPWkN2whVqVuzBLMr35h2TA/dm7VJWdoxsm/KIwsJS8bFI+hTe35bUP3C8Xu+lY4+SOuJdDZUQuM2P9NADt/lYViIigeWVy65BLcsU3qNKvBk/iAqY1zduXx3MjcxUzAUidpk6OnL6hG0uBPmYqlXL3u2jZB1FwdzmZo/iOjXq0Kg0I7tYUbiIIUy28he/dpxtaX7Iha5iHAB7nx0y0M7KoihmF8l6+sr9QGfm+N+DCqBWWQa336RieY/uxz/c8CF4Jexz8ahXf0/+EZJjA6+48umU3OL3lEnOgMoom2xB0IsEP0wsMI4HKagv/h21+YuaBH33CuzyyUkDQ=="}
一月 22, 2019 10:58:56 下午 com.alipay.demo.trade.Main test_trade_precreate
信息: 支付宝预下单成功: )
一月 22, 2019 10:58:56 下午 com.alipay.demo.trade.Main dumpResponse
信息: code:10000, msg:Success
一月 22, 2019 10:58:56 下午 com.alipay.demo.trade.Main dumpResponse
信息: body:{"alipay_trade_precreate_response":{"code":"10000","msg":"Success","out_trade_no":"tradeprecreate15481691348134324306","qr_code":"https:\/\/qr.alipay.com\/bax06491yqcl41q5rnzb00e8"},"sign":"K68FrpzgPWkN2whVqVuzBLMr35h2TA/dm7VJWdoxsm/KIwsJS8bFI+hTe35bUP3C8Xu+lY4+SOuJdDZUQuM2P9NADt/lYViIigeWVy65BLcsU3qNKvBk/iAqY1zduXx3MjcxUzAUidpk6OnL6hG0uBPmYqlXL3u2jZB1FwdzmZo/iOjXq0Kg0I7tYUbiIIUy28he/dpxtaX7Iha5iHAB7nx0y0M7KoihmF8l6+sr9QGfm+N+DCqBWWQa336RieY/uxz/c8CF4Jexz8ahXf0/+EZJjA6+48umU3OL3lEnOgMoom2xB0IsEP0wsMI4HKagv/h21+YuaBH33CuzyyUkDQ=="}
一月 22, 2019 10:58:56 下午 com.alipay.demo.trade.Main test_trade_precreate
信息: filePath:/Users/sudo/Desktop/qr-tradeprecreate15481691348134324306.png

交易创建的参数

  • 发送
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
{
"out_trade_no": "tradeprecreate15481691348134324306", //外部交易订单号
"seller_id": "", //销售id
"total_amount": "0.01", //总金额
"undiscountable_amount": "0", //不参加折扣的金额
"subject": "xxx品牌xxx门店当面付扫码消费", //标题
"body": "购买商品3件共20.00元", //购买说明
//商品详情
"goods_detail": [
{
"goods_id": "goods_id001", //商品id
"goods_name": "xxx小面包", //商品名称
"quantity": 1, //商品数量
"price": "10" //商品单价
},
{
"goods_id": "goods_id002",
"goods_name": "xxx牙刷",
"quantity": 2,
"price": "5"
}
],
"operator_id": "test_operator_id", //操作员的id
"store_id": "test_store_id", //店铺id
//扩展字段
"extend_params": {
"sys_service_provider_id": "2088100200300400500" //在回调时使用,交互时常用
},
"timeout_express": "120m" //交易超时时间,120分钟
}
  • 响应
1
2
3
4
5
6
7
{
"code": "10000", //响应码
"msg": "Success", //信息
"out_trade_no": "tradeprecreate15481691348134324306", //外部交易订单号
"qr_code": "https:\/\/qr.alipay.com\/bax06491yqcl41q5rnzb00e8" //二维码,可以通过base64方式传给前端展示,或者将二维码上传到后台ftp服务器进行展示
},"sign": "K68FrpzgPWkN2whVqVuzBLMr35h2TA/dm7VJWdoxsm/KIwsJS8bFI+hTe35bUP3C8Xu+lY4+SOuJdDZUQuM2P9NADt/lYViIigeWVy65BLcsU3qNKvBk/iAqY1zduXx3MjcxUzAUidpk6OnL6hG0uBPmYqlXL3u2jZB1FwdzmZo/iOjXq0Kg0I7tYUbiIIUy28he/dpxtaX7Iha5iHAB7nx0y0M7KoihmF8l6+sr9QGfm+N+DCqBWWQa336RieY/uxz/c8CF4Jexz8ahXf0/+EZJjA6+48umU3OL3lEnOgMoom2xB0IsEP0wsMI4HKagv/h21+YuaBH33CuzyyUkDQ=="
}

可以用二维码生成器将返回的qr_code生成,用支付宝沙箱版进行扫码支付测试

至此,支付宝当面付的官方demo就调通了,可以进行后续开发