mirror of
https://github.com/bitwarden/browser
synced 2025-12-16 00:03:56 +00:00
restore and error messages on inapp purchase
This commit is contained in:
@@ -55,6 +55,10 @@
|
|||||||
[disabled]="purchaseBtn.loading" [appApiAction]="purchasePromise">
|
[disabled]="purchaseBtn.loading" [appApiAction]="purchasePromise">
|
||||||
<b>{{'premiumPurchase' | i18n}}</b>
|
<b>{{'premiumPurchase' | i18n}}</b>
|
||||||
</button>
|
</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>
|
<button type="button" data-dismiss="modal">{{'close' | i18n}}</button>
|
||||||
<div class="right" *ngIf="!isPremium">
|
<div class="right" *ngIf="!isPremium">
|
||||||
<button #refreshBtn type="button" appBlurClick (click)="refresh()" [disabled]="refreshBtn.loading"
|
<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 {
|
export class PremiumComponent extends BasePremiumComponent {
|
||||||
purchasePromise: Promise<any>;
|
purchasePromise: Promise<any>;
|
||||||
|
restorePromise: Promise<any>;
|
||||||
canMakeMacAppStorePayments = false;
|
canMakeMacAppStorePayments = false;
|
||||||
|
canRestorePurchase = false;
|
||||||
|
|
||||||
constructor(i18nService: I18nService, platformUtilsService: PlatformUtilsService,
|
constructor(i18nService: I18nService, platformUtilsService: PlatformUtilsService,
|
||||||
tokenService: TokenService, apiService: ApiService,
|
tokenService: TokenService, apiService: ApiService,
|
||||||
@@ -45,6 +47,7 @@ export class PremiumComponent extends BasePremiumComponent {
|
|||||||
if (!this.canMakeMacAppStorePayments) {
|
if (!this.canMakeMacAppStorePayments) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
this.setCanRestorePurchase();
|
||||||
remote.inAppPurchase.on('transactions-updated', (event, transactions) => {
|
remote.inAppPurchase.on('transactions-updated', (event, transactions) => {
|
||||||
this.ngZone.run(async () => {
|
this.ngZone.run(async () => {
|
||||||
if (!Array.isArray(transactions)) {
|
if (!Array.isArray(transactions)) {
|
||||||
@@ -64,27 +67,15 @@ export class PremiumComponent extends BasePremiumComponent {
|
|||||||
if (payment.productIdentifier !== 'premium_annually') {
|
if (payment.productIdentifier !== 'premium_annually') {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const receiptUrl = remote.inAppPurchase.getReceiptURL();
|
await this.makePremium(this.purchasePromise);
|
||||||
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 { }
|
|
||||||
// Finish the transaction.
|
// Finish the transaction.
|
||||||
remote.inAppPurchase.finishTransactionByDate(transaction.transactionDate);
|
remote.inAppPurchase.finishTransactionByDate(transaction.transactionDate);
|
||||||
break;
|
break;
|
||||||
case 'failed':
|
case 'failed':
|
||||||
// tslint:disable-next-line
|
// 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.
|
// Finish the transaction.
|
||||||
remote.inAppPurchase.finishTransactionByDate(transaction.transactionDate);
|
remote.inAppPurchase.finishTransactionByDate(transaction.transactionDate);
|
||||||
break;
|
break;
|
||||||
@@ -122,11 +113,56 @@ export class PremiumComponent extends BasePremiumComponent {
|
|||||||
} catch { }
|
} 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() {
|
private async finalizePremium() {
|
||||||
await this.apiService.refreshIdentityToken();
|
await this.apiService.refreshIdentityToken();
|
||||||
await this.syncService.fullSync(true);
|
await this.syncService.fullSync(true);
|
||||||
this.platformUtilsService.showToast('success', null, this.i18nService.t('premiumUpdated'));
|
this.platformUtilsService.showToast('success', null, this.i18nService.t('premiumUpdated'));
|
||||||
this.messagingService.send('purchasedPremium');
|
this.messagingService.send('purchasedPremium');
|
||||||
this.isPremium = this.tokenService.getPremium();
|
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": {
|
"premiumPurchase": {
|
||||||
"message": "Purchase Premium"
|
"message": "Purchase Premium"
|
||||||
},
|
},
|
||||||
|
"premiumRestore": {
|
||||||
|
"message": "Restore Premium"
|
||||||
|
},
|
||||||
"premiumPurchaseAlert": {
|
"premiumPurchaseAlert": {
|
||||||
"message": "You can purchase premium membership on the bitwarden.com web vault. Do you want to visit the website now?"
|
"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": {
|
"selectOneCollection": {
|
||||||
"message": "You must select at least one collection."
|
"message": "You must select at least one collection."
|
||||||
|
},
|
||||||
|
"premiumUpdated": {
|
||||||
|
"message": "You've upgraded to premium."
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user