2

I am following a tutorial for implementing in-App purchases, https://www.youtube.com/watch?v=dwPFtwDJ7tc&t=1170s and one line of code is different to the tutorial when I try to write it.

The video says to write the line of code as:

SwiftyStoreKit.verifyReceipt(password: sharedSecret, completion: {(more code added here)})

However when I write it out the only options that come up involve another part, this being 'using:'

SwiftyStoreKit.verifyReceipt(using: 'ReceiptValidator', password: 'String?', completion: '(VerifyReceiptResult) -> Void')

If I just delete the 'using:' section an error occurs. I'm a fairly new developer so it would be great if someone could explain what this means, and how to fill this section out.

whutson
  • 43
  • 9

2 Answers2

3

First of all you need to create AppleReceiptValidator object and pass this objet to first paramete like shown below.

let validator = AppleReceiptValidator(service: .production)
SwiftyStoreKit.verifyReceipt(using: validator, password: "Your_shared_secret") { (result) in
    switch result {
        case .success(receipt: let receiptInfo): self.reactOn(info: receiptInfo)
        case .error(error: let receiptError): self.reactOn(error: receiptError)
        }
    completion()
}

I want to notice that in the Apple documentation is written that you should not send receipt to the Apple backend from your app. It should be done in this way:

  1. Your app sends receipt to your backend.
  2. Your backend sends receipt to Apple backend for validation.
  3. Your backend gets response from the apple.
  4. Your backend sends result back to your app is receipt valid or invalid.

In the Apple Documentation is written about password: "Only used for receipts that contain auto-renewable subscriptions. Your app’s shared secret (a hexadecimal string)."

More about it here.

Ramis
  • 13,985
  • 7
  • 81
  • 100
0

The full correct code for the current version of swiftyStoreKit to verify in-app subscription including verifying the receipt first per the official guide of swiftyStoreKit is:

let appleValidator = AppleReceiptValidator(service: .production, sharedSecret: "your-shared-secret")
SwiftyStoreKit.verifyReceipt(using: appleValidator) { result in
switch result {
case .success(let receipt):
    let productId = "com.musevisions.SwiftyStoreKit.Subscription"
    // Verify the purchase of a Subscription
    let purchaseResult = SwiftyStoreKit.verifySubscription(
        ofType: .autoRenewable, // or .nonRenewing (see below)
        productId: productId,
        inReceipt: receipt)

    switch purchaseResult {
    case .purchased(let expiryDate, let items):
        print("\(productId) is valid until \(expiryDate)\n\(items)\n")
    case .expired(let expiryDate, let items):
        print("\(productId) is expired since \(expiryDate)\n\(items)\n")
    case .notPurchased:
        print("The user has never purchased \(productId)")
    }

   case .error(let error):
      print("Receipt verification failed: \(error)")
   }
}

You can find it including other examples here: https://cocoapods.org/pods/SwiftyStoreKit#verify-purchase

Michael
  • 40
  • 4