目前項目中比較經常使用的第三方支付無非就是支付寶支付和微信支付。下面介紹一下Vue中H5頁面如何使用支付寶支付。其實很簡單的,只不過是調本身後臺的一個接口而已(後臺根據支付寶文檔,寫好支付接口)。前端
觸發支付寶支付調用後臺接口,後臺會返回支付寶提供的form表單,咱們只要在vue裏面建立新節點,將返回的form表單append進去,並提交就能夠喚起支付寶支付。另在此說一下這個returnUrl, 它是支付後支付寶回調的頁面。具體能夠根據自身業務,後臺寫死或者由前端控制。vue
methods () { /** * 支付寶支付 */ goAlipay () { this.$loading.show() const data = { /* 自身接口所需的一些參數 */ ... amount: this.price, /* 支付後支付寶return的url */ // returnUrl: 'www.baidu.com' returnUrl: window.location.origin + window.location.pathname + '?userParams=' + this.userParams } this.$http( this.$apiSetting.alipay, data ).then(res => { this.$loading.hide() if (res.data.statusCode === '000000') { const div = document.createElement('div') /* 此處form就是後臺返回接收到的數據 */ div.innerHTML = res.data.data.alipayInfo document.body.appendChild(div) document.forms[0].submit() } }, error => { this.$loading.hide() console.log(error) }) } }