本文只接入了支付宝中的APP支付,如果要拓展更多支付方式的的话,请看文末补充
项目支付流程
- 前端发起创建订单请求
- 后端接受请求创建订单,并将订单参数进行支付宝对应签名并返回
- 前端拿到签名后调起支付宝支付
- 本文主要写的就是2的过程
前期准备工作:
- 创建应用
- 创建公私钥
- 进入沙箱环境
- 进行沙箱模拟测试
- 代码
一:创建应用
登录:https://openhome.alipay.com/develop/manage 按照提示对应创建自己的应用
二:创建公私钥
-
在支付宝应用,开发设置,设置接口加签方式(
我使用密钥加签,证书加密太复杂还没有研究
)
-
按照指示下载
支付宝开放平台密钥工具应用
并进行公私钥生成支付宝公钥
三:回到开发平台页面点击进入沙箱
或者直接点击链接:https://open.alipay.com/api/apiDebug
四:进行沙箱模拟测试
- 根据业务需求选择合适的API进行测试,模拟出来的签名数据前端拿出调用就行(
我已经找前端测试过了,是可以调起支付宝进行支付的
)
五:代码
- 导入maven依赖
<!--支付宝SDK-->
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>4.38.52.ALL</version>
</dependency>
AliPayUtils.java
https://open.alipay.com/develop/sandbox/app 可以获取公私钥和appId和卖家买家信息
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.domain.AlipayTradeCreateModel;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.request.AlipayTradeCreateRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.alipay.api.response.AlipayTradeCreateResponse;
/**
* @author miao
*/
public class AliPayUtils {
private static final String ALI_PAY_PRIVATE_KEY = "你生成的私钥/自定义私钥";
private static final String ALI_PAY_PUBLIC_KEY = "你生成的公钥/自定义公钥";
private static final String APP_ID = "沙箱中appId";
private static AlipayConfig alipayConfig;
static {
alipayConfig = new AlipayConfig();
//服务地址,固定写死
alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
alipayConfig.setAppId(APP_ID);
alipayConfig.setPrivateKey(ALI_PAY_PRIVATE_KEY);
alipayConfig.setFormat("json");
alipayConfig.setAlipayPublicKey(ALI_PAY_PUBLIC_KEY);
alipayConfig.setCharset("UTF8");
alipayConfig.setSignType("RSA2");
}
public static String createAppTradePay(String orderId, String totalAmount, String subject) throws AlipayApiException {
AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
model.setOutTradeNo(orderId);
model.setTotalAmount(totalAmount);
model.setSubject(subject);
request.setBizModel(model);
AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
if (response.isSuccess()) {
return response.getBody();
}
return null;
}
public static String createTradeCreate(String orderId, String totalAmount, String subject,String buyerId) throws AlipayApiException {
AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
AlipayTradeCreateRequest request = new AlipayTradeCreateRequest();
AlipayTradeCreateModel model = new AlipayTradeCreateModel();
model.setOutTradeNo(orderId);
model.setTotalAmount(totalAmount);
model.setSubject(subject);
model.setBuyerId(buyerId);
request.setBizModel(model);
AlipayTradeCreateResponse response = alipayClient.execute(request);
if (response.isSuccess()) {
//目前只知道这个sign有用,但是还没具体用到
return JSONObject.parseObject(response.getBody()).getString("sign");
}
return null;
}
}
- 测试类
public static void main(String[] args) throws AlipayApiException {
// String result = AliPayUtils.createAppTradePay("123456", "19.52", "测试APP支付");
// System.out.println(result);
String result = AliPayUtils.createTradeCreate("123456","111222","测试当面付","2中沙箱账号的买家uid");
System.out.println(result);
}
如果还要接入支付宝的其他支付,直接通过沙箱查看文档进行测试即可
- 希望我的文章能给你带来帮助,我接入的时候看到别人的文章有点乱,如果有问题请评论,我们一起交流