diff --git a/android/src/main/java/com/chargebee/android/ChargebeeReactNativeModule.kt b/android/src/main/java/com/chargebee/android/ChargebeeReactNativeModule.kt index 098f5c7..0df7ef1 100644 --- a/android/src/main/java/com/chargebee/android/ChargebeeReactNativeModule.kt +++ b/android/src/main/java/com/chargebee/android/ChargebeeReactNativeModule.kt @@ -5,9 +5,11 @@ import com.chargebee.android.billingservice.CBPurchase import com.chargebee.android.exceptions.CBException import com.chargebee.android.exceptions.CBProductIDResult import com.chargebee.android.models.CBProduct +import com.chargebee.android.models.PurchaseResult import com.chargebee.android.network.ReceiptDetail import com.chargebee.android.utils.convertArrayToWritableArray import com.chargebee.android.utils.convertListToWritableArray +import com.chargebee.android.utils.convertPurchaseResultToDictionary import com.chargebee.android.utils.convertReadableArray import com.facebook.react.bridge.* @@ -64,19 +66,22 @@ class ChargebeeReactNativeModule internal constructor(context: ReactApplicationC CBPurchase.retrieveProducts(it, productIds, object : CBCallback.ListProductsCallback> { override fun onSuccess(productDetails: ArrayList) { - CBPurchase.purchaseProduct( - productDetails.first(), - customerId, - object : CBCallback.PurchaseCallback { + if(productDetails.size > 0) { + CBPurchase.purchaseProduct( + productDetails.first(), + customerId, + object : CBCallback.PurchaseCallback { - override fun onSuccess(receiptDetail: ReceiptDetail, status: Boolean) { - promise.resolve(receiptDetail.toString()) - } + override fun onSuccess(receiptDetail: ReceiptDetail, status: Boolean) { + val purchaseResult = PurchaseResult(receiptDetail, status) + promise.resolve(convertPurchaseResultToDictionary(purchaseResult, status)) + } - override fun onError(error: CBException) { - promise.reject(error.message, error) - } - }) + override fun onError(error: CBException) { + promise.reject(error.message, error) + } + }) + } } override fun onError(error: CBException) { promise.reject(error.message, error) diff --git a/android/src/main/java/com/chargebee/android/models/PurchaseResult.kt b/android/src/main/java/com/chargebee/android/models/PurchaseResult.kt new file mode 100644 index 0000000..7df275e --- /dev/null +++ b/android/src/main/java/com/chargebee/android/models/PurchaseResult.kt @@ -0,0 +1,12 @@ +package com.chargebee.android.models + +import com.chargebee.android.network.ReceiptDetail +import java.io.Serializable + +data class PurchaseResult ( + val subscriptionId: String?, + val planId: String?, + val status: Boolean +) : Serializable { + constructor(receiptDetail: ReceiptDetail, status: Boolean) : this(receiptDetail.subscription_id, receiptDetail.plan_id, status) +} diff --git a/android/src/main/java/com/chargebee/android/utils/Converter.kt b/android/src/main/java/com/chargebee/android/utils/Converter.kt index 3f47921..8915aa5 100644 --- a/android/src/main/java/com/chargebee/android/utils/Converter.kt +++ b/android/src/main/java/com/chargebee/android/utils/Converter.kt @@ -1,6 +1,7 @@ package com.chargebee.android.utils import com.chargebee.android.models.CBProduct +import com.chargebee.android.models.PurchaseResult import com.facebook.react.bridge.* @@ -39,3 +40,11 @@ internal fun convertProductToDictionary(product: CBProduct): WritableMap { writableMap.putString("currencyCode", product.skuDetails.priceCurrencyCode) return writableMap } + +internal fun convertPurchaseResultToDictionary(product: PurchaseResult, status: Boolean): WritableMap { + val writableMap: WritableMap = WritableNativeMap() + writableMap.putString("subscription_id", product.subscriptionId) + writableMap.putString("plan_id", product.planId) + writableMap.putBoolean("status", status) + return writableMap +} diff --git a/example/ios/ChargebeeReactNativeExample.xcodeproj/project.pbxproj b/example/ios/ChargebeeReactNativeExample.xcodeproj/project.pbxproj index b83528b..681bcf6 100644 --- a/example/ios/ChargebeeReactNativeExample.xcodeproj/project.pbxproj +++ b/example/ios/ChargebeeReactNativeExample.xcodeproj/project.pbxproj @@ -12,6 +12,8 @@ 13B07FBC1A68108700A75B9A /* AppDelegate.mm in Sources */ = {isa = PBXBuildFile; fileRef = 13B07FB01A68108700A75B9A /* AppDelegate.mm */; }; 13B07FBF1A68108700A75B9A /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 13B07FB51A68108700A75B9A /* Images.xcassets */; }; 13B07FC11A68108700A75B9A /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 13B07FB71A68108700A75B9A /* main.m */; }; + 2B8B5CB0299F9BEC0090B370 /* ChargebeeSyncedProducts.storekit in Resources */ = {isa = PBXBuildFile; fileRef = 2B8B5CAF299F96CC0090B370 /* ChargebeeSyncedProducts.storekit */; }; + 2B8B5CB1299F9BED0090B370 /* ChargebeeSyncedProducts.storekit in Resources */ = {isa = PBXBuildFile; fileRef = 2B8B5CAF299F96CC0090B370 /* ChargebeeSyncedProducts.storekit */; }; 7699B88040F8A987B510C191 /* libPods-ChargebeeReactNativeExample-ChargebeeReactNativeExampleTests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 19F6CBCC0A4E27FBF8BF4A61 /* libPods-ChargebeeReactNativeExample-ChargebeeReactNativeExampleTests.a */; }; 81AB9BB82411601600AC10FF /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 81AB9BB72411601600AC10FF /* LaunchScreen.storyboard */; }; /* End PBXBuildFile section */ diff --git a/example/src/App.tsx b/example/src/App.tsx index 4ae4113..cd9991e 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -1,6 +1,6 @@ import React, { useEffect } from 'react'; -import { StyleSheet, View, Button } from 'react-native'; +import { StyleSheet, View, Button, Alert } from 'react-native'; import Chargebee, { Product } from '@chargebee/react-native-chargebee'; export default function App() { @@ -13,7 +13,7 @@ export default function App() { useEffect(() => { configure(site, apiKey, sdkKey); - }, []); + }); const retrieveProductIdentifiers = async () => { const queryParams = new Map(); @@ -21,6 +21,7 @@ export default function App() { try { const result = await Chargebee.retrieveProductIdentifiers(queryParams); console.log(result); + openAlert(result); productIdentifiers = result; } catch (error) { console.error(error); @@ -32,6 +33,7 @@ export default function App() { try { const result = await Chargebee.retrieveProducts(productIdentifiers); products = result; + openAlert(result); console.log(result); } catch (error) { console.error(error); @@ -45,6 +47,7 @@ export default function App() { 'graceperiodtest3' ); console.log(result); + openAlert(result); } catch (error) { console.error(error); } @@ -56,13 +59,19 @@ export default function App() { publishableApiKey: apiKey, sdkKey: sdkKey, }); + console.log('Configured ', site); + openAlert('Configured: ' + site); + }; + + const openAlert = (response: any) => { + Alert.alert(JSON.stringify(response)); }; return (