Skip to content

Commit 4caa506

Browse files
committed
Added callback support for purchase validation
1 parent 90a1df0 commit 4caa506

File tree

10 files changed

+77
-44
lines changed

10 files changed

+77
-44
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Versions
22

3+
## 6.2.1+6
4+
- Added callbacks support for purchase validation API
5+
36
## 6.2.1+5
47
- Added support for useReceiptValidationSandbox API
58

android/src/main/java/com/appsflyer/appsflyersdk/AppsflyerSdkPlugin.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public class AppsflyerSdkPlugin implements MethodCallHandler, FlutterPlugin, Act
7171
private Boolean gcdCallback = false;
7272
private Boolean oaoaCallback = false;
7373
private Boolean udlCallback = false;
74+
private Boolean validatePurchaseCallback = false;
7475

7576
private void onAttachedToEngine(Context applicationContext, BinaryMessenger messenger) {
7677
this.mContext = applicationContext;
@@ -108,6 +109,9 @@ private void startListening(Object arguments, Result rawResult) {
108109
if (callbackName.equals(AppsFlyerConstants.AF_UDL_CALLBACK)){
109110
udlCallback = true;
110111
}
112+
if (callbackName.equals(AppsFlyerConstants.AF_VALIDATE_PURCHASE)){
113+
validatePurchaseCallback = true;
114+
}
111115
Map<String, Object> args = new HashMap<>();
112116
args.put("id", callbackName);
113117
mCallbacks.put(callbackName, args);
@@ -379,12 +383,14 @@ private void registerValidatorListener() {
379383
AppsFlyerInAppPurchaseValidatorListener validatorListener = new AppsFlyerInAppPurchaseValidatorListener() {
380384
@Override
381385
public void onValidateInApp() {
382-
JSONObject obj = new JSONObject();
383-
384386
try {
385-
obj.put("status", AF_SUCCESS);
386-
obj.put("type", AF_VALIDATE_PURCHASE);
387-
sendEventToDart(obj, AF_EVENTS_CHANNEL);
387+
JSONObject obj = new JSONObject();
388+
if(validatePurchaseCallback){
389+
runOnUIThread(obj, AppsFlyerConstants.AF_VALIDATE_PURCHASE, AF_SUCCESS);
390+
}else{
391+
obj.put("status", AF_SUCCESS);
392+
sendEventToDart(obj, AF_EVENTS_CHANNEL);
393+
}
388394
} catch (JSONException e) {
389395
e.printStackTrace();
390396
}
@@ -393,12 +399,15 @@ public void onValidateInApp() {
393399

394400
@Override
395401
public void onValidateInAppFailure(String s) {
396-
JSONObject obj = new JSONObject();
397402
try {
398-
obj.put("status", AF_FAILURE);
399-
obj.put("type", AF_VALIDATE_PURCHASE);
403+
JSONObject obj = new JSONObject();
400404
obj.put("error", s);
401-
sendEventToDart(obj, AF_EVENTS_CHANNEL);
405+
if(validatePurchaseCallback){
406+
runOnUIThread(obj, AppsFlyerConstants.AF_VALIDATE_PURCHASE, AF_FAILURE);
407+
}else{
408+
obj.put("status", AF_FAILURE);
409+
sendEventToDart(obj, AF_EVENTS_CHANNEL);
410+
}
402411
} catch (JSONException e) {
403412
e.printStackTrace();
404413
}

doc/API.md

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@
3030
- [getHostName](#getHostName)
3131
- [getHostPrefix](#getHostPrefix)
3232
- [updateServerUninstallToken](#updateServerUninstallToken)
33-
- [validateAndLogInAppAndroidPurchase](#validateAndLogInAppAndroidPurchase)
34-
- [validateAndLogInAppIosPurchase](#validateAndLogInAppIosPurchase)
33+
- [Validate Purchase](#validatePurchase)
3534
- [setPushNotification](#setPushNotification)
3635
- [stream](#streams)
3736
---
@@ -305,13 +304,18 @@ _Example:_
305304
appsFlyerSdk.updateServerUninstallToken("token");
306305
```
307306
---
308-
**<a id="validateAndLogInAppAndroidPurchase"> `Future<dynamic> validateAndLogInAppAndroidPurchase(
307+
**<a id="validatePurchase"> Validate Purchase**
308+
309+
310+
***Android:***
311+
312+
`Future<dynamic> validateAndLogInAppAndroidPurchase(
309313
String publicKey,
310314
String signature,
311315
String purchaseData,
312316
String price,
313317
String currency,
314-
Map<String, String> additionalParameters)`**
318+
Map<String, String> additionalParameters)`
315319

316320
_Example:_
317321
```dart
@@ -323,8 +327,10 @@ appsFlyerSdk.validateAndLogInAppAndroidPurchase(
323327
"currency",
324328
{"fs": "fs"});
325329
```
326-
---
327-
**<a id="validateAndLogInAppIosPurchase"> `Future<dynamic> validateAndLogInAppIosPurchase(
330+
331+
***iOS:***
332+
333+
**`Future<dynamic> validateAndLogInAppIosPurchase(
328334
String productIdentifier,
329335
String price,
330336
String currency,
@@ -341,15 +347,26 @@ appsFlyerSdk.validateAndLogInAppIosPurchase(
341347
"additionalParameters");
342348
```
343349

344-
345-
***To use the purchase validation feature in sandbox mode call the follow API:***
350+
***Purchase validation sandbox mode for iOS:***
346351

347352
`void useReceiptValidationSandbox(bool isSandboxEnabled)`
348353

349354
_Example:_
350355
```dart
351356
appsFlyerSdk.useReceiptValidationSandbox(true);
352357
```
358+
359+
***Purchase validation callback***
360+
361+
`void onPurchaseValidation(Function callback)`
362+
363+
_Example:_
364+
```dart
365+
appsflyerSdk.onPurchaseValidation((res){
366+
print("res: " + res.toString());
367+
});
368+
```
369+
353370
---
354371
**<a id="setPushNotification"> `void setPushNotification(bool isEnabled)`**
355372

example/lib/main_page.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ class MainPageState extends State<MainPage> {
2222
void initState() {
2323
super.initState();
2424
final AppsFlyerOptions options = AppsFlyerOptions(
25-
afDevKey: DotEnv().env["DEV_KEY"],
26-
appId: DotEnv().env["APP_ID"],
27-
showDebug: true);
25+
afDevKey: DotEnv().env["DEV_KEY"],
26+
appId: DotEnv().env["APP_ID"],
27+
showDebug: true);
2828
_appsflyerSdk = AppsflyerSdk(options);
2929
_appsflyerSdk.onAppOpenAttribution((res) {
3030
print("res: " + res.toString());

ios/Classes/AppsFlyerStreamHandler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ NS_ASSUME_NONNULL_BEGIN
1616

1717
@interface AppsFlyerStreamHandler: NSObject<FlutterStreamHandler, AppsFlyerLibDelegate, AppsFlyerDeepLinkDelegate>
1818

19-
- (void) sendObject:(NSDictionary*) obj;
19+
- (void) sendValidatePurchaseResponseToFlutter:(NSDictionary*) obj;
2020

2121
@end
2222

ios/Classes/AppsFlyerStreamHandler.m

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,20 @@ - (void)didResolveDeepLink:(AppsFlyerDeepLinkResult* _Nonnull) deepLinkResult {
155155
}
156156
}
157157

158-
- (void)sendObject:(NSDictionary *)message{
158+
- (void)sendValidatePurchaseResponseToFlutter:(NSDictionary *)message{
159159
NSError *error;
160-
NSData *JSON = [NSJSONSerialization dataWithJSONObject:message options:0 error:&error];
160+
// NSData *JSON = [NSJSONSerialization dataWithJSONObject:message options:0 error:&error];
161+
NSString *JSONString = [self mapToJson:message withError:error];
161162
if (error) {
162163
return;
163164
}
164-
NSString *JSONString = [[NSString alloc] initWithData:JSON encoding:NSUTF8StringEncoding];
165-
_eventSink(JSONString);
165+
NSDictionary *fullResponse = @{
166+
@"id": afValidatePurchase,
167+
@"data": JSONString,
168+
@"status": afSuccess
169+
};
170+
JSONString = [self mapToJson:fullResponse withError:error];
171+
[AppsflyerSdkPlugin.callbackChannel invokeMethod:@"callListener" arguments:JSONString];
166172
}
167173

168174
@end

ios/Classes/AppsflyerSdkPlugin.m

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -299,16 +299,15 @@ - (void)onValidateSuccess: (NSDictionary*) data{
299299
@"data": data
300300
};
301301

302-
[_streamHandler sendObject:message];
302+
[_streamHandler sendValidatePurchaseResponseToFlutter:message];
303303
}
304304

305305
-(void)onValidateFail:(NSError*)error{
306306
NSDictionary* message = @{
307-
@"type": afValidatePurchase,
308-
@"status": afSuccess,
307+
@"status": afFailure,
309308
@"error": @"error connecting"
310309
};
311-
[_streamHandler sendObject:message];
310+
[_streamHandler sendValidatePurchaseResponseToFlutter:message];
312311
[self performSelectorOnMainThread:@selector(handleCallback:) withObject:@[message,afValidatePurchaseChannel] waitUntilDone:NO];
313312
}
314313

lib/src/appsflyer_sdk.dart

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -202,14 +202,15 @@ class AppsflyerSdk {
202202
}
203203

204204
///Returns `Stream`. Accessing AppsFlyer purchase validation data
205-
void _registerValidatePurchaseCallback() {
205+
Stream<dynamic> _registerValidatePurchaseStream() {
206206
if (_afValidtaPurchaseController == null) {
207207
_afValidtaPurchaseController = StreamController(onCancel: () {
208208
_afValidtaPurchaseController.close();
209209
});
210210

211211
_registerPurchaseValidateListener();
212212
}
213+
return _afValidtaPurchaseController.stream;
213214
}
214215

215216
///initialize the SDK, using the options initialized from the constructor|
@@ -368,16 +369,15 @@ class AppsflyerSdk {
368369
String price,
369370
String currency,
370371
Map<String, String> additionalParameters) {
371-
372-
_registerValidatePurchaseCallback();
373-
return _methodChannel.invokeMethod("validateAndLogInAppAndroidPurchase", {
374-
'publicKey': publicKey,
375-
'signature': signature,
376-
'purchaseData': purchaseData,
377-
'price': price,
378-
'currency': currency,
379-
'additionalParameters': additionalParameters
380-
});
372+
373+
return _methodChannel.invokeMethod("validateAndLogInAppAndroidPurchase", {
374+
'publicKey': publicKey,
375+
'signature': signature,
376+
'purchaseData': purchaseData,
377+
'price': price,
378+
'currency': currency,
379+
'additionalParameters': additionalParameters
380+
});
381381
}
382382

383383
///Accessing AppsFlyer purchase validation data
@@ -386,9 +386,7 @@ class AppsflyerSdk {
386386
String price,
387387
String currency,
388388
String transactionId,
389-
Map<String, String> additionalParameters) async {
390-
await Future.delayed(Duration(seconds: 10));
391-
_registerValidatePurchaseCallback();
389+
Map<String, String> additionalParameters)async{
392390
return await _methodChannel.invokeMethod("validateAndLogInAppIosPurchase", {
393391
'productIdentifier': productIdentifier,
394392
'price': price,

lib/src/callbacks.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Future<void> _methodCallHandler(MethodCall call) async {
1818
case "onAppOpenAttribution":
1919
case "onInstallConversionData":
2020
case "onDeepLinking":
21+
case "validatePurchase":
2122
String data = callMap["data"];
2223
Map decodedData = jsonDecode(data);
2324
Map fullResponse = {

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: appsflyer_sdk
22
description: A Flutter plugin for AppsFlyer SDK. Supports iOS and Android.
3-
version: 6.2.1+5
3+
version: 6.2.1+6
44

55
homepage: https://github.com/AppsFlyerSDK/flutter_appsflyer_sdk
66

0 commit comments

Comments
 (0)