在上一節中其實已經準備好了調起支付接口的各項參數微信支付接口web端能用app嗎,只需使用wx.()即可
目錄:
微信小程序接入微信支付(一):大概流程與準備需知
微信小程序接入微信支付(二):后臺調用統一下單接口
微信小程序接入微信支付(三):小程序端調用支付接口
微信小程序接入微信支付(四):接收支付結果通知與沙箱測試
代碼如下:
wx.requestPayment(
{
'timeStamp': '',
'nonceStr': '',
'package': '',
'signType': 'MD5',

'paySign': '',
'success':function(res){},
'fail':function(res){}, //fail包含取消支付和支付失敗等多種情況,記得區分
'complete':function(res){}
})
為了嚴謹考慮,通常會在接收到接口返回的結果后再次去后臺調用微信支付查詢訂單接口,二次確認訂單狀態微信支付接口web端能用app嗎,從而保證訂單是否支付成功,以便做出相應的處理。
微信支付查詢訂單接口官方文檔:
代碼:
/**
* 請求微信查詢訂單接口,獲取訂單狀態, 把訂單狀態返回給小程序端
*
* @param order_code 商戶訂單號
* @return

* @throws Exception
*/
@GetMapping("/queryorder")
@ResponseBody
public String queryOrder(@Param("order_code") String order_code) throws Exception {
//
Map<String, String> map = new HashMap<>();
map.put("appid", appId);
map.put("mch_id", mchId);
map.put("out_trade_no", order_code);
map.put("nonce_str", getRandomString());
map.put("sign_type", "MD5");
//生成符合規格的簽名串

String stringSignTemp = getParamStr(map,realKey); //realKey -- key
//進行MD5運算,并轉為大寫,獲得sign參數的值
String signValue = MD5(stringSignTemp.toString()).toUpperCase();
//把sign放入map中
map.put("sign",signValue);
//當sign參數有值時
if (map.get("sign").trim().length()>0) {
//map轉義為xml格式
String requestParam = new String(mapToXml(map).getBytes(),"utf-8");
LOG.info("----queryorder requestParam: " + requestParam);
//發送請求
String result = sendPostParam(queryOrderUrl, requestParam); //queryOrderUrl -- https://api.mch.weixin.qq.com/pay/orderquery
LOG.info("----queryorder result: " + result);
//將返回的結果從xml字符串轉義為map

Map<String, String> resultMap = xmlToMap(result);
String return_code;
if (resultMap.containsKey("return_code")) {
return_code = resultMap.get("return_code");
}
else {
throw new Exception(String.format("No `return_code` in XML: %s", result));
}
if (return_code.equals("FAIL")) {
throw new Exception("return_code : fail, the result xml string is :" + result);
}
else if (return_code.equals("SUCCESS")) {
//只傳trade_state

if (resultMap.get("trade_state") != null) {
return resultMap.get("trade_state");
}else {
throw new Exception("query order warning: " + resultMap.get("err_code") + "--" + resultMap.get("err_code_des") );
}
}
else {
throw new Exception(String.format("return_code value %s is invalid in XML: %s", return_code, result));
}
}
return null;
}
相關外部方法寫在上一節:微信小程序接入微信支付(二):后臺調用統一下單接口