Skip to content

Commit dcad508

Browse files
committed
VAL2 Feature iOS
1 parent d0bfca4 commit dcad508

4 files changed

Lines changed: 52 additions & 55 deletions

File tree

index.d.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,6 @@ declare module "react-native-appsflyer" {
113113
productId: string;
114114
}
115115

116-
export interface AppsFlyerInAppPurchaseValidationCallback {
117-
onSuccess?: (result?: any) => void;
118-
onError?: (error?: any) => void;
119-
}
120-
121116
export interface SetEmailsOptions {
122117
emails?: string[];
123118
emailsCryptType: AF_EMAIL_CRYPT_TYPE | 0 | 3;
@@ -380,9 +375,9 @@ declare module "react-native-appsflyer" {
380375
*/
381376
validateAndLogInAppPurchaseV2(
382377
purchaseDetails: AFPurchaseDetails,
383-
additionalParameters?: object,
384-
callback?: AppsFlyerInAppPurchaseValidationCallback
385-
): (() => void) | void;
378+
additionalParameters?: { [key: string]: any },
379+
callback?: (data: any) => void
380+
): void;
386381

387382
updateServerUninstallToken(token: string, successC?: SuccessCB): void;
388383
sendPushNotificationData(pushPayload: object, errorC?: ErrorCB): void;

index.js

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,36 @@ appsFlyer.onDeepLink = (callback) => {
738738
};
739739
};
740740

741+
742+
/**
743+
* New validateAndLogInAppPurchase API with AFPurchaseDetails support.
744+
*/
745+
appsFlyer.validateAndLogInAppPurchaseV2 = (purchaseDetails, additionalParameters , callback) => {
746+
const listener = appsFlyerEventEmitter.addListener("onValidationResult", (_data) => {
747+
if (callback && typeof callback === 'function') {
748+
if (typeof _data === 'string') {
749+
try {
750+
const parsed = JSON.parse(_data);
751+
callback(parsed);
752+
} catch {
753+
callback(new AFParseJSONException('Invalid JSON string', _data));
754+
}
755+
} else {
756+
callback(_data);
757+
}
758+
}
759+
});
760+
761+
eventsMap["onValidationResult"] = listener;
762+
763+
RNAppsFlyer.validateAndLogInAppPurchaseV2(purchaseDetails, additionalParameters);
764+
765+
// unregister listener (suppose should be called from componentWillUnmount() )
766+
return function remove() {
767+
listener.remove();
768+
};
769+
};
770+
741771
/**
742772
* Anonymize user Data.
743773
* Use this API during the SDK Initialization to explicitly anonymize a user's installs, events and sessions.
@@ -859,36 +889,6 @@ appsFlyer.validateAndLogInAppPurchase = (purchaseInfo, successCallback, errorCal
859889
return RNAppsFlyer.validateAndLogInAppPurchase(purchaseInfo, successCallback, errorCallback);
860890
};
861891

862-
/**
863-
* New validateAndLogInAppPurchase API with AFPurchaseDetails support.
864-
*/
865-
appsFlyer.validateAndLogInAppPurchaseV2 = (purchaseDetails, additionalParameters, callback) => {
866-
console.log('[AppsFlyer] Using new validateAndLogInAppPurchaseV2 API with AFPurchaseDetails');
867-
868-
if (callback && typeof callback === typeof Function) {
869-
const listener = appsFlyerEventEmitter.addListener(
870-
"onValidationResult",
871-
(_data) => {
872-
try {
873-
let data = JSON.parse(_data);
874-
callback(data);
875-
} catch (_error) {
876-
callback(new AFParseJSONException("Invalid data structure", _data));
877-
}
878-
}
879-
);
880-
881-
eventsMap["onValidationResult"] = listener;
882-
883-
// unregister listener (suppose should be called from componentWillUnmount() )
884-
return function remove() {
885-
listener.remove();
886-
};
887-
}
888-
889-
return RNAppsFlyer.validateAndLogInAppPurchaseV2(purchaseDetails, additionalParameters);
890-
};
891-
892892
appsFlyer.setUseReceiptValidationSandbox = (isSandbox) => {
893893
return RNAppsFlyer.setUseReceiptValidationSandbox(isSandbox);
894894
};

