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

244 lines
9.3 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';
import { PaymentMethodType } from 'jslib-common/enums/paymentMethodType';
2019-02-19 23:06:01 +01:00
import { ApiService } from 'jslib-common/abstractions/api.service';
import { PlatformUtilsService } from 'jslib-common/abstractions/platformUtils.service';
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 {
@Input() showMethods = true;
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
constructor(private platformUtilsService: PlatformUtilsService, private apiService: ApiService) {
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 = () => {
this.stripe = (window as any).Stripe(process.env.STRIPE_KEY);
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?cache=${process.env.CACHE_TAG}`;
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 => {
2019-02-19 05:40:04 +01:00
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 => {
2019-02-19 05:40:04 +01:00
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({
authorization: process.env.BRAINTREE_KEY,
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:39:40 +01:00
if (this.method === PaymentMethodType.Credit) {
resolve([null, this.method]);
} else 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) {
if (this.method === PaymentMethodType.Card) {
this.apiService.postSetupPayment().then(clientSecret =>
2020-06-13 01:33:29 +02:00
this.stripe.handleCardSetup(clientSecret, this.stripeCardNumberElement))
.then((result: any) => {
if (result.error) {
reject(result.error.message);
} else if (result.setupIntent && result.setupIntent.status === 'succeeded') {
resolve([result.setupIntent.payment_method, this.method]);
} else {
reject();
}
});
2019-02-19 05:40:04 +01:00
} else {
this.stripe.createToken('bank_account', this.bank).then((result: any) => {
if (result.error) {
reject(result.error.message);
} else if (result.token && result.token.id != null) {
resolve([result.token.id, this.method]);
} else {
reject();
}
});
2019-02-19 05:40:04 +01:00
}
2018-06-29 04:27:32 +02:00
}
});
}
handleStripeCardPayment(clientSecret: string, successCallback: () => Promise<any>): Promise<any> {
return new Promise<void>((resolve, reject) => {
if (this.showMethods && this.stripeCardNumberElement == null) {
reject();
return;
}
const handleCardPayment = () => this.showMethods ?
2020-06-13 01:33:29 +02:00
this.stripe.handleCardSetup(clientSecret, this.stripeCardNumberElement) :
this.stripe.handleCardSetup(clientSecret);
return handleCardPayment().then(async (result: any) => {
if (result.error) {
reject(result.error.message);
} else if (result.paymentIntent && result.paymentIntent.status === 'succeeded') {
if (successCallback != null) {
await successCallback();
}
resolve();
} else {
reject();
}
});
});
}
2019-02-19 05:40:04 +01:00
private setStripeElement() {
window.setTimeout(() => {
if (this.showMethods && 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
}
}