當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
支付宝和微信的JSSDK发起支付
生活随笔
收集整理的這篇文章主要介紹了
支付宝和微信的JSSDK发起支付
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
支付寶:
引入alipay的jsapi文件:
<script src="https://a.alipayobjects.com/g/h5-lib/alipayjsapi/3.0.6/alipayjsapi.min.js"></script>點擊支付按鈕調用的后臺創建交易的接口,返回tradeNO
this.API.trade_create({total_amount:0.01,subject:localStorage.tablename+'點餐',buyer_id:localStorage.user_id,shop_id: localStorage.shop_id,seller_id:localStorage.seller_id,out_trade_no:this.orderdetail['pos_reference'],payment:'CODEPAY'}).then((response) => {//這條接口主要是為了拿到tradeNO,前端只需拿到這個就可以了this.alipayPay(response);}, (response) => {mui.toast('網絡錯誤');});//傳入tradeNOalipayPay: function (response) {this.tradePay(response);}//發起支付tradePay: function (tradeNO) {let that = this;this.alipayReady(function(){// 通過傳入交易號喚起快捷調用方式(注意tradeNO大小寫嚴格)AlipayJSBridge.call("tradePay", {tradeNO: tradeNO}, function (data) {//支付成功后data.resultCode是900if ("9000" == data.resultCode) {that.processActionLog(tradeNO);//這是我做掃碼點餐的數據回流給支付寶的代碼,沒用到可以直接去掉that.$store.dispatch('user_record',{orderid:that.orderdetail['id'],shop_id:localStorage.shop_id,user_id:localStorage.user_id,merchant_pid: localStorage.seller_id,tablename:localStorage.tablename,i:localStorage.i,status:14,statusDesc:'已支付',action_type:'order_dishes'});that.$router.push({path:'/orderinfo'});}});});}微信:
首先安裝下jssdk
npm i -S weixin-js-sdkmain.js引入
import wx from 'weixin-js-sdk' Vue.prototype.$wx = wx;點擊支付按鈕發起支付
this.API.toPay({//參數根據后臺需要ordersn:this.orderdetail['pos_reference'],amount:this.orderdetail['amount_total'],user_id:localStorage.user_id,payment:'CODEPAY'}).then((response) => {//獲取后臺返回的支付的數據,調用jssdk發起支付this.weixinPay(response);}, (response) => {mui.toast('網絡錯誤');});//發起支付的方法weixinPay: function (response) {let that = this;this.$wx.config({debug: false, appId: response['sdk_appid'],timestamp: response['sdk_timestamp'],nonceStr: response['sdk_noncestr'],signature: response['sign'],jsApiList: ['chooseWXPay']});this.$wx.ready(function(){that.$wx.chooseWXPay({timestamp: response['sdk_timestamp'],nonceStr: response['sdk_noncestr'],package: response['sdk_package'],signType: response['sdk_signtype'],paySign: response['sdk_paysign'],success: function(res) {that.$router.push({path:'/orderinfo'});},fail: function(res){console.log(JSON.stringify(res));}});});this.$wx.error(function(res) {console.log(JSON.stringify(res));});}另一種發起微信支付方式,不使用jssdk
//拿到后臺返回的支付信息,調用onBridgeReady onBridgeReady: function (response) {this.initWeixinReady(response);},initWeixinReady: function (response) {WeixinJSBridge.invoke('getBrandWCPayRequest', {"appId":response['sdk_appid'], //公眾號名稱,由商戶傳入 "timeStamp":response['sdk_timestamp'], //時間戳,自1970年以來的秒數 "nonceStr":response['sdk_noncestr'], //隨機串 "package":response['sdk_package'], "signType":response['sdk_signtype'], //微信簽名方式: "paySign":response['sdk_paysign'] //微信簽名 },function(res){ if(res.err_msg == "get_brand_wcpay_request:ok" ) {mui.toast('支付成功');}else{mui.toast('支付失敗');} });if (typeof WeixinJSBridge == "undefined"){if( document.addEventListener ){document.addEventListener('WeixinJSBridgeReady', this.onBridgeReady, false);}else if (document.attachEvent){document.attachEvent('WeixinJSBridgeReady', this.onBridgeReady); document.attachEvent('onWeixinJSBridgeReady', this.onBridgeReady);}}}支付寶支付完成后關閉窗口:
AlipayJSBridge.call('closeWebview');微信支付完成后關閉窗口:
that.$wx.closeWindow();總結
以上是生活随笔為你收集整理的支付宝和微信的JSSDK发起支付的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Redis数据类型--列表类型
- 下一篇: Node.js 入门详解(一)