Hook up native
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import type { PurchasesStoreProduct } from 'react-native-purchases'
|
||||
|
||||
export enum EntitlementId {
|
||||
Core = 'core',
|
||||
}
|
||||
@@ -22,7 +24,7 @@ export type Offering =
|
||||
id: OfferingId
|
||||
platform: PlatformId.Ios | PlatformId.Android
|
||||
price: number
|
||||
package: undefined
|
||||
package: PurchasesStoreProduct
|
||||
}
|
||||
| {
|
||||
id: OfferingId
|
||||
|
||||
@@ -1,28 +1,41 @@
|
||||
import React from 'react'
|
||||
import {useQuery, useMutation} from '@tanstack/react-query'
|
||||
import Purchases from 'react-native-purchases'
|
||||
|
||||
import {isIOS} from '#/platform/detection'
|
||||
import {api} from '#/state/purchases/subscriptions/api'
|
||||
import {OfferingId, PlatformId, Offering, SubscriptionGroupId} from '#/state/purchases/subscriptions/types'
|
||||
|
||||
export function useSubscriptionGroup(group: SubscriptionGroupId) {
|
||||
return useQuery<{ offerings: Offering[] }>({
|
||||
queryKey: ['subscriptions', 'core'],
|
||||
queryFn() {
|
||||
queryKey: ['subscription-group', group],
|
||||
async queryFn() {
|
||||
const platform = isIOS ? 'ios' : 'android'
|
||||
const { data, error, response } = await api(`/subscriptions/${group}?platform=${platform}`).json()
|
||||
if (error || !data) {
|
||||
console.log(error, response)
|
||||
throw new Error('Failed to fetch subscription group')
|
||||
}
|
||||
|
||||
const { offerings } = data
|
||||
const productIds = offerings.map((o: any) => {
|
||||
return isIOS ? o.productId : o.productId.split(':')[0]
|
||||
})
|
||||
// console.log({ offers: false })
|
||||
// const offers = await Purchases.getOfferings()
|
||||
// console.log({ offers })
|
||||
const products = await Purchases.getProducts(productIds)
|
||||
|
||||
return {
|
||||
offerings: [
|
||||
{
|
||||
id: OfferingId.CoreMonthly,
|
||||
offerings: offerings.map((o: any) => {
|
||||
const product = products.find((p: any) => p.identifier === o.productId)
|
||||
return {
|
||||
id: o.id as OfferingId,
|
||||
platform: isIOS ? PlatformId.Ios : PlatformId.Android,
|
||||
price: 800,
|
||||
package: undefined,
|
||||
},
|
||||
{
|
||||
id: OfferingId.CoreAnnual,
|
||||
platform: isIOS ? PlatformId.Ios : PlatformId.Android,
|
||||
price: 8000,
|
||||
package: undefined,
|
||||
},
|
||||
]
|
||||
price: product?.priceString,
|
||||
package: product,
|
||||
}
|
||||
}),
|
||||
}
|
||||
}
|
||||
})
|
||||
@@ -35,6 +48,13 @@ export function usePurchaseOffering() {
|
||||
email,
|
||||
offering,
|
||||
}: { did: string, email: string, offering: Offering }) {
|
||||
if (offering.platform === PlatformId.Web) {
|
||||
throw new Error('Unsupported platform')
|
||||
}
|
||||
|
||||
Purchases.logIn(did)
|
||||
Purchases.setEmail(email)
|
||||
Purchases.purchaseStoreProduct(offering.package)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user