通过使用 Remotec 的答案,我可以进行快速结帐。它虽然给出了一些违规警告。用户选择货币后,我已经在我的函数中呈现。我已经从角度模板传递了“这个”。我用过 Angular 6 和 Angular 材料 2
<div class="container">
<div *ngFor="let currency of currencies">
</div>
<div class="row">
<form >
<mat-form-field appearance="standard" class="col-sm-12 col-md-6">
<mat-label>Cost</mat-label>
<input matInput placeholder="Amount" [(ngModel)]="cost" required disabled="true" name="amount" id="amount">
</mat-form-field>
<mat-form-field appearance="standard" class="col-sm-12 col-md-6">
<mat-select placeholder="Select Currency" [(ngModel)]="selectedCurrency" name="curr" id="curr" (selectionChange)="CurrencyChange(cost,selectedCurrency,this)" >
<mat-option *ngFor="let c of currencies" [value]="c.value" >
{{c.viewValue}}
</mat-option>
</mat-select>
</mat-form-field>
</form>
</div>
<div id="paypal-button" class="align-content-between align-content-center"></div>
</div>
在 CurrencyChange 函数中,我已经传递了这个,在 paypal 函数中,我再次调用了我的 angular 函数来完成付款。我不知道这是否是一个好习惯。但这行得通。
export class PaymentComponent implements OnInit {
cost = '1';
currency = 'INR';
selectedCurrency = "0";
currencies: Currency[] = [
{
value: "0",
viewValue: "Select Currency"
},
{
"value": "INR",
"viewValue": "Indian Ruppe"
}, {
"value": "USD",
"viewValue": "US Dollar"
},
{
"value": "EUR",
"viewValue": "EURO"
}]
private loadExternalScript(scriptUrl: string) {
return new Promise((resolve, reject) => {
const scriptElement = document.createElement('script')
scriptElement.src = scriptUrl
scriptElement.onload = resolve
document.body.appendChild(scriptElement)
})
}
ngOnInit() {
this.loadExternalScript("https://www.paypalobjects.com/api/checkout.js");
}
constructor() { }
paymentSuccess(payment) {
//alert('Payment Success');
}
CurrencyChange(cost, selectedCurreny,self): void {
document.getElementById("paypal-button").innerHTML = "";
if (selectedCurreny == 0) {
alert('Please select the Country');
return;
}
//reset earlier inserted paypal button
paypal.Button.render({
env: 'sandbox',
client: {
production: 'AQ9IbOayBJxHmad9DMGoysS4UhzE-usUqfSQ-CLzSn3M96qvZny5vZZ2VkNzn6EBTnE2UU4L8PDkqJJE',
sandbox: 'AQ9IbOayBJxHmad9DMGoysS4UhzE-usUqfSQ-CLzSn3M96qvZny5vZZ2VkNzn6EBTnE2UU4L8PDkqJJE'
},
commit: true,
payment: function (data, actions) {
return actions.payment.create({
payment: {
transactions: [
{
amount: { total: cost, currency: selectedCurreny }
}
]
}
})
},
onAuthorize: function (data, actions) {
return actions.payment.execute().then(function (payment) {
alert('Payment Successful')
self.paymentSuccess(payment);
console.log(payment)
})
}
}, '#paypal-button');
}
}