mirror of
https://github.com/streetwriters/notesnook.git
synced 2026-02-24 12:12:54 +01:00
mobile: fix subscriptions not fulfilling on ios
This commit is contained in:
@@ -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
|
||||
? [
|
||||
|
||||
@@ -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 = () => {
|
||||
|
||||
@@ -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)`
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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 = {
|
||||
|
||||
Reference in New Issue
Block a user