Skip to content

Commit

Permalink
🐛 - Fix Android tip jar refunds (#341)
Browse files Browse the repository at this point in the history
Tipping on Android were always refunded after a few days. 
The reason is that we didn't flush the purchase ("acknowledge" it to the
play store):
> your app must then acknowledge the purchase. This acknowledgement
communicates to Google Play that you have granted entitlement for the
purchase.

We had the flushing mechanism set in the `TipJarScreen`, but putting it
there will possibly hav the flushing effect not run (if the user has
exit the screen before it ran, or for whatever reason else. Putting in
the main `App` component will ensure it'll be flushed, either in the
current session or in the next app relaunch.
  • Loading branch information
guytepper authored Nov 5, 2023
1 parent a8a8e88 commit df8a38c
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 32 deletions.
2 changes: 1 addition & 1 deletion android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ android {
applicationId "com.betterrail"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 75
versionCode 78
versionName "2.2.2"
missingDimensionStrategy "store", "play"
}
Expand Down
28 changes: 25 additions & 3 deletions app/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import { RootStore, RootStoreProvider, setupRootStore } from "./models"
import { ToggleStorybook } from "../storybook/toggle-storybook"
import { setInitialLanguage, setUserLanguage } from "./i18n/i18n"
import "react-native-console-time-polyfill"
import { withIAPContext } from "react-native-iap"
import { useIAP, initConnection, finishTransaction, getAvailablePurchases, withIAPContext } from "react-native-iap"
import PushNotification from "react-native-push-notification"

// Disable tracking in development environment
Expand All @@ -51,7 +51,6 @@ import { enableScreens } from "react-native-screens"
import { canRunLiveActivities, monitorLiveActivities } from "./utils/ios-helpers"
import { useDeepLinking } from "./hooks/use-deep-linking"
import { openActiveRide } from "./utils/helpers/ride-helpers"
import { useStations } from "./data/stations"
enableScreens()

export const queryClient = new QueryClient()
Expand All @@ -65,7 +64,7 @@ function App() {
const [rootStore, setRootStore] = useState<RootStore | undefined>(undefined)
const [localeReady, setLocaleReady] = useState(false)
const appState = useRef(AppState.currentState)
const stations = useStations()
const { currentPurchase } = useIAP()

useDeepLinking(rootStore, navigationRef)

Expand Down Expand Up @@ -139,6 +138,29 @@ function App() {
})
}, [])

useEffect(() => {
// load products and flush available purchases for the tip jar
// see: https://github.com/dooboolab-community/react-native-iap/issues/126
// and: https://react-native-iap.dooboolab.com/docs/guides/purchases
const flushAvailablePurchases = async () => {
try {
await initConnection()
const availablePurchases = await getAvailablePurchases()

availablePurchases.forEach((purchase) => {
finishTransaction({ purchase, isConsumable: true })
})
} catch (error) {
console.error("Failed to connect to IAP and finish all available transactions", error)
}
}

// to avoid prompting for login during development, only flush purchases in production
if (!__DEV__) {
flushAvailablePurchases()
}
}, [currentPurchase])

