In App Purchases using Soomla. Quick and simple guide.

Recently I had to integrate in-game purchases in my game, and the question arose: «What Unity plugin should I use for that?» There are such things like OpenIAB, Prime31, Unibill and Soomla. We won’t pay attention to other less known plugins, because some of them are either not being updated at all, or have critical errors. If you want to know why Soomla was chosen and how to integratе it, keep reading.
So, there are a lot different plugins, as can be seen. Judging by the title of the article you can guess which system I eventually chose. But why?
 

OpenIAB

Pros:

  • Free
  •  Openness 
  •  Huge community 
  •  Simplicity
  •  Fairly good documentation
  •  Support for iOS and Android (Later it turned out to not be true)

Cons:

  •   There is almost no information about working with iOS
  •   Rare updates (last one was 4 months ago)

IMO:
I had previously worked with this system for Android, but I was disappointed when I decided to try to work on iOS. I had to abandon this system, because OpenIAB didn’t work correctly: the events were unresponsive, plus some issues with store initialization. Undoubtedly OpenIAB works flawlessly on Android. No wonder, even the source in C++ was originally written for Android only. 

Unibill

Pros:

  •   Support for multiple stores
  •   Excellent documentation
  •   Simple and easy to learn

Cons:

  •   Price ($175)
  •   There’s practically no community

IMO:

With Unibill everything was easy. As for me, the price of $175 is not little money, especially considering that there are cheaper analogs, so I skipped this one. If you have money and don’t want to spend a lot of time, this is your best bet!

Prime[31]

Pros:

  •   Excellent documentation
  •   Simple and easy to learn
  •   Supports multiple stores
  •   Big community
  •   The price of a plugin for one of the platforms is $70.

IMO:

prime[31] plugins are famed for their quality, but the price could be lower. $70 for each platform is not the best price. The advantages of this technology are its huge community and excellent documentation. In my case I needed a plugin for two platforms only, so the price would be lower than Unibill’s price. Despite these facts, I chose the following system.

Soomla

Pros:

  •   Excellent documentation
  •   Simple and easy to learn
  •   Supports major mobile platforms.
  •   Big community.
  •   Free of charge

 

Cons:

  •   Work through their personal account

IMO:

As for me, Soomla is a great option: it has all the advantages of plugins by prime[31], plus it’s completely free. At first, I was embarrassed that I must work through a personal account on their website, but in fact, I rarely use it, so I chose this option.

Integration into the project

At first we need to download the plugin. You can download it from AssetStore or the official website, there is no difference. Both sources are updated regularly.
After you import the plugin into your project, you need to set the editor settings.
 

After you have created a project in your personal Soomla account and got the GameKey, you must assign the key to the Soomla Secret variable. Without it nothing will work!
 

DebugNative and DebugUnity are optional. In Android Settings the package name is assigned automatically. Then you have to select the Android stores, you are using. I guess it is clear where to get the API Key. Receipt Validation is optional too. Now that we set the foundation, it is time to move on to the scripts. 
We need to create classes for our products, so that we’ll be able to buy them. For example:
Initialazation
 

using Soomla.Store;// Сonnect to Soomla
using Soomla;// Сonnect to Soomla

public class dataIAP : IStoreAssets  { // inherits from the IStoreAssets interface 

		public int GetVersion() { //initialization of the GetVersion function. It is important exactly what the function returns, if you check the info in the Soomla personal account 			return 0;
		}

		public VirtualCurrency[] GetCurrencies() { // Specify all of our currencies, that we sell for real money	
			return new VirtualCurrency[]{ITEM_OBJECT};
		}

		public VirtualCurrencyPack[] GetCurrencyPacks(){ // Specify all packs with our currency
			return new VirtualCurrencyPack[]{PACK_1_OBJECT, PACK_2_OBJECT};
		}

		public VirtualGood[] GetGoods() {
			return new VirtualGood[0]; // Virtual Goods
		}

		public VirtualCategory[] GetCategories() {
			return new VirtualCategory[0]; // Categories of virtual goods and real things
		}

		public const string ITEM_ID = "ITEM_ID";// specify any convenient ID
		public const string PACK_1 = "ID_PACK_1";// specify id, that we set in the stores (App Store, Google Play) for this pack
		public const string PACK_2= "ID_PACK_2";// ";// specify id, that we set in the stores (App Store, Google Play) for this pack

		public static VirtualCurrency ITEM_OBJECT = new VirtualCurrency(//create an object of virtual currency
			"ITEM_ID ",//Name									
			"ITEM_COIN", //Info about currency										
			ITEM_ID //id of virtual currency in the pack
			);

