Skip to content

Instantly share code, notes, and snippets.

@enappd
Last active June 10, 2021 07:47
Show Gist options
  • Save enappd/f2fa6ec6470b6a967382925b3fe1aa82 to your computer and use it in GitHub Desktop.
Save enappd/f2fa6ec6470b6a967382925b3fe1aa82 to your computer and use it in GitHub Desktop.
Payment function for Ionic 4 PayPal plugin
import { Component } from '@angular/core';
import { PayPal, PayPalPayment, PayPalConfiguration } from '@ionic-native/paypal/ngx';
@Component({
selector: 'app-paypal',
templateUrl: 'paypal.page.html',
styleUrls: ['paypal.page.scss'],
})
export class PaypalPage {
constructor(private payPal: PayPal) {
}
paymentAmount: string = '3.33';
currency: string = 'USD';
currencyIcon: string = '$';
payWithPaypal() {
console.log("Pay ????");
this.payPal.init({
PayPalEnvironmentProduction: 'YOUR_PRODUCTION_CLIENT_ID',
PayPalEnvironmentSandbox: 'YOUR_SANDBOX_CLIENT_ID'
}).then(() => {
// Environments: PayPalEnvironmentNoNetwork, PayPalEnvironmentSandbox, PayPalEnvironmentProduction
this.payPal.prepareToRender('PayPalEnvironmentSandbox', new PayPalConfiguration({
// Only needed if you get an "Internal Service Error" after PayPal login!
//payPalShippingAddressOption: 2 // PayPalShippingAddressOptionPayPal
})).then(() => {
let payment = new PayPalPayment(this.paymentAmount, this.currency, 'Description', 'sale');
this.payPal.renderSinglePaymentUI(payment).then((res) => {
console.log(res);
// Successfully paid
// Example sandbox response
//
// {
// "client": {
// "environment": "sandbox",
// "product_name": "PayPal iOS SDK",
// "paypal_sdk_version": "2.16.0",
// "platform": "iOS"
// },
// "response_type": "payment",
// "response": {
// "id": "PAY-1AB23456CD789012EF34GHIJ",
// "state": "approved",
// "create_time": "2016-10-03T13:33:33Z",
// "intent": "sale"
// }
// }
}, () => {
// Error or render dialog closed without being successful
});
}, () => {
// Error in configuration
});
}, () => {
// Error in initialization, maybe PayPal isn't supported or something else
});
}
}
@najamkhan20
Copy link

how we get sandbox client id?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment