頁面按鈕顯示:javascript
<!--支付確認按鈕--> <article class="btn-group"> <a href="javascript:void(0);" click='wx_pay()' class="btn btn-w100 f28"> 下一步 </a> </article>
點擊支付:php
var wx_pay = function(){ var ordernum=$("#hid_ordernum").val(); var ip=$("#hid_ip").val(); var ra=$('input:radio[name="pay-type"]:checked').val(); if(ra=="weixin"){ var open_id = "{{open_id}}"; if(open_id.length <= 0 ){ //若是用戶尚未微信給的open_id 就去獲取一個 這個是靜默受權獲取 $.get("/wechat/weixin_snsapi_base/"+ordernum,{},function(data){ window.location.href = data.wx_redirect_url; }) }else{ $.ajax({ type: "POST", url: "/wechat/unified_order", data: {'ordernum':ordernum,"ip":ip}, success:function (data) { if(data["code"]=="SUCCESS"){ //調起微信支付 onBridgeReady(data["dict"]); }else{ $("#div_box_message").html(data['msg']); $("#div_show_box").show(); }; } }); } }else { $("#div_box_message").html("請選擇你支付方式!"); $("#div_show_box").show(); return false; } }
支付的首先調用微信統一下單接口,微信那邊建立預支付訂單:html
@wechat.route('/unified_order', methods=['POST']) def unified_order(): '''統一下單接口 price, time_start, time_out,artwork_id''' ip = request.form.get("ip") if request.form.get( "ip") else webutil.request_ip() ordernum = request.form.get("ordernum") if not ordernum: return jsonify({"code": -1}) open_id = webutil.session_get_user_open_id(ordernum) # print '1----', open_id # if not open_id: # # 用戶沒有微信受權先受權獲取open_id # url = open_weixin_snsapi_base(ordernum, ip) # return redirect(url) # print '2----', open_id # print '3----', open_id # print '4----', open_id order = Orders.query.filter_by(ordernum=ordernum).first() price = order.order_price time_start = order.created_on time_out = order.created_on artwork_id = order.artwork_id res = _unified_order(ordernum, price, ip, time_start, time_out, artwork_id, open_id) # 組裝返回值 code = "FAIL" msg = res['return_msg'] dict = {} if res["return_code"] == "SUCCESS" and res['result_code'] == "SUCCESS": dict['appId'] = res['appid'] dict['timeStamp'] = '{}'.format(int(time.time())) dict['nonceStr'] = uuid.uuid4().hex dict['package'] = "prepay_id=%s" % res['prepay_id'] dict['signType'] = "MD5" dict['paySign'] = __get_sign(dict) code = "SUCCESS" elif res["return_code"] == "SUCCESS": msg = res['err_code_des'] print dict return jsonify({"code": code, "msg": msg, "dict": dict})
def _unified_order(ordernum, price, ip, time_start, time_out, artwork_id, openid): """統一下單接口 公衆帳號ID appid 是 String(32) wxd678efh567hg6787 微信支付分配的公衆帳號ID(企業號corpid即爲此appId) 商戶號 mch_id 是 String(32) 1230000109 微信支付分配的商戶號 設備號 device_info 否 String(32) 013467007045764 自定義參數,能夠爲終端設備號(門店號或收銀設備ID),PC網頁或公衆號內支付能夠傳"WEB" 隨機字符串 nonce_str 是 String(32) 5K8264ILTKCH16CQ2502SI8ZNMTM67VS 隨機字符串,長度要求在32位之內。推薦隨機數生成算法 簽名 sign 是 String(32) C380BEC2BFD727A4B6845133519F3AD6 經過簽名算法計算得出的簽名值,詳見簽名生成算法 簽名類型 sign_type 否 String(32) HMAC-SHA256 簽名類型,默認爲MD5,支持HMAC-SHA256和MD5。 商品描述 body 是 String(128) 騰訊充值中心-QQ會員充值 商品簡單描述,該字段請按照規範傳遞,具體請見參數規定 商品詳情 detail 否 String(6000) 附加數據 attach 否 String(127) 深圳分店 附加數據,在查詢API和支付通知中原樣返回,可做爲自定義參數使用。 商戶訂單號 out_trade_no 是 String(32) 20150806125346 商戶系統內部訂單號,要求32個字符內、且在同一個商戶號下惟一。 詳見商戶訂單號 標價幣種 fee_type 否 String(16) CNY 符合ISO 4217標準的三位字母代碼,默認人民幣:CNY,詳細列表請參見貨幣類型 標價金額 total_fee 是 Int 88 訂單總金額,單位爲分,詳見支付金額 終端IP spbill_create_ip 是 String(16) 123.12.12.123 APP和網頁支付提交用戶端ip,Native支付填調用微信支付API的機器IP。 交易起始時間 time_start 否 String(14) 20091225091010 訂單生成時間,格式爲yyyyMMddHHmmss,如2009年12月25日9點10分10秒錶示爲20091225091010。其餘詳見時間規則 交易結束時間 time_expire 否 String(14) 20091227091010 訂單失效時間,格式爲yyyyMMddHHmmss,如2009年12月27日9點10分10秒錶示爲20091227091010。其餘詳見時間規則 注意:最短失效時間間隔必須大於5分鐘 商品標記 goods_tag 否 String(32) WXG 商品標記,使用代金券或立減優惠功能時須要的參數,說明詳見代金券或立減優惠 通知地址 notify_url 是 String(256) http://www.weixin.qq.com/wxpay/pay.php 異步接收微信支付結果通知的回調地址,通知url必須爲外網可訪問的url,不能攜帶參數。 交易類型 trade_type 是 String(16) JSAPI 取值以下:JSAPI,NATIVE,APP等,說明詳見參數規定 商品ID product_id 否 String(32) 12235413214070356458058 trade_type=NATIVE時(即掃碼支付),此參數必傳。此參數爲二維碼中包含的商品ID,商戶自行定義。 指定支付方式 limit_pay 否 String(32) no_credit 上傳此參數no_credit--可限制用戶不能使用信用卡支付 用戶標識 openid 否 String(128) oUpF8uMuAJO_M2pxb1Q9zNjWeS6o trade_type=JSAPI時(即公衆號支付),此參數必傳,此參數爲微信用戶在商戶對應appid下的惟一標識。openid如何獲取,可參考【獲取openid】。企業號請使用【企業號OAuth2.0接口】獲取企業號內成員userid,再調用【企業號userid轉openid接口】進行轉換 'device_info': 'WEB', "sign_type": "MD5", """ return_url = os.path.join("http://", DOMAIN_NAME, "wechat/wx_notify") dict = { 'appid': appid, 'mch_id': "1435556302", "nonce_str": uuid.uuid4().hex, "body": "詩藝-藝術品", "out_trade_no": ordernum, "total_fee": int(price * 100), "spbill_create_ip": ip, "notify_url": return_url, "trade_type": "JSAPI", "openid": openid } # if time_out and time_start: # dict["time_start"] = time_start # dict["time_out"] = time_start + timedelta(minutes=30) if artwork_id: dict["product_id"] = artwork_id # 生成簽名 dict['sign'] = __get_sign(dict) print dict, '-----' # dict轉換爲xml格式 xml = __json_to_xml(dict) url = "https://api.mch.weixin.qq.com/pay/unifiedorder" req = urllib2.Request( url=url, headers={'Content-Type': 'text/xml'}, data=str(xml)) f = urllib2.urlopen(req) res = f.read() res = __xml_to_json(res) return res.get("xml")
統一下單完成以後調起微信支付:前端
//jsapi微信支付 function onBridgeReady(data){ WeixinJSBridge.invoke( 'getBrandWCPayRequest', data, function(res){ if(res.err_msg == "get_brand_wcpay_request:ok") {
//跳轉到支付成功界面 window.location.href="/order/pay_finish"; } // 使用以上方式判斷前端返回,微信團隊鄭重提示:res.err_msg將在用戶支付成功後返回 ok,但並不保證它絕對可靠。 else{ //alert(res.err_msg); } } ); }
微信支付就完成了,基本會成功,可是這裏說不保證絕對可靠,因此須要調用微信另外一個用來查詢訂單支付是否成功接口。java