How to handle Product.PurchaseOption in StoreKit 2?

Trying add a set of Product.PurchaseOption when calling purchase function in StoreKit 2 on iOS 15. My expectation was for a successful transaction coming back from StoreKit would still include these purchase options allowing me to capture the options on delayed transactions (e.g. parental approval).

In my particular scenario, I'd like to pass a custom string parameter to the purchase function for a non-renewable subscription:

let product: Product = ... // non-renewable subscription

// setup purchase options
var purchaseOptions = Set<Product.PurchaseOption>()
let myCustomStringOption = Product.PurchaseOption.custom(key: "myCustomString", value: "ABC4711")
purchaseOptions.insert(myCustomStringOption)

// call product purchase w/ options
let result = try await product.purchase(options: purchaseOptions)

// evaluate result
switch result {
   case .success(let verificationResult):
      // purchaseOptions not available as part of the purchase result
      // let purchaseOptions = verificationResult...?

      await transaction.finish()
   case .userCancelled, .pending:
      break
   default:
      break
}

However, the purchaseOption "never makes it to the other side" of the purchase process.

Any suggestions on how to obtain the purchase options after a successful and verified purchase?

Thanks!

Replies

PurchaseOption is for sending parameters to the App Store for a purchase. Those values are not necessarily returned in the Transaction for the purchase. That said, one option you may find useful is .appAccountToken(_:). StoreKit 2 provides a way to add a UUID value to a purchase, to correlate an account in your system to the purchase. This value will persist in the appAccountToken property of the Transaction.

  • Ok, I tried that. And indeed, that would've been the expected behaviour for custom options as well. After all, what's the point of having custom options, if I can't access them once a transaction has successfully completed?

    However, appAccountToken does not persist within a test environment using a local StoreKit configuration file in Xcode. Does this only persist when connecting to the App Store server? 

  • No, the appAccountToken will persist in transactions in all environments, including Xcode.

    If you continue to have issues with appAccountToken, please file feedback with the Feedback Assistant and follow up to this thread with the ID. It would be great if you could include more details to help us understand the problem. Attaching a sample project that reproduces the problem would be very useful.

  • The scenario in which I'm seeing appAccountToken go missing is when you resubscribe from Settings>App Store>Sandbox Account>Manage rather than from the app itself. I'm not clear why the (cancelled + expired) Subscription even keeps showing up there at all. Is this even possible in Production? (Also, "Clear Purchase History" on the sandbox testing account doesn't remove the cancelled + expired sub from this page - at least not yet, in my testing.)