嗨,我在Angular 5中集成了PayPal。我使用此功能渲染Paypal按钮
ngAfterViewChecked(): void {
if (!this.addScript) {
this.addPaypalScript().then(() => {
this.abbonamenti.forEach((item, index) => {
paypal.Button.render(this.payPalConfig +index, '#paypal-checkout-btn' + index);
this.paypalLoad = false;
})
})
}
}
这需要为每个按钮加载不同的PayPal配置:
payPalConfig = {
env: 'sandbox',
client: {
sandbox: 'AfOCNI-QeZrhkX2lT5e4xY0S2KTfgoDarEXwk4mkK0ge4EoeW25VN5cg5ZlNRrdJrWUctHWBGGbP4d2V',
production: 'AUiobQL_EOnmPB4ytmb5ZZHbLZT4hXk7UZgMzGwkd8HFIR6qZ5qJZM3JOb91O4y5frw4197ygPyfbor0'
},
commit: true,
// payment() is called when the button is clicked
payment: function(data, actions) {
// Make a call to the REST api to create the payment
return actions.payment.create({
payment: {
transactions: [
{
amount: { total: 40, currency: 'EUR' }
}
]
}
});
},
// onAuthorize() is called when the buyer approves the payment
onAuthorize: function(data, actions) {
// Make a call to the REST api to execute the payment
return actions.payment.execute().then(function() {
window.alert('Payment Complete!');
});
}
};
,但是某些串联或索引也不起作用。如果我尝试在函数名称中添加+索引,则会出现语法错误。我不知道。我很沮丧。
答案 0 :(得分:1)
您不能在对象上应用+
运算符。参见下面的行
paypal.Button.render(this.payPalConfig +index, '#paypal-checkout-btn' + index);
上面一行中的this.payPalConfig
是object
,它导致了此错误。
您应该删除+
和index
,只需传递直接对象
paypal.Button.render(this.payPalConfig, '#paypal-checkout-btn' + index);
配置应为
payPalConfig = new PayPalConfig(PayPalIntegrationType.ClientSideREST, PayPalEnvironment.Sandbox, {
commit: true,
client: {
sandbox: 'yourSandboxClientId',
},
button: {
label: 'paypal',
},
onPaymentComplete: (data, actions) => {
console.log('OnPaymentComplete');
},
onCancel: (data, actions) => {
console.log('OnCancel');
},
onError: (err) => {
console.log('OnError');
},
transactions: [{
amount: {
currency: 'USD',
total: 9
}
}]
});