mirror of
https://github.com/bitwarden/browser.git
synced 2024-11-27 12:36:14 +01:00
purchase premium from the desktop app
This commit is contained in:
parent
ce3cbe4387
commit
0e52c0d164
2
jslib
2
jslib
@ -1 +1 @@
|
||||
Subproject commit 255bd3962d268bec8f66411676049803bb29e0d3
|
||||
Subproject commit ec012c99341ee65c0d5a7a7fdf153e79bfb274be
|
@ -1,19 +1,121 @@
|
||||
import { Component } from '@angular/core';
|
||||
import { remote } from 'electron';
|
||||
import * as fs from 'fs';
|
||||
|
||||
import {
|
||||
Component,
|
||||
NgZone,
|
||||
} from '@angular/core';
|
||||
|
||||
import { ApiService } from 'jslib/abstractions/api.service';
|
||||
import { I18nService } from 'jslib/abstractions/i18n.service';
|
||||
import { MessagingService } from 'jslib/abstractions/messaging.service';
|
||||
import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service';
|
||||
import { SyncService } from 'jslib/abstractions/sync.service';
|
||||
import { TokenService } from 'jslib/abstractions/token.service';
|
||||
|
||||
import { PremiumComponent as BasePremiumComponent } from 'jslib/angular/components/premium.component';
|
||||
|
||||
import { Utils } from 'jslib/misc/utils';
|
||||
|
||||
@Component({
|
||||
selector: 'app-premium',
|
||||
templateUrl: 'premium.component.html',
|
||||
})
|
||||
export class PremiumComponent extends BasePremiumComponent {
|
||||
formPromise: Promise<any>;
|
||||
canMakeMacAppStorePayments = false;
|
||||
|
||||
constructor(i18nService: I18nService, platformUtilsService: PlatformUtilsService,
|
||||
tokenService: TokenService, apiService: ApiService) {
|
||||
tokenService: TokenService, apiService: ApiService,
|
||||
private ngZone: NgZone, private messagingService: MessagingService,
|
||||
private syncService: SyncService) {
|
||||
super(i18nService, platformUtilsService, tokenService, apiService);
|
||||
}
|
||||
|
||||
async ngOnInit() {
|
||||
await super.ngOnInit();
|
||||
if (this.isPremium || !this.platformUtilsService.isMacAppStore()) {
|
||||
return;
|
||||
}
|
||||
this.canMakeMacAppStorePayments = remote.inAppPurchase.canMakePayments();
|
||||
if (!this.canMakeMacAppStorePayments) {
|
||||
return;
|
||||
}
|
||||
remote.inAppPurchase.on('transactions-updated', (event, transactions) => {
|
||||
this.ngZone.run(async () => {
|
||||
if (!Array.isArray(transactions)) {
|
||||
return;
|
||||
}
|
||||
// Check each transaction.
|
||||
transactions.forEach((transaction) => {
|
||||
const payment = transaction.payment;
|
||||
switch (transaction.transactionState) {
|
||||
case 'purchasing':
|
||||
// tslint:disable-next-line
|
||||
console.log(`Purchasing ${payment.productIdentifier}...`);
|
||||
break;
|
||||
case 'purchased':
|
||||
// tslint:disable-next-line
|
||||
console.log(`${payment.productIdentifier} purchased.`);
|
||||
if (payment.productIdentifier !== 'premium_annually') {
|
||||
return;
|
||||
}
|
||||
const receiptUrl = remote.inAppPurchase.getReceiptURL();
|
||||
const receiptBuffer = fs.readFileSync(receiptUrl);
|
||||
const receiptB64 = Utils.fromBufferToB64(receiptBuffer);
|
||||
const fd = new FormData();
|
||||
fd.append('paymentMethodType', '1');
|
||||
fd.append('paymentToken', receiptB64);
|
||||
fd.append('additionalStorageGb', '0');
|
||||
try {
|
||||
this.formPromise = this.apiService.postPremium(fd).then((paymentResponse) => {
|
||||
if (paymentResponse.success) {
|
||||
return this.finalizePremium();
|
||||
}
|
||||
});
|
||||
} catch { }
|
||||
// Finish the transaction.
|
||||
remote.inAppPurchase.finishTransactionByDate(transaction.transactionDate);
|
||||
break;
|
||||
case 'failed':
|
||||
// tslint:disable-next-line
|
||||
console.log(`Failed to purchase ${payment.productIdentifier}.`);
|
||||
// Finish the transaction.
|
||||
remote.inAppPurchase.finishTransactionByDate(transaction.transactionDate);
|
||||
break;
|
||||
case 'restored':
|
||||
// tslint:disable-next-line
|
||||
console.log(`The purchase of ${payment.productIdentifier} has been restored.`);
|
||||
break;
|
||||
case 'deferred':
|
||||
// tslint:disable-next-line
|
||||
console.log(`The purchase of ${payment.productIdentifier} has been deferred.`);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async purchase() {
|
||||
if (this.isPremium || !this.canMakeMacAppStorePayments) {
|
||||
await super.purchase();
|
||||
return;
|
||||
}
|
||||
remote.inAppPurchase.purchaseProduct('premium_annually', 1, (isValid) => {
|
||||
if (!isValid) {
|
||||
// TODO?
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private async finalizePremium() {
|
||||
await this.apiService.refreshIdentityToken();
|
||||
await this.syncService.fullSync(true);
|
||||
this.platformUtilsService.showToast('success', null, this.i18nService.t('premiumUpdated'));
|
||||
this.messagingService.send('purchasedPremium');
|
||||
this.isPremium = this.tokenService.getPremium();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user