@@ -14,6 +14,12 @@ import { proxyEnv } from "@/src/lib/proxy-env"
1414import { queryClient } from "@/src/lib/query-client"
1515import { toast } from "@/src/lib/toast"
1616
17+ import {
18+ buildAppleVerificationRequest ,
19+ isKnownAppleSubscriptionPurchase ,
20+ selectSignedTransactionInfo ,
21+ } from "./apple-iap-purchase"
22+
1723const billingSubscriptionQueryKey = [ "billingSubscription" ]
1824
1925type BillingSubscriptionResponse = {
@@ -123,40 +129,45 @@ export const AppleIAPProvider = ({ children }: PropsWithChildren) => {
123129
124130 const verifyPurchase = useCallback (
125131 async ( purchase : Purchase ) => {
126- const productId = purchase . id
127- const jwsRepresentation =
128- purchase . purchaseToken ||
129- ( await getTransactionJwsIOS ( productId ) . catch ( ( ) => null ) ) ||
130- ( await validateReceipt ( { apple : { sku : productId } } )
131- . then ( ( result ) => ( "jwsRepresentation" in result ? result . jwsRepresentation : undefined ) )
132- . catch ( ( ) => { } ) )
133-
134- if ( ! jwsRepresentation ) {
135- throw new Error ( t ( "subscription.actions.upgrade_error" ) )
132+ const productId = purchase . productId
133+ let signedTransactionInfo = selectSignedTransactionInfo ( purchase . purchaseToken )
134+
135+ if ( ! signedTransactionInfo ) {
136+ signedTransactionInfo = selectSignedTransactionInfo (
137+ await getTransactionJwsIOS ( productId ) . catch ( ( ) => null ) ,
138+ )
139+ }
140+
141+ if ( ! signedTransactionInfo ) {
142+ signedTransactionInfo = selectSignedTransactionInfo (
143+ await validateReceipt ( { apple : { sku : productId } } )
144+ . then ( ( result ) =>
145+ "jwsRepresentation" in result ? result . jwsRepresentation : undefined ,
146+ )
147+ . catch ( ( ) => undefined ) ,
148+ )
136149 }
137150
138151 const response = await followClient . request < {
139152 code : number
140153 data : BillingSubscriptionResponse
141154 } > ( "/billing/apple/verify" , {
142155 method : "POST" ,
143- body : {
144- signedTransactionInfo : jwsRepresentation ,
145- } ,
156+ body : buildAppleVerificationRequest ( purchase , signedTransactionInfo ) ,
146157 } )
147158
148159 if ( response . code !== 0 ) {
149160 throw new Error ( "Failed to verify Apple subscription" )
150161 }
151162 } ,
152- [ t , validateReceipt ] ,
163+ [ validateReceipt ] ,
153164 )
154165
155166 useEffect ( ( ) => {
156167 if (
157168 Platform . OS !== "ios" ||
158169 ! currentPurchase ||
159- ! knownSubscriptionIds . has ( currentPurchase . id )
170+ ! isKnownAppleSubscriptionPurchase ( currentPurchase , knownSubscriptionIds )
160171 ) {
161172 return
162173 }
@@ -280,7 +291,7 @@ export const AppleIAPProvider = ({ children }: PropsWithChildren) => {
280291 await new Promise ( ( resolve ) => setTimeout ( resolve , 300 ) )
281292
282293 const restoredPurchases = availablePurchasesRef . current . filter ( ( purchase ) =>
283- knownSubscriptionIds . has ( purchase . id ) ,
294+ isKnownAppleSubscriptionPurchase ( purchase , knownSubscriptionIds ) ,
284295 )
285296
286297 if ( restoredPurchases . length === 0 ) {
0 commit comments