mirror of
https://github.com/bitwarden/browser.git
synced 2024-11-23 11:56:00 +01:00
restore and error messages on inapp purchase
This commit is contained in:
parent
2f84e2fd82
commit
5baea84a6a
@ -55,6 +55,10 @@
|
||||
[disabled]="purchaseBtn.loading" [appApiAction]="purchasePromise">
|
||||
<b>{{'premiumPurchase' | i18n}}</b>
|
||||
</button>
|
||||
<button #restoreBtn type="button" class="primary" appBlurClick (click)="restore()"
|
||||
*ngIf="canRestorePurchase" [disabled]="restoreBtn.loading" [appApiAction]="restorePromise">
|
||||
<b>{{'premiumRestore' | i18n}}</b>
|
||||
</button>
|
||||
<button type="button" data-dismiss="modal">{{'close' | i18n}}</button>
|
||||
<div class="right" *ngIf="!isPremium">
|
||||
<button #refreshBtn type="button" appBlurClick (click)="refresh()" [disabled]="refreshBtn.loading"
|
||||
|
@ -27,7 +27,9 @@ import { Utils } from 'jslib/misc/utils';
|
||||
})
|
||||
export class PremiumComponent extends BasePremiumComponent {
|
||||
purchasePromise: Promise<any>;
|
||||
restorePromise: Promise<any>;
|
||||
canMakeMacAppStorePayments = false;
|
||||
canRestorePurchase = false;
|
||||
|
||||
constructor(i18nService: I18nService, platformUtilsService: PlatformUtilsService,
|
||||
tokenService: TokenService, apiService: ApiService,
|
||||
@ -45,6 +47,7 @@ export class PremiumComponent extends BasePremiumComponent {
|
||||
if (!this.canMakeMacAppStorePayments) {
|
||||
return;
|
||||
}
|
||||
this.setCanRestorePurchase();
|
||||
remote.inAppPurchase.on('transactions-updated', (event, transactions) => {
|
||||
this.ngZone.run(async () => {
|
||||
if (!Array.isArray(transactions)) {
|
||||
@ -64,27 +67,15 @@ export class PremiumComponent extends BasePremiumComponent {
|
||||
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', '6');
|
||||
fd.append('paymentToken', receiptB64);
|
||||
fd.append('additionalStorageGb', '0');
|
||||
try {
|
||||
this.purchasePromise = this.apiService.postPremium(fd).then((paymentResponse) => {
|
||||
if (paymentResponse.success) {
|
||||
return this.finalizePremium();
|
||||
}
|
||||
});
|
||||
await this.purchasePromise;
|
||||
} catch { }
|
||||
await this.makePremium(this.purchasePromise);
|
||||
// Finish the transaction.
|
||||
remote.inAppPurchase.finishTransactionByDate(transaction.transactionDate);
|
||||
break;
|
||||
case 'failed':
|
||||
// tslint:disable-next-line
|
||||
console.log(`Failed to purchase ${payment.productIdentifier}.`);
|
||||
console.log(`Failed to purchase ${payment.productIdentifier}.` +
|
||||
`${transaction.errorCode} = ${transaction.errorMessage}`);
|
||||
this.platformUtilsService.showToast('error', null, transaction.errorMessage);
|
||||
// Finish the transaction.
|
||||
remote.inAppPurchase.finishTransactionByDate(transaction.transactionDate);
|
||||
break;
|
||||
@ -122,11 +113,56 @@ export class PremiumComponent extends BasePremiumComponent {
|
||||
} catch { }
|
||||
}
|
||||
|
||||
async restore() {
|
||||
if (this.isPremium || !this.canMakeMacAppStorePayments) {
|
||||
return;
|
||||
}
|
||||
let makePremium = false;
|
||||
try {
|
||||
const request = new IapCheckRequest();
|
||||
request.paymentMethodType = PaymentMethodType.AppleInApp;
|
||||
this.restorePromise = this.apiService.postIapCheck(request);
|
||||
await this.restorePromise;
|
||||
makePremium = true;
|
||||
} catch { }
|
||||
if (makePremium) {
|
||||
await this.makePremium(this.restorePromise);
|
||||
}
|
||||
}
|
||||
|
||||
private async makePremium(promise: Promise<any>) {
|
||||
const receiptUrl = remote.inAppPurchase.getReceiptURL();
|
||||
const receiptBuffer = fs.readFileSync(receiptUrl);
|
||||
const receiptB64 = Utils.fromBufferToB64(receiptBuffer);
|
||||
const fd = new FormData();
|
||||
fd.append('paymentMethodType', '6');
|
||||
fd.append('paymentToken', receiptB64);
|
||||
fd.append('additionalStorageGb', '0');
|
||||
try {
|
||||
promise = this.apiService.postPremium(fd).then((paymentResponse) => {
|
||||
if (paymentResponse.success) {
|
||||
return this.finalizePremium();
|
||||
}
|
||||
});
|
||||
await promise;
|
||||
} catch { }
|
||||
}
|
||||
|
||||
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();
|
||||
this.setCanRestorePurchase();
|
||||
}
|
||||
|
||||
private setCanRestorePurchase() {
|
||||
if (!this.isPremium && this.canMakeMacAppStorePayments) {
|
||||
const receiptUrl = remote.inAppPurchase.getReceiptURL();
|
||||
this.canRestorePurchase = receiptUrl != null;
|
||||
} else {
|
||||
this.canRestorePurchase = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -983,6 +983,9 @@
|
||||
"premiumPurchase": {
|
||||
"message": "Purchase Premium"
|
||||
},
|
||||
"premiumRestore": {
|
||||
"message": "Restore Premium"
|
||||
},
|
||||
"premiumPurchaseAlert": {
|
||||
"message": "You can purchase premium membership on the bitwarden.com web vault. Do you want to visit the website now?"
|
||||
},
|
||||
@ -1248,5 +1251,8 @@
|
||||
},
|
||||
"selectOneCollection": {
|
||||
"message": "You must select at least one collection."
|
||||
},
|
||||
"premiumUpdated": {
|
||||
"message": "You've upgraded to premium."
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user