您的位置:首页 > 其它

支付宝的集成与使用(第三方)

2016-10-24 15:48 357 查看
app开发中总是用到支付宝的集成,这个总结一下流程

一、1、https://doc.open.alipay.com/doc2/detail?treeId=59&articleId=103563&docType=1 

在这个链接下载移动支付demo(里面有三部分--openssl、服务端demo---可以将相应的demo引入工程中,有包的话注意包的引入、客户端demo)

--AlipayConfig.java这个文件要兑换相应的自己应用的信息

eg:

public static String partner =“pid”---这个需要修改

public static String private_key=“商户私钥”--这个需要修改

public static String ali_public_key=“支付宝公钥”----这个一般不需要修改--在合作伙伴秘钥管理--查看支付宝公钥就可以看到

2、生成相应的公钥、秘钥,一般下载的demo中就有生成说明

3、登录支付宝账户--用显示让你去签约--点击立即签约-签约管理--(就会看到你账户的签约产品(已经有签约的话))

4、在你的商户下面有查看pid 、key 点击需要输入支付密码--输入支付密码、就可以查看pid 、合作伙伴秘钥管理(在它下面rsa加密--点击添加密钥---将你生成的公钥加进去)

这样就可以和前端联调了

二、前端主要用到

支付宝账户、pid、生成的秘钥

三、微信回调

public Map<String, Object> weiXinPayOrderBack(HttpServletRequest request,
HttpServletResponse response) throws Exception {
Map<String,Object> resultMap = new HashMap<String,Object>();
// log.info("---------------微信支付通知接口【start】---------------");
String responseXML = "";
// HttpServletRequest request = this.getRequest();
// HttpServletResponse response = this.getResponse();
response.setContentType("text/html;charset=utf-8");
String return_code = "";
String result_code = "";
// 用户openid
String openid = "";
// 商户订单号
String out_trade_no = "";
// 微信交易订单号
String transaction_id = "";
// 交易完成时间
String time_end = "";
// 商家附加参数数据,微信会原样返回
String attach = "";
try {
InputStream inStream = request.getInputStream();
ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inStream.read(buffer)) != -1) {
outSteam.write(buffer, 0, len);
}
outSteam.close();
inStream.close();
String result = new String(outSteam.toByteArray(), "utf-8");// 获取微信调用我们notify_url的返回信息
// log.info("-----result-----" + result);
Map<Object, Object> map = XMLUtil.doXMLParse(result);
// log.info("-----map.size()-----" + map.size());
if (map != null) {
for (Object keyValue : map.keySet()) {
// log.info("-----keyValue-----" + keyValue + "="
// + map.get(keyValue));

switch (keyValue.toString()) {
case "return_code":
return_code = (String) map.get(keyValue);
break;
case "result_code":
result_code = (String) map.get(keyValue);
break;
case "openid":
openid = (String) map.get(keyValue);
break;
case "out_trade_no":
out_trade_no = (String) map.get(keyValue);
break;
case "transaction_id":
transaction_id = (String) map.get(keyValue);
break;
case "time_end":
time_end = (String) map.get(keyValue);
break;
case "attach":
attach = (String) map.get(keyValue);
break;
}
}
if ("SUCCESS".equals(return_code)
&& "SUCCESS".equals(result_code)) {
out_trade_no = out_trade_no.substring(0, 20); // 截取原始订单号
// log.info("-----out_trade_no(截取原始订单号)-----" + result);
// ------支付成功,修改订单状态等业务操作--------
updateOrderPay(out_trade_no);
// if (isUpdate) {
// log.info("--------------更新订单状态成功!--------------");
// } else {
// log.info("--------------更新订单状态失败!--------------");
// }
// log.info("--------------更新订单状态--------结束------");

// 向微信返回成功接收信息
responseXML = PayCommonUtil.setXML("SUCCESS", "");
resultMap.put("code", "200");
resultMap.put("msg", "支付成功");

// log.info("---------------微信支付通知接口【success】---------------");
} else {
// log.info("微信支付通知失败====无返回数据######");
responseXML = PayCommonUtil.setXML("FAIL", "");
resultMap.put("code", "500");
resultMap.put("msg", "支付失败");
}
}

} catch (Exception e) {
e.printStackTrace();
}
// log.info("---------------微信支付通知接口【end】---------------");
return resultMap;
}


四、支付宝回调
public Map<String, Object> alipayPayOrderBack(HttpServletRequest request,
HttpServletResponse response) throws Exception {
// log.info("---------------------/app/mobi/alipay/callback------------------------" );
Map<String,Object> resultMap = new HashMap<String,Object>();
Map<String,String> params = AliPayUtil.getAliPayMap(request, response);
//商户订单号
String paymentNo = 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");
//实际支付金额
String factAmount = new String(request.getParameter("total_fee").getBytes("ISO-8859-1"),"UTF-8");

// log.info("---------------------------- paymentNo : "+ paymentNo );
// log.info("---------------------------- trade_no : "+ trade_no );
// log.info("---------------------------- trade_status : "+ trade_status );
// log.info("---------------------------- pushRetabeId : "+ pushRetabeId );
// log.info("---------------------------- retabeAmount : "+ retabeAmount );
// log.info("---------------------------- factAmount : "+ factAmount );

if(AlipayNotify.verify(params)){//验证成功
// log.info("-------------------------- success ------------------------");
if(trade_status.equals("TRADE_FINISHED") || trade_status.equals("TRADE_SUCCESS")){
updateOrderPay(paymentNo);
}
response.getWriter().write("success");
resultMap.put("code", "200");
resultMap.put("msg", "支付成功");
}else{//验证失败
// log.info("-------------------------- error ------------------------");
response.getWriter().println("fail");
resultMap.put("code", "500");
resultMap.put("msg", "支付失败");
}
return resultMap;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: