Advertisement
Guest User

Untitled

a guest
Apr 28th, 2014
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.08 KB | None | 0 0
  1. // (c) Copyright HutongGames, LLC 2010-2013. All rights reserved.
  2.  
  3. using UnityEngine;
  4. using System.Collections.Generic;
  5. using Prime31;
  6.  
  7. namespace HutongGames.PlayMaker.Actions
  8. {
  9.     [ActionCategory("StoreKit")]
  10.     [Tooltip("Make a purchase")]
  11.     public class PurchaseProduct : FsmStateAction
  12.     {
  13.         [RequiredField]
  14.         [Tooltip("product ID from iTunesConnect. MUST match exactly what you have there!")]
  15.         public FsmString productIdentifier;
  16.         [RequiredField]
  17.         [Tooltip("How many of this product would you like to buy? Often 1. iOS Only")]
  18.         public FsmInt quantity;
  19.  
  20.         [RequiredField]
  21.         public FsmEvent purchaseFinished;
  22.         public FsmEvent purchaseFailed;
  23.         public FsmEvent purchaseCancelled;
  24.  
  25.         [Tooltip("If a error occurs where should it be stored")]
  26.         public FsmString errorString;
  27.  
  28.         [Tooltip("Is this purchase a consumable? Only required for Android")]
  29.         public FsmBool isAConsumable;
  30.  
  31.         [Tooltip("Is this purchase a subscription? Only Required for Android")]
  32.         public FsmBool isASubscription;
  33.  
  34.         public override void OnEnter()
  35.         {
  36. #if UNITY_IOS
  37.             StoreKitManager.purchaseSuccessfulEvent += purchaseSuccessfulEvent;
  38.             StoreKitManager.purchaseCancelledEvent += purchaseCancelledEvent;
  39.             StoreKitManager.purchaseFailedEvent += purchaseFailedEvent;
  40.  
  41.             var products = StoreKitHelper.Instance.GetProducts();
  42.             if( products != null && products.Count > 0 )
  43.             {
  44.                 StoreKitBinding.purchaseProduct( productIdentifier.Value, quantity.Value );
  45.             }
  46.             else {
  47.                 errorString.Value = "No Products found. Make sure you request the products first";
  48.                 Fsm.Event(purchaseFailed);
  49.                 Finish();
  50.             }
  51. #elif UNITY_ANDROID
  52.  
  53.             GoogleIABManager.purchaseSucceededEvent += purchaseSucceededEvent;
  54.             GoogleIABManager.purchaseFailedEvent += purchaseFailedEvent;
  55.             GoogleIABManager.consumePurchaseSucceededEvent += purchaseSucceededEvent;
  56.             GoogleIABManager.consumePurchaseFailedEvent += purchaseFailedEvent;
  57.  
  58.             if(isASubscription.Value) {
  59.                 GoogleIAB.purchaseProduct( productIdentifier.Value, "THIS02983049IS098093ONE08MIGHTY92834PROFESSIONAL09283049PAYLOAD)(*&@" );
  60.             }
  61.             else {
  62.                 GoogleIAB.purchaseProduct( productIdentifier.Value, "THIS02983049IS098093ONE08MIGHTY92834PROFESSIONAL09283049PAYLOAD)(*&@" );
  63.             }
  64.  
  65. #else
  66.             Finish();
  67. #endif
  68.         }
  69. #if UNITY_IOS
  70.         void purchaseFailedEvent( string error )
  71.         {
  72.             Debug.Log( "purchaseFailedEvent: " + error );
  73.             errorString.Value = error;
  74.             Fsm.Event(purchaseFailed);
  75.             Finish();
  76.         }
  77.        
  78.        
  79.         void purchaseCancelledEvent( string error )
  80.         {
  81.             Debug.Log( "purchaseCancelledEvent: " + error );
  82.             errorString.Value = error;
  83.             Fsm.Event(purchaseCancelled);
  84.             Finish();
  85.         }
  86.        
  87.        
  88.         void purchaseSuccessfulEvent( StoreKitTransaction transaction )
  89.         {
  90.             Debug.Log( "purchaseSuccessfulEvent: " + transaction );
  91.             Fsm.Event(purchaseFinished);
  92.  
  93.             Finish();
  94.         }
  95.  
  96.         public override void OnExit()
  97.         {
  98.             StoreKitManager.purchaseSuccessfulEvent -= purchaseSuccessfulEvent;
  99.             StoreKitManager.purchaseCancelledEvent -= purchaseCancelledEvent;
  100.             StoreKitManager.purchaseFailedEvent -= purchaseFailedEvent;
  101.         }
  102. #elif UNITY_ANDROID
  103.         void purchaseSucceededEvent( GooglePurchase purchase )
  104.         {
  105.             Debug.Log( "purchaseSuccessfulEvent: " + purchase );
  106.  
  107.             if(isAConsumable.Value) {
  108.                 GoogleIAB.consumeProduct( productIdentifier.Value );
  109.             }
  110.  
  111.             Fsm.Event(purchaseFinished);
  112.             Finish();
  113.         }
  114.        
  115.        
  116.         void purchaseFailedEvent( string error )
  117.         {
  118.             if (error.ToLower().Contains ("cancel")) {
  119.                 Debug.Log( "purchaseCancelledEvent: " + error );
  120.                 errorString.Value = error;
  121.                 Fsm.Event(purchaseCancelled);
  122.                 Finish();
  123.             } else {
  124.                 Debug.Log( "purchaseFailedEvent: " + error );
  125.                 errorString.Value = error;
  126.                 Fsm.Event(purchaseFailed);
  127.                 Finish();
  128.             }
  129.                            
  130.         }
  131.  
  132.         public override void OnExit()
  133.         {
  134.  
  135.             GoogleIABManager.purchaseSucceededEvent -= purchaseSucceededEvent;
  136.             GoogleIABManager.purchaseFailedEvent -= purchaseFailedEvent;
  137.             GoogleIABManager.consumePurchaseSucceededEvent -= purchaseSucceededEvent;
  138.             GoogleIABManager.consumePurchaseFailedEvent -= purchaseFailedEvent;
  139.         }
  140. #endif
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement