赞
踩
在众多项目中不可或缺的就是支付功能,如何实现模拟付款?这篇文章带你揭秘支付宝沙箱支付
支付宝沙箱支付(Alipay Sandbox Payment
)是支付宝提供的一个模拟支付环境,用于开发和测试支付宝支付功能的开发者工具。
沙箱环境提供了一个安全、隔离的环境,使开发者能够模拟支付过程,测试支付功能,而不会使用真实资金。
使用支付宝沙箱支付,开发者可以更安全、更有效地进行支付功能的开发和测试,避免了对真实环境的影响和风险。一旦支付功能在沙箱环境中验证通过,开发者可以将其部署到真实的支付宝生产环境中,与真实用户进行交互和支付。
登陆支付宝:https://openhome.alipay.com/
点击登录,用自己手机的支付宝扫码授权登录
登录成功之后,点击旁边的控制台,找到开发工具推荐,选择沙箱
下载安装支付宝开放平台开发助手:https://opendocs.alipay.com/common/02kipk
点击工具下载安装对应版本(注意不要安装在含有空格的目录路径下,否则会导致公私钥乱码的问题)
打开支付宝开放平台开发助手,选择密钥方式,选择RSA2
方式,最后点击生成密钥即可生成得到私钥和公钥。
找到沙箱应用->开发信息,选择自定义密钥
生成支付宝公钥:
下载沙箱支付宝(只支持安卓):https://open.alipay.com/develop/sandbox/tool/alipayclint
找到沙箱工具,点击类似支付宝图标,手机扫描进行下载(方便后续付款)
注意:下载完成之后,选择支付宝账号登录,填入沙箱账号的买家的账号以及密码进行登录
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-easysdk</artifactId>
<version>2.0.1</version>
</dependency>
开发文档:https://opendocs.alipay.com/common/02kg69?pathHash=17912ef4
package com.zking.starshop.config; import com.alipay.easysdk.factory.Factory; import com.alipay.easysdk.kernel.Config; import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse; import com.zking.starshop.pojo.Order; import org.springframework.stereotype.Component; public class AlipayConfig { private Config aliconfig() { Config config = new Config(); //沙箱支付宝地址 config.gatewayHost = "openapi-sandbox.dl.alipaydev.com"; //协议https config.protocol = "https"; //应用ID,您的APPID,收款账号既是您的APPID对应支付宝账号 config.appId = "9021000133665552"; //支付宝公钥 config.alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgSS47Rb0eRR03JtilfxRRYpT9bySklR7Mwdik9WhdWJ4h43qk88AUVLqJFgLChj386iBqxBZCqGr4V/tNHocn1faX4wtOgYCtPlT4YGQJwFsd6h7sdFoC9xXYUnR89FedUoFmZwNv4F0Cg9M8e/wzQis+X7CvYdtaKfsoFFkMcVjjEEibnOi6X4IBgA1XmtYmaIP816gG7i9Olo4/qJYEG/epcLAjo8p3DOK2lIg9dlGaMqRxIFn1rT4GOgzmWnxLcW1lZjg7PCtmYvRb+eS50kVL0eUxJPcEQkgInZ2ZGUx5solDNgutFpTySfboEt19KOL9HBCXwxRT79vT0IaIwIDAQAB"; //签名方式 config.signType = "RSA2"; //商户私钥(应用私钥),您的PKCS8格式RSA2私钥(支付宝开放平台) config.merchantPrivateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCL1GjHWX7tpctmSh/ohBz6CoDz5uWOiWUrOS0oqO7ywQMQafB13IoXivmEYwoJqPFjeSdm+5UvW+jZnsgOdO8P4dxyD7jTeeOMaLHSr7JPisGH2qfGcvqFv9wE8qnyvO3RSgtzaMXdh0hzHysPIJ/+wKjcbqzpEA0seSoDQstSpXJO5LZPt93aaSP4MDjoG0omUEsLyWgsdqE68a761HCzJHeJicTZfE7v/d0/AntrCcEMHygJrDXJW8kke3Bp32Yb5XxMKQzMCY1775e6equ+sILB1IRkFYBXUB8xEQaM3cr2Z5rKz2UtX+K2XV3/slSdIgjbL/2V11aoqUFNpCS1AgMBAAECggEAK1ZyAxch19YC7Hkc2pvXv0dyICX4D+1Vhcf6zxdMt8BKitPKyzzHL/TK0NVBTy0lY4cxRyHCYyNBT/F+X7yuO2XtRQzKZADrHaF1O69ovG5s/QYv6bnJG5sTPhVXxvv3WV5xnhti9/3t3oBSgCXEcNiVfXxv6uwmqRoEg244Riy7Kkmuis65kHqtc8MyOAIG1sGS4bBUA4x+SHlG+4v9/ectdAZdU/6v1SlaYt2hQP/YWGEZeN9/U3SeUyowO/UFwvJ7kpiYzHFlcbUJaVYNDYuxBTYPZaQUvqyH5CEUCoui3X+PjH5XxPIG0i75zonEFAb1op391haFb3IPdzSRIQKBgQDQA+1s7clawtbte+3fG0eirZZA3V++yeCIhZ1aYFvhFkn0VziTNFA7055F4kDj69MsowqcdWlzWsPJcBAW4HPCIuT0cmxpnwKbmxNVtT3KNYrhV3Bgxk6lgRWNbGRDgBU7xxbARMABBa3m5NOY0IY/v7X9XQ5w1E5RJmjy4kT7SwKBgQCsFd6spAIEdpj+i8dELsb26dl6KLnZINc1LgZl4oZa2zNMggX1uWNHl98kqzMSOIUBmneiivjzmTGjDCHnoCQh5/WPJZtsKV6kAC+0E4/hyvpK/tH7ipNnJ0cXDalU5qzDf/JqdCO0Xzp0wgnfBv97orPQFe5S7j9fkrE/E+df/wKBgQDDaU0brW4gwzShOHDHzJrJKgUivlP5qTAzUEH36tmlZAjpbx6IuvY8nZnr0JZhBuME5c1MGciMQ1H15D5ZH1XfpGQtd2h8+JG8mDvboj1/vjjPZBL/9otfUxpqFs9B0gsgcvDegjdVPn6Vj5Vv00SqknBmqYqtV+SSu2CP+xwObQKBgBp9z2bSZMEdJNZi3E1Byj1uqJ0msd2cMaUtLaz3n2lTP2VyysoWPRNjbOWvOVB/xPIHPo2WSVbu6XZJQtrui0Fw/RuJVZDkGX6zzKQzMr42yitLaTYJEkIaCNaissOwMz5xbvc2Y9y7oDsjN/POlvwIgxfwmFzJ8pWa9pBYVxEvAoGAJacRD+sq2AMce8ycjwMETHlvFVATQN1c48XAkb6Pjp8OIFhKQ5qTLQvu0rZfQA/Db+WkU45uD6RIXi26O+FnzmmsMkML8UKKFZfBzTlbU4hEaJACUd0tpwkGxDwRm+LoD0WQrUxlWFRXIECjtuus1uW0ULZpwzu2qfP2n+1ugZg="; return config; } public String goAlipay(Order order) { try { // 1. 设置参数(全局只需设置一次) Factory.setOptions(aliconfig()); // 2. 发起API调用(subject商品标题、outTradeNo订单编号、totalAmount总金额、returnUrl异步通知地址) AlipayTradePagePayResponse response = Factory.Payment.Page() .pay("商城项目收款", order.getOid().toString(), order.getTotal().toString(), //支付成功之后的异步通知(跳出到自己系统的哪个位置) "http://localhost:8080/order/payDone"); return response.body; } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } }
找到订单生成接口:
@RequestMapping("/pay") public String toOrder(User user,String oid){ //根据订单id查询订单 Order order = orderService.getById(oid); //调用支付宝的支付功能 String body = new AlipayConfig().goAlipay(order); System.out.println(body); return body; } @RequestMapping("/payDone") public String toOrder(@RequestParam Map<String,String> ms) throws Exception { //验签 Boolean f = Factory.Payment.Common().verifyNotify(ms); if(!f){ throw new BusinessException(JsonResponseStatus.UN_KNOWN); } String oid = ms.get("out_trade_no");//获取到订单id //修改订单状态 orderService.update(new UpdateWrapper<Order>() .eq("oid",oid) .set("status",1) .set("pay_date",new Date())); return "<script>\n" + "\talert(\"支付成功!\")\n" + "\tlocation.href='/'\n" + "</script>"; }
以上就是支付宝沙箱支付的下载、配置以及效果演示
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。