Skip to content

Commit e05358d

Browse files
committed
feat: add verify and track feature
1 parent 7da6287 commit e05358d

File tree

5 files changed

+250
-9
lines changed

5 files changed

+250
-9
lines changed

Assets/Adjust/Native/iOS/AdjustUnity.h

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ typedef void (*AdjustDelegateLastDeeplinkGetter)(const char* lastDeeplink);
2121
typedef void (*AdjustDelegateSdkVersionGetter)(const char* sdkVersion);
2222
typedef void (*AdjustDelegateAttCallback)(int status);
2323
typedef void (*AdjustDelegatePurchaseVerificationCallback)(const char* verificationResult);
24+
typedef void (*AdjustDelegateVerifyAndTrackCallback)(const char* verificationResult);
2425
typedef void (*AdjustDelegateResolvedDeeplinkCallback)(const char* deeplink);
2526
typedef void (*AdjustDelegateSkanErrorCallback)(const char* error);
2627

Assets/Adjust/Native/iOS/AdjustUnity.mm

+93
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,99 @@ void _AdjustProcessAndResolveDeeplink(const char* deeplink, AdjustDelegateResolv
764764
}
765765
}
766766

767+
void _AdjustVerifyAndTrackAppStorePurchase(
768+
const char* eventToken,
769+
double revenue,
770+
const char* currency,
771+
const char* receipt,
772+
const char* productId,
773+
const char* transactionId,
774+
const char* callbackId,
775+
const char* deduplicationId,
776+
const char* jsonCallbackParameters,
777+
const char* jsonPartnerParameters,
778+
AdjustDelegateVerifyAndTrackCallback callback) {
779+
NSString *strEventToken = isStringValid(eventToken) == true ? [NSString stringWithUTF8String:eventToken] : nil;
780+
ADJEvent *event = [[ADJEvent alloc] initWithEventToken:strEventToken];
781+
782+
// revenue and currency
783+
if (revenue != -1 && currency != NULL) {
784+
NSString *stringCurrency = [NSString stringWithUTF8String:currency];
785+
[event setRevenue:revenue currency:stringCurrency];
786+
}
787+
788+
// callback parameters
789+
NSArray *arrayCallbackParameters = convertArrayParameters(jsonCallbackParameters);
790+
if (arrayCallbackParameters != nil) {
791+
NSUInteger count = [arrayCallbackParameters count];
792+
for (int i = 0; i < count;) {
793+
NSString *key = arrayCallbackParameters[i++];
794+
NSString *value = arrayCallbackParameters[i++];
795+
[event addCallbackParameter:key value:value];
796+
}
797+
}
798+
799+
// partner parameters
800+
NSArray *arrayPartnerParameters = convertArrayParameters(jsonPartnerParameters);
801+
if (arrayPartnerParameters != nil) {
802+
NSUInteger count = [arrayPartnerParameters count];
803+
for (int i = 0; i < count;) {
804+
NSString *key = arrayPartnerParameters[i++];
805+
NSString *value = arrayPartnerParameters[i++];
806+
[event addPartnerParameter:key value:value];
807+
}
808+
}
809+
810+
// transaction ID
811+
if (transactionId != NULL) {
812+
NSString *strTransactionId = [NSString stringWithUTF8String:transactionId];
813+
[event setTransactionId:strTransactionId];
814+
}
815+
816+
// product ID
817+
if (productId != NULL) {
818+
NSString *strProductId = [NSString stringWithUTF8String:productId];
819+
[event setProductId:strProductId];
820+
}
821+
822+
// receipt (base64 encoded string)
823+
if (receipt != NULL) {
824+
NSString *strReceipt = [NSString stringWithUTF8String:receipt];
825+
NSData *dataReceipt = [[NSData alloc] initWithBase64EncodedString:strReceipt options:0];
826+
[event setReceipt:dataReceipt];
827+
}
828+
829+
// callback ID
830+
if (callbackId != NULL) {
831+
NSString *strCallbackId = [NSString stringWithUTF8String:callbackId];
832+
[event setCallbackId:strCallbackId];
833+
}
834+
835+
// deduplication ID
836+
if (deduplicationId != NULL) {
837+
NSString *strDeduplicationId = [NSString stringWithUTF8String:deduplicationId];
838+
[event setDeduplicationId:strDeduplicationId];
839+
}
840+
841+
[Adjust verifyAndTrackAppStorePurchase:event
842+
withCompletionHandler:^(ADJPurchaseVerificationResult * _Nonnull verificationResult) {
843+
// TODO: nil checks
844+
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
845+
addValueOrEmpty(dictionary, @"verificationStatus", verificationResult.verificationStatus);
846+
addValueOrEmpty(dictionary, @"code", [NSString stringWithFormat:@"%d", verificationResult.code]);
847+
addValueOrEmpty(dictionary, @"message", verificationResult.message);
848+
849+
NSData *dataVerificationInfo = [NSJSONSerialization dataWithJSONObject:dictionary
850+
options:0
851+
error:nil];
852+
NSString *strVerificationInfo = [[NSString alloc] initWithBytes:[dataVerificationInfo bytes]
853+
length:[dataVerificationInfo length]
854+
encoding:NSUTF8StringEncoding];
855+
const char* verificationInfoCString = [strVerificationInfo UTF8String];
856+
callback(verificationInfoCString);
857+
}];
858+
}
859+
767860
void _AdjustSetTestOptions(const char* overwriteUrl,
768861
const char* extraPath,
769862
long timerIntervalInMilliseconds,

Assets/Adjust/Scripts/Adjust.cs

+36-8
Original file line numberDiff line numberDiff line change
@@ -685,14 +685,6 @@ public static void VerifyPlayStorePurchase(
685685
#if UNITY_IOS
686686
Debug.Log("[Adjust]: Play Store purchase verification is only supported for Android platform.");
687687
#elif UNITY_ANDROID
688-
if (purchase == null ||
689-
purchase.ProductId == null ||
690-
purchase.PurchaseToken == null)
691-
{
692-
Debug.Log("[Adjust]: Invalid Play Store purchase parameters.");
693-
return;
694-
}
695-
696688
AdjustAndroid.VerifyPlayStorePurchase(purchase, verificationResultCallback);
697689
#else
698690
Debug.Log(errorMsgPlatform);
@@ -715,6 +707,42 @@ public static void ProcessAndResolveDeeplink(string deeplink, Action<string> cal
715707
#endif
716708
}
717709

710+
public static void VerifyAndTrackAppStorePurchase(
711+
AdjustEvent adjustEvent,
712+
Action<AdjustPurchaseVerificationResult> callback)
713+
{
714+
if (IsEditor())
715+
{
716+
return;
717+
}
718+
719+
#if UNITY_IOS
720+
AdjustiOS.VerifyAndTrackAppStorePurchase(adjustEvent, callback);
721+
#elif UNITY_ANDROID
722+
Debug.Log("[Adjust]: App Store purchase verification is only supported for iOS platform.");
723+
#else
724+
Debug.Log(errorMsgPlatform);
725+
#endif
726+
}
727+
728+
public static void VerifyAndTrackPlayStorePurchase(
729+
AdjustEvent adjustEvent,
730+
Action<AdjustPurchaseVerificationResult> verificationResultCallback)
731+
{
732+
if (IsEditor())
733+
{
734+
return;
735+
}
736+
737+
#if UNITY_IOS
738+
Debug.Log("[Adjust]: Play Store purchase verification is only supported for Android platform.");
739+
#elif UNITY_ANDROID
740+
AdjustAndroid.VerifyAndTrackPlayStorePurchase(adjustEvent, verificationResultCallback);
741+
#else
742+
Debug.Log(errorMsgPlatform);
743+
#endif
744+
}
745+
718746
private static bool IsEditor()
719747
{
720748
#if UNITY_EDITOR

Assets/Adjust/Scripts/AdjustAndroid.cs

+64
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class AdjustAndroid
1919
private static SessionTrackingFailedListener onSessionTrackingFailedListener;
2020
private static SessionTrackingSucceededListener onSessionTrackingSucceededListener;
2121
private static VerificationResultListener onVerificationResultListener;
22+
private static VerificationResultListener onVerifyAndTrackListener;
2223
private static DeeplinkResolutionListener onDeeplinkResolvedListener;
2324

2425
public static void InitSdk(AdjustConfig adjustConfig)
@@ -578,6 +579,69 @@ public static void ProcessAndResolveDeeplink(string url, Action<string> resolved
578579
ajcAdjust.CallStatic("processAndResolveDeeplink", ajoUri, ajoCurrentActivity, onDeeplinkResolvedListener);
579580
}
580581

582+
public static void VerifyAndTrackPlayStorePurchase(
583+
AdjustEvent adjustEvent,
584+
Action<AdjustPurchaseVerificationResult> verificationInfoCallback)
585+
{
586+
AndroidJavaObject ajoAdjustEvent = new AndroidJavaObject("com.adjust.sdk.AdjustEvent", adjustEvent.EventToken);
587+
588+
// check if user has set revenue for the event
589+
if (adjustEvent.Revenue != null)
590+
{
591+
ajoAdjustEvent.Call("setRevenue", (double)adjustEvent.Revenue, adjustEvent.Currency);
592+
}
593+
594+
// check if user has added any callback parameters to the event
595+
if (adjustEvent.CallbackParameters != null)
596+
{
597+
for (int i = 0; i < adjustEvent.CallbackParameters.Count; i += 2)
598+
{
599+
string key = adjustEvent.CallbackParameters[i];
600+
string value = adjustEvent.CallbackParameters[i + 1];
601+
ajoAdjustEvent.Call("addCallbackParameter", key, value);
602+
}
603+
}
604+
605+
// check if user has added any partner parameters to the event
606+
if (adjustEvent.PartnerParameters != null)
607+
{
608+
for (int i = 0; i < adjustEvent.PartnerParameters.Count; i += 2)
609+
{
610+
string key = adjustEvent.PartnerParameters[i];
611+
string value = adjustEvent.PartnerParameters[i + 1];
612+
ajoAdjustEvent.Call("addPartnerParameter", key, value);
613+
}
614+
}
615+
616+
// check if user has set deduplication ID for the event
617+
if (adjustEvent.DeduplicationId != null)
618+
{
619+
ajoAdjustEvent.Call("setDeduplicationId", adjustEvent.DeduplicationId);
620+
}
621+
622+
// check if user has added callback ID to the event
623+
if (adjustEvent.CallbackId != null)
624+
{
625+
ajoAdjustEvent.Call("setCallbackId", adjustEvent.CallbackId);
626+
}
627+
628+
// check if user has added product ID to the event
629+
if (adjustEvent.ProductId != null)
630+
{
631+
ajoAdjustEvent.Call("setProductId", adjustEvent.ProductId);
632+
}
633+
634+
// check if user has added purchase token to the event
635+
if (adjustEvent.PurchaseToken != null)
636+
{
637+
ajoAdjustEvent.Call("setPurchaseToken", adjustEvent.PurchaseToken);
638+
}
639+
640+
onVerifyAndTrackListener = new VerificationResultListener(verificationInfoCallback);
641+
642+
ajcAdjust.CallStatic("verifyAndTrackPlayStorePurchase", ajoAdjustEvent, onVerifyAndTrackListener);
643+
}
644+
581645
// used for testing only
582646
public static void SetTestOptions(Dictionary<string, string> testOptions)
583647
{

Assets/Adjust/Scripts/AdjustiOS.cs

+56-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class AdjustiOS
2020
private static List<Action<string>> appSdkVersionGetterCallbacks;
2121
private static List<Action<int>> appAttCallbacks;
2222
private static Action<AdjustPurchaseVerificationResult> appPurchaseVerificationCallback;
23+
private static Action<AdjustPurchaseVerificationResult> appVerifyAndTrackCallback;
2324
private static Action<string> appResolvedDeeplinkCallback;
2425
private static Action<string> appSkanErrorCallback;
2526

@@ -238,6 +239,21 @@ private static extern void _AdjustVerifyAppStorePurchase(
238239
string receipt,
239240
AdjustDelegatePurchaseVerificationCallback callback);
240241

242+
private delegate void AdjustDelegateVerifyAndTrackCallback(string verificationResult);
243+
[DllImport("__Internal")]
244+
private static extern void _AdjustVerifyAndTrackAppStorePurchase(
245+
string eventToken,
246+
double revenue,
247+
string currency,
248+
string receipt,
249+
string productId,
250+
string transactionId,
251+
string callbackId,
252+
string deduplicationId,
253+
string jsonCallbackParameters,
254+
string jsonPartnerParameters,
255+
AdjustDelegatePurchaseVerificationCallback callback);
256+
241257
// public API
242258
public AdjustiOS() {}
243259

@@ -325,7 +341,7 @@ public static void TrackEvent(AdjustEvent adjustEvent)
325341
deduplicationId,
326342
stringJsonCallbackParameters,
327343
stringJsonPartnerParameters);
328-
}
344+
}
329345

330346
public static void Enable()
331347
{
@@ -587,6 +603,36 @@ public static void ProcessAndResolveDeeplink(string deeplink, Action<string> cal
587603
_AdjustProcessAndResolveDeeplink(deeplink, ResolvedDeeplinkCallbackMonoPInvoke);
588604
}
589605

606+
public static void VerifyAndTrackAppStorePurchase(
607+
AdjustEvent adjustEvent,
608+
Action<AdjustPurchaseVerificationResult> callback)
609+
{
610+
double revenue = AdjustUtils.ConvertDouble(adjustEvent.Revenue);
611+
string eventToken = adjustEvent.EventToken;
612+
string currency = adjustEvent.Currency;
613+
string receipt = adjustEvent.Receipt;
614+
string productId = adjustEvent.ProductId;
615+
string transactionId = adjustEvent.TransactionId;
616+
string callbackId = adjustEvent.CallbackId;
617+
string deduplicationId = adjustEvent.DeduplicationId;
618+
string stringJsonCallbackParameters = AdjustUtils.ConvertReadOnlyCollectionOfPairsToJson(adjustEvent.CallbackParameters);
619+
string stringJsonPartnerParameters = AdjustUtils.ConvertReadOnlyCollectionOfPairsToJson(adjustEvent.PartnerParameters);
620+
appVerifyAndTrackCallback = callback;
621+
622+
_AdjustVerifyAndTrackAppStorePurchase(
623+
eventToken,
624+
revenue,
625+
currency,
626+
receipt,
627+
productId,
628+
transactionId,
629+
callbackId,
630+
deduplicationId,
631+
stringJsonCallbackParameters,
632+
stringJsonPartnerParameters,
633+
VerifyAndTrackCallbackMonoPInvoke);
634+
}
635+
590636
// used for testing only (don't use this in your app)
591637
public static void SetTestOptions(Dictionary<string, string> testOptions)
592638
{
@@ -777,6 +823,15 @@ private static void PurchaseVerificationCallbackMonoPInvoke(string verificationR
777823
}
778824
}
779825

826+
[AOT.MonoPInvokeCallback(typeof(AdjustDelegateVerifyAndTrackCallback))]
827+
private static void VerifyAndTrackCallbackMonoPInvoke(string verificationResult) {
828+
if (appVerifyAndTrackCallback != null)
829+
{
830+
appVerifyAndTrackCallback(new AdjustPurchaseVerificationResult(verificationResult));
831+
appVerifyAndTrackCallback = null;
832+
}
833+
}
834+
780835
[AOT.MonoPInvokeCallback(typeof(AdjustDelegateResolvedDeeplinkCallback))]
781836
private static void ResolvedDeeplinkCallbackMonoPInvoke(string deeplink) {
782837
if (appResolvedDeeplinkCallback != null)

0 commit comments

Comments
 (0)