bitwarden-estensione-browser/src/app/settings/payment.component.ts

214 lines
7.8 KiB
TypeScript
Raw Normal View History

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';
2019-02-20 23:33:05 +01:00
import { WebConstants } from '../../services/webConstants';
2018-06-29 04:27:32 +02:00
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;
2019-02-21 02:16:06 +01:00
@Input() method = PaymentMethodType.Card;
2018-07-16 23:17:07 +02:00
@Input() hideBank = false;
@Input() hidePaypal = false;
2019-02-21 02:16:06 +01:00
@Input() hideCredit = 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
2019-02-21 02:16:06 +01:00
paymentMethodType = PaymentMethodType;
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-20 23:33:05 +01:00
this.stripe = (window as any).Stripe(this.platformUtilsService.isDev() ?
WebConstants.stripeTestKey : WebConstants.stripeLiveKey);
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');
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) {
2019-02-21 02:16:06 +01:00
this.hidePaypal = this.method !== PaymentMethodType.PayPal;
this.hideBank = this.method !== PaymentMethodType.BankAccount;
this.hideCredit = this.method !== PaymentMethodType.Credit;
2018-07-16 23:17:07 +02:00
}
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 { }
}
});
2019-02-21 02:16:06 +01:00
}, 500);
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 { }
}
2019-02-21 02:16:06 +01:00
}, 500);
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-21 02:16:06 +01:00
if (this.method === PaymentMethodType.PayPal) {
2019-02-19 05:40:04 +01:00
window.setTimeout(() => {
(window as any).braintree.dropin.create({
2019-02-20 23:33:05 +01:00
authorization: this.platformUtilsService.isDev() ?
WebConstants.btSandboxKey : WebConstants.btProductionKey,
2019-02-19 05:40:04 +01:00
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) => {
2019-02-21 02:16:06 +01:00
if (this.method === PaymentMethodType.PayPal) {
2018-06-29 04:27:32 +02:00
this.btInstance.requestPaymentMethod().then((payload: any) => {
2019-02-21 02:16:06 +01:00
resolve([payload.nonce, this.method]);
2018-06-29 04:27:32 +02:00
}).catch((err: any) => {
reject(err.message);
});
2019-02-21 02:16:06 +01:00
} else if (this.method === PaymentMethodType.Card || this.method === PaymentMethodType.BankAccount) {
2019-02-19 05:40:04 +01:00
let sourceObj: any = null;
let createObj: any = null;
2019-02-21 02:16:06 +01:00
if (this.method === PaymentMethodType.Card) {
2019-02-19 05:40:04 +01:00
sourceObj = this.stripeCardNumberElement;
} else {
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-21 02:16:06 +01:00
resolve([result.token.id, this.method]);
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(() => {
2019-02-21 02:16:06 +01:00
if (this.method === PaymentMethodType.Card) {
2019-02-19 05:40:04 +01:00
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
}
}