ios/RNAppsFlyer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ static NSString *const IOS_14_ONLY = @"Feature only supported o
5151
#define afOnInstallConversionDataLoaded @"onInstallConversionDataLoaded"
5252
#define afDeepLink @"onDeepLinkListener"
5353
#define afOnDeepLinking @"onDeepLinking"
54+
#define afOnValidationResult @"onValidationResult"
5455

5556
// User Invites, Cross Promotion
5657
#define afCpAppID @"crossPromotedAppId"

ios/RNAppsFlyer.m

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ -(void) handleCallback:(NSDictionary *) message {
476476
}
477477

478478
- (NSArray<NSString *> *)supportedEvents {
479-
return @[afOnAttributionFailure,afOnAppOpenAttribution,afOnInstallConversionFailure, afOnInstallConversionDataLoaded, afOnDeepLinking];
479+
return @[afOnAttributionFailure,afOnAppOpenAttribution,afOnInstallConversionFailure, afOnInstallConversionDataLoaded, afOnDeepLinking, afOnValidationResult];
480480
}
481481

482482
-(void) reportOnFailure:(NSString *)errorMessage type:(NSString*) type {
@@ -608,7 +608,7 @@ - (BOOL)isExpoApp {
608608
additionalParameters:(NSDictionary*)additionalParameters) {
609609

610610
if (!purchaseDetails || [purchaseDetails isKindOfClass:[NSNull class]]) {
611-
[self sendEventWithName:@"onValidationResult" body:@"{\"error\": \"Purchase details are required\"}"];
611+
[self sendEventWithName:afOnValidationResult body:@"{\"error\": \"Purchase details are required\"}"];
612612
return;
613613
}
614614

@@ -617,7 +617,7 @@ - (BOOL)isExpoApp {
617617
NSString* transactionId = [purchaseDetails objectForKey:@"transactionId"];
618618

619619
if (!purchaseType || !productId || !transactionId) {
620-
[self sendEventWithName:@"onValidationResult" body:@"{\"error\": \"purchaseType, productId, and transactionId are required\"}"];
620+
[self sendEventWithName:afOnValidationResult body:@"{\"error\": \"purchaseType, productId, and transactionId are required\"}"];
621621
return;
622622
}
623623

@@ -629,20 +629,21 @@ - (BOOL)isExpoApp {
629629
afPurchaseType = AFSDKPurchaseTypeOneTimePurchase;
630630
}
631631

632-
// Create AFSDKPurchaseDetails object
633-
AFSDKPurchaseDetails* details = [[AFSDKPurchaseDetails alloc] initWithPurchaseType:afPurchaseType
634-
productId:productId
635-
transactionId:transactionId];
636-
637-
[[AppsFlyerLib shared] validateAndLogInAppPurchase:details
638-
extraEventValues:additionalParameters
639-
completionHandler:^(AFSDKValidateAndLogResult * _Nullable result) {
640-
if (result && result.isValid) {
641-
[self sendEventWithName:@"onValidationResult" body:@"{\"result\": true, \"message\": \"In App Purchase Validation completed successfully!\"}"];
642-
} else {
643-
[self sendEventWithName:@"onValidationResult" body:@"{\"error\": \"Purchase validation failed\"}"];
644-
}
645-
}];
632+
AFSDKPurchaseDetails* details = [[AFSDKPurchaseDetails alloc] initWithProductId:productId transactionId:transactionId purchaseType:afPurchaseType];
633+
634+
[[AppsFlyerLib shared] validateAndLogInAppPurchase:details purchaseAdditionalDetails:additionalParameters completion:^(NSDictionary * _Nullable response, NSError * _Nullable error) {
635+
if (error == nil) {
636+
BOOL valid = [response[@"result"] boolValue];
637+
if (valid) {
638+
[self sendEventWithName:afOnValidationResult body:@"{\"result\": true, \"message\": \"In App Purchase Validation completed successfully!\"}"];
639+
} else {
640+
NSString *msg = response[@"message"] ?: @"Purchase validation failed";
641+
[self sendEventWithName:afOnValidationResult body:@"{\"result\": false, \"message\": \"Purchase validation failed\"}"];
642+
}
643+
} else {
644+
[self sendEventWithName:afOnValidationResult body:[NSString stringWithFormat:@"{\"error\": \"%@\"}", error.localizedDescription]];
645+
}
646+
}];
646647
}
647648

648649
RCT_EXPORT_METHOD(sendPushNotificationData: (NSDictionary*)pushPayload errorCallBack:(RCTResponseErrorBlock)errorCallBack) {

0 commit comments

Comments
 (0)