Skip to content

Commit

Permalink
Separated callbacks to onSuccess onFailure, modified native impl acco…
Browse files Browse the repository at this point in the history
…rdingly
  • Loading branch information
al-af committed Oct 6, 2024
1 parent 87774e4 commit 2a293ee
Show file tree
Hide file tree
Showing 16 changed files with 245 additions and 114 deletions.
8 changes: 4 additions & 4 deletions PurchaseConnector/constants/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ class AppsFlyerConstants {
static readonly MISSING_CONFIGURATION_EXCEPTION_MSG: string = "Could not create an instance without configuration";

// Adding method constants
static readonly SUBSCRIPTION_PURCHASE_VALIDATION_RESULT_LISTENER: string =
"onSubscriptionValidationResult";
static readonly IN_APP_VALIDATION_RESULT_LISTENER: string =
"onInAppPurchaseValidationResult";
static readonly SUBSCRIPTION_VALIDATION_SUCCESS: string = 'subscriptionValidationSuccess';
static readonly SUBSCRIPTION_VALIDATION_FAILURE: string = 'subscriptionValidationFailure';
static readonly IN_APP_PURCHASE_VALIDATION_SUCCESS: string = 'inAppPurchaseValidationSuccess';
static readonly IN_APP_PURCHASE_VALIDATION_FAILURE: string = 'inAppPurchaseValidationFailure';
static readonly DID_RECEIVE_PURCHASE_REVENUE_VALIDATION_INFO: string =
"onDidReceivePurchaseRevenueValidationInfo";

Expand Down
2 changes: 1 addition & 1 deletion PurchaseConnector/utils/connector_callbacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { IosError, JVMThrowable } from "../models";
export type PurchaseConnectorListener = (data: any) => void;

// Type definition for a listener which gets called when the `PurchaseConnectorImpl` receives purchase revenue validation info for iOS.
export type DidReceivePurchaseRevenueValidationInfo = (validationInfo?: Map<string, any>, error?: IosError) => void;
export type onReceivePurchaseRevenueValidationInfo = (validationInfo?: Map<string, any>, error?: IosError) => void;

// Invoked when a 200 OK response is received from the server.
// Note: An INVALID purchase is considered to be a successful response and will also be returned by this callback.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,25 +131,25 @@ public void stopObservingTransactions() {
private final MappedValidationResultListener arsListener = new MappedValidationResultListener() {
@Override
public void onFailure(String result, Throwable error) {
handleError(EVENT_SUBSCRIPTION_VALIDATION_RESULT, result, error);
handleError(EVENT_SUBSCRIPTION_VALIDATION_FAILURE, result, error);
}

@Override
public void onResponse(Map<String, Object> response) {
handleSuccess(EVENT_SUBSCRIPTION_VALIDATION_RESULT, response);
handleSuccess(EVENT_SUBSCRIPTION_VALIDATION_SUCCESS, response);
}
};

// Initialization of the VIAPListener
private final MappedValidationResultListener viapListener = new MappedValidationResultListener() {
@Override
public void onFailure(String result, Throwable error) {
handleError(EVENT_IN_APP_PURCHASE_VALIDATION_RESULT, result, error);
handleError(EVENT_IN_APP_PURCHASE_VALIDATION_FAILURE, result, error);
}

@Override
public void onResponse(Map<String, Object> response) {
handleSuccess(EVENT_IN_APP_PURCHASE_VALIDATION_RESULT, response);
handleSuccess(EVENT_IN_APP_PURCHASE_VALIDATION_SUCCESS, response);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ public class RNAppsFlyerConstants {
final static String AF_ADDITIONAL_PARAMETERS = "additionalParameters";

//Purchase Connector
final static String EVENT_SUBSCRIPTION_VALIDATION_RESULT = "onSubscriptionValidationResult";
final static String EVENT_IN_APP_PURCHASE_VALIDATION_RESULT = "onInAppPurchaseValidationResult";
final static String EVENT_SUBSCRIPTION_VALIDATION_SUCCESS = "subscriptionValidationSuccess";
final static String EVENT_SUBSCRIPTION_VALIDATION_FAILURE = "subscriptionValidationFailure";
final static String EVENT_IN_APP_PURCHASE_VALIDATION_SUCCESS = "inAppPurchaseValidationSuccess";
final static String EVENT_IN_APP_PURCHASE_VALIDATION_FAILURE = "inAppPurchaseValidationFailure";
final static String ENABLE_MODULE_MESSAGE = "Please set appsflyer.enable_purchase_connector to true in your gradle.properties file.";
}

3 changes: 3 additions & 0 deletions demos/appsflyer-react-native-app/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,6 @@ buck-out/

# CocoaPods
/ios/Pods/

#Secrets
.env
3 changes: 3 additions & 0 deletions demos/appsflyer-react-native-app/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {createStackNavigator} from '@react-navigation/stack';
import HomeScreen from './components/HomeScreen.js';
import Cart from './components/Cart.js';
import Item from './components/Item.js';
import {withIAPContext} from 'react-native-iap';

const Stack = createStackNavigator();
try {
Expand Down Expand Up @@ -59,3 +60,5 @@ const App = () => {
const styles = StyleSheet.create({});

export default App;

//export default withIAPContext(App);
2 changes: 2 additions & 0 deletions demos/appsflyer-react-native-app/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ android {
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "1.0"
//RNIap
missingDimensionStrategy "store", "play"
}
splits {
abi {
Expand Down
2 changes: 2 additions & 0 deletions demos/appsflyer-react-native-app/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ buildscript {
compileSdkVersion = 34
targetSdkVersion = 34
ndkVersion = "20.1.5948944"
//For RNIap
supportLibVersion = "28.0.0"
}
repositories {
google()
Expand Down
17 changes: 15 additions & 2 deletions demos/appsflyer-react-native-app/babel.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
module.exports = {
presets: ['module:metro-react-native-babel-preset'],
};
presets: ["module:metro-react-native-babel-preset"],
plugins: [
[
"module:react-native-dotenv",
{
moduleName: "@env",
path: ".env",
blacklist: null,
whitelist: null,
safe: false,
allowUndefined: true,
},
],
],
};
4 changes: 2 additions & 2 deletions demos/appsflyer-react-native-app/components/AppsFlyer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ export const AF_clickOnItem = 'af_click_on_item';

const initOptions = {
isDebug: true,
devKey: 'Us4GmXxXx46Qed',
devKey: 'WdpTVAcYwmxsaQ4WeTspmh',
onInstallConversionDataListener: true,
timeToWaitForATTUserAuthorization: 10,
onDeepLinkListener: true,
appId: '741993747',
appId: '1201211633',
};

// AppsFlyer initialization flow. ends with initSdk.
Expand Down
130 changes: 91 additions & 39 deletions demos/appsflyer-react-native-app/components/Cart.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,105 @@
/* @flow weak */

import React from 'react';
import {View, Text, StyleSheet, ScrollView} from 'react-native';
import {ListItem, Avatar, Button} from 'react-native-elements'
import {View, Text, StyleSheet, ScrollView, Alert} from 'react-native';
import {ListItem, Avatar, Button} from 'react-native-elements';
//import {InAppPurchase} from 'react-native-iap';

const Cart = ({route, navigation}) => {
const {productList, removeProductFromCart, checkout} = route.params;
const {productList, removeProductFromCart, checkout} = route.params;

/*
const productIds = [
'one1',
'non.cons2',
'auto.renew',
'non.renew',
'cons.test',
'nonconsumable.purchase1',
'autorenewable.purchase1',
'nonrenewing.purchase1',
];
const handleRemove = product => {
removeProductFromCart(product);
navigation.goBack();
}
InAppPurchase.getProducts(productIds)
.then(products => {
console.log('Products:', products);
})
.catch(error => {
console.log('Error fetching products:', error);
});
*/

const handleRemove = product => {
removeProductFromCart(product);
navigation.goBack();
};

const handleCheckout = () => {
if (productList.length !== 0) {
checkout();
navigation.goBack();
}
}
const handleCheckout = () => {
if (productList.length !== 0) {
checkout();
navigation.goBack();
} else {
Alert.alert(
'Cart Empty', // Alert Title
'The cart is empty.', // Alert Message
[
{
text: 'OK',
onPress: () => console.log('OK Pressed'),
style: 'cancel',
},
],
{
cancelable: true, // whether the alert can be dismissed by tapping outside of the alert box
onDismiss: () => console.log('Alert dismissed'), // a callback that gets called when the alert is dismissed
},
);
}
};

return (<View style={styles.container}>
return (
<View style={styles.container}>
<ScrollView>
{
productList.map((product, index) => {
return (<ListItem.Swipeable key={index} bottomDivider="bottomDivider" rightContent={<Button title = "Delete" buttonStyle = {{ minHeight: '100%', backgroundColor: 'red' }}onPress = {
() => handleRemove(product)
}
/>}>
<Avatar source={{
uri: product.image
}}/>
<ListItem.Content>
<ListItem.Title>{product.name}</ListItem.Title>
<ListItem.Subtitle>{`${product.price} USD`}</ListItem.Subtitle>
</ListItem.Content>
</ListItem.Swipeable>)
})
}
{productList.map((product, index) => {
return (
<ListItem.Swipeable
key={index}
bottomDivider="bottomDivider"
rightContent={
<Button
title="Delete"
buttonStyle={{minHeight: '100%', backgroundColor: 'red'}}
onPress={() => handleRemove(product)}
/>
}>
<Avatar
source={{
uri: product.image,
}}
/>
<ListItem.Content>
<ListItem.Title>{product.name}</ListItem.Title>
<ListItem.Subtitle>{`${product.price} USD`}</ListItem.Subtitle>
</ListItem.Content>
</ListItem.Swipeable>
);
})}
</ScrollView>
<Button buttonStyle={styles.checkoutButton} title='Checkout'onPress={handleCheckout}/>
</View>);
}
<Button
buttonStyle={styles.checkoutButton}
title="Checkout"
onPress={handleCheckout}
/>
</View>
);
};
export default Cart;

const styles = StyleSheet.create({
container: {
flex: 1
},
checkoutButton: {
marginBottom: 10
}
container: {
flex: 1,
},
checkoutButton: {
height: 75,
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
13B07FBF1A68108700A75B9A /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 13B07FB51A68108700A75B9A /* Images.xcassets */; };
13B07FC11A68108700A75B9A /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 13B07FB71A68108700A75B9A /* main.m */; };
758FBD406B8A60C781CB65C7 /* libPods-AppsFlyerExample.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 3772C10A05F15474D2AE8E5B /* libPods-AppsFlyerExample.a */; };
7E0676052CABDEBC0055F176 /* StoreKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 7E0676042CABDEBC0055F176 /* StoreKit.framework */; };
81AB9BB82411601600AC10FF /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 81AB9BB72411601600AC10FF /* LaunchScreen.storyboard */; };
8A99A8955B526C01707AFBFA /* libPods-AppsFlyerExample-AppsFlyerExampleTests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 1C982642725666DF8D1E1F96 /* libPods-AppsFlyerExample-AppsFlyerExampleTests.a */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -39,6 +40,7 @@
1C982642725666DF8D1E1F96 /* libPods-AppsFlyerExample-AppsFlyerExampleTests.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-AppsFlyerExample-AppsFlyerExampleTests.a"; sourceTree = BUILT_PRODUCTS_DIR; };
3772C10A05F15474D2AE8E5B /* libPods-AppsFlyerExample.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-AppsFlyerExample.a"; sourceTree = BUILT_PRODUCTS_DIR; };
5CAE30CF50FDE233EFB84891 /* Pods-AppsFlyerExample.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-AppsFlyerExample.release.xcconfig"; path = "Target Support Files/Pods-AppsFlyerExample/Pods-AppsFlyerExample.release.xcconfig"; sourceTree = "<group>"; };
7E0676042CABDEBC0055F176 /* StoreKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = StoreKit.framework; path = System/Library/Frameworks/StoreKit.framework; sourceTree = SDKROOT; };
81AB9BB72411601600AC10FF /* LaunchScreen.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; name = LaunchScreen.storyboard; path = AppsFlyerExample/LaunchScreen.storyboard; sourceTree = "<group>"; };
BE4C6B4402435E3245DA5B5C /* Pods-AppsFlyerExample.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-AppsFlyerExample.debug.xcconfig"; path = "Target Support Files/Pods-AppsFlyerExample/Pods-AppsFlyerExample.debug.xcconfig"; sourceTree = "<group>"; };
C6E030350442BE719CA4C6F7 /* Pods-AppsFlyerExample-AppsFlyerExampleTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-AppsFlyerExample-AppsFlyerExampleTests.debug.xcconfig"; path = "Target Support Files/Pods-AppsFlyerExample-AppsFlyerExampleTests/Pods-AppsFlyerExample-AppsFlyerExampleTests.debug.xcconfig"; sourceTree = "<group>"; };
Expand All @@ -61,6 +63,7 @@
buildActionMask = 2147483647;
files = (
758FBD406B8A60C781CB65C7 /* libPods-AppsFlyerExample.a in Frameworks */,
7E0676052CABDEBC0055F176 /* StoreKit.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -101,6 +104,7 @@
2D16E6871FA4F8E400B85C8A /* Frameworks */ = {
isa = PBXGroup;
children = (
7E0676042CABDEBC0055F176 /* StoreKit.framework */,
ED297162215061F000B7C4FE /* JavaScriptCore.framework */,
3772C10A05F15474D2AE8E5B /* libPods-AppsFlyerExample.a */,
1C982642725666DF8D1E1F96 /* libPods-AppsFlyerExample-AppsFlyerExampleTests.a */,
Expand Down Expand Up @@ -345,7 +349,7 @@
};
FD10A7F022414F080027D42C /* Start Packager */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
buildActionMask = 8;
files = (
);
inputFileListPaths = (
Expand All @@ -357,7 +361,7 @@
);
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
runOnlyForDeploymentPostprocessing = 1;
shellPath = /bin/sh;
shellScript = "export RCT_METRO_PORT=\"${RCT_METRO_PORT:=8081}\"\necho \"export RCT_METRO_PORT=${RCT_METRO_PORT}\" > \"${SRCROOT}/../node_modules/react-native/scripts/.packager.env\"\nif [ -z \"${RCT_NO_LAUNCH_PACKAGER+xxx}\" ] ; then\n if nc -w 5 -z localhost ${RCT_METRO_PORT} ; then\n if ! curl -s \"http://localhost:${RCT_METRO_PORT}/status\" | grep -q \"packager-status:running\" ; then\n echo \"Port ${RCT_METRO_PORT} already in use, packager is either not running or not running correctly\"\n exit 2\n fi\n else\n open \"$SRCROOT/../node_modules/react-native/scripts/launchPackager.command\" || echo \"Can't start packager automatically\"\n fi\nfi\n";
showEnvVarsInLog = 0;
Expand Down Expand Up @@ -468,7 +472,7 @@
"-ObjC",
"-lc++",
);
PRODUCT_BUNDLE_IDENTIFIER = com.AppsFlyer.AppsFlyerExample;
PRODUCT_BUNDLE_IDENTIFIER = com.appsflyer.inapppurchase;
PRODUCT_NAME = AppsFlyerExample;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 5.0;
Expand Down Expand Up @@ -497,7 +501,7 @@
"-ObjC",
"-lc++",
);
PRODUCT_BUNDLE_IDENTIFIER = com.AppsFlyer.AppsFlyerExample;
PRODUCT_BUNDLE_IDENTIFIER = com.appsflyer.inapppurchase;
PRODUCT_NAME = AppsFlyerExample;
SWIFT_VERSION = 5.0;
VERSIONING_SYSTEM = "apple-generic";
Expand Down
7 changes: 5 additions & 2 deletions demos/appsflyer-react-native-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@
"lint": "eslint ."
},
"dependencies": {
"@react-navigation/native": "^6.0.2",
"@react-navigation/stack": "^6.0.7",
"@react-navigation/native": "^6.1.18",
"@react-navigation/stack": "^6.4.1",
"D": "^1.0.0",
"react": "18.1.0",
"react-native": "0.70.6",
"react-native-appsflyer": "file:../../",
"react-native-dotenv": "^3.4.11",
"react-native-elements": "^3.4.2",
"react-native-gesture-handler": "^1.10.3",
"react-native-iap": "^12.15.4",
"react-native-safe-area-context": "^3.3.2",
"react-native-screens": "^3.34.0",
"react-native-vector-icons": "8.1.0"
Expand Down
6 changes: 3 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import SubscriptionValidationResult from "../models/subscription_validation_resu
import {
OnResponse,
OnFailure,
DidReceivePurchaseRevenueValidationInfo,
onReceivePurchaseRevenueValidationInfo,
} from "../utils/connector_callbacks";

declare module "react-native-appsflyer" {
Expand Down Expand Up @@ -188,8 +188,8 @@ declare module "react-native-appsflyer" {
onResponse: OnResponse<InAppPurchaseValidationResult>,
onFailure: OnFailure
): void;
setDidReceivePurchaseRevenueValidationInfo(
callback: DidReceivePurchaseRevenueValidationInfo
setOnReceivePurchaseRevenueValidationInfo(
callback: onReceivePurchaseRevenueValidationInfo
): void;
}

Expand Down
Loading

0 comments on commit 2a293ee

Please sign in to comment.