		public static VirtualCurrencyPack PACK_1_OBJECT = new VirtualCurrencyPack(//create object of pack of virtual currency
			"PACK_1",//pack name										
			"",//Description												
			PACK_1 ,// specify id of the pack
			100, //count of currencies in the pack
			ITEM_ID ,//id of currencies in the pack
			new PurchaseWithMarket(PACK_1 , 1.99) //first - id of the pack in a store, second - price(they can be different)
			);
		public static VirtualCurrencyPack PACK_2_OBJECT = new VirtualCurrencyPack(//Just like for the first pack
			"PACK_2",										
			"",												
			PACK_2,
			200, 
			ITEM_ID ,
			new PurchaseWithMarket(PACK_2, 5.99)
			);
	}

After reading the comments, you can understand the idea, but I missed 2 things: virtual goods and categories. They will be useful for those who want to monitor purchases made using both in-game currency and the currency sold through Soomla personal account.
So, we are all set with the in-game purchases, but how to pass the data to the plugin and make purchases?

 This is quite simple:
 Soomla initialization

using Soomla;
using Soomla.Store;

public class Test {
	
	public Test () {

		SoomlaStore.Initialize (new dataIAP ());//Pass when initializing our object data packs
        #if UNITY_ANDROID
		SoomlaStore.StartIabServiceInBg (); //MUST HAVE for Android, otherwise it won’t work
		#endif
	}

	public void PayPack_1()
	{
		SoomlaStore.BuyMarketItem (StoreInfo.CurrencyPacks [0].ItemId, "");// Buy the first pack
	}

	public void PayPack_2()
	{
		SoomlaStore.BuyMarketItem (StoreInfo.CurrencyPacks [1].ItemId, "");// Buy the second pack
	}

    public void Quit()
	{
		SoomlaStore.StopIabServiceInBg ();
	}
}

The initialization is very simple, but what about the main available events?

 

Events
 

using System;
using System.Collections.Generic;
using Soomla;
using Soomla.Store;

public class ExampleEventHandler {
	// To keep things simple I’ll use “real purchase” for “the purchase paid with real money”. Same thing with the “in-game purchase” term.
	public ExampleEventHandler () {
		//Called, when the good is purchased for real currency						StoreEvents.OnMarketPurchase += onMarketPurchase;
		// Called, when the good is purchased for in-game currency					StoreEvents.OnItemPurchased += onItemPurchased;
		// Called, if billing is supported
		StoreEvents.OnBillingSupported += onBillingSupported;
		// Called, if billing is not supported
		StoreEvents.OnBillingNotSupported += onBillingNotSupported;
		// Called, when started a real purchase.
		StoreEvents.OnMarketPurchaseStarted += onMarketPurchaseStarted;
		// Called, when started an in-game purchase.					StoreEvents.OnItemPurchaseStarted += onItemPurchaseStarted;
		// Called, when the real purchase was canceled	
		StoreEvents.OnMarketPurchaseCancelled += onMarketPurchaseCancelled;
		// Called, when the store is initialized
		StoreEvents.OnSoomlaStoreInitialized += onSoomlaStoreInitialized;
		// Called in case of internal Soomla error
		StoreEvents.OnUnexpectedStoreError += onUnexpectedStoreError;

		//Only for Android
#if UNITY_ANDROID && !UNITY_EDITOR
		StoreEvents.OnIabServiceStarted += onIabServiceStarted;
		StoreEvents.OnIabServiceStopped += onIabServiceStopped;
#endif
	}
	public void onUnexpectedStoreError(int errorCode) {
		SoomlaUtils.LogError ("ExampleEventHandler", "error with code: " + errorCode);
	}
	public void onMarketPurchase(PurchasableVirtualItem pvi, string payload, Dictionary<string, string> extra) {

	}
	public void onItemPurchased(PurchasableVirtualItem pvi, string payload) {

	}
	public void onBillingSupported() {

	}
	public void onBillingNotSupported() {

	}
	public void onMarketPurchaseStarted(PurchasableVirtualItem pvi) {

	}
	public void onItemPurchaseStarted(PurchasableVirtualItem pvi) {

	}
	public void onMarketPurchaseCancelled(PurchasableVirtualItem pvi) {

	}
	public void onCurrencyBalanceChanged(VirtualCurrency virtualCurrency, int balance, int amountAdded) {

	}
	public void onSoomlaStoreInitialized() {
		
	}

#if UNITY_ANDROID && !UNITY_EDITOR
	public void onIabServiceStarted() {

	}
	public void onIabServiceStopped() {

	}
#endif
}

That's all, the system is completely ready to rock!

Let me tell you something about your personal Soomla account. There you can monitor your users transactions paid with both real money and in-game currency. To simplify the monitoring process we use goods and categories, but you need Soomla Highway to work with this thing. This article is not about that.

Now a little bit about testing. Purchasing system testing on Android is easy: you upload a build, say, for beta-testing, then test it. In case of iOS, you must create a sandbox account first. 
 

 

All data posted on the site represents accessible information that can be browsed and downloaded for free from the web.

http://habrahabr.ru/post/263899/

 

User replies

No replies yet