// Before we show the app, we have to wait for our state to be ready.
// In the meantime, don't render anything. This will be the background
// color set in native by rootView's background color. You can replace
Expand Down
16 changes: 3 additions & 13 deletions app/screens/settings/settings-tip-jar-screen.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useEffect, useState } from "react"
import { observer } from "mobx-react-lite"
import { View, ViewStyle, TextStyle, Platform, ActivityIndicator } from "react-native"
import { ProductPurchase, Purchase, RequestPurchase, useIAP } from "react-native-iap"
import { ProductPurchase, RequestPurchase, useIAP } from "react-native-iap"
import { Screen, Text } from "../../components"
import { color, isDarkMode, spacing } from "../../theme"
import { TouchableOpacity } from "react-native-gesture-handler"
Expand Down Expand Up @@ -83,10 +83,10 @@ const TIP_AMOUNT: TextStyle = {

const TOTAL_TIPS: TextStyle = { textAlign: "center", marginTop: spacing[4] }

const PRODUCT_IDS = ["better_rail_tip_1", "better_rail_tip_2", "better_rail_tip_3", "better_rail_tip_4"]

const installSource = getInstallerPackageNameSync()

const PRODUCT_IDS = ["better_rail_tip_1", "better_rail_tip_2", "better_rail_tip_3", "better_rail_tip_4"]

export const TipJarScreen = observer(function TipJarScreen() {
const [isLoading, setIsLoading] = useState(false)
const [thanksModalVisible, setModalVisible] = useState(false)
Expand All @@ -97,18 +97,8 @@ export const TipJarScreen = observer(function TipJarScreen() {
useIAP()

useEffect(() => {
// see: https://github.com/dooboolab-community/react-native-iap/issues/126
const flushAvailablePurchases = async () => {
await getAvailablePurchases()
availablePurchases.forEach(async (purchase) => {
await finishTransaction({ purchase, isConsumable: true })
})
}

if (connected) {
getProducts({ skus: PRODUCT_IDS })

flushAvailablePurchases()
}
}, [connected, getProducts])

Expand Down
20 changes: 10 additions & 10 deletions ios/BetterRail.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1156,7 +1156,7 @@
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_ENTITLEMENTS = BetterRail/BetterRailDebug.entitlements;
CODE_SIGN_STYLE = Manual;
CURRENT_PROJECT_VERSION = 1;
CURRENT_PROJECT_VERSION = 3;
DEVELOPMENT_TEAM = "";
"DEVELOPMENT_TEAM[sdk=iphoneos*]" = UE6BVYPPFX;
ENABLE_BITCODE = NO;
Expand Down Expand Up @@ -1196,7 +1196,7 @@
CODE_SIGN_IDENTITY = "Apple Development";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Distribution";
CODE_SIGN_STYLE = Manual;
CURRENT_PROJECT_VERSION = 1;
CURRENT_PROJECT_VERSION = 3;
DEVELOPMENT_TEAM = "";
"DEVELOPMENT_TEAM[sdk=iphoneos*]" = UE6BVYPPFX;
INFOPLIST_FILE = BetterRail/Info.plist;
Expand Down Expand Up @@ -1359,7 +1359,7 @@
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
"CODE_SIGN_IDENTITY[sdk=watchos*]" = "iPhone Developer";
CODE_SIGN_STYLE = Manual;
CURRENT_PROJECT_VERSION = 1;
CURRENT_PROJECT_VERSION = 3;
DEBUG_INFORMATION_FORMAT = dwarf;
DEVELOPMENT_TEAM = "";
"DEVELOPMENT_TEAM[sdk=watchos*]" = UE6BVYPPFX;
Expand Down Expand Up @@ -1401,7 +1401,7 @@
"CODE_SIGN_IDENTITY[sdk=watchos*]" = "iPhone Distribution";
CODE_SIGN_STYLE = Manual;
COPY_PHASE_STRIP = NO;
CURRENT_PROJECT_VERSION = 1;
CURRENT_PROJECT_VERSION = 3;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
DEVELOPMENT_TEAM = "";
"DEVELOPMENT_TEAM[sdk=watchos*]" = UE6BVYPPFX;
Expand Down Expand Up @@ -1437,7 +1437,7 @@
CODE_SIGN_ENTITLEMENTS = "WatchBetterRail Extension/WatchBetterRail Extension.entitlements";
"CODE_SIGN_IDENTITY[sdk=watchos*]" = "iPhone Developer";
CODE_SIGN_STYLE = Manual;
CURRENT_PROJECT_VERSION = 1;
CURRENT_PROJECT_VERSION = 3;
DEBUG_INFORMATION_FORMAT = dwarf;
DEVELOPMENT_TEAM = "";
"DEVELOPMENT_TEAM[sdk=watchos*]" = UE6BVYPPFX;
Expand Down Expand Up @@ -1478,7 +1478,7 @@
"CODE_SIGN_IDENTITY[sdk=watchos*]" = "iPhone Distribution";
CODE_SIGN_STYLE = Manual;
COPY_PHASE_STRIP = NO;
CURRENT_PROJECT_VERSION = 1;
CURRENT_PROJECT_VERSION = 3;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
DEVELOPMENT_TEAM = "";
"DEVELOPMENT_TEAM[sdk=watchos*]" = UE6BVYPPFX;
Expand Down Expand Up @@ -1516,7 +1516,7 @@
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
CODE_SIGN_ENTITLEMENTS = BetterRailWidgetExtensionDebug.entitlements;
CODE_SIGN_STYLE = Manual;
CURRENT_PROJECT_VERSION = 1;
CURRENT_PROJECT_VERSION = 3;
DEBUG_INFORMATION_FORMAT = dwarf;
DEVELOPMENT_TEAM = "";
"DEVELOPMENT_TEAM[sdk=iphoneos*]" = UE6BVYPPFX;
Expand Down Expand Up @@ -1557,7 +1557,7 @@
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Distribution";
CODE_SIGN_STYLE = Manual;
COPY_PHASE_STRIP = NO;
CURRENT_PROJECT_VERSION = 1;
CURRENT_PROJECT_VERSION = 3;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
DEVELOPMENT_TEAM = "";
"DEVELOPMENT_TEAM[sdk=iphoneos*]" = UE6BVYPPFX;
Expand Down Expand Up @@ -1590,7 +1590,7 @@
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
CODE_SIGN_ENTITLEMENTS = StationIntent/StationIntentDebug.entitlements;
CODE_SIGN_STYLE = Manual;
CURRENT_PROJECT_VERSION = 1;
CURRENT_PROJECT_VERSION = 3;
DEBUG_INFORMATION_FORMAT = dwarf;
DEVELOPMENT_TEAM = "";
"DEVELOPMENT_TEAM[sdk=iphoneos*]" = UE6BVYPPFX;
Expand Down Expand Up @@ -1629,7 +1629,7 @@
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Distribution";
CODE_SIGN_STYLE = Manual;
COPY_PHASE_STRIP = NO;
CURRENT_PROJECT_VERSION = 1;
CURRENT_PROJECT_VERSION = 3;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
DEVELOPMENT_TEAM = "";
"DEVELOPMENT_TEAM[sdk=iphoneos*]" = UE6BVYPPFX;
Expand Down
2 changes: 1 addition & 1 deletion ios/BetterRail/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
<string>3</string>
<key>ITSAppUsesNonExemptEncryption</key>
<false/>
<key>LSApplicationQueriesSchemes</key>
Expand Down
2 changes: 1 addition & 1 deletion ios/BetterRailWidget/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<key>CFBundleShortVersionString</key>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<string>3</string>
<key>NSExtension</key>
<dict>
<key>NSExtensionPointIdentifier</key>
Expand Down
2 changes: 1 addition & 1 deletion ios/StationIntent/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<key>CFBundleShortVersionString</key>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<string>3</string>
<key>NSExtension</key>
<dict>
<key>NSExtensionAttributes</key>
Expand Down
2 changes: 1 addition & 1 deletion ios/WatchBetterRail Extension/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<key>CFBundleShortVersionString</key>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<string>3</string>
<key>CLKComplicationPrincipalClass</key>
<string>$(PRODUCT_MODULE_NAME).ComplicationController</string>
<key>NSExtension</key>
Expand Down
2 changes: 1 addition & 1 deletion ios/WatchBetterRail/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<key>CFBundleShortVersionString</key>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<string>3</string>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
Expand Down

0 comments on commit df8a38c

Please sign in to comment.