微信公众号支付整体流程记录备忘
- - 移动开发 - ITeye博客相比支付宝支付,微信公众号支付的实现以及过程真的是比较复杂,而且坑多,都是血泪史. 首先,需要登录微信公众平台, https://mp.weixin.qq.com. 查看微信支付的开发配置,这里就可以看到对应的支付授权目录以及测试目录,可以选择使用线上作为支付测试,但是不推荐. 使用测试授权目录时,注意需要设置测试白名单,规定哪些人可以进行支付测试.
<xml>
<appid><![CDATA[wx1exxxx]]></appid>
<body><![CDATA[JSAPI_payment_test]]></body>
<mch_id>1242312122</mch_id>
<nonce_str><![CDATA[6aghlqz18duhfebole531dce0r7bw0td]]></nonce_str>
<notify_url><![CDATA[http://xxxx.com/xxx]]></notify_url>
<openid><![CDATA[ogGCluNRaxBTNFWZzS_kH-rRez_Q]]></openid>
<out_trade_no><![CDATA[nraxbtnfwzzskhrrezq1434590817259]]></out_trade_no>
<spbill_create_ip><![CDATA[119.161.230.131]]></spbill_create_ip>
<total_fee>1</total_fee>
<trade_type><![CDATA[JSAPI]]></trade_type>
<sign><![CDATA[F415B11A1C1B4894085FD703CBD14B71]]></sign>
</xml>
<xml> <return_code><![CDATA[SUCCESS]]></return_code> <return_msg><![CDATA[OK]]></return_msg> <appid><![CDATA[xxx]]></appid> <mch_id><![CDATA[1212]]></mch_id> <nonce_str><![CDATA[amxU3MOLatSWVzua]]></nonce_str> <sign><![CDATA[E458BE2C4F23C6F22B7561E74F41DEEF]]></sign><result_code><![CDATA[SUCCESS]]></result_code> <prepay_id><![CDATA[wx201506180927207ee0b107300739613144]]></prepay_id> <trade_type><![CDATA[JSAPI]]></trade_type> </xml>
https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri={{url}}%2Fwxpay%2FpayModelAndView?parameterName=${xxxx}&response_type=code&scope=snsapi_base&state=123#wechat_redirect
${url}/wxpay/payModelAndView?
https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
{
"access_token": "OezXcEiiBSKSxW0eoylIeGhaJjUxzVpRR4o6hX-jAhOn160_GRNWPwzcWR_QSO4gbjzWHPV6zuNazuJp3spc2gptHLcR-g2QetMKeDGZ3IJD6PbJCf2YKyw6k4aeiFbdJgfJgNBXKfZ0dPb98IKR_w",
"expires_in": 7200,
"refresh_token": "OezXcEiiBSKSxW0eoylIeGhaJjUxzVpRR4o6hX-jAhOn160_GRNWPwzcWR_QSO4g7r7Y2BQy_p7bmrjxH8YN3scFXn7C4fUnNn9AFDcz_qW5ErAi4Lp9p18PcLv60yUtOBSwd8MfDIKap12lVExOAg",
"openid": "ogGCluNRaxBTNFWZzS_kH-rRez_Q",
"scope": "snsapi_base"
}
public String getSign(Map<String, String> items, String APISecret) throws NoSuchAlgorithmException, UnsupportedEncodingException {
Map<String, String> tmp = new TreeMap<String, String>(items);
StringBuilder sb = new StringBuilder();
for(Map.Entry<String, String> entry : tmp.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
if(StringUtils.isEmpty(value)) continue;
sb.append(key).append("=").append(value).append("&");
}
sb.append("key=").append(APISecret);
return publicService.padStr(new BigInteger(1, MessageDigest.getInstance("MD5").digest(sb.toString().getBytes(CharEncoding.UTF_8))).toString(16).toUpperCase(), "0", 32);
}
//将userId和out_trade_no等信息写入payment_result表 paymentPublicService.insertStubToPaymentResultTable(userId, PaymentResult.CHANNEL.WEIXIN, coachProductId, outTradeNo);
$(function(){
alert("xxxxxxxx");
callPay();
function onBridgeReady(){
WeixinJSBridge.invoke(
'getBrandWCPayRequest', {
"appId" : '${appId}', //公e众号名称,由商户传入
"timeStamp": '${timeStamp}', //时间戳,自1970年以来的秒数
"nonceStr" : '${nonceStr}', //随机串
"package" : '${package1}', //预支付ID参数
"signType" : '${signType}', //微信签名方式:
"paySign" : '${paySign}' //微信签名
},
function(res){
if(res.err_msg == "get_brand_wcpay_request:ok" ) {
alert("支付成功");
window.location.href="/student/student_booking";
}else if(res.err_msg == "get_brand_wcpay_request:cancel" ){
alert("支付过程中用户取消");
window.location.href="student_pay.jsp";
}else{
alert('支付失败');
window.location.href="student_pay.jsp";
}
}
);
}
function callPay(){
if (typeof WeixinJSBridge == "undefined"){
if( document.addEventListener ){
document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
}else if (document.attachEvent){
document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
}
}else{
onBridgeReady();
}
}
})
String xml = IOUtils.toString(request.getInputStream(), CharEncoding.UTF_8);
<xml>
<return_code><![CDATA[SUCCESS]]></return_code>
<return_msg><![CDATA[OK]]></return_msg>
</xml>