From 0e52c0d164610a222b709a4b1251d27309379f04 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Wed, 18 Sep 2019 16:48:49 -0400 Subject: [PATCH] purchase premium from the desktop app --- jslib | 2 +- src/app/accounts/premium.component.ts | 106 +++++++++++++++++++++++++- 2 files changed, 105 insertions(+), 3 deletions(-) diff --git a/jslib b/jslib index 255bd3962d..ec012c9934 160000 --- a/jslib +++ b/jslib @@ -1 +1 @@ -Subproject commit 255bd3962d268bec8f66411676049803bb29e0d3 +Subproject commit ec012c99341ee65c0d5a7a7fdf153e79bfb274be diff --git a/src/app/accounts/premium.component.ts b/src/app/accounts/premium.component.ts index e8c515714c..6515599097 100644 --- a/src/app/accounts/premium.component.ts +++ b/src/app/accounts/premium.component.ts @@ -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; + 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(); + } }