mirror of
https://github.com/bitwarden/browser
synced 2025-01-06 15:38:42 +01:00
62ad39e697
* Create tracker that can await until expected observables are received. * Test dates are almost equal * Remove unused class method * Allow for updating active account in accout service fake * Correct observable tracker behavior Clarify documentation * Transition config service to state provider Updates the config fetching behavior to be lazy and ensure that any emitted value has been updated if older than a configurable value (statically compiled). If desired, config fetching can be ensured fresh through an async. * Update calls to config service in DI and bootstrapping * Migrate account server configs * Fix global config fetching * Test migration rollback * Adhere to implementation naming convention * Adhere to abstract class naming convention * Complete config abstraction rename * Remove unnecessary cli config service * Fix builds * Validate observable does not complete * Use token service to determine authed or unauthed config pull * Remove superfluous factory config * Name describe blocks after the thing they test * Remove implementation documentation Unfortunately the experience when linking to external documentation is quite poor. Instead of following the link and retrieving docs, you get a link that can be clicked to take you out of context to the docs. No link _does_ retrieve docs, but lacks indication in the implementation that documentation exists at all. On the balance, removing the link is the better experience. * Fix storybook
160 lines
4.8 KiB
TypeScript
160 lines
4.8 KiB
TypeScript
import {
|
|
Component,
|
|
ElementRef,
|
|
EventEmitter,
|
|
Input,
|
|
OnInit,
|
|
Output,
|
|
ViewChild,
|
|
} from "@angular/core";
|
|
import { firstValueFrom } from "rxjs";
|
|
|
|
import { ApiService } from "@bitwarden/common/abstractions/api.service";
|
|
import { OrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction";
|
|
import { PaymentMethodType } from "@bitwarden/common/billing/enums";
|
|
import { BitPayInvoiceRequest } from "@bitwarden/common/billing/models/request/bit-pay-invoice.request";
|
|
import { ConfigService } from "@bitwarden/common/platform/abstractions/config/config.service";
|
|
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
|
|
import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service";
|
|
import { StateService } from "@bitwarden/common/platform/abstractions/state.service";
|
|
|
|
export type PayPalConfig = {
|
|
businessId?: string;
|
|
buttonAction?: string;
|
|
};
|
|
|
|
@Component({
|
|
selector: "app-add-credit",
|
|
templateUrl: "add-credit.component.html",
|
|
})
|
|
export class AddCreditComponent implements OnInit {
|
|
@Input() creditAmount: string;
|
|
@Input() showOptions = true;
|
|
@Input() method = PaymentMethodType.PayPal;
|
|
@Input() organizationId: string;
|
|
@Output() onAdded = new EventEmitter();
|
|
@Output() onCanceled = new EventEmitter();
|
|
|
|
@ViewChild("ppButtonForm", { read: ElementRef, static: true }) ppButtonFormRef: ElementRef;
|
|
|
|
paymentMethodType = PaymentMethodType;
|
|
ppButtonFormAction: string;
|
|
ppButtonBusinessId: string;
|
|
ppButtonCustomField: string;
|
|
ppLoading = false;
|
|
subject: string;
|
|
returnUrl: string;
|
|
formPromise: Promise<any>;
|
|
|
|
private userId: string;
|
|
private name: string;
|
|
private email: string;
|
|
private region: string;
|
|
|
|
constructor(
|
|
private stateService: StateService,
|
|
private apiService: ApiService,
|
|
private platformUtilsService: PlatformUtilsService,
|
|
private organizationService: OrganizationService,
|
|
private logService: LogService,
|
|
private configService: ConfigService,
|
|
) {
|
|
const payPalConfig = process.env.PAYPAL_CONFIG as PayPalConfig;
|
|
this.ppButtonFormAction = payPalConfig.buttonAction;
|
|
this.ppButtonBusinessId = payPalConfig.businessId;
|
|
}
|
|
|
|
async ngOnInit() {
|
|
if (this.organizationId != null) {
|
|
if (this.creditAmount == null) {
|
|
this.creditAmount = "20.00";
|
|
}
|
|
this.ppButtonCustomField = "organization_id:" + this.organizationId;
|
|
const org = await this.organizationService.get(this.organizationId);
|
|
if (org != null) {
|
|
this.subject = org.name;
|
|
this.name = org.name;
|
|
}
|
|
} else {
|
|
if (this.creditAmount == null) {
|
|
this.creditAmount = "10.00";
|
|
}
|
|
this.userId = await this.stateService.getUserId();
|
|
this.subject = await this.stateService.getEmail();
|
|
this.email = this.subject;
|
|
this.ppButtonCustomField = "user_id:" + this.userId;
|
|
}
|
|
this.region = await firstValueFrom(this.configService.cloudRegion$);
|
|
this.ppButtonCustomField += ",account_credit:1";
|
|
this.ppButtonCustomField += `,region:${this.region}`;
|
|
this.returnUrl = window.location.href;
|
|
}
|
|
|
|
async submit() {
|
|
if (this.creditAmount == null || this.creditAmount === "") {
|
|
return;
|
|
}
|
|
|
|
if (this.method === PaymentMethodType.PayPal) {
|
|
this.ppButtonFormRef.nativeElement.submit();
|
|
this.ppLoading = true;
|
|
return;
|
|
}
|
|
if (this.method === PaymentMethodType.BitPay) {
|
|
try {
|
|
const req = new BitPayInvoiceRequest();
|
|
req.email = this.email;
|
|
req.name = this.name;
|
|
req.credit = true;
|
|
req.amount = this.creditAmountNumber;
|
|
req.organizationId = this.organizationId;
|
|
req.userId = this.userId;
|
|
req.returnUrl = this.returnUrl;
|
|
this.formPromise = this.apiService.postBitPayInvoice(req);
|
|
const bitPayUrl: string = await this.formPromise;
|
|
this.platformUtilsService.launchUri(bitPayUrl);
|
|
} catch (e) {
|
|
this.logService.error(e);
|
|
}
|
|
return;
|
|
}
|
|
try {
|
|
this.onAdded.emit();
|
|
} catch (e) {
|
|
this.logService.error(e);
|
|
}
|
|
}
|
|
|
|
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 (e) {
|
|
this.logService.error(e);
|
|
}
|
|
this.creditAmount = "";
|
|
}
|
|
|
|
get creditAmountNumber(): number {
|
|
if (this.creditAmount != null && this.creditAmount !== "") {
|
|
try {
|
|
return parseFloat(this.creditAmount);
|
|
} catch (e) {
|
|
this.logService.error(e);
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
}
|