2019-02-20 23:33:05 +01:00
|
|
|
import {
|
|
|
|
Component,
|
|
|
|
ElementRef,
|
|
|
|
EventEmitter,
|
|
|
|
Input,
|
|
|
|
OnInit,
|
|
|
|
Output,
|
|
|
|
ViewChild,
|
|
|
|
} from "@angular/core";
|
|
|
|
|
2022-06-14 17:10:53 +02:00
|
|
|
import { ApiService } from "@bitwarden/common/abstractions/api.service";
|
|
|
|
import { PayPalConfig } from "@bitwarden/common/abstractions/environment.service";
|
|
|
|
import { LogService } from "@bitwarden/common/abstractions/log.service";
|
2022-09-27 22:25:19 +02:00
|
|
|
import { OrganizationService } from "@bitwarden/common/abstractions/organization/organization.service.abstraction";
|
2022-06-14 17:10:53 +02:00
|
|
|
import { PlatformUtilsService } from "@bitwarden/common/abstractions/platformUtils.service";
|
|
|
|
import { StateService } from "@bitwarden/common/abstractions/state.service";
|
|
|
|
import { PaymentMethodType } from "@bitwarden/common/enums/paymentMethodType";
|
2022-10-18 19:01:42 +02:00
|
|
|
import { BitPayInvoiceRequest } from "@bitwarden/common/models/request/bit-pay-invoice.request";
|
2019-02-22 04:48:59 +01:00
|
|
|
|
2019-02-20 23:33:05 +01:00
|
|
|
@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();
|
|
|
|
|
2020-08-17 16:04:38 +02:00
|
|
|
@ViewChild("ppButtonForm", { read: ElementRef, static: true }) ppButtonFormRef: ElementRef;
|
2019-02-20 23:33:05 +01:00
|
|
|
|
|
|
|
paymentMethodType = PaymentMethodType;
|
2021-09-09 23:18:46 +02:00
|
|
|
ppButtonFormAction: string;
|
|
|
|
ppButtonBusinessId: string;
|
2019-02-20 23:33:05 +01:00
|
|
|
ppButtonCustomField: string;
|
2019-02-21 02:37:27 +01:00
|
|
|
ppLoading = false;
|
2019-02-20 23:33:05 +01:00
|
|
|
subject: string;
|
2019-02-22 04:48:59 +01:00
|
|
|
returnUrl: string;
|
2019-02-20 23:33:05 +01:00
|
|
|
formPromise: Promise<any>;
|
|
|
|
|
2019-02-22 04:48:59 +01:00
|
|
|
private userId: string;
|
|
|
|
private name: string;
|
|
|
|
private email: string;
|
|
|
|
|
2021-12-14 17:10:26 +01:00
|
|
|
constructor(
|
|
|
|
private stateService: StateService,
|
|
|
|
private apiService: ApiService,
|
|
|
|
private platformUtilsService: PlatformUtilsService,
|
|
|
|
private organizationService: OrganizationService,
|
|
|
|
private logService: LogService
|
|
|
|
) {
|
2021-09-09 23:18:46 +02:00
|
|
|
const payPalConfig = process.env.PAYPAL_CONFIG as PayPalConfig;
|
|
|
|
this.ppButtonFormAction = payPalConfig.buttonAction;
|
|
|
|
this.ppButtonBusinessId = payPalConfig.businessId;
|
2019-02-20 23:33:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2021-12-14 17:10:26 +01:00
|
|
|
const org = await this.organizationService.get(this.organizationId);
|
2019-02-20 23:33:05 +01:00
|
|
|
if (org != null) {
|
|
|
|
this.subject = org.name;
|
2019-02-22 04:48:59 +01:00
|
|
|
this.name = org.name;
|
2019-02-20 23:33:05 +01:00
|
|
|
}
|
|
|
|
} else {
|
2019-02-22 00:03:39 +01:00
|
|
|
if (this.creditAmount == null) {
|
|
|
|
this.creditAmount = "10.00";
|
|
|
|
}
|
2021-12-14 17:10:26 +01:00
|
|
|
this.userId = await this.stateService.getUserId();
|
|
|
|
this.subject = await this.stateService.getEmail();
|
2019-02-22 04:48:59 +01:00
|
|
|
this.email = this.subject;
|
|
|
|
this.ppButtonCustomField = "user_id:" + this.userId;
|
2019-02-20 23:33:05 +01:00
|
|
|
}
|
2019-02-22 03:48:02 +01:00
|
|
|
this.ppButtonCustomField += ",account_credit:1";
|
2019-02-22 04:48:59 +01:00
|
|
|
this.returnUrl = window.location.href;
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2019-02-20 23:33:05 +01:00
|
|
|
|
|
|
|
async submit() {
|
2019-02-22 04:48:59 +01:00
|
|
|
if (this.creditAmount == null || this.creditAmount === "") {
|
|
|
|
return;
|
2019-02-20 23:33:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.method === PaymentMethodType.PayPal) {
|
|
|
|
this.ppButtonFormRef.nativeElement.submit();
|
2019-02-21 02:37:27 +01:00
|
|
|
this.ppLoading = true;
|
2021-12-17 15:57:11 +01:00
|
|
|
return;
|
|
|
|
}
|
2019-02-22 04:48:59 +01:00
|
|
|
if (this.method === PaymentMethodType.BitPay) {
|
2021-12-17 15:57:11 +01:00
|
|
|
try {
|
2019-02-22 04:48:59 +01:00
|
|
|
const req = new BitPayInvoiceRequest();
|
|
|
|
req.email = this.email;
|
|
|
|
req.name = this.name;
|
|
|
|
req.credit = true;
|
2019-02-20 23:33:05 +01:00
|
|
|
req.amount = this.creditAmountNumber;
|
|
|
|
req.organizationId = this.organizationId;
|
2019-02-22 04:48:59 +01:00
|
|
|
req.userId = this.userId;
|
|
|
|
req.returnUrl = this.returnUrl;
|
|
|
|
this.formPromise = this.apiService.postBitPayInvoice(req);
|
|
|
|
const bitPayUrl: string = await this.formPromise;
|
2019-02-20 23:33:05 +01:00
|
|
|
this.platformUtilsService.launchUri(bitPayUrl);
|
|
|
|
} catch (e) {
|
|
|
|
this.logService.error(e);
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
2019-02-20 23:33:05 +01:00
|
|
|
this.onAdded.emit();
|
|
|
|
} catch (e) {
|
2021-10-20 18:30:04 +02:00
|
|
|
this.logService.error(e);
|
2019-02-20 23:33:05 +01:00
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2019-02-20 23:33:05 +01:00
|
|
|
|
2021-12-17 15:57:11 +01:00
|
|
|
cancel() {
|
2019-02-20 23:33:05 +01:00
|
|
|
this.onCanceled.emit();
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
|
2019-02-20 23:33:05 +01:00
|
|
|
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;
|
2021-10-20 18:30:04 +02:00
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2021-10-20 18:30:04 +02:00
|
|
|
} catch (e) {
|
2019-02-20 23:33:05 +01:00
|
|
|
this.logService.error(e);
|
|
|
|
}
|
|
|
|
this.creditAmount = "";
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2019-02-22 04:48:59 +01:00
|
|
|
|
|
|
|
get creditAmountNumber(): number {
|
|
|
|
if (this.creditAmount != null && this.creditAmount !== "") {
|
|
|
|
try {
|
|
|
|
return parseFloat(this.creditAmount);
|
2021-10-20 18:30:04 +02:00
|
|
|
} catch (e) {
|
|
|
|
this.logService.error(e);
|
2019-02-22 04:48:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2019-02-20 23:33:05 +01:00
|
|
|
}
|