2018-06-29 04:27:32 +02:00
|
|
|
import {
|
|
|
|
Component,
|
2018-06-30 19:36:39 +02:00
|
|
|
Input,
|
2018-06-29 04:27:32 +02:00
|
|
|
OnInit,
|
|
|
|
} from '@angular/core';
|
|
|
|
|
|
|
|
import { I18nService } from 'jslib/abstractions/i18n.service';
|
|
|
|
import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service';
|
|
|
|
|
|
|
|
const Keys = {
|
|
|
|
stripeTest: 'pk_test_KPoCfZXu7mznb9uSCPZ2JpTD',
|
|
|
|
stripeLive: 'pk_live_bpN0P37nMxrMQkcaHXtAybJk',
|
|
|
|
btSandbox: 'sandbox_r72q8jq6_9pnxkwm75f87sdc2',
|
|
|
|
btProduction: 'production_qfbsv8kc_njj2zjtyngtjmbjd',
|
|
|
|
};
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-payment',
|
|
|
|
templateUrl: 'payment.component.html',
|
|
|
|
})
|
|
|
|
export class PaymentComponent implements OnInit {
|
2018-06-30 19:36:39 +02:00
|
|
|
@Input() showOptions = true;
|
2018-07-16 23:17:07 +02:00
|
|
|
@Input() method = 'card';
|
|
|
|
@Input() hideBank = false;
|
|
|
|
@Input() hidePaypal = false;
|
2018-06-30 19:36:39 +02:00
|
|
|
|
2018-06-29 04:27:32 +02:00
|
|
|
card: any = {
|
|
|
|
number: null,
|
|
|
|
exp_month: null,
|
|
|
|
exp_year: null,
|
|
|
|
address_country: '',
|
|
|
|
address_zip: null,
|
|
|
|
};
|
2018-07-16 23:17:07 +02:00
|
|
|
bank: any = {
|
|
|
|
routing_number: null,
|
|
|
|
account_number: null,
|
|
|
|
account_holder_name: null,
|
|
|
|
account_holder_type: '',
|
|
|
|
currency: 'USD',
|
|
|
|
country: 'US',
|
|
|
|
};
|
2018-06-29 04:27:32 +02:00
|
|
|
cardExpMonthOptions: any[];
|
|
|
|
cardExpYearOptions: any[];
|
|
|
|
|
|
|
|
private stripeScript: HTMLScriptElement;
|
|
|
|
private btScript: HTMLScriptElement;
|
|
|
|
private btInstance: any = null;
|
|
|
|
|
|
|
|
constructor(i18nService: I18nService, private platformUtilsService: PlatformUtilsService) {
|
|
|
|
this.stripeScript = window.document.createElement('script');
|
|
|
|
this.stripeScript.src = 'https://js.stripe.com/v2/';
|
|
|
|
this.stripeScript.async = true;
|
|
|
|
this.stripeScript.onload = () => {
|
|
|
|
(window as any).Stripe.setPublishableKey(
|
|
|
|
this.platformUtilsService.isDev() ? Keys.stripeTest : Keys.stripeLive);
|
|
|
|
};
|
|
|
|
this.btScript = window.document.createElement('script');
|
|
|
|
this.btScript.src = 'https://js.braintreegateway.com/web/dropin/1.4.0/js/dropin.min.js';
|
|
|
|
this.btScript.async = true;
|
|
|
|
|
|
|
|
this.cardExpMonthOptions = [
|
|
|
|
{ name: '-- ' + i18nService.t('select') + ' --', value: null },
|
|
|
|
{ name: '01 - ' + i18nService.t('january'), value: '01' },
|
|
|
|
{ name: '02 - ' + i18nService.t('february'), value: '02' },
|
|
|
|
{ name: '03 - ' + i18nService.t('march'), value: '03' },
|
|
|
|
{ name: '04 - ' + i18nService.t('april'), value: '04' },
|
|
|
|
{ name: '05 - ' + i18nService.t('may'), value: '05' },
|
|
|
|
{ name: '06 - ' + i18nService.t('june'), value: '06' },
|
|
|
|
{ name: '07 - ' + i18nService.t('july'), value: '07' },
|
|
|
|
{ name: '08 - ' + i18nService.t('august'), value: '08' },
|
|
|
|
{ name: '09 - ' + i18nService.t('september'), value: '09' },
|
|
|
|
{ name: '10 - ' + i18nService.t('october'), value: '10' },
|
|
|
|
{ name: '11 - ' + i18nService.t('november'), value: '11' },
|
|
|
|
{ name: '12 - ' + i18nService.t('december'), value: '12' },
|
|
|
|
];
|
|
|
|
|
|
|
|
this.cardExpYearOptions = [
|
|
|
|
{ name: '-- ' + i18nService.t('select') + ' --', value: null },
|
|
|
|
];
|
|
|
|
const year = (new Date()).getFullYear();
|
2018-07-16 23:17:07 +02:00
|
|
|
for (let i = year; i < (year + 15); i++) {
|
2018-06-29 04:27:32 +02:00
|
|
|
this.cardExpYearOptions.push({ name: i.toString(), value: i.toString().slice(-2) });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit() {
|
2018-07-16 23:17:07 +02:00
|
|
|
if (!this.showOptions) {
|
|
|
|
this.hidePaypal = this.method !== 'paypal';
|
|
|
|
this.hideBank = this.method !== 'bank';
|
|
|
|
}
|
2018-06-29 04:27:32 +02:00
|
|
|
window.document.head.appendChild(this.stripeScript);
|
2018-07-16 23:17:07 +02:00
|
|
|
if (!this.hidePaypal) {
|
|
|
|
window.document.head.appendChild(this.btScript);
|
|
|
|
}
|
2018-06-29 04:27:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ngOnDestroy() {
|
|
|
|
window.document.head.removeChild(this.stripeScript);
|
|
|
|
Array.from(window.document.querySelectorAll('iframe')).forEach((el) => {
|
|
|
|
if (el.src != null && el.src.indexOf('stripe') > -1) {
|
|
|
|
window.document.body.removeChild(el);
|
|
|
|
}
|
|
|
|
});
|
2018-07-16 23:17:07 +02:00
|
|
|
if (!this.hidePaypal) {
|
|
|
|
window.document.head.removeChild(this.btScript);
|
|
|
|
}
|
2018-06-29 04:27:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
changeMethod() {
|
|
|
|
if (this.method !== 'paypal') {
|
|
|
|
this.btInstance = null;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
window.setTimeout(() => {
|
|
|
|
(window as any).braintree.dropin.create({
|
|
|
|
authorization: this.platformUtilsService.isDev() ? Keys.btSandbox : Keys.btProduction,
|
|
|
|
container: '#bt-dropin-container',
|
|
|
|
paymentOptionPriority: ['paypal'],
|
|
|
|
paypal: {
|
|
|
|
flow: 'vault',
|
|
|
|
buttonStyle: {
|
|
|
|
label: 'pay',
|
|
|
|
size: 'medium',
|
|
|
|
shape: 'pill',
|
|
|
|
color: 'blue',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, (createErr: any, instance: any) => {
|
|
|
|
if (createErr != null) {
|
|
|
|
// tslint:disable-next-line
|
|
|
|
console.error(createErr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.btInstance = instance;
|
|
|
|
});
|
|
|
|
}, 250);
|
|
|
|
}
|
|
|
|
|
|
|
|
createPaymentToken(): Promise<string> {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
if (this.method === 'paypal') {
|
|
|
|
this.btInstance.requestPaymentMethod().then((payload: any) => {
|
|
|
|
resolve(payload.nonce);
|
|
|
|
}).catch((err: any) => {
|
|
|
|
reject(err.message);
|
|
|
|
});
|
2018-07-16 23:17:07 +02:00
|
|
|
} else if (this.method === 'card') {
|
2018-06-29 04:27:32 +02:00
|
|
|
(window as any).Stripe.card.createToken(this.card, (status: number, response: any) => {
|
|
|
|
if (status === 200 && response.id != null) {
|
|
|
|
resolve(response.id);
|
|
|
|
} else if (response.error != null) {
|
|
|
|
reject(response.error.message);
|
|
|
|
} else {
|
|
|
|
reject();
|
|
|
|
}
|
|
|
|
});
|
2018-07-16 23:17:07 +02:00
|
|
|
} else if (this.method === 'bank') {
|
|
|
|
(window as any).Stripe.bankAccount.createToken(this.bank, (status: number, response: any) => {
|
|
|
|
if (status === 200 && response.id != null) {
|
|
|
|
resolve(response.id);
|
|
|
|
} else if (response.error != null) {
|
|
|
|
reject(response.error.message);
|
|
|
|
} else {
|
|
|
|
reject();
|
|
|
|
}
|
|
|
|
});
|
2018-06-29 04:27:32 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getCountry(): string {
|
|
|
|
return this.card.address_country;
|
|
|
|
}
|
|
|
|
}
|