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';
|
|
|
|
|
2019-02-19 23:06:01 +01:00
|
|
|
import { PaymentMethodType } from 'jslib/enums/paymentMethodType';
|
|
|
|
|
2018-06-29 04:27:32 +02:00
|
|
|
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',
|
|
|
|
};
|
|
|
|
|
2019-02-19 05:40:04 +01:00
|
|
|
const StripeElementStyle = {
|
|
|
|
base: {
|
|
|
|
color: '#333333',
|
|
|
|
fontFamily: '"Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif, ' +
|
|
|
|
'"Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"',
|
2019-02-19 06:23:15 +01:00
|
|
|
fontSize: '14px',
|
2019-02-19 05:40:04 +01:00
|
|
|
fontSmoothing: 'antialiased',
|
|
|
|
},
|
|
|
|
invalid: {
|
2019-02-19 05:53:36 +01:00
|
|
|
color: '#333333',
|
2019-02-19 05:40:04 +01:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const StripeElementClasses = {
|
|
|
|
focus: 'is-focused',
|
|
|
|
empty: 'is-empty',
|
|
|
|
invalid: 'is-invalid',
|
|
|
|
};
|
|
|
|
|
2018-06-29 04:27:32 +02:00
|
|
|
@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-17 21:07:32 +02:00
|
|
|
@Input() method: 'card' | 'paypal' | 'bank' = 'card';
|
2018-07-16 23:17:07 +02:00
|
|
|
@Input() hideBank = false;
|
|
|
|
@Input() hidePaypal = false;
|
2018-06-30 19:36:39 +02:00
|
|
|
|
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
|
|
|
|
|
|
|
private btScript: HTMLScriptElement;
|
|
|
|
private btInstance: any = null;
|
2019-02-19 05:40:04 +01:00
|
|
|
private stripeScript: HTMLScriptElement;
|
|
|
|
private stripe: any = null;
|
|
|
|
private stripeElements: any = null;
|
|
|
|
private stripeCardNumberElement: any = null;
|
|
|
|
private stripeCardExpiryElement: any = null;
|
|
|
|
private stripeCardCvcElement: any = null;
|
2018-06-29 04:27:32 +02:00
|
|
|
|
2019-02-19 05:40:04 +01:00
|
|
|
constructor(private platformUtilsService: PlatformUtilsService) {
|
2018-06-29 04:27:32 +02:00
|
|
|
this.stripeScript = window.document.createElement('script');
|
2019-02-19 05:40:04 +01:00
|
|
|
this.stripeScript.src = 'https://js.stripe.com/v3/';
|
2018-06-29 04:27:32 +02:00
|
|
|
this.stripeScript.async = true;
|
|
|
|
this.stripeScript.onload = () => {
|
2019-02-19 05:40:04 +01:00
|
|
|
this.stripe = (window as any).Stripe(
|
2018-06-29 04:27:32 +02:00
|
|
|
this.platformUtilsService.isDev() ? Keys.stripeTest : Keys.stripeLive);
|
2019-02-19 05:40:04 +01:00
|
|
|
this.stripeElements = this.stripe.elements();
|
|
|
|
this.setStripeElement();
|
2018-06-29 04:27:32 +02:00
|
|
|
};
|
|
|
|
this.btScript = window.document.createElement('script');
|
2018-09-11 23:40:56 +02:00
|
|
|
this.btScript.src = 'scripts/dropin.js';
|
2018-06-29 04:27:32 +02:00
|
|
|
this.btScript.async = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2019-02-19 05:40:04 +01:00
|
|
|
window.setTimeout(() => {
|
|
|
|
Array.from(window.document.querySelectorAll('iframe')).forEach((el) => {
|
|
|
|
if (el.src != null && el.src.indexOf('stripe') > -1) {
|
|
|
|
try {
|
|
|
|
window.document.body.removeChild(el);
|
|
|
|
} catch { }
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, 200);
|
2018-07-16 23:17:07 +02:00
|
|
|
if (!this.hidePaypal) {
|
|
|
|
window.document.head.removeChild(this.btScript);
|
2019-02-19 05:40:04 +01:00
|
|
|
window.setTimeout(() => {
|
|
|
|
Array.from(window.document.head.querySelectorAll('script')).forEach((el) => {
|
|
|
|
if (el.src != null && el.src.indexOf('paypal') > -1) {
|
|
|
|
try {
|
|
|
|
window.document.head.removeChild(el);
|
|
|
|
} catch { }
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const btStylesheet = window.document.head.querySelector('#braintree-dropin-stylesheet');
|
|
|
|
if (btStylesheet != null) {
|
|
|
|
try {
|
|
|
|
window.document.head.removeChild(btStylesheet);
|
|
|
|
} catch { }
|
|
|
|
}
|
|
|
|
}, 200);
|
2018-07-16 23:17:07 +02:00
|
|
|
}
|
2018-06-29 04:27:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
changeMethod() {
|
2019-02-19 05:40:04 +01:00
|
|
|
this.btInstance = null;
|
2018-06-29 04:27:32 +02:00
|
|
|
|
2019-02-19 05:40:04 +01:00
|
|
|
if (this.method === 'paypal') {
|
|
|
|
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',
|
|
|
|
},
|
2018-06-29 04:27:32 +02:00
|
|
|
},
|
2019-02-19 05:40:04 +01:00
|
|
|
}, (createErr: any, instance: any) => {
|
|
|
|
if (createErr != null) {
|
|
|
|
// tslint:disable-next-line
|
|
|
|
console.error(createErr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.btInstance = instance;
|
|
|
|
});
|
|
|
|
}, 250);
|
|
|
|
} else {
|
|
|
|
this.setStripeElement();
|
|
|
|
}
|
2018-06-29 04:27:32 +02:00
|
|
|
}
|
|
|
|
|
2019-02-19 23:06:01 +01:00
|
|
|
createPaymentToken(): Promise<[string, PaymentMethodType]> {
|
2018-06-29 04:27:32 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
if (this.method === 'paypal') {
|
|
|
|
this.btInstance.requestPaymentMethod().then((payload: any) => {
|
2019-02-19 23:06:01 +01:00
|
|
|
resolve([payload.nonce, PaymentMethodType.PayPal]);
|
2018-06-29 04:27:32 +02:00
|
|
|
}).catch((err: any) => {
|
|
|
|
reject(err.message);
|
|
|
|
});
|
2018-07-17 21:07:32 +02:00
|
|
|
} else if (this.method === 'card' || this.method === 'bank') {
|
2019-02-19 23:06:01 +01:00
|
|
|
let type = PaymentMethodType.Card;
|
2019-02-19 05:40:04 +01:00
|
|
|
let sourceObj: any = null;
|
|
|
|
let createObj: any = null;
|
|
|
|
if (this.method === 'card') {
|
|
|
|
sourceObj = this.stripeCardNumberElement;
|
|
|
|
} else {
|
2019-02-19 23:06:01 +01:00
|
|
|
type = PaymentMethodType.BankAccount;
|
2019-02-19 05:40:04 +01:00
|
|
|
sourceObj = 'bank_account';
|
|
|
|
createObj = this.bank;
|
|
|
|
}
|
|
|
|
this.stripe.createToken(sourceObj, createObj).then((result: any) => {
|
|
|
|
if (result.error) {
|
|
|
|
reject(result.error.message);
|
|
|
|
} else if (result.token && result.token.id != null) {
|
2019-02-19 23:06:01 +01:00
|
|
|
resolve([result.token.id, type]);
|
2018-07-16 23:17:07 +02:00
|
|
|
} else {
|
|
|
|
reject();
|
|
|
|
}
|
|
|
|
});
|
2018-06-29 04:27:32 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-02-19 05:40:04 +01:00
|
|
|
private setStripeElement() {
|
|
|
|
window.setTimeout(() => {
|
|
|
|
if (this.method === 'card') {
|
|
|
|
if (this.stripeCardNumberElement == null) {
|
|
|
|
this.stripeCardNumberElement = this.stripeElements.create('cardNumber', {
|
|
|
|
style: StripeElementStyle,
|
|
|
|
classes: StripeElementClasses,
|
|
|
|
placeholder: '',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (this.stripeCardExpiryElement == null) {
|
|
|
|
this.stripeCardExpiryElement = this.stripeElements.create('cardExpiry', {
|
|
|
|
style: StripeElementStyle,
|
|
|
|
classes: StripeElementClasses,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (this.stripeCardCvcElement == null) {
|
|
|
|
this.stripeCardCvcElement = this.stripeElements.create('cardCvc', {
|
|
|
|
style: StripeElementStyle,
|
|
|
|
classes: StripeElementClasses,
|
|
|
|
placeholder: '',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
this.stripeCardNumberElement.mount('#stripe-card-number-element');
|
|
|
|
this.stripeCardExpiryElement.mount('#stripe-card-expiry-element');
|
|
|
|
this.stripeCardCvcElement.mount('#stripe-card-cvc-element');
|
|
|
|
}
|
|
|
|
}, 50);
|
2018-06-29 04:27:32 +02:00
|
|
|
}
|
|
|
|
}
|