Skip to content

Commit

Permalink
feat: adds purchase products api implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
cb-haripriyan committed Feb 7, 2023
1 parent 9d83c5b commit 6398208
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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.network.ReceiptDetail
import com.chargebee.android.utils.convertArrayToWritableArray
import com.chargebee.android.utils.convertListToWritableArray
import com.chargebee.android.utils.convertReadableArray
Expand Down Expand Up @@ -38,11 +39,6 @@ class ChargebeeReactNativeModule internal constructor(context: ReactApplicationC
}
}

@ReactMethod
override fun purchaseProduct(productId: String, customerId: String, promise: Promise) {
promise.resolve("TO BE IMPLEMENTED")
}

@ReactMethod
override fun retrieveProducts(productIds: ReadableArray, promise: Promise) {
val activity = currentActivity
Expand All @@ -60,6 +56,35 @@ class ChargebeeReactNativeModule internal constructor(context: ReactApplicationC
}
}

@ReactMethod
override fun purchaseProduct(productId: String, customerId: String, promise: Promise) {
val activity = currentActivity
activity?.let {
val productIds = arrayListOf(productId)
CBPurchase.retrieveProducts(it, productIds,
object : CBCallback.ListProductsCallback<ArrayList<CBProduct>> {
override fun onSuccess(productDetails: ArrayList<CBProduct>) {
CBPurchase.purchaseProduct(
productDetails.first(),
customerId,
object : CBCallback.PurchaseCallback<String> {

override fun onSuccess(receiptDetail: ReceiptDetail, status: Boolean) {
promise.resolve(receiptDetail.toString())
}

override fun onError(error: CBException) {
promise.reject(error.message, error)
}
})
}
override fun onError(error: CBException) {
promise.reject(error.message, error)
}
})
}
}

private fun getFormattedQueryParams(queryParams: ReadableMap): Array<String> {
if (queryParams != null)
return arrayOf(queryParams.getString("limit") ?: "")
Expand Down
19 changes: 18 additions & 1 deletion ios/ChargebeeHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,23 @@ public class ChargebeeHelper: NSObject {
}

@objc public func purchaseProduct(productId: String, customerId: String, resolver: @escaping RCTPromiseResolveBlock, rejecter: @escaping RCTPromiseRejectBlock) {
resolver("TO BE IMPLEMENTED")
CBPurchase.shared.retrieveProducts(withProductID: [productId]) { result in
DispatchQueue.main.async {
switch result {
case let .success(products):
let product: CBProduct = products.self.first!;
CBPurchase.shared.purchaseProduct(product: product, customerId: customerId) { result in
switch result {
case .success(let result):
resolver(result)
case .failure(let error as NSError):
rejecter("\(error.code)", error.localizedDescription, error)
}
}
case let .failure(error as NSError):
rejecter("\(error.code)", error.localizedDescription, error)
}
}
}
}
}
7 changes: 5 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export interface Product {
readonly currencyCode: string;
}


export default class Chargebee {
public static configure({
site,
Expand All @@ -47,6 +46,7 @@ export default class Chargebee {
ChargebeeReactNative.configure(site, publishableApiKey, sdkKey);
}

// TODO: Refactor to use types for query
public static async retrieveProductIdentifiers(
queryParams: Map<string, string>
): Promise<Array<string>> {
Expand All @@ -60,7 +60,10 @@ export default class Chargebee {
}

// TODO: Refactor to pass Product object
public static async purchaseProduct(productId: string, customerId: string | null): Promise<string> {
public static async purchaseProduct(
productId: string,
customerId: string | null
): Promise<string> {
return ChargebeeReactNative.purchaseProduct(productId, customerId);
}
}

0 comments on commit 6398208

Please sign in to comment.