Skip to content

Commit

Permalink
feat: adds retrieve product identifiers implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
cb-haripriyan committed Jan 31, 2023
1 parent a7523d2 commit 5e50c76
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.chargebee.example
package com.chargebee.android

import com.chargebee.android.Chargebee
import com.chargebee.android.ChargebeeReactNativeSpec
import com.chargebee.android.billingservice.CBPurchase
import com.chargebee.android.exceptions.CBProductIDResult
import com.facebook.react.bridge.ReactApplicationContext
Expand Down
2 changes: 2 additions & 0 deletions android/src/oldarch/ChargebeeReactNativeSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ 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

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)

}
49 changes: 33 additions & 16 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,51 @@
import React, { useState } from 'react';
import React from 'react';

import { StyleSheet, View, Text } from 'react-native';
import { StyleSheet, View, Button } from 'react-native';
import Chargebee from '@chargebee/react-native-chargebee';

export default function App() {
const [configComplete, isConfigComplete] = useState(false);
const site = 'site';
const apiKey = 'test_key';
const sdkKey = 'sdk-key';
const apiKey = 'apiKey';
const sdkKey = 'sdkKey';

React.useEffect(() => {
Chargebee.configure({
site: site,
publishableApiKey: apiKey,
sdkKey: sdkKey,
});
isConfigComplete(true);
configure(site, apiKey, sdkKey);
}, []);

return (
<View style={styles.container}>
{!configComplete ? (
<Text>Pending config</Text>
) : (
<Text>Config complete</Text>
)}
<Button
title="Configure"
onPress={() => configure(site, apiKey, sdkKey)}
/>
<Button
title="Retrieve Product Identifers"
onPress={retrieveProductIdentifiers}
/>
</View>
);
}

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

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
12 changes: 12 additions & 0 deletions ios/ChargebeeHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,16 @@ public class ChargebeeHelper: NSObject {
Chargebee.configure(site: site, apiKey: apiKey, sdkKey: sdkKey)
}

@objc public func retrieveProductIdentifiers(queryParams: Dictionary<String, String>, resolver: @escaping RCTPromiseResolveBlock, rejecter: @escaping RCTPromiseRejectBlock) {
CBPurchase.shared.retrieveProductIdentifers(queryParams: queryParams) { result in
switch result {
case let .success(products):
resolver(products)
case let .failure(error as NSError):
rejecter("\(error.code)", error.localizedDescription, error)
}
}

}

}
9 changes: 9 additions & 0 deletions ios/ChargebeeReactNative.mm
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ @implementation ChargebeeReactNative
[helper configureWithSite:site apiKey:publishableApiKey sdkKey:sdkKey];
}

RCT_REMAP_METHOD(retrieveProductIdentifiers,
retrieveProductIdentifiersWithQueryParams:(NSDictionary *)queryParams
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)
{
ChargebeeHelper* helper = [[ChargebeeHelper alloc] init];
[helper retrieveProductIdentifiersWithQueryParams:queryParams 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
1 change: 1 addition & 0 deletions src/NativeChargebeeReactNative.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { TurboModuleRegistry } from 'react-native';

export interface Spec extends TurboModule {
configure(site: string, publishableApiKey: string, sdkKey: string): void;
retrieveProductIdentifiers(queryParams: Object): Promise<Array<string>>;
}

export default TurboModuleRegistry.getEnforcing<Spec>('ChargebeeReactNative');
6 changes: 6 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,10 @@ export default class Chargebee {
}: ChargebeeConfig): void {
ChargebeeReactNative.configure(site, publishableApiKey, sdkKey);
}

public static async retrieveProductIdentifiers(
queryParams: Map<string, string>
): Promise<Array<string>> {
return ChargebeeReactNative.retrieveProductIdentifiers(queryParams);
}
}

0 comments on commit 5e50c76

Please sign in to comment.