paypal html5 支付,PayPal H5支付组件

//- Title: H5 PayPal 支付 组件

.paypal.flex-cc

#loading

van-loading#loading(size='36' type='spinner' color='#666')

#paypal-container

.money

span £

span {{$route.query.totalAmount}}

#paypal-payment-button

import Cookies from 'js-cookie'

export default {

name: 'paypal',

components: {},

props: {},

data() {

return {}

},

created() {

// 创建PayPal支付

this.createPayPal()

},

methods: {

// 创建PayPal支付

createPayPal() {

// clientId 客户端ID & currency 支付货币种类

const { clientId, currency } = this.$route.query

// 创建script标签,引入PayPal SDK文件

let script = document.createElement('script')

script.type = 'text/javascript'

// clientId

script.src = `https://www.paypal.com/sdk/js?client-id=${clientId}&disable-funding=credit,card&currency=${currency}`

// 添加元素到head

document.getElementsByTagName('head')[0].appendChild(script)

// 引入成功

script.onload = () => {

console.log('js资源已加载成功了')

// 创建支付按钮

this.createPayPalButton()

}

// 引入失败

script.onerror = () => {

console.log('js资源加载失败了,返回原生')

}

},

// 创建支付按钮

createPayPalButton() {

const BASE_API_URL = process.env.VUE_APP_BASE_API // 请求根路径

const Authorization = Cookies.get('Authorization')

const { payId } = this.$route.query

// 创建script标签

let script = document.createElement('script')

script.type = 'text/javascript'

script.text = `

paypal.Buttons({

style : {

layout: 'vertical',

color: 'black',

shape: 'rect',

label: 'checkout',

tagline: false

},

onInit: () => {

// 支付按钮加载完成 通知原生关闭loading

console.log('--------1.支付按钮加载完成 关闭loading--------')

// 自动点击 执行支付

console.log('--------2.自动点击 执行支付--------')

// 如果按钮获取到 就 自动执行点击 否则显示按钮

const paypalButton = window.frames[1].document.querySelector('.paypal-button')

if (paypalButton) paypalButton.click()

else {

document.querySelector('#paypal-container').style.display = 'block'

document.querySelector('#loading').style.display = 'none'

}

},

createOrder: function (data, actions) {

return fetch('${BASE_API_URL}api/payment/paypal/v2/create', {

method: 'post',

headers: {

'content-type': 'application/json',

'Authorization': '${Authorization}',

},

body: JSON.stringify({

payId: '${payId}',

})

}).then(function (res) {

return res.json();

}).then(function (data) {

console.log('--------3. 获取支付ID-------- payId: ', data.data)

if (!data.data) alert('retCode: ' + data.retCode + ' message: ' + data.message)

else {

document.querySelector('#paypal-container').style.display = 'none'

document.querySelector('#loading').style.display = 'block'

}

return data.data;

});

},

onApprove: function (data, actions) {

return fetch('${BASE_API_URL}api/payment/paypal/v2/capture', {

method: 'POST',

headers: {

'content-type': 'application/json',

'Authorization': '${Authorization}',

},

body: JSON.stringify({

orderID: data.orderID,

})

}).then(function (res) {

return res.json();

}).then(function (details) {

if (details.error === 'INSTRUMENT_DECLINED') {

return actions.restart();

}

console.log("--------4.创建订单Success--------")

console.log("--------5.打开Loading--------")

return fetch('${BASE_API_URL}api/payment/paypal/v2/order', {

method: 'post',

headers: {

'content-type': 'application/json',

'Authorization': '${Authorization}',

},

body: JSON.stringify({

orderID: data.orderID

})

}).then(function (res) {

return res.json();

}).then(function (ret) {

console.log(ret.retCode);

if (ret.retCode != 200) {

// 支付失败,跳转到支付失败页 todo

console.log('--------6.支付失败 返回原生--------')

return;

}

// 跳转到支付成功页 todo

console.log('--------6.支付成功 返回原生--------')

});

});

},

onCancel: function (data) {

// 跳回原生 todo

console.log('--------7.点击取消 返回原生--------')

}

}).render('#paypal-payment-button')

`

// 引入script

document.getElementsByTagName('head')[0].appendChild(script)

},

},

}

.paypal {

width 100%

height 100vh

flex-direction column

background #F5F5F5

}

#paypal-container {

width calc(100% - 40px)

display none

.money {

font-size 26px

font-weight 700

text-align center

margin-bottom 150px

span:last-child {

font-size 40px

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值