feat: iap (#1996)
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { LoggingDomain, log } from '@/Logging'
|
||||
import { loadPurchaseFlowUrl } from '@/Components/PurchaseFlow/PurchaseFlowFunctions'
|
||||
import { InternalEventBus } from '@standardnotes/snjs'
|
||||
import { InternalEventBus, AppleIAPProductId } from '@standardnotes/snjs'
|
||||
import { action, makeObservable, observable } from 'mobx'
|
||||
import { WebApplication } from '../../Application/Application'
|
||||
import { AbstractViewController } from '../Abstract/AbstractViewController'
|
||||
@@ -26,15 +27,67 @@ export class PurchaseFlowController extends AbstractViewController {
|
||||
this.currentPane = currentPane
|
||||
}
|
||||
|
||||
openPurchaseFlow = (): void => {
|
||||
openPurchaseFlow = (plan = AppleIAPProductId.ProPlanYearly): void => {
|
||||
const user = this.application.getUser()
|
||||
if (!user) {
|
||||
this.isOpen = true
|
||||
return
|
||||
}
|
||||
|
||||
if (this.application.isNativeIOS()) {
|
||||
void this.beginIosIapPurchaseFlow(plan)
|
||||
} else {
|
||||
loadPurchaseFlowUrl(this.application).catch(console.error)
|
||||
}
|
||||
}
|
||||
|
||||
openPurchaseWebpage = () => {
|
||||
loadPurchaseFlowUrl(this.application).catch((err) => {
|
||||
console.error(err)
|
||||
this.application.alertService.alert(err).catch(console.error)
|
||||
})
|
||||
}
|
||||
|
||||
beginIosIapPurchaseFlow = async (plan: AppleIAPProductId): Promise<void> => {
|
||||
const result = await this.application.mobileDevice().purchaseSubscriptionIAP(plan)
|
||||
|
||||
log(LoggingDomain.Purchasing, 'BeginIosIapPurchaseFlow result', result)
|
||||
|
||||
if (!result) {
|
||||
void this.application.alertService.alert('Your purchase was canceled or failed. Please try again.')
|
||||
return
|
||||
}
|
||||
|
||||
const showGenericError = () => {
|
||||
void this.application.alertService.alert(
|
||||
'There was an error confirming your purchase. Please contact support at help@standardnotes.com.',
|
||||
)
|
||||
}
|
||||
|
||||
log(LoggingDomain.Purchasing, 'Confirming result with our server')
|
||||
|
||||
const token = await this.application.getNewSubscriptionToken()
|
||||
|
||||
if (!token) {
|
||||
log(LoggingDomain.Purchasing, 'Unable to generate subscription token')
|
||||
showGenericError()
|
||||
return
|
||||
}
|
||||
|
||||
const confirmResult = await this.application.subscriptions.confirmAppleIAP(result, token)
|
||||
|
||||
log(LoggingDomain.Purchasing, 'Server confirm result', confirmResult)
|
||||
|
||||
if (confirmResult) {
|
||||
void this.application.alerts.alert(
|
||||
'Please allow a few minutes for your subscription benefits to activate. You will see a confirmation alert in the app when your subscription is ready.',
|
||||
'Your purchase was successful!',
|
||||
)
|
||||
} else {
|
||||
showGenericError()
|
||||
}
|
||||
}
|
||||
|
||||
closePurchaseFlow = (): void => {
|
||||
this.isOpen = false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user