沙箱部署方式:-SpringBoot整合沙箱支付(使用支付宝) - alipay

📅 2026-11-30 👤 拼多多助力营销技巧 📂 pdd帮助力 👁️ 290 次阅读

注册支付宝开发者账户,进入支付宝开发平台:

https://openhome.alipay.com/platform/developerIndex.htm

沙箱快捷入口:

https://open.alipay.com/develop/sandbox/app

内网穿透

内网穿透软件下载

沙箱测试方法:使用沙箱支付之前,我们要做的准备工具:

第一步:申请一个沙箱测试账号

支付宝沙箱测试账号申请 进入链接后扫码登录!

第二步:电脑下载一个支付宝提供的客户端用于生成RSA2

客户端工具下载(密钥生成操作文档)

下载安装完成后,开始使用,使用参考文档:帮助文档

后端

依赖

com.alipay.sdk

alipay-easysdk

2.2.0--按照自己本地的版本

com.alipay.sdk

alipay-sdk-java

配置

alipay:

gatewayUrl: https://openapi-sandbox.dl.alipaydev.com/gateway.do ---支付宝网址

appId: your appId ---支付宝沙箱的ID

alipayPublicKey: your alipayPublicKey ---沙箱的支付宝公钥

appPrivateKey: your appPrivateKey ---沙箱的私钥

returnUrl: http://localhost:8060/#/pages/money/paySuccess ---我用的vue2访问地址

notifyUrl:

实体类工具

@ConfigurationProperties(prefix = "alipay")

public class AlipayConfig {

/**

* 支付宝网关

*/

private String gatewayUrl;

/**

* 应用ID

*/

private String appId;

/**

* 应用私钥

*/

private String appPrivateKey;

/**

* 支付宝公钥

*/

private String alipayPublicKey;

/**

* 用户确认支付后,支付宝调用的页面返回路径

* 开发环境为:http://localhost:8060/#/pages/money/paySuccess

*/

private String returnUrl;

/**

* 支付成功后,支付宝服务器主动通知商户服务器里的异步通知回调(需要公网能访问)

* 开发环境为:http://localhost:8085/alipay/notify

*/

private String notifyUrl;

/**

* 参数返回格式,只支持JSON

*/

private String format = "JSON";

/**

* 请求使用的编码格式

*/

private String charset = "UTF-8";

/**

* 生成签名字符串所使用的签名算法类型

*/

private String signType = "RSA2";

}

配置的实体类

public class AlipayLay {

// 应用ID,您的APPID,收款账号既是您的APPID对应支付宝账号

public String appId;

// 应用私钥,就是工具生成的应用私钥

public String merchantPrivateKey;

// 支付宝公钥,对应APPID下的支付宝公钥。

public String alipayPublicKey;

// 支付宝会悄悄的给我们发送一个请求,告诉我们支付成功的信息

public String notifyUrl;

//同步通知,支付成功,一般跳转到成功页

public String returnUrl; 拼多多帮助力入口

// 签名方式

private String signType;

// 字符编码格式

private String charset;

//订单超时时间

private String timeout = "1m";

// 支付宝网关;https://openapi-sandbox.dl.alipaydev.com/gateway.do

public String gatewayUrl;

}

controller

@Operation(summary = "支付宝手机网站支付")

@RequestMapping(value = "/webPay", method = RequestMethod.GET)

public void webPay(AliPayParam aliPayParam, HttpServletResponse response) throws IOException {

response.setContentType("text/html;charset=" + alipayConfig.getCharset());

response.getWriter().write(alipayService.webPay(aliPayParam));

response.getWriter().flush();

response.getWriter().close();

}

serviceImpl

public String webPay(AliPayParam aliPayParam) {

AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest ();

if(StrUtil.isNotEmpty(alipayConfig.getNotifyUrl())){

//异步接收地址,公网可访问

request.setNotifyUrl(alipayConfig.getNotifyUrl());

}

if(StrUtil.isNotEmpty(alipayConfig.getReturnUrl())){

//同步跳转地址

request.setReturnUrl(alipayConfig.getReturnUrl());

}

//******必传参数******

JSONObject bizContent = new JSONObject();

//商户订单号,商家自定义,保持唯一性

bizContent.put("out_trade_no", aliPayParam.getOutTradeNo());

//支付金额,最小值0.01元

bizContent.put("total_amount", aliPayParam.getTotalAmount());

//订单标题,不可使用特殊符号

bizContent.put("subject", aliPayParam.getSubject());

//手机网站支付默认传值FAST_INSTANT_TRADE_PAY

bizContent.put("product_code", "QUICK_WAP_WAY");

request.setBizContent(bizContent.toString());

String formHtml = null;

try {

formHtml = alipayClient.pageExecute(request).getBody();

} catch (AlipayApiException e) {

e.printStackTrace();

}

return formHtml;

}

