<div id="paypal-container"></div>
导入paypal-js库,网址:
https://www.npmjs.com/package/@paypal/paypal-js?activeTab=readme
<script>
import { loadScript } from "@paypal/paypal-js";
let paypal;
export default {
data() {
return {
clientId: {
sandbox: "xxxx",
production:"xxx",
},
};
},
methods: {
async pay_with_paypal() {
try {
paypal = await loadScript({
clientId: this.clientId.sandbox,//在开发者后台获取
currency: "XXX"//货币单位,默认USD,需要和购物车的货币单位保持一致
});
} catch (error) {
console.log(error, "error with paypal");
}
if (paypal) {
try {
//Buttons系统默认只有paypal和信用卡、借记卡支付两种
await paypal.Buttons({
async createOrder() {
//调起创建支付订单的接口
},
async onApprove(data) {
//付款接口
},
onCancel(data){
console.log('取消');
}
})
.render("#paypal-container");
} catch (error) {
console.log(error, "paypal button");
}
}
},
},
};
</script>