Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Receipt for purchaseProduct() #91

Closed
San-Jeevan opened this issue Oct 10, 2016 · 15 comments
Closed

Receipt for purchaseProduct() #91

San-Jeevan opened this issue Oct 10, 2016 · 15 comments
Labels
answered Questions which have accepted answers. type: question

Comments

@San-Jeevan
Copy link

Why does it not return a receipt when you perform SwiftyStoreKit.purchaseProduct().

All I get back is productID which is useless if you are going to perform validation on a remote server (and not verification on client);

@mingyeow
Copy link

@San-Jeevan - did you ever figure this out?

@mingyeow
Copy link

i have the same problem as well

@San-Jeevan
Copy link
Author

No but I found a workaround that works nicely.
When purchase is success, I call on this method:

   func triggerReceiptRefresh(){
        SwiftyStoreKit.verifyReceipt() { result in
            switch result {
            case .Success(let receipt):
                // Verify the purchase of Consumable or NonConsumable
                let purchaseResult = SwiftyStoreKit.verifyPurchase(
                    productId: "com.xxx.blblablab",
                    inReceipt: receipt
                );
                //RECEIPT IS THE RECEIPT OF THE LATEST PURCHASE!
                switch purchaseResult {
                case .Purchased(let expiresDate):
                    print("Product is purchased.")
                case .NotPurchased:
                    print("The user has never purchased this product")
                }
            case .Error(let error):
                print("Receipt verification failed: \(error)")
            }
        }
    }

@francisjervis
Copy link

I figured out the same solution, however I am unable to access the transaction ID or other values from the receipt here - did anyone manage to do that?

@San-Jeevan
Copy link
Author

@francisjervis the receipt is a json object. in_app is a property of the receipt. in_app.transaction_id is what you are looking for.

@francisjervis
Copy link

francisjervis commented Oct 16, 2016

@San-Jeevan Thank you for your reply! However, when I try to use
let r = receipt.in_app.transaction_id
in my code, I get a "value of type ReceiptInfo has no member 'in_app" error - I looked at the SSK code and it looks like this property may not be exposed. Were you able to access this property in the callback from VerifyReceipt?

@San-Jeevan
Copy link
Author

@francisjervis yes.
I think in_app is an array.

does this work?
let r = receipt.in_app[0].transaction_id

@francisjervis
Copy link

Thank you once again! Unfortunately no, it does not - the following code based on #10 does, though:

                if let receiptInfo: NSArray = receipt["latest_receipt_info"] as? NSArray {
                    let lastReceipt = receiptInfo.lastObject as! NSDictionary
                    let r = lastReceipt["original_transaction_id"] as! String
                    print (r)
                }
                else {
                    print("error")
                }

@bizz84
Copy link
Owner

bizz84 commented Oct 19, 2016

@francisjervis That is correct.
So if you want to purchase a product and verify its receipt you can do this:

SwiftyStoreKit.purchaseProduct("your.product.id") { result in
  if case .success(let productId) = result {
     SwiftyStoreKit.verifyReceipt(password: "your-shared-secret") { result in
         // receipt is a [String: AnyObject]
         // check receipt["latest_receipt_info"] and lastReceipt["original_transaction_id"] as above
         // optionally validate purchase (example for a non-consumable purchase)
         let purchaseResult = SwiftyStoreKit.verifyPurchase(
                        productId: productId,
                        inReceipt: receipt)
         // purchaseResult is either .purchased or .notPurchased
     }
  }

@bizz84 bizz84 added type: question answered Questions which have accepted answers. labels Oct 19, 2016
@bizz84
Copy link
Owner

bizz84 commented Oct 19, 2016

Also note that the receipt does not contain only the latest receipt.
In other words, if you print receipt["receipt"] you'll see the info for all purchases completed so far.

@San-Jeevan
Copy link
Author

Id like to ask for this issue to remain open a little longer because I just got a mail from a customer from Russian who used the in app purchase and i cannot see anything in my server log files!

There is something dodgy going on, im gonna test a little more.

@San-Jeevan
Copy link
Author

Well I have received more in-app purchases and the one case I was talking about appears to be a russian scam attempt. (claiming he bought, but didnt receive the purchased item, turns out it was fake receipt)

@San-Jeevan
Copy link
Author

just wanted to give an update.

I am receiving more complaints from people that they havent been credited for the purchase.
I check my server logs and I havent received anything but itunes store says I did get purchase.

this library is too unstable if you are going to use a 3rd party server to verify the receipt.
unreliable after refreshreceipt and on purchase.

sucks really.
I have yet to decide if I am gonna use that other swift library for store or implement a layer of additional check on top of this library.

@aehlke
Copy link

aehlke commented Jan 14, 2018

@San-Jeevan any progress updates on this?

@zsong
Copy link

zsong commented Jun 19, 2023

Getting this error

Receipt verification failed: receiptInvalid(receipt: ["status": 21002], status: SwiftyStoreKit.ReceiptStatus.malformedOrMissingData)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
answered Questions which have accepted answers. type: question
Projects
None yet
Development

No branches or pull requests

6 participants