• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
Question by kendogar · May 24, 2016 at 01:54 PM · unity 5iaphashtable

Getting Unity IAP receipt data to validate with playfab (or somewhere else)

Hello there,

I'm kinda stuck at the moment.

I implemented the Unity IAP system and after making a purchase I receive the receipt.

Now I have to extract values from the receipt to validate the receipt itself, specifically the json/signature for validating the google receipt and the receipt data/purchase price for ios.

I realize the data is there, as http://docs.unity3d.com/Manual/UnityIAPPurchaseReceipts.html says so, but I currently don't know how to access it.

Can you help me with that problem?

Thanks in advance.

Comment
A-Zhdanov
fsg_andrew
tessellation
Meltdown
prathvipatil

People who like this

5 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by nicholasr · May 24, 2016 at 10:22 PM

@kendogar Sure.

There are a few use-cases: (1) validating the receipt client-side (locally, to block on-device consumption of product for fraudulent purchases) using our CrossPlatformValidator class, (2) validating the receipt server-side (for reporting, not for blocking), and (3) validating the receipt through other means.

(1) I recommend looking at this doc: http://docs.unity3d.com/Manual/UnityIAPValidatingReceipts.html Specifically, look at the "ProcessPurchase" code sample there. It gives you a boolean in your app with the fact of "valid or not".

(2) Unity Analytics will automatically receive Unity IAP transaction logs to generate reports, validating receipts for you server-side if you configure with Analytics your game's cryptographic public keys, showing you what money could be verified cryptographically. Use the Data Explorer in Unity Analytics: https://analytics.cloud.unity3d.com/ > "Data Explorer" > "Metrics and Custom Events" > change the drop-down to various Verified Revenue options to see the info!

(3) If you just want to parse out the receipt data for passing to PlayFab (et al), then I recommend also following the link's instructions of (1). This means adding the Google Play Public Key from your Play Store Developer Console and using the little Unity IAP addition to the Window menu, first.

(3.1) Or if you're feeling adventurous then crack the Unity Receipt field (product.receipt) as it's JSON-wrapped-JSON for GooglePlay, and JSON-wrapped-base64-wrapped-ASN.1 for Apple. MiniJSON is a JSON parser I've had luck with.

 var wrapper = (Dictionary<string, object>) MiniJson.JsonDecode (purchaseEventArgs.purchasedProduct.receipt);
 if (null == wrapper) {
     return;
 }

 // Corresponds to http://docs.unity3d.com/Manual/UnityIAPPurchaseReceipts.html
 var store = (string)wrapper ["Store"];
 var payload = (string)wrapper ["Payload"]; // For Apple this will be the base64 encoded ASN.1 receipt
 
 // For GooglePlay payload contains more JSON
 if (Application.platform == RuntimePlatform.Android) {
     var gpDetails = (Dictionary<string, object>)MiniJson.JsonDecode (payload);
     var gpJson = (string)details ["json"];
     var gpSig = (string)details ["signature"];
 }

Comment
A-Zhdanov
milox777
fsg_andrew
bekici
KarolRG
Nolex
Meltdown

People who like this

7 Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kendogar · May 25, 2016 at 05:26 AM 1
Share

Thank you nicholasr, your explanation really cleared things up for me!

Have a wonderful week.

avatar image piidz · Apr 26, 2017 at 12:28 PM 0
Share

I have followed your instruction with regards to MiniJson but I am getting an invalid cast exception with it.

could you provide a link which MiniJson you are using? as I noticed yours is MiniJson.JsonDecode as compared to mine MiniJSON.jsonDecode

 string receipt = e.purchasedProduct.receipt;
 var wrapper = (Dictionary<string, object>) MiniJSON.jsonDecode (receipt);
 
         if (wrapper == null) {
             throw new InvalidReceiptDataException ();
         }
 
         var store = (string) wrapper["Store"];
         var transactionID = (string) wrapper["TransactionID"];
         var payload = (string) wrapper["Payload"];
 
         var details = (Dictionary<string, object>) MiniJSON.jsonDecode (payload);
         var receiptJson = (string) details["json"];
         var signature = (string) details["signature"];
avatar image wu_zhen · Jun 06, 2017 at 09:07 AM 0
Share

how to get the original order nuber after purchas successful? There seems to be some wrong about the doc。 “ AppleReceipt receipt = new AppleValidator(AppleTangle.Data()).Validate(receiptData);” I can not find "AppleTangle.Data()", it seems not defined。

avatar image prathvipatil · Jul 24, 2022 at 12:04 PM 0
Share

The code is not working. Passed keys are not working. I'm getting an error the key is not found.

avatar image prathvipatil prathvipatil · Jul 24, 2022 at 05:59 PM 0
Share

found an easy solution

 char[] charsToTrim = { '\\', '"', ':', ',' };
             string data = getBetween(Product.receipt, "orderId", "packageName").Trim(charsToTrim);                                   
             Debug.Log(data + " data");
         
 
          string getBetween(string strSource,string strStart, string strEnd)
         {
             if (strSource.Contains(strStart) && strSource.Contains(strEnd))
             {
                 int Start, End;
                 Start = strSource.IndexOf(strStart, 0) + strStart.Length;
                 End = strSource.IndexOf(strEnd, Start);
                 return strSource.Substring(Start, End - Start);
             }
 
             return "";
         }
avatar image

Answer by GaMeRCOD · Aug 18, 2017 at 02:29 PM

@nicholasr Did you use the same approach for IOS receipt? As the IOS receipt has different parameters so how did you extract the ReceiptData, CurrencyCode and PurchasePrice from the IOS receipt to send to Playfab.

Comment
fsg_andrew

People who like this

1 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nicholasr · Sep 18, 2017 at 06:38 PM 0
Share

The ProcessPurchase(PurchaseEventArgs) callback has a Product instance in the purchasedProduct field, which contains some of these fields (currency, price). https://docs.unity3d.com/ScriptReference/Purchasing.Product.html

Then, using the IAppleConfiguation, it's possible to extract Apple's binary ASN.1 AppReceipt. https://docs.unity3d.com/Manual/UnityIAPiOSMAS.html

avatar image

Answer by faunaface · Nov 07, 2016 at 04:59 AM

Hi there,

This is all great, could you give more information on how does this work with Samsung Galaxy store? I am using Unity 5.4.1f1 with the latest version of IAP plugins that support Samsung galaxy IAP.

How do I get the receipt for a Samsung transaction? Should be we relying on the Google Playstore receipt validation instead?

Comment

People who like this

0 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nicholasr · Nov 17, 2016 at 12:58 AM 0
Share

Hi @faunaface -

Samsung provides receipt information suitable for Remote Validation.

Local Validation would require additional cryptographic data to be included in transactions from Samsung.

Still, I'll call what we receive a "receipt" for convenience's sake: a "purchaseId". We also receive a "paymentId" which is what we map to "transactionId". We pack the "purchaseId" into the Unity IAP receipt field as a trivial JSON string.

You may already know how to access the Unity IAP receipt field, from your earlier work. After successful purchase, in the app's ProcessPurchase(Purchasing.PurchaseEventArgs e) implementation, the receipt will be available in e.purchasedProduct.receipt. This string is packed up by Unity IAP to contain each App Store's various receipt fields. As suggested above, this string should look something like: {"purchaseId":"d215d9abcd17b12578a21c0ea7d8821747b64939732a3243b538d8bcae245590"}.

The Remote Validation server-to-server API for validating this receipt is described in the Samsung IAP Programming Guide "ProgrammingGuide_SamsungInAppPurchaseSDK_v4.0.0.pdf" document: http://developer.samsung.com/iap/guide E.g. call https://iap.samsungapps.com/iap/appsItemVerifyIAPReceipt.as?protocolVersion=2.0&purchaseID=d 215d9abcd17b12578a21c0ea7d8821747b64939732a3243b538d8bcae245590 and parse the response.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

89 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

IAPs problems after porting Google Play Store game to Amazon Appstore 0 Answers

[SOLVED] Unit IAP google play response to update result 1 Answer

Getting trouble in migrating the Unity IAP old version to new version of IAP in existing project 0 Answers

disable ads admob with iap 0 Answers

Unity Services errors when waking PC from sleep 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges