我试图在我的Angular项目中实现PayPal。我找到了本教程并按照步骤操作。按钮看起来很完美,但是当我按下按钮时,出现了PayPal模态,但抛出了错误:
POST https://www.sandbox.paypal.com/v1/oauth2/token 401(未经授权(
my app.component.ts:
addScript = false;
paypalLoad = true;
finalAmount = 1;
paypalConfig = {
env: 'sandbox',
client: {
sandbox: localStorage.getItem('paypal_token'), // this generate the backend
production: '<your-production-key here>'
},
commit: true,
payment: (data, actions) => {
return actions.payment.create({
payment: {
transactions: [
{ amount: {
total: this.finalAmount,
currency: 'HUF'
}
}
]
}
});
},
onAuthorize: (data, actions) => {
return actions.payment.execute().then((payment) => {
// Do something when payment is successful.
});
}
};
ngAfterViewChecked() {
if (!this.addScript) {
this.addPaypalScript().then(() => {
paypal.Button.render(this.paypalConfig, '#paypal-checkout-btn');
this.paypalLoad = false;
});
}
}
addPaypalScript() {
this.addScript = true;
return new Promise((resolve, reject) => {
let scripttagElement;
scripttagElement = document.createElement('script');
scripttagElement.src = 'https://www.paypalobjects.com/api/checkout.js';
scripttagElement.onload = resolve;
document.body.appendChild(scripttagElement);
});
}
和我的应用程序组件.html
<input type="number" [(ngModel)]="finalAmount" style="padding-bottom: 10px;">
<h2 *ngIf="paypalLoad">Paypal button is loading</h2>
<div id="paypal-checkout-btn"></div>
问题是访问密钥。我设置错了。