沙箱测试工具类

public static void main(String[] args) {

// 商户订单号,商户网站订单系统中唯一订单号,必填

String out_trade_no ="155464888"; // 随便写一个数字

// 订单名称,必填

String subject = "充值会员";

// 付款金额,必填

String total_amount="50";

// 商品描述,可空

String body = "会员厉害";

// 超时时间 可空

String timeout_express="2m";

// 销售产品码 必填

String product_code="QUICK_WAP_WAY";

/**********************/

// SDK 公共请求类,包含公共请求参数,以及封装了签名与验签,开发者无需关注签名与验签

//调用RSA签名方式

AlipayLay alipayLay = new AlipayLay();

// 支付宝公钥

alipayLay.setAlipayPublicKey("your alipayPublicKey");

alipayLay.setAppId("your appId");

alipayLay.setGatewayUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");

alipayLay.setCharset("UTF-8");

alipayLay.setSignType("RSA2");

// 私钥

alipayLay.setMerchantPrivateKey("your appPrivateKey");

alipayLay.setReturnUrl("www.baidu.com");

AlipayClient client = new DefaultAlipayClient(alipayLay.getGatewayUrl(), alipayLay.getAppId(), alipayLay.getMerchantPrivateKey(), "json", alipayLay.getCharset(), alipayLay.getAlipayPublicKey(), alipayLay.getSignType());

AlipayTradeWapPayRequest alipay_request = new AlipayTradeWapPayRequest();

// 封装请求支付信息

AlipayTradeWapPayModel model=new AlipayTradeWapPayModel();

model.setOutTradeNo(out_trade_no);

model.setSubject(subject);

model.setTotalAmount(total_amount);

model.setBody(body);

model.setTimeoutExpress(timeout_express);

model.setProductCode(product_code);

alipay_request.setBizModel(model);

// 设置异步通知地址

alipay_request.setNotifyUrl(alipayLay.getNotifyUrl());

// 设置同步地址

alipay_request.setReturnUrl(alipayLay.getReturnUrl());

// form表单生产

String form = "";

try {

// 调用SDK生成表单

form = client.pageExecute(alipay_request).getBody();

System.out.println("=============================================================");

System.out.println(form);

// response.setContentType("text/html;charset=" + AlipayConfig.CHARSET);

// response.getWriter().write(form);//直接将完整的表单html输出到页面

// response.getWriter().flush();

// response.getWriter().close();

} catch (AlipayApiException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

前端测试(我用的HBuilderX)

测试接口

接口方法

methods: {

//选择支付方式

changePayType(type) {

this.payType = type;

},

//确认支付

confirm: async function() {

if(USE_ALIPAY){

if(this.payType!=1){

uni.showToast({

title:"暂不支持微信支付!",

icon:"none"

})

return;

}

window.location.href = API_BASE_URL+"/alipay/webPay?outTradeNo=" + this.orderInfo.orderSn + "&subject=" + this.orderInfo.receiverName + "的商品订单" + "&totalAmount=" + this.orderInfo.totalAmount

}else{

payOrderSuccess({

orderId: this.orderId,

payType: this.payType

}).then(response => {

uni.redirectTo({

url: '/pages/money/paySuccess'

})

});

}

},

}

测试页面(电商)

调用支付宝小程序

调用你的支付宝沙箱程序

页面最后的效果

内网穿透

首先在本站注册账号

点击注册

登录后,点击左边 购买隧道,免费/付费均可根据需要选择隧道协议,这里以web演示,购买隧道本机建立web服务,如 nginx/apache/iis 等,默认80端口

确保 http://127.0.0.1 可以访问,如

5.在 natapp.cn 根据您的本机下载对应的客户端,比如我的本机是win10,64位,则下载Windows 64位的客户端

6.下载之后,解压至任意目录,得到natapp.exe (linux下无需解压,直接 wget)

7. windows下,直接双击natapp.exe 即可. cmd -authtoken= 参数方式运行. windows ,点击开始->运行->命令行提示符 后进入 natapp.exe的目录

运行

natapp -authtoken=9ab6b9040a624f40

9.运行成功,都可以得到如下界面:

10.将natapp分配的网址(上图Forwarding ),鼠标选定然后复制下来(选定之后单击鼠标右键),在浏览器中访问,可以看到内网穿透成功了!

内网穿透效果(测试一个你现有的端口号以及数据)

黑窗口运行测试!

按照我自己的个人了解写的笔记!

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