BranchMetrics / ios-branch-deep-linking-attribution

The Branch iOS SDK for deep linking and attribution. Branch helps mobile apps grow with deep links / deeplinks that power paid acquisition and re-engagement campaigns, referral programs, content sharing, deep linked emails, smart banners, custom user onboarding, and more.
https://help.branch.io/developers-hub/docs/ios-sdk-overview
MIT License
731 stars 228 forks source link

Double Purchase Event with ApplePay Fix #1402

Closed NidhiDixit09 closed 2 months ago

NidhiDixit09 commented 3 months ago

Reference

INTENG-20410 - [Multiple] Purchase event being triggered twice in iOS https://branch.atlassian.net/browse/INTENG-20410

Summary

In Event queue, dequeue function is replaced with - (void)remove:(BNCServerRequest *)request function. This fix will, instead of removing first event in the queue, will remove current event from the queue.

Detailed Description

When In-App-Purchase sheet or Apple Pay sheet appears branch goes to inactive state. After payment is done, logEvent API is called inside the Apple/Stripe payment API callback. And then sheet is closed and app comes back to active state again. This creates two events -

* Standard Purchase Event
* Open Event

Following is order of events happened inside SDK:

Fix: Instead of removing first event in the queue, current event (whose processing finished) should be removed.

Type Of Change

Testing Instructions

Use following sample app for testing. https://github.com/BranchMetrics/InAppPurchaseAPITest

This app uses sandbox envirnoment for testing.

Validate Following :

After you close Apple Pay Purchase Sheet,

Without Fix --> You you will see double purchase events in logs and No open event. With Fix --> You you will see one purchase event and one open event in logs.

cc @BranchMetrics/saas-sdk-devs for visibility.

nsingh-branch commented 2 months ago

I was able to repo the issue in the testbed by using the "In-App Purchase Event" button and changing the updatedTransactions delegate method to this. It doesn't reproduce 100% of the time, but I am frequently able to see the logEvent callback being hit twice and two identical Purchase events going out.

case SKPaymentTransactionStatePurchased: {
    NSLog(@"Transaction succesfully purchased");

    BranchEvent *event  = [BranchEvent standardEvent:BranchStandardEventPurchase];

    [event logEventWithCompletion:^(BOOL success, NSError * _Nullable error) {
        [[SKPaymentQueue defaultQueue] finishTransaction:(SKPaymentTransaction *)transaction];
        NSLog(@"Logged Test Event!");
    }];

    break;
}

With fix in this PR, I successfully see only one proper Purchase event going out each time now.