Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

mobile: fix subscriptions not fulfilling on ios

Ammar Ahmed50137e0994b1
diff --git a/apps/mobile/app/components/premium/pricing-plans.tsx b/apps/mobile/app/components/premium/pricing-plans.tsx
index 6cd625d2b..cf3614708 100644
--- a/apps/mobile/app/components/premium/pricing-plans.tsx
+++ b/apps/mobile/app/components/premium/pricing-plans.tsx
@@ -20,7 +20,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import React, { useCallback, useEffect, useState } from "react";
import { ActivityIndicator, Platform, Text, View } from "react-native";
import * as RNIap from "react-native-iap";
-import { db } from "../../common/database";
+import { DatabaseLogger, db } from "../../common/database";
import { usePricing } from "../../hooks/use-pricing";
import {
eSendEvent,
@@ -49,6 +49,20 @@ import { Walkthrough } from "../walkthroughs";
import { PricingItem } from "./pricing-item";
import { useSettingStore } from "../../stores/use-setting-store";
+const UUID_PREFIX = "0bdaea";
+const UUID_VERSION = "4";
+const UUID_VARIANT = "a";
+
+function toUUID(str: string) {
+ return [
+ UUID_PREFIX + str.substring(0, 2), // 6 digit prefix + first 2 oid digits
+ str.substring(2, 6), // # next 4 oid digits
+ UUID_VERSION + str.substring(6, 9), // # 1 digit version(0x4) + next 3 oid digits
+ UUID_VARIANT + str.substring(9, 12), // # 1 digit variant(0b101) + 1 zero bit + next 3 oid digits
+ str.substring(12)
+ ].join("-");
+}
+
const promoCyclesMonthly = {
1: "first month",
2: "first 2 months",
@@ -177,11 +191,15 @@ export const PricingPlans = ({
.offerToken
: null;
+ DatabaseLogger.info(
+ `Subscription Requested initiated for user ${toUUID(user.id)}`
+ );
+
await RNIap.requestSubscription({
sku: product?.productId,
obfuscatedAccountIdAndroid: user.id,
obfuscatedProfileIdAndroid: user.id,
- appAccountToken: user.id,
+ appAccountToken: toUUID(user.id),
andDangerouslyFinishTransactionAutomaticallyIOS: false,
subscriptionOffers: androidOfferToken
? [
diff --git a/apps/mobile/app/hooks/use-app-events.js b/apps/mobile/app/hooks/use-app-events.js
index 77ea3c37b..998d41d11 100644
--- a/apps/mobile/app/hooks/use-app-events.js
+++ b/apps/mobile/app/hooks/use-app-events.js
@@ -460,7 +460,6 @@ export const useAppEvents = () => {
}
clearMessage();
- subscribeToIAPListeners();
if (!login) {
user = await db.user.fetchUser();
setUser(user);
@@ -479,7 +478,11 @@ export const useAppEvents = () => {
userEmailConfirmed: true
});
}
+
+ subscribeToIAPListeners();
} catch (e) {
+ DatabaseLogger.error(error);
+
ToastEvent.error(e, "An error occurred", "global");
}
@@ -503,14 +506,15 @@ export const useAppEvents = () => {
);
const subscribeToIAPListeners = useCallback(async () => {
- RNIap.flushFailedPurchasesCachedAsPendingAndroid()
- .catch(() => {})
- .then(() => {
- refValues.current.subsriptionSuccessListener =
- RNIap.purchaseUpdatedListener(onSuccessfulSubscription);
- refValues.current.subsriptionErrorListener =
- RNIap.purchaseErrorListener(onSubscriptionError);
- });
+ if (Platform.OS === "android") {
+ try {
+ await RNIap.flushFailedPurchasesCachedAsPendingAndroid();
+ } catch (e) {}
+ }
+ refValues.current.subsriptionSuccessListener =
+ RNIap.purchaseUpdatedListener(onSuccessfulSubscription);
+ refValues.current.subsriptionErrorListener =
+ RNIap.purchaseErrorListener(onSubscriptionError);
}, []);
const unSubscribeFromIAPListeners = () => {
diff --git a/apps/mobile/app/screens/settings/subscription.js b/apps/mobile/app/screens/settings/subscription.js
index a86ea6398..4abc56074 100644
--- a/apps/mobile/app/screens/settings/subscription.js
+++ b/apps/mobile/app/screens/settings/subscription.js
@@ -42,6 +42,9 @@ export const Subscription = () => {
user?.subscription?.type !== SUBSCRIPTION_STATUS.PREMIUM &&
user?.subscription?.type !== SUBSCRIPTION_STATUS.BETA;
+ const hasCancelledPremium =
+ SUBSCRIPTION_STATUS.PREMIUM_CANCELLED === user?.subscription?.type;
+
const subscriptionProviderInfo =
SUBSCRIPTION_PROVIDER[user?.subscription?.provider];
@@ -63,10 +66,7 @@ export const Subscription = () => {
return;
}
- if (
- user?.subscription?.type === SUBSCRIPTION_STATUS.PREMIUM_CANCELLED &&
- Platform.OS === "android"
- ) {
+ if (hasCancelledPremium && Platform.OS === "android") {
if (user.subscription?.provider === 3) {
ToastEvent.show({
heading: "Subscribed on web",
@@ -111,16 +111,14 @@ export const Subscription = () => {
title={
!user?.isEmailConfirmed
? "Confirm your email"
- : user.subscription?.provider === 3 &&
- user.subscription?.type ===
- SUBSCRIPTION_STATUS.PREMIUM_CANCELLED
+ : user.subscription?.provider === 3 && hasCancelledPremium
? "Manage subscription from desktop app"
- : user.subscription?.type ===
- SUBSCRIPTION_STATUS.PREMIUM_CANCELLED &&
+ : hasCancelledPremium &&
Platform.OS === "android" &&
Config.GITHUB_RELEASE !== "true"
? "Resubscribe from Google Playstore"
- : user.subscription?.type === SUBSCRIPTION_STATUS.PREMIUM_EXPIRED
+ : user.subscription?.type ===
+ SUBSCRIPTION_STATUS.PREMIUM_EXPIRED || hasCancelledPremium
? `Resubscribe to Pro (${getPrice() || "$4.49"} / mo)`
: `Get Pro (${getPrice() || "$4.49"} / mo)`
}
diff --git a/apps/mobile/app/services/premium.js b/apps/mobile/app/services/premium.js
index e082066f9..00f8da925 100644
--- a/apps/mobile/app/services/premium.js
+++ b/apps/mobile/app/services/premium.js
@@ -233,7 +233,7 @@ const subscriptions = {
*
* @returns {RNIap.Purchase} subscription
*/
- get: async () => {
+ get: () => {
if (Platform.OS === "android") return;
let _subscriptions = MMKV.getString("subscriptionsIOS");
if (!_subscriptions) return [];
@@ -299,12 +299,14 @@ const subscriptions = {
"Content-Type": "application/json"
}
};
+
+ console.log("Subscription.verify", requestData);
try {
let result = await fetch(
- "https://payments.streetwriters.co/apple/verify",
+ "http://192.168.43.5:4264/apple/verify",
requestData
);
-
+ console.log("Subscribed", result);
let text = await result.text();
if (!result.ok) {
@@ -312,6 +314,8 @@ const subscriptions = {
await subscriptions.clear(subscription);
}
return;
+ } else {
+ await subscriptions.clear(subscription);
}
} catch (e) {
console.log("subscription error", e);
@@ -321,15 +325,18 @@ const subscriptions = {
},
clear: async (_subscription) => {
if (Platform.OS === "android") return;
- let _subscriptions = await subscriptions.get();
+ let _subscriptions = subscriptions.get();
let subscription = null;
if (_subscription) {
subscription = _subscription;
} else {
subscription = _subscriptions.length > 0 ? _subscriptions[0] : null;
}
+
if (subscription) {
- await RNIap.finishTransaction(subscription.transactionId);
+ await RNIap.finishTransaction({
+ purchase: subscription
+ });
await RNIap.clearTransactionIOS();
await subscriptions.remove(subscription.transactionId);
}
diff --git a/apps/mobile/app/utils/constants.js b/apps/mobile/app/utils/constants.js
index c9efff324..03a80b55e 100644
--- a/apps/mobile/app/utils/constants.js
+++ b/apps/mobile/app/utils/constants.js
@@ -75,7 +75,7 @@ export const SUBSCRIPTION_STATUS_STRINGS = {
2: Platform.OS === "ios" ? "Pro" : "Beta",
5: "Pro",
6: "Expired",
- 7: "Pro"
+ 7: "Pro (cancelled)"
};
export const SUBSCRIPTION_PROVIDER = {