Advertisement
Fulke

Prime31 IAB

May 28th, 2014
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.87 KB | None | 0 0
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using Prime31;
  6.  
  7. public class StoreController : MonoBehaviour {
  8.     void OnEnable()
  9.     {
  10.         // Listen to all events for illustration purposes
  11.         GoogleIABManager.billingSupportedEvent += billingSupportedEvent;
  12.         GoogleIABManager.billingNotSupportedEvent += billingNotSupportedEvent;
  13.         GoogleIABManager.queryInventorySucceededEvent += queryInventorySucceededEvent;
  14.         GoogleIABManager.queryInventoryFailedEvent += queryInventoryFailedEvent;
  15.         GoogleIABManager.purchaseCompleteAwaitingVerificationEvent += purchaseCompleteAwaitingVerificationEvent;
  16.         GoogleIABManager.purchaseSucceededEvent += purchaseSucceededEvent;
  17.         GoogleIABManager.purchaseFailedEvent += purchaseFailedEvent;
  18.         GoogleIABManager.consumePurchaseSucceededEvent += consumePurchaseSucceededEvent;
  19.         GoogleIABManager.consumePurchaseFailedEvent += consumePurchaseFailedEvent;
  20.     }
  21.  
  22.     void OnDisable()
  23.     {
  24.         // Remove all event handlers
  25.         GoogleIABManager.billingSupportedEvent -= billingSupportedEvent;
  26.         GoogleIABManager.billingNotSupportedEvent -= billingNotSupportedEvent;
  27.         GoogleIABManager.queryInventorySucceededEvent -= queryInventorySucceededEvent;
  28.         GoogleIABManager.queryInventoryFailedEvent -= queryInventoryFailedEvent;
  29.         GoogleIABManager.purchaseCompleteAwaitingVerificationEvent += purchaseCompleteAwaitingVerificationEvent;
  30.         GoogleIABManager.purchaseSucceededEvent -= purchaseSucceededEvent;
  31.         GoogleIABManager.purchaseFailedEvent -= purchaseFailedEvent;
  32.         GoogleIABManager.consumePurchaseSucceededEvent -= consumePurchaseSucceededEvent;
  33.         GoogleIABManager.consumePurchaseFailedEvent -= consumePurchaseFailedEvent;
  34.     }
  35.    
  36.     public void init(){
  37.         var key = "<my key>";
  38.         GoogleIAB.init( key );
  39.     }
  40.  
  41.     public void queryInventory(){
  42.         // enter all the available skus from the Play Developer Console in this array so that item information can be fetched for them
  43.         var skus = new string[] {
  44.             "leap_issue_0",
  45.             "leap_issue_1"
  46.         };
  47.         GoogleIAB.queryInventory( skus );
  48.     }
  49.  
  50.     public bool areSubscriptionsSupported(){
  51.         return GoogleIAB.areSubscriptionsSupported();
  52.     }
  53.  
  54.     bool billingSupported;
  55.     public bool isBillingSupported(){
  56.         return billingSupported;
  57.     }
  58.  
  59.     public void purchaseProduct(string productID){
  60.         GoogleIAB.purchaseProduct(productID);
  61.     }
  62.  
  63.     public void consumeProduct(string productID){
  64.         GoogleIAB.consumeProduct (productID);
  65.     }
  66.    
  67.     //Android IAB Events
  68.     void billingSupportedEvent(){
  69.         Debug.Log( "billingSupportedEvent" );
  70.         billingSupported = true;
  71.     }
  72.  
  73.     void billingNotSupportedEvent(string error){
  74.         Debug.Log( "billingNotSupportedEvent: " + error );
  75.         billingSupported = false;
  76.     }
  77.  
  78.     public List<string> skuNames;
  79.     public List<string> skuPrices;
  80.     public List<string> skuDescriptions;
  81.     void queryInventorySucceededEvent(List<GooglePurchase> purchases, List<GoogleSkuInfo> skus){
  82.         //Clear sku stuff incase we queryInventory more than once
  83.         skuNames.Clear ();
  84.         skuPrices.Clear ();
  85.         skuDescriptions.Clear ();
  86.        
  87.         //Store sku names for purchases and prices and descriptions so it can be added to labels etc
  88.         for(int i = 0; i < skus.Count; i++){
  89.             skuNames.Add(skus[i].productId);
  90.             skuPrices.Add(skus[i].price);
  91.             skuDescriptions.Add(skus[i].description);
  92.         }
  93.        
  94.         //Find previous purchases and set them in playerprefs
  95.         for (int i = 0; i < purchases.Count; i++) {
  96.             PlayerPrefs.SetString(purchases[i].productId, "Purchased");
  97.             Debug.Log(PlayerPrefs.GetString(purchases[i].productId));
  98.         }  
  99.        
  100.         Debug.Log( string.Format( "queryInventorySucceededEvent. total purchases: {0}, total skus: {1}", purchases.Count, skus.Count ) );
  101.        
  102.         //Debug purchases and skus
  103.         Debug.Log ("purchases"+ "\n");
  104.         for (int i = 0; i < purchases.Count; i++) {
  105.             Debug.Log("Purchase ID: " + purchases[i].productId + "\n" +
  106.                       "Purchase State: " + purchases[i].purchaseState + "\n");
  107.         }
  108.         Debug.Log ("skus "+ "\n");
  109.         for (int i = 0; i < skus.Count; i++) {
  110.             Debug.Log("Sku Name: " + skuNames[i] + "\n" +
  111.                       "Sku Description: " + skuDescriptions[i] + "\n" +
  112.                       "Sku Price: " + skuPrices[i] + "\n");
  113.         }
  114.     }
  115.  
  116.     void queryInventoryFailedEvent(string error){
  117.         Debug.Log( "queryInventoryFailedEvent: " + error );
  118.     }
  119.  
  120.     void purchaseCompleteAwaitingVerificationEvent(string purchaseData, string signature){
  121.         Debug.Log( "purchaseCompleteAwaitingVerificationEvent. purchaseData: " + purchaseData + ", signature: " + signature );
  122.     }
  123.  
  124.     void purchaseSucceededEvent(GooglePurchase purchase){
  125.         Debug.Log( "purchaseSucceededEvent: " + purchase );
  126.     }
  127.    
  128.     void purchaseFailedEvent(string error){
  129.         Debug.Log( "purchaseFailedEvent: " + error );
  130.     }
  131.  
  132.     void consumePurchaseSucceededEvent(GooglePurchase purchase){
  133.         Debug.Log( "consumePurchaseSucceededEvent: " + purchase );
  134.     }
  135.  
  136.     void consumePurchaseFailedEvent(string error){
  137.         Debug.Log( "consumePurchaseFailedEvent: " + error );
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement