最近研究了一下PayPal H5支付 測試代碼如下
<template lang="pug">
//- 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
</template>
<script>
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¤cy=${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)
},
},
}
</script>
<style scoped lang="stylus">
.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
}
}
}
</style>
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。