Paypal provides an easy way to integrate its express checkout solution but what is the best solution to use this solution in an angular2 project working written in typescript?
I've used a solution like this:
Method to load external scripts
private loadExternalScript(scriptUrl: string) {
return new Promise((resolve, reject) => {
const scriptElement = document.createElement('script')
scriptElement.src = scriptUrl
scriptElement.onload = resolve
document.body.appendChild(scriptElement)
})
Component Code
ngAfterViewInit(): void {
this.loadExternalScript("https://www.paypalobjects.com/api/checkout.js").then(() => {
paypal.Button.render({
env: 'sandbox',
client: {
production: '',
sandbox: ''
},
commit: true,
payment: function (data, actions) {
return actions.payment.create({
payment: {
transactions: [
{
amount: { total: '1.00', currency: 'USD' }
}
]
}
})
},
onAuthorize: function(data, actions) {
return actions.payment.execute().then(function(payment) {
// TODO
})
}
}, '#paypal-button');
});
}
Credit
Andrei Odri's answer here: script tag in angular2 template / hook when template dom is loaded
you can implement paypal checkout with angular 4 like this :
import { Component, OnInit } from '@angular/core';
declare let paypal: any;
@Component({
selector: 'app-page-offers',
templateUrl: './page-offers.component.html',
styleUrls: ['./page-offers.component.sass']
})
export class PageOffersComponent implements OnInit {
constructor() {}
ngOnInit() {
$.getScript( 'https://www.paypalobjects.com/api/checkout.js', function() {
paypal.Button.render({
[...]
})
[...]
Enjoy :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With