Skip to content

Commit

Permalink
feat: adds retrieve products implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
cb-haripriyan committed Feb 2, 2023
1 parent 8a29370 commit c091cfb
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ package com.chargebee.android
import com.chargebee.android.billingservice.CBPurchase
import com.chargebee.android.exceptions.CBProductIDResult
import com.chargebee.android.utils.convertArrayToWritableArray
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.bridge.*

class ChargebeeReactNativeModule internal constructor(context: ReactApplicationContext) :
ChargebeeReactNativeSpec(context) {
Expand Down Expand Up @@ -36,6 +33,10 @@ class ChargebeeReactNativeModule internal constructor(context: ReactApplicationC
}
}

override fun retrieveProducts(productIds: ReadableArray, promise: Promise) {
TODO("Not yet implemented")
}

private fun getFormattedQueryParams(queryParams: ReadableMap): Array<String> {
if (queryParams != null)
return arrayOf(queryParams.getString("limit") ?: "")
Expand Down
7 changes: 3 additions & 4 deletions android/src/oldarch/ChargebeeReactNativeSpec.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.chargebee.android

import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.bridge.*

abstract class ChargebeeReactNativeSpec internal constructor(context: ReactApplicationContext) :
ReactContextBaseJavaModule(context) {
abstract fun configure(site: String, publishableApiKey: String, sdkKey: String = "")
abstract fun retrieveProductIdentifiers(queryParams: ReadableMap, promise: Promise)

abstract fun retrieveProducts(productIds: ReadableArray, promise: Promise)

}
56 changes: 35 additions & 21 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useEffect } from 'react';

import { StyleSheet, View, Button } from 'react-native';
import Chargebee from '@chargebee/react-native-chargebee';
Expand All @@ -8,10 +8,42 @@ export default function App() {
const apiKey = 'apiKey';
const sdkKey = 'sdkKey';

React.useEffect(() => {
let productIdentifiers: string[] = [];

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);
productIdentifiers = result;
} catch (error) {
console.error(error);
}
};

// TODO: Refactor Examples
const retrieveProducts = async () => {
try {
const result = await Chargebee.retrieveProducts(productIdentifiers);
console.log(result);
} catch (error) {
console.error(error);
}
};

const configure = (site: string, apiKey: string, sdkKey: string) => {
Chargebee.configure({
site: site,
publishableApiKey: apiKey,
sdkKey: sdkKey,
});
};

return (
<View style={styles.container}>
<Button
Expand All @@ -22,29 +54,11 @@ export default function App() {
title="Retrieve Product Identifers"
onPress={retrieveProductIdentifiers}
/>
<Button title="Retrieve Products" onPress={retrieveProducts} />
</View>
);
}

const retrieveProductIdentifiers = async () => {
const queryParams = new Map<string, string>();
queryParams.set('limit', '100');
try {
const result = await Chargebee.retrieveProductIdentifiers(queryParams);
console.log(result);
} catch (error) {
console.error(error);
}
};

const configure = (site: string, apiKey: string, sdkKey: string) => {
Chargebee.configure({
site: site,
publishableApiKey: apiKey,
sdkKey: sdkKey,
});
};

const styles = StyleSheet.create({
container: {
flex: 1,
Expand Down
4 changes: 4 additions & 0 deletions ios/ChargebeeHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ public class ChargebeeHelper: NSObject {
}
}

@objc public func retrieveProducts(productIds: Array<String>, resolver: @escaping RCTPromiseResolveBlock, rejecter: @escaping RCTPromiseRejectBlock) {
resolver("")
}

}
9 changes: 9 additions & 0 deletions ios/ChargebeeReactNative.mm
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ @implementation ChargebeeReactNative
[helper retrieveProductIdentifiersWithQueryParams:queryParams resolver:resolve rejecter:reject];
}

RCT_REMAP_METHOD(retrieveProducts,
retrieveProductsWithProductIds:(NSArray *)productIds
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)
{
ChargebeeHelper* helper = [[ChargebeeHelper alloc] init];
[helper retrieveProductsWithProductIds:productIds resolver:resolve rejecter:reject];
}

// Don't compile this code when we build for the old architecture.
#ifdef RCT_NEW_ARCH_ENABLED
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
Expand Down
2 changes: 2 additions & 0 deletions src/NativeChargebeeReactNative.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import type { TurboModule } from 'react-native';
import { TurboModuleRegistry } from 'react-native';
import type { Product } 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>;
}

export default TurboModuleRegistry.getEnforcing<Spec>('ChargebeeReactNative');
14 changes: 14 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ export interface ChargebeeConfig {
sdkKey?: string | null;
}

export interface Product {
readonly id: string;
readonly title: string;
readonly price: string;
readonly currencyCode: string;
}


export default class Chargebee {
public static configure({
site,
Expand All @@ -44,4 +52,10 @@ export default class Chargebee {
): Promise<Array<string>> {
return ChargebeeReactNative.retrieveProductIdentifiers(queryParams);
}

public static async retrieveProducts(
productIds: Array<string>
): Promise<Array<Product>> {
return ChargebeeReactNative.retrieveProducts(productIds);
}
}

0 comments on commit c091cfb

Please sign in to comment.