Skip to content

A plugin for Unity to use Bazaar in-app purchase in Android

Notifications You must be signed in to change notification settings

cafebazaar/UnityIAP

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 

Repository files navigation

Bazaar In-app Billing for Unity3d

In order to provide Iab for Unity3d, Bazaar used SOOMLA which is the first store creation platform for mobile games. SOOMLA helps you to easily create your in-game economy and storefront. You can check SOOMLA's github (here) for more information.

Getting Started

  1. Download Bazaar unitypackage file here and double-click on it. It'll import all the necessary files into your project.
  2. Drag the "StoreEvents" Prefab from ../Assets/Soomla/Prefabs into your scene. You should see it listed in the "Hierarchy" panel.
  3. On the menu bar click "Soomla -> Edit Settings" and change the values for "Custom Secret", "Public Key" and "Soom Sec":
    • Custom Secret - is an encryption secret you provide that will be used to secure your data.
    • Public Key - is the public key given to you from Bazaar. (iOS doesn't have a public key).
    • Soom Sec - is a special secret SOOMLA uses to increase your data protection.
      Choose both secrets wisely. You can't change them after you launch your game!
  4. Create your own implementation of IStoreAssets in order to describe your specific game's assets (example). Initialize StoreController with the class you just created:

    StoreController.Initialize(newYourStoreAssetsImplementation());

    Initialize StoreController ONLY ONCE when your application loads.

    Initialize StoreController in the "Start()" function of a 'MonoBehaviour' and NOT in the "Awake()" function. SOOMLA has its own 'MonoBehaviour' and it needs to be "Awakened" before you initialize.

  5. You'll need an event handler in order to be notified about in-app purchasing related events. refer to the Event Handling section for more information.

And that's it ! You have storage and in-app purchasing capabilities... ALL-IN-ONE.

Unity & Android

Starting IAB Service in background

If you have your own storefront implemented inside your game, it's recommended that you open the IAB Service in the background when the store opens and close it when the store is closed.

// Start Iab ServiceStoreController.StartIabServiceInBg();// Stop Iab ServiceStoreController.StopIabServiceInBg();

Don't forget to close the Iab Service when your store is closed. You don't have to do this at all, this is just an optimization.

What's next? In App Purchasing.

Soomla designed the new modelV3 to support 2 ways you can let your users purchase stuff in your game: PurchaseWithMarket and PurchaseWithVirtualItem.

PurchaseWithMarket is a PurchaseType that allows users to purchase a VirtualItem with Google Play or the App Store.
PurchaseWithVirtualItem is a PurchaseType that lets your users purchase a VirtualItem with a different VirtualItem. For Example: Buying 1 Sword with 100 Gems.

In order to define the way your various virtual items (Goods, Coins ...) are purchased, you'll need to create your implementation of IStoreAsset (the same one from step 4 in the "Getting Started" above).

Here is an example:

Lets say you have a VirtualCurrencyPack you call TEN_COINS_PACK and a VirtualCurrency you call COIN_CURRENCY:

VirtualCurrencyPackTEN_COINS_PACK=newVirtualCurrencyPack("10 Coins",// name"A pack of 10 coins",// description"10_coins",// item id10,// number of currencies in the packCOIN_CURRENCY_ITEM_ID,// the currency associated with this packnewPurchaseWithMarket("com.soomla.ten_coin_pack",1.99));

Now you can use StoreInventory to buy your new VirtualCurrencyPack:

StoreInventory.buyItem(TEN_COINS_PACK.ItemId);

And that's it! SOOMLA knows how to contact Bazaar and will redirect your users to their purchasing system to complete the transaction. Don't forget to subscribe to store events in order to get the notified of successful or failed purchases (see Event Handling).

Storage & Meta-Data

When you initialize StoreController, it automatically initializes two other classes: StoreInventory and StoreInfo:

  • StoreInventory is a convenience class to let you perform operations on VirtualCurrencies and VirtualGoods. Use it to fetch/change the balances of VirtualItems in your game (using their ItemIds!)
  • StoreInfo is where all meta data information about your specific game can be retrieved. It is initialized with your implementation of IStoreAssets and you can use it to retrieve information about your specific game.

ATTENTION: because SOOMLA is using JNI (Android) you should make as little calls as possible to StoreInfo. Look in the example project for the way SOOMLA created a sort of a cache to hold your game's information in order to not make too many calls to StoreInfo. SOOMLA updates this cache using an event handler. (see ExampleLocalStoreInfo and ExampleEventHandler).

Example Usages

  • Get VirtualCurrency with itemId "currency_coin":

    VirtualCurrencycoin=StoreInfo.GetVirtualCurrencyByItemId("currency_coin");
  • Give the user 10 pieces of a virtual currency with itemId "currency_coin":

    StoreInventory.GiveItem("currency_coin",10);
  • Take 10 virtual goods with itemId "green_hat":

    StoreInventory.TakeItem("green_hat",10);
  • Get the current balance of green hats (virtual goods with itemId "green_hat"):

    intgreenHatsBalance=StoreInventory.GetItemBalance("green_hat");

Event Handling

SOOMLA lets you subscribe to store events, get notified and implement your own application specific behaviour to those events.

Your behaviour is an addition to the default behaviour implemented by SOOMLA. You don't replace SOOMLA's behaviour.

The 'Events' class is where all event go through. To handle various events, just add your specific behaviour to the delegates in the Events class.

For example, if you want to 'listen' to a MerketPurchase event:

Events.OnMarketPurchase += onMarketPurchase;

public void onMarketPurchase(PurchasableVirtualItem pvi) { Debug.Log("Going to purchase an item with productId: " + pvi.ItemId); }

About

A plugin for Unity to use Bazaar in-app purchase in Android

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published