1
0
mirror of https://github.com/bitwarden/browser synced 2025-01-08 16:33:38 +01:00
bitwarden-estensione-browser/src/app/settings/add-credit.component.ts

117 lines
3.7 KiB
TypeScript
Raw Normal View History

2019-02-20 23:33:05 +01:00
import {
Component,
ElementRef,
EventEmitter,
Input,
OnInit,
Output,
ViewChild,
} from '@angular/core';
import { ToasterService } from 'angular2-toaster';
import { Angulartics2 } from 'angulartics2';
import { I18nService } from 'jslib/abstractions/i18n.service';
import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service';
import { UserService } from 'jslib/abstractions/user.service';
import { PaymentMethodType } from 'jslib/enums/paymentMethodType';
import { WebConstants } from '../../services/webConstants';
@Component({
selector: 'app-add-credit',
templateUrl: 'add-credit.component.html',
})
export class AddCreditComponent implements OnInit {
2019-02-22 00:03:39 +01:00
@Input() creditAmount: string;
2019-02-20 23:33:05 +01:00
@Input() showOptions = true;
@Input() method = PaymentMethodType.PayPal;
@Input() organizationId: string;
@Output() onAdded = new EventEmitter();
@Output() onCanceled = new EventEmitter();
@ViewChild('ppButtonForm', { read: ElementRef }) ppButtonFormRef: ElementRef;
paymentMethodType = PaymentMethodType;
ppButtonFormAction = WebConstants.paypal.buttonActionProduction;
ppButtonBusinessId = WebConstants.paypal.businessIdProduction;
ppButtonCustomField: string;
2019-02-22 00:03:39 +01:00
ppReturnUrl: string;
2019-02-21 02:37:27 +01:00
ppLoading = false;
2019-02-20 23:33:05 +01:00
subject: string;
formPromise: Promise<any>;
constructor(private userService: UserService, private i18nService: I18nService,
private analytics: Angulartics2, private toasterService: ToasterService,
platformUtilsService: PlatformUtilsService) {
if (platformUtilsService.isDev()) {
this.ppButtonFormAction = WebConstants.paypal.buttonActionSandbox;
this.ppButtonBusinessId = WebConstants.paypal.businessIdSandbox;
}
}
async ngOnInit() {
if (this.organizationId != null) {
2019-02-22 00:03:39 +01:00
if (this.creditAmount == null) {
this.creditAmount = '20.00';
}
2019-02-20 23:33:05 +01:00
this.ppButtonCustomField = 'organization_id:' + this.organizationId;
const org = await this.userService.getOrganization(this.organizationId);
if (org != null) {
this.subject = org.name;
}
} else {
2019-02-22 00:03:39 +01:00
if (this.creditAmount == null) {
this.creditAmount = '10.00';
}
2019-02-20 23:33:05 +01:00
const userId = await this.userService.getUserId();
this.subject = await this.userService.getEmail();
this.ppButtonCustomField = 'user_id:' + userId;
}
2019-02-22 03:48:02 +01:00
this.ppButtonCustomField += ',account_credit:1';
2019-02-22 00:03:39 +01:00
this.ppReturnUrl = window.location.href;
2019-02-20 23:33:05 +01:00
}
async submit() {
if (this.creditAmount == null || this.creditAmount === '') {
return;
}
if (this.method === PaymentMethodType.PayPal) {
this.ppButtonFormRef.nativeElement.submit();
2019-02-21 02:37:27 +01:00
this.ppLoading = true;
2019-02-20 23:33:05 +01:00
return;
}
try {
this.analytics.eventTrack.next({
action: 'Added Credit',
});
this.toasterService.popAsync('success', null, this.i18nService.t('updatedPaymentMethod'));
this.onAdded.emit();
} catch { }
}
changeMethod() {
// TODO:
}
cancel() {
this.onCanceled.emit();
}
formatAmount() {
try {
if (this.creditAmount != null && this.creditAmount !== '') {
const floatAmount = Math.abs(parseFloat(this.creditAmount));
if (floatAmount > 0) {
this.creditAmount = parseFloat((Math.round(floatAmount * 100) / 100).toString())
.toFixed(2).toString();
return;
}
}
} catch { }
this.creditAmount = '';
}
}