Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storekit Appstore purchase promotion

this code is what is given on the apple developer website for when a user clicks on the promotional app-store product and it tells to check to see if can complete the transaction? how do I go about checking that? because then I have to cater if the transaction has failed or deferred and can't seem to figure out how to do that.

//MARK: - SKPaymentTransactionObserver

func paymentQueue(_ queue: SKPaymentQueue, shouldAddStorePayment payment: SKPayment,
    forProduct product: SKProduct) -> Bool {
      // Check to see if you can complete the transaction.
      // Return true if you can.
 return true
}

There is also the next scenarios I have to cater for which I find to be the same scenario as checking if the transaction can be completed

func paymentQueue(_ queue: SKPaymentQueue, shouldAddStorePayment payment: SKPayment,
    forProduct product: SKProduct) -> Bool {

       // ... Add code here to check if your app must defer the transaction.
     let shouldDeferPayment = ...

     // If you must defer until onboarding is completed, then save the payment and return false.

     if shouldDeferPayment {
        self.savedPayment = payment
        return false
     }

    // ... Add code here to check if your app must cancel the transaction.
    let shouldCancelPayment = ...
    // If you must cancel the transaction, then return false:
    if shouldCancelPayment {
       return false
    }
}

      // (If you canceled the transaction, provide feedback to the user.)

     // Continuing a previously deferred payment
     SKPaymentQueue.default().add(savedPayment)

  )

How do I check to see if the payment failed or needs to be deferred or can be completed as it says in both the code parts?

like image 472
Astrum Avatar asked Mar 29 '18 07:03

Astrum


1 Answers

Whether the transaction needs to be deferred or not depends entirely on your app and any requirements your app may have.

As an example, say your app required the user to set up an account before they can purchase a subscription.

If the user begins the purchase in the App Store, the shouldAddStorePayment method will be called after your app is launched to complete the purchase.

At this point you may detect that the user has not set up an account in your app (indeed, the app could have been installed as a result of them tapping the promoted IAP). In this case you would return false fromshouldAddStorePayment because your app is not in a position to be able to complete the purchase.

Your app would then continue with its normal on-boarding process which gets the user to establish their account.

Once the account is established you want to complete the purchase; this is both a good user experience and ensures that you don't miss a sale.

This is where the other sample code in your question comes in; it shows how you can save the payment and initiate the purchase at a later stage.

In summary, when shouldAddStorePayment is called:

  1. Determine if there is some reason that you cannot complete the purchase now
  2. If there is, return false, otherwise return true
  3. If you return false, save the purchase details so that you can initiate the purchase at a later stage when whatever was preventing the purchase has been resolved.
like image 143
Paulw11 Avatar answered Oct 20 '22 00:10

Paulw11