IAP Payment Revalidation / Server Validation in Unity - In most vernacular terms, hands-on instructions for your series.

Keywords: Unity Google iOS JSON

A previous article wrote about Unity's paid IAP access.
Later, there were some problems, and a large number of purchase orders appeared when the data were counted. But in fact, the amount of money in the account did not increase. @ $... ...... *@ & @ Preliminary judgment may exist some users through other channels to pay for the phenomenon, and then the following operations to verify whether the user's order is true and effective.
The principle is simple, that is, when the user purchases, we send the user's purchase information to Goole/APPLE for verification. Then it judges whether the order is valid by the information returned, and finally executes the corresponding logic in the game.
Here we are validated by the server, that is, we need to
1. We send information to our designated server (not directly to Goole/APPLE, which is safer to write online).
2. Specify the server side for validation logic and give the return value. This can be handed over to a small partner on the server side. I don't know much about the server.
3. We parse the return value and associate it with the client logic.
In theory, 1,3 is going to be done in Unity.

Below is the code section, you need to compare the IAP payment code to see [link address][ http://blog.csdn.net/qq_39860954/article/details/78880767]

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Purchasing;

// Placing the Purchaser class in the CompleteProject namespace allows it to interact with ScoreManager, 
// one of the existing Survival Shooter scripts.
// Deriving the Purchaser class from IStoreListener enables it to receive messages from Unity Purchasing.
//The script needs to be initialized before calling the purchase method
    public class Purchaser : MonoBehaviour, IStoreListener

    {  //Defining commodities
        private const string product_1 = "commodity ID1";
        private const string product_2 = "commodity ID2";
        private const string product_3 = "commodity ID3";

        private static IStoreController m_StoreController;          // The Unity Purchasing system.
        private static IExtensionProvider m_StoreExtensionProvider; // The store-specific Purchasing subsystems.

        public static string kProductIDConsumable = "consumable";
        public static string kProductIDNonConsumable = "nonconsumable";
        public static string kProductIDSubscription = "subscription";
        // Apple App Store-specific product identifier for the subscription product.
        private static string kProductNameAppleSubscription = "com.unity3d.subscription.new";
        // Google Play Store-specific product identifier subscription product.
        private static string kProductNameGooglePlaySubscription = "com.unity3d.subscription.original";
        void Start()
        {
            // If we haven't set up the Unity Purchasing reference
            if (m_StoreController == null)
            {
                // Begin to configure our connection to Purchasing
                InitializePurchasing();
            }
        }
        public void InitializePurchasing()
        {
            // If we have already connected to Purchasing ...
            if (IsInitialized())
            {
                // ... we are done here.
                return;
            }
            // Create a builder, first passing in a suite of Unity provided stores.
            var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());

            //Add the commodity ID and the commodity ID of the corresponding type definition
            builder.AddProduct(product_1, ProductType.Consumable, new IDs
            {
                {"commodity ID1", GooglePlay.Name }
            });
            builder.AddProduct(product_2, ProductType.Consumable, new IDs
            {
                {"commodity ID1", GooglePlay.Name }
            });
            builder.AddProduct(product_3, ProductType.Consumable, new IDs
            {
                {"commodity ID1", GooglePlay.Name }
            });


            // Kick off the remainder of the set-up with an asynchrounous call, passing the configuration 
            // and this class' instance. Expect a response either in OnInitialized or OnInitializeFailed.
            UnityPurchasing.Initialize(this, builder);
        }
        private bool IsInitialized()
        {
            // Only say we are initialized if both the Purchasing references are set.
            return m_StoreController != null && m_StoreExtensionProvider != null;
        }
        public void BuyConsumable()
        {
            // Buy the consumable product using its general identifier. Expect a response either 
            // through ProcessPurchase or OnPurchaseFailed asynchronously.
            BuyProductID(kProductIDConsumable);
        }
        public void BuyNonConsumable()
        {
            // Buy the non-consumable product using its general identifier. Expect a response either 
            // through ProcessPurchase or OnPurchaseFailed asynchronously.
            BuyProductID(kProductIDNonConsumable);
        }
        public void BuySubscription()
        {
            // Buy the subscription product using its the general identifier. Expect a response either 
            // through ProcessPurchase or OnPurchaseFailed asynchronously.
            // Notice how we use the general product identifier in spite of this ID being mapped to
            // custom store-specific identifiers above.
            BuyProductID(kProductIDSubscription);
        }

    //The Method of Purchasing Commodity Call
        public void BuyProductID(string productId)
        {
            // If Purchasing has been initialized ...
            if (IsInitialized())
            {
                // ... look up the Product reference with the general product identifier and the Purchasing 
                // system's products collection.
                Product product = m_StoreController.products.WithID(productId);

                // If the look up found a product for this device's store and that product is ready to be sold ... 
                if (product != null && product.availableToPurchase)
                {
                    Debug.Log(string.Format("Purchasing product asychronously: '{0}'", product.definition.id));
                    // ... buy the product. Expect a response either through ProcessPurchase or OnPurchaseFailed 
                    // asynchronously.
                    m_StoreController.InitiatePurchase(product);
                }
                // Otherwise ...
                else
                {
                    // ... report the product look-up failure situation  
                    Debug.Log("BuyProductID: FAIL. Not purchasing product, either is not found or is not available for purchase");
                }
            }
            // Otherwise ...
            else
            {
                // ... report the fact Purchasing has not succeeded initializing yet. Consider waiting longer or 
                // retrying initiailization.
                Debug.Log("BuyProductID FAIL. Not initialized.");
            }
        }


        // Restore purchases previously made by this customer. Some platforms automatically restore purchases, like Google. 
        // Apple currently requires explicit purchase restoration for IAP, conditionally displaying a password prompt.
        public void RestorePurchases()
        {
            // If Purchasing has not yet been set up ...
            if (!IsInitialized())
            {
                // ... report the situation and stop restoring. Consider either waiting longer, or retrying initialization.
                Debug.Log("RestorePurchases FAIL. Not initialized.");
                return;
            }

            // If we are running on an Apple device ... 
            if (Application.platform == RuntimePlatform.IPhonePlayer ||
                Application.platform == RuntimePlatform.OSXPlayer)
            {
                // ... begin restoring purchases
                Debug.Log("RestorePurchases started ...");

                // Fetch the Apple store-specific subsystem.
                var apple = m_StoreExtensionProvider.GetExtension<IAppleExtensions>();
                // Begin the asynchronous process of restoring purchases. Expect a confirmation response in 
                // the Action<bool> below, and ProcessPurchase if there are previously purchased products to restore.
                apple.RestoreTransactions((result) => {
                    // The first phase of restoration. If no more responses are received on ProcessPurchase then 
                    // no purchases are available to be restored.
                    Debug.Log("RestorePurchases continuing: " + result + ". If no further messages, no purchases available to restore.");
                });
            }
            // Otherwise ...
            else
            {
                // We are not running on an Apple device. No work is necessary to restore purchases.
                Debug.Log("RestorePurchases FAIL. Not supported on this platform. Current = " + Application.platform);
            }
        }
        //  
        // --- IStoreListener
        //
        public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
        {
            // Purchasing has succeeded initializing. Collect our Purchasing references.
            Debug.Log("OnInitialized: PASS");

            // Overall Purchasing system, configured with products for this application.
            m_StoreController = controller;
            // Store specific subsystem, for accessing device-specific store features.
            m_StoreExtensionProvider = extensions;
        }


        public void OnInitializeFailed(InitializationFailureReason error)
        {
            // Purchasing set-up has not succeeded. Check error for reason. Consider sharing this reason with the user.
            Debug.Log("OnInitializeFailed InitializationFailureReason:" + error);
        }
    //This part is the modification part, and the second verification is processed in this method.
    //Processing methods after purchasing different commodities correspond to defined commodities
        public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args)
        {
        //Get and parse the data you need to upload. Resolve to string type
        var wrapper = (Dictionary<string, object>) MiniJson.JsonDecode (args.purchasedProduct.receipt);
        if (null == wrapper) {
            return PurchaseProcessingResult.Complete;
        }

        // Corresponds to http://docs.unity3d.com/Manual/UnityIAPPurchaseReceipts.html
        var store = (string)wrapper ["Store"];
        //The payload below is the IOS data for validating commodity information. That is, the part we need to upload.
        var payload = (string)wrapper ["Payload"]; // For Apple this will be the base64 encoded ASN.1 receipt

#if UNITY_IPHONE
        StartCoroutine(PostRepict("http://www.xxxxxxxxxxxxxx/purchase/Verifytrade",args.purchasedProduct.definition.id,payload));

#endif


        //For GooglePlay payload contains more JSON
        if (Application.platform == RuntimePlatform.Android)
        {
            var gpDetails = (Dictionary<string, object>)MiniJson.JsonDecode(payload);
            var gpJson = (string)gpDetails["json"];
            var gpSig = (string)gpDetails["signature"];

    //The data of Google validating commodity information contained in gpJson needs to be parsed at the server side, and the corresponding key is "purchaseToken".
            StartCoroutine(PostRepict("http://www.xxxxxxxxxxxxx/purchase/Andverifytrade", args.purchasedProduct.definition.id, gpJson));
        }

            // A consumable product has been purchased by this user.

        // Return a flag indicating whether this product has completely been received, or if the application needs 
        // to be reminded of this purchase at next app launch. Use PurchaseProcessingResult.Pending when still 
        // saving purchased products to the cloud, and when that save is delayed. 
        return PurchaseProcessingResult.Complete;
        }
    //Unity to transfer information to the server method, www: server address, payID: commodity ID, validation of commodity data. Google needs to pass on the commodity ID (necessary) IOS (unnecessary).
        IEnumerator PostRepict(string www,string payID,string gpJson)

    {
        WWWForm form  = new WWWForm();  
        form.AddField("productId",payID);  
        form.AddField("purchaseToken",gpJson);  
        WWW getData = new WWW(www,form); 

        yield return getData;  

        if(getData.error!= null)  
        {  
            Debug.Log(getData.text);  
        }   
        //srcString returns information for the server.
        string srcString = getData.text;  

        try {
        //Parse the data returned by the server and judge according to the logic set by the server. For example, if the value of "code" in the returned data is 200, it is a real order, so continue the client logic when the decision is successful.
            JSONNode jsonNode = JSONNode.Parse(srcString);

            if(jsonNode["code"].AsInt==200)
            {
                //Judged as a real order, here I put the in-game purchasing logic in this position.
                  if (String.Equals(payID., product_1, StringComparison.Ordinal))
            {
                        //Logic of Success in Goods 1 Purchase

            } else if (String.Equals(payID, product_2, StringComparison.Ordinal)) {

                        //Logic of Success in Goods 2 Purchase     
            }
            else if (String.Equals(payID, product_3, StringComparison.Ordinal))
            {
                        //Logic of Success in Goods 3 Purchase
            }


            }else
            {
             Debug.Log(string.Format("ProcessPurchase: FAIL. Unrecognized product: '{0}'", args.purchasedProduct.definition.id));
            //Decided to be a false order
            }

        } catch(System.Exception e) {
            Debug.LogError ("Server Error:" + e.Message);
            //Other network issues

        }

    }

        public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason)
        {
            // A product purchase attempt did not succeed. Check failureReason for more detail. Consider sharing 
            // this reason with the user to guide their troubleshooting actions.
            Debug.Log(string.Format("OnPurchaseFailed: FAIL. Product: '{0}', PurchaseFailureReason: {1}", product.definition.storeSpecificId, failureReason));
        }
    }

The above code has been verified to be feasible, but there are still some areas that can be optimized.
1. Because the logic after payment is done by Start Coroutine, payment may be successful without real-time data updates.
2. Payment failure is not handled separately, such as the page showing payment failure.
3. Secondary validation is only a set of processing logic as part of the purchase. It has no effect on the situation that the game value is changed directly instead of purchasing falsely.

Posted by q1234ask on Tue, 14 May 2019 17:47:26 -0700