Skip to content

Commit

Permalink
feat: adds purchase products api response
Browse files Browse the repository at this point in the history
  • Loading branch information
cb-haripriyan committed Feb 17, 2023
1 parent e538fc5 commit 0ed6109
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.*

Expand Down Expand Up @@ -64,19 +66,22 @@ class ChargebeeReactNativeModule internal constructor(context: ReactApplicationC
CBPurchase.retrieveProducts(it, productIds,
object : CBCallback.ListProductsCallback<ArrayList<CBProduct>> {
override fun onSuccess(productDetails: ArrayList<CBProduct>) {
CBPurchase.purchaseProduct(
productDetails.first(),
customerId,
object : CBCallback.PurchaseCallback<String> {
if(productDetails.size > 0) {
CBPurchase.purchaseProduct(
productDetails.first(),
customerId,
object : CBCallback.PurchaseCallback<String> {

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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
@@ -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.*


Expand Down Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
15 changes: 12 additions & 3 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -13,14 +13,15 @@ export default function App() {

useEffect(() => {
configure(site, apiKey, sdkKey);
}, []);
});

const retrieveProductIdentifiers = async () => {
const queryParams = new Map<string, string>();
queryParams.set('limit', '1');
try {
const result = await Chargebee.retrieveProductIdentifiers(queryParams);
console.log(result);
openAlert(result);
productIdentifiers = result;
} catch (error) {
console.error(error);
Expand All @@ -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);
Expand All @@ -45,6 +47,7 @@ export default function App() {
'graceperiodtest3'
);
console.log(result);
openAlert(result);
} catch (error) {
console.error(error);
}
Expand All @@ -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 (
<View style={styles.container}>
<Button
title="Configure"
onPress={() => configure(site, apiKey, androidSdkKey)}
onPress={() => configure(site, apiKey, sdkKey)}
/>
<Button
title="Retrieve Product Identifers"
Expand Down
40 changes: 40 additions & 0 deletions ios/CBPurchaseResult.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// CBPurchase.swift
// ChargebeeReactNative
//
// Created by cb-haripriyan on 17/02/23.
//

import Foundation

struct CBPurchaseResult: Codable {

let subscriptionId: String?
let planId: String?
let status: Bool

enum CodingKeys: String, CodingKey {
case subscriptionId = "subscription_id"
case planId = "plan_id"
case status
}
}

extension CBPurchaseResult {
init(fromTuple: (status:Bool, subscriptionId:String?, planId:String?)) {
self.subscriptionId = fromTuple.subscriptionId
self.status = fromTuple.status
self.planId = fromTuple.planId
}
}

extension CBPurchaseResult {
var asDictionary: [String:Any] {
var dictionary: [String: Any] = [:]
dictionary["subscription_id"] = self.subscriptionId
dictionary["plan_id"] = self.planId
dictionary["status"] = self.status
return dictionary
}
}

4 changes: 3 additions & 1 deletion ios/ChargebeeHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Foundation
import Chargebee

public class ChargebeeHelper: NSObject {

@objc public func configure(site: String, apiKey: String, sdkKey: String?) {
Chargebee.configure(site: site, apiKey: apiKey, sdkKey: sdkKey)
}
Expand Down Expand Up @@ -45,7 +46,8 @@ public class ChargebeeHelper: NSObject {
CBPurchase.shared.purchaseProduct(product: product, customerId: customerId) { result in
switch result {
case .success(let result):
resolver(result)
let purchasedProduct = CBPurchaseResult(fromTuple: result)
resolver(purchasedProduct.asDictionary)
case .failure(let error as NSError):
rejecter("\(error.code)", error.localizedDescription, error)
}
Expand Down
4 changes: 2 additions & 2 deletions src/NativeChargebeeReactNative.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { TurboModule } from 'react-native';
import { TurboModuleRegistry } from 'react-native';
import type { Product } from '.';
import type { Product, Purchase } from '.';

export interface Spec extends TurboModule {
configure(site: string, publishableApiKey: string, sdkKey: string): void;
retrieveProductIdentifiers(queryParams: Object): Promise<Array<string>>;
retrieveProducts(productIds: Array<string>): Promise<Product>;
purchaseProduct(productId: string, customerId: string): Promise<string>;
purchaseProduct(productId: string, customerId: string): Promise<Purchase>;
}

export default TurboModuleRegistry.getEnforcing<Spec>('ChargebeeReactNative');
8 changes: 7 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ export interface Product {
readonly currencyCode: string;
}

export interface Purchase {
readonly subscriptionId: string;
readonly planId: string;
readonly status: string;
}

export default class Chargebee {
public static configure({
site,
Expand All @@ -63,7 +69,7 @@ export default class Chargebee {
public static async purchaseProduct(
productId: string,
customerId: string | null
): Promise<string> {
): Promise<Purchase> {
return ChargebeeReactNative.purchaseProduct(productId, customerId);
}
}

0 comments on commit 0ed6109

Please sign in to comment.