一:支付宝对接流程图
对接多用到的相关参数:
参数 | 说明 | |
---|---|---|
APPID | ||
商家私钥 | ||
支付宝公钥 | ||
支付回调地址 | ||
网关地址 | ||
加密签名算法RSA2 |
二:申请注册
1.访问一下网页注册企业账号:
https://openhome.alipay.com/platform/developerIndex.htm
2.登录之后,创建应用:
3.生成公钥和私钥
访问沙箱环境开发助手
https://opendocs.alipay.com/open/291/105971#LDsXr
生成应用公钥和私钥
应用公钥:
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjfji0NC5l8B6Z2/e/D3MHgB2lDcc/n6Q2HXFFTXbu2LQui3gNdB5E43IOtf82JjnvJbL9aM9XRAY8V9gNE6cy1xrTbEJgwmm35VY5NJTxRHqsOxnoI6gUmOMI+oBcDaP71ZHSfWFFI2rvgcXysJbgoMwCTkxr1t+UqIlc7fLkT0dkcBAfvCGjVk+spu0aHFFl/knpfRvyB8lyq+aXPrZ47WoYEQhqDIvy754Dm/Reis3EG/YYLRoRjna3eoWVqI9ZGgzEBhq1DFeA8w5o8bXcyxII+3GAdypRrS7oaaEgeXe+lBkXDjPxWXlqQ587eTtSm+bsF8N9YEypB8LkqcNBQIDAQAB
应用私钥:
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCN+OLQ0LmXwHpnb978PcweAHaUNxz+fpDYdcUVNdu7YtC6LeA10HkTjcg61/zYmOe8lsv1oz1dEBjxX2A0TpzLXGtNsQmDCabflVjk0lPFEeqw7GegjqBSY4wj6gFwNo/vVkdJ9YUUjau+BxfKwluCgzAJOTGvW35SoiVzt8uRPR2RwEB+8IaNWT6ym7RocUWX+Sel9G/IHyXKr5pc+tnjtahgRCGoMi/LvngOb9F6KzcQb9hgtGhGOdrd6hZWoj1kaDMQGGrUMV4DzDmjxtdzLEgj7cYB3KlGtLuhpoSB5d76UGRcOM/FZeWpDnzt5O1Kb5uwXw31gTKkHwuSpw0FAgMBAAECggEAA4W7SucC08sIvmw+Ig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XYW4CKYIWBqknJhb0m+boSfhynqAnzVoFLXxA+sK4kn9JRZ7vQyx/hP81yhHqLiMGoJAAVcumv1GcjWVscqG+GJ836O+bon3UUSVbQ5HD+Iio+MjS91cLtMl2Gz7vn7/VuzF9qPWC7V
支付宝公钥:
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyp3KWAEUIDDGSB9pEm9lf/IPpMKxRdbtPv69YgiujCp1sTiiHXJo91qJ8rCY2lJdP4HR7c8oIzvqM4zn9FHQwJpSNwczey4CzymrmIyZVGjolD9BW0h4lDEwtgxyfRBZOZ1PDrduh4EHPXscjAuqL69dYFAGkx8drMYYVCHp9hfynP6JintoV9Ksh8Cn9Q/FBZwgcse9EukvAvWtzqSMkooQ/VeyTfr/+Y44Vci7NPjHQkgC9ZeeZA/bGbVYzto3BYrtAtUHfThmaSPpxlrq5risZcxJ5/m/ZTpCyBwB/DaYAPfzhf8XMODNXyHr8F/1QlKAfPncnF3dJTkKRr8qowIDAQAB
用应用公钥生成支付公钥
至此获取支付宝支付所需的所有参数
三:开始开发
支付
@ApiImplicitParam("创建订单预支付")
@RequestMapping("/doPost")
public String doPost(AlipayParam alipayParam) {
AlipayClient alipayClient = new DefaultAlipayClient(serverUrl, appId, privateKey, format, charset, publicKey, signType);
AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();//创建API对应的request
alipayRequest.setReturnUrl("{returnUrl}");
alipayRequest.setNotifyUrl("{notifyUrl}");//在公共参数中设置回跳和通知地址
Map<String, String> map = new HashMap<>();
map.put("out_trade_no", alipayParam.getOrderId());
map.put("total_amount", alipayParam.getAmount());
map.put("subject", alipayParam.getSubject());
alipayRequest.setBizContent(JSONObject.toJSONString(map));
try {
AlipayTradeWapPayResponse response = alipayClient.pageExecute(alipayRequest);
if (response.isSuccess()) {
System.out.println("调用成功");
} else {
System.out.println("调用失败");
}
log.info("支付宝支付结束,响应为:{}", JSON.toJSON(response));
return response.getBody();
} catch (Exception e) {
log.error("【 Ali pay 异常 】", e);
}
return "fail";
}
交易查询
@ApiImplicitParam("交易查询")
@RequestMapping("dealQuery")
public String query(AlipayParam alipayParam) throws Exception {
AlipayClient alipayClient = new DefaultAlipayClient(serverUrl, appId, privateKey, format, charset, publicKey, signType); //获得初始化的AlipayClient
AlipayTradeQueryRequest request = new AlipayTradeQueryRequest(); //创建API对应的request类
Map<String, String> map = new HashMap<>();
map.put("out_trade_no", alipayParam.getOrderId());
request.setBizContent(JSONObject.toJSONString(map)); //设置业务参数
AlipayTradeQueryResponse response = alipayClient.execute(request); //通过alipayClient调用API,获得对应的response类
System.out.print(response.getBody());
//根据response中的结果继续业务逻辑处理
return response.getBody();
}
退款
@ApiImplicitParam("退款")
@RequestMapping("refund")
public String refund(AlipayRefundParam alipayRefundParam) throws Exception {
AlipayClient alipayClient = new DefaultAlipayClient(serverUrl, appId, privateKey, format, charset, publicKey, signType); //获得初始化的AlipayClient
AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();//创建API对应的request类
Map<String, String> map = new HashMap<>();
map.put("out_trade_no", alipayRefundParam.getOrderId());
map.put("out_request_no", alipayRefundParam.getOut_request_no());
map.put("refund_amount", alipayRefundParam.getRefund_amount());
request.setBizContent(JSONObject.toJSONString(map)); //设置业务参数
AlipayTradeRefundResponse response = alipayClient.execute(request);//通过alipayClient调用API,获得对应的response类
System.out.print(response.getBody());
//根据response中的结果继续业务逻辑处理
return response.getBody();
}
@ApiImplicitParam("退款交易查询")
@RequestMapping("refundQuery")
public String refundQuery(AlipayRefundParam alipayRefundParam) throws Exception {
AlipayClient alipayClient = new DefaultAlipayClient(serverUrl, appId, privateKey, format, charset, publicKey, signType);
AlipayTradeFastpayRefundQueryRequest request = new AlipayTradeFastpayRefundQueryRequest();
Map<String, String> map = new HashMap<>();
map.put("out_trade_no", alipayRefundParam.getOrderId());
map.put("out_request_no", alipayRefundParam.getOut_request_no());
request.setBizContent(JSONObject.toJSONString(map)); //设置业务参数
AlipayTradeFastpayRefundQueryResponse response = alipayClient.execute(request);
if (response.isSuccess()) {
System.out.println("调用成功");
} else {
System.out.println("调用失败");
}
return response.getBody();
}
@ApiImplicitParam("交易关闭")
@RequestMapping("tradingClosed")
public String tradingClosed(AlipayRefundParam alipayRefundParam) throws Exception {
AlipayClient alipayClient = new DefaultAlipayClient(serverUrl, appId, privateKey, format, charset, publicKey, signType);
AlipayTradeCloseRequest request = new AlipayTradeCloseRequest();
Map<String, String> map = new HashMap<>();
map.put("out_trade_no", alipayRefundParam.getOrderId());
request.setBizContent(JSONObject.toJSONString(map)); //设置业务参数
AlipayTradeCloseResponse response = alipayClient.execute(request);
if (response.isSuccess()) {
System.out.println("调用成功");
} else {
System.out.println("调用失败");
}
return response.getBody();
}
@ApiImplicitParam("支付宝手机网站支付验签/异步回调")
@RequestMapping("/notify")
public void notifyUrl(HttpServletRequest request, HttpServletResponse response) throws Exception {
PrintWriter out = response.getWriter();
//获取支付宝POST过来反馈信息
Map<String, String> params = new HashMap<String, String>();
Map requestParams = request.getParameterMap();
for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext(); ) {
String name = (String) iter.next();
String[] values = (String[]) requestParams.get(name);
String valueStr = "";
for (int i = 0; i < values.length; i++) {
valueStr = (i == values.length - 1) ? valueStr + values[i]
: valueStr + values[i] + ",";
}
//乱码解决,这段代码在出现乱码时使用。如果mysign和sign不相等也可以使用这段代码转化
//valueStr = new String(valueStr.getBytes("ISO-8859-1"), "gbk");
params.put(name, valueStr);
}
//获取支付宝的通知返回参数,可参考技术文档中页面跳转同步通知参数列表(以下仅供参考)//
//商户订单号
String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"), "UTF-8");
//支付宝交易号
String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"), "UTF-8");
//交易状态
String trade_status = new String(request.getParameter("trade_status").getBytes("ISO-8859-1"), "UTF-8");
//获取支付宝的通知返回参数,可参考技术文档中页面跳转同步通知参数列表(以上仅供参考)//
//计算得出通知验证结果
//boolean AlipaySignature.rsaCheckV1(Map<String, String> params, String publicKey, String charset, String sign_type)
boolean verify_result = AlipaySignature.rsaCheckV1(params, publicKey, charset, signType);
if (verify_result) {//验证成功
//
//请在这里加上商户的业务逻辑程序代码
//——请根据您的业务逻辑来编写程序(以下代码仅作参考)——
if (trade_status.equals("TRADE_FINISHED")) {
//判断该笔订单是否在商户网站中已经做过处理
//如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序
//请务必判断请求时的total_fee、seller_id与通知时获取的total_fee、seller_id为一致的
//如果有做过处理,不执行商户的业务程序
//注意:
//如果签约的是可退款协议,退款日期超过可退款期限后(如三个月可退款),支付宝系统发送该交易状态通知
//如果没有签约可退款协议,那么付款完成后,支付宝系统发送该交易状态通知。
} else if (trade_status.equals("TRADE_SUCCESS")) {
//判断该笔订单是否在商户网站中已经做过处理
//如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序
//请务必判断请求时的total_fee、seller_id与通知时获取的total_fee、seller_id为一致的
//如果有做过处理,不执行商户的业务程序
//注意:
//如果签约的是可退款协议,那么付款完成后,支付宝系统发送该交易状态通知。
}
//——请根据您的业务逻辑来编写程序(以上代码仅作参考)——
out.println("success"); //请不要修改或删除
//
} else {//验证失败
out.println("fail");
}
}
其他可参考支付宝对接API文档:
https://opendocs.alipay.com/apis/api_1/alipay.trade.page.pay