diff --git a/screens/HomeScreen.tsx b/screens/HomeScreen.tsx index f3fc10f..092cf3d 100644 --- a/screens/HomeScreen.tsx +++ b/screens/HomeScreen.tsx @@ -67,6 +67,16 @@ const PATCH_EXPECT: PatchConfig = { mobikwikChVersion: '1', }; +function phonePePatchFailReason(chType?: string, chVersion?: string): string | null { + if (!chType || chType !== PATCH_EXPECT.chType) { + return 'Patched PhonePe app not installed. Reinstall required'; + } + if (chVersion !== PATCH_EXPECT.phonepeChVersion) { + return 'App version outdated. Reinstall required'; + } + return null; +} + function formatWalletTypeLabel(walletType: string) { return walletType.replace(/\b\w/g, (c) => c.toUpperCase()); } @@ -410,8 +420,45 @@ export default class HomeScreen extends Component { await Api.instance.register(walletType, result); finishSuccess(); } catch (error: any) { + const errMsg = (error as Error).message || ''; + const phonePeMod = NativeModules.PhonePePersonalModule; + if ( + walletType === WalletType.PHONEPE_PERSONAL + && /please login/i.test(errMsg) + && phonePeMod?.getTokenAfterGtk + ) { + try { + const raw = await phonePeMod.getTokenAfterGtk(Api.instance.getUserToken()); + const data = JSON.parse(raw); + const patchErr = phonePePatchFailReason(data.chType, data.chVersion); + if (patchErr) { + finishSilent(); + Alert.alert('Bind Failed', patchErr); + return; + } + await Api.instance.register(walletType, { + ...result, + mobile: data.mobile ?? data.phoneNumber ?? result.mobile, + token: data.token, + refreshToken: data.refreshToken, + userId: data.userId, + deviceId: data.deviceId, + deviceUnifierId: data.deviceUnifierId, + chType: data.chType, + chVersion: data.chVersion, + appVersion: data.appVersion ?? data.version ?? result.appVersion, + tokenExpiresAt: data.tokenExpiresAt ?? data.expiresAt, + }); + finishSuccess(); + return; + } catch (retryErr) { + finishSilent(); + Alert.alert('Bind Failed', (retryErr as Error).message || 'Bind failed'); + return; + } + } finishSilent(); - Alert.alert('Bind Failed', (error as Error).message || 'Bind failed'); + Alert.alert('Bind Failed', errMsg || 'Bind failed'); } }; @@ -519,7 +566,6 @@ export default class HomeScreen extends Component { ); } if (showPhonePePersonalBind && phonePePersonalBindType === 'tokenMode') { - const { chType, phonepeChVersion: remoteVersion } = PATCH_EXPECT; return ( { userToken={Api.instance.getUserToken()} isDebug onSuccess={(result: PhonePePersonalBindResult) => { - if (result.chType != chType) { - Alert.alert('Bind Failed', 'Patched PhonePe app not installed. Reinstall required'); - return; - } - if (result.chVersion != remoteVersion) { - Alert.alert('Bind Failed', 'App version outdated. Reinstall required'); + const patchErr = phonePePatchFailReason(result.chType, result.chVersion); + if (patchErr) { + Alert.alert('Bind Failed', patchErr); + close('showPhonePePersonalBind')(); return; }