支付api目前国内属支付宝和微信两家独大。最难理解的是微信支付。支付宝相对扫好点。
支付宝支付 首先在蚂蚁金开放平台申请 账号 获得到一个 APPID 商户私密 merchant_private_key PKCS8格式RSA2私钥和支付宝支付宝公钥 alipay_public_key
支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
具体细节参照支付宝介入文档。https://docs.open.alipay.com/api_1
支付宝APP支付业务流程 官方流程
public static Map<String,String> PayWayForApp(String content,String subject,String orderNumber,String timeOut,
String amount,String productId) {
Map<String,String> res = new HashMap<>();
AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
model.setBody(subject);
model.setSubject(content);
model.setOutTradeNo(orderNumber);
model.setTimeoutExpress(timeOut);
model.setTotalAmount(amount);
model.setProductCode("QUICK_MSECURITY_PAY");
request.setBizModel(model);
AlipayTradeAppPayResponse response=null;
try {
response= alipayClient.sdkExecute(request);
String resPay=URLDecoder.decode(response.getBody(),"utf-8");
JSONObject jsonObject =JSON.parseObject(resPay.split("&")[2].split("=")[1]);
res.put("out_trade_no",jsonObject.getString("out_trade_no"));
res.put("timestamp",resPay.split("&")[8].split("=")[1]);
res.put("body",jsonObject.getString("body"));
res.put("product_code",jsonObject.getString("product_code"));
res.put("subject",jsonObject.getString("subject"));
res.put("timeout_express",jsonObject.getString("timeout_express"));
res.put("total_amount",jsonObject.getString("total_amount"));
res.put("sign",resPay.split("&")[6].split("=")[1]);
} catch (AlipayApiException e) {
e.printStackTrace();
res.put(e.getErrCode(),e.getErrMsg());
return res;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return res;
}
上图中的是 当用户使用APP下订单,提交选购商品 调用线下与交易接口获得支付数据,然后将支付数据发送给调用端
需要注意 支付宝的支付最小单位为元 微信支付的最先单位为分
在APP支付时 调用 alipayClient.sdkExecute(request); 二维码支付时 使用 response = alipayClient.execute(request);
二维码支付所需的支付链接 可直接获得response.getQrCode()
当支付完成后,在预支付的时候设置的 notifyUrl 支付宝服务器会进行回调。 要确保改url是可以公网访问的。
支付宝Post发过来的数据 需要进行签名认证 确保数据来源可靠
public static String getResultPay(HttpServletRequest request){
Map<String,String> params = new HashMap<String,String>();
Map requestParams = request.getParameterMap();
String orderNum;
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] + ",";
if (i==8){
orderNum=values[i];
}
}
//乱码解决,这段代码在出现乱码时使用。
//valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
params.put(name, valueStr);
}
boolean flag=false;
try {
flag = AlipaySignature.rsaCheckV1(params, AlipayConfig.alipay_public_key, AlipayConfig.charset,"RSA2");
} catch (AlipayApiException e) {
e.printStackTrace();
}
if (flag){
return params.get("out_trade_no");
}else {
return "fail";
}
}
注意 支付宝服务器在用户支付成功后会分多次回调用户设置的接口,在此业务开发需要做好处理。避免重复修改改定单状态。
APP支付 需要进行二次确认 即APP在支付完成后要在此向服务发起请求,确认该笔订单是否支付成功,支付宝服务器也会POST过来数据。
到此 支付宝支付模块就完成了