![]() |
CotC C# SDK for Unity
v1.4.0.1
Making social games is easy !
|
In-app purchase functionality is provided with the in-app purchase package, which may be downloaded separately. The API reference provided by the package is available here.
CotC offer services that make it easier to provide in-app purchases within your game, while maintaining a familiar and reassuring user experience on each platform. That means that the purchases are made through the AppStore on iOS or Google Play on Android for instance.
This also means that you need to configure the products to be sold within your application using the corresponding backend for each platform. Typically, if you are selling your application to iOS and Android devices, you will need to have an AppStore as well as a Google Play Developer membership. This configuration needs to be partially replicated on Clan of the Cloud servers in order for it to present a catalog to the player and perform security checks on purchases. The backoffice is used for this job as described below.
As a very generic overview, an in-app purchase may involve the following processes:
One important thing to note is that purchases are consumed on the device in order to allow buying them again later. Meaning that track about them may be difficult to recover. And while a purchase is related to an iTunes account, the recorded purchase is associated with a Clan of the Cloud account. It means that if an user creates a new account on Clan of the Cloud, even though he is still using the same iTunes account, his purchase history will start out blank.
Products must be configured on iTunes. First, connect with your user name on iTunes Connect.
If you have not configured your app yet, click on the plus sign in the top-left corner and choose New iOS app. Follow the instructions; you may need to register an additional bundle identifier in the Developer Center. What you choose here is not relevant in regard to your integration with CotC. It will be used when distributing your app on the AppStore.
Advice: even if you are eager to test your integration, we recommend you to fill this information thoroughly. Some of it might be difficult to change later on.
Click on the create button. You will be redirected to the interface allowing to configure your app. There, you need to fill general information about your application. In general, we recommend you to fill as much information as possible. But in any case, you need to fill enough information so that the circular sign besides the application logo becomes yellow, indicating "Ready for submission".
Then open the "In-app Purchases" tab. Click on create new, select "consumable" to create a new product. Add enough information (including a screenshot and a default translation) so that the indicator becomes yellow (ready to submit). What is relevant here is the Product identifier (the reference name is just for yourself). This is the information that you will report to the Clan of the Cloud backoffice in order for the product to be purchasable with the SDK.
Then, open the Clan of the Cloud backoffice and under Games -> Store, create a new product. Fill out at least the Product ID on the AppStore field with the product identifier as noted before.
Now you should be ready to test. For that you need to create test users (sandbox users) as described here (external link). On the target test device, you need to sign out from the AppStore (from the system settings). Do not sign in with your test account from this screen, as you will be prompted to input your credit card information, which you do not want. Launch your app and when prompted to sign in, do so with your test account. The purchase should work. If not, please verify all settings (yellow lights for both the product and app state in iTunes Connect, valid appStoreId
on the Clan of the Cloud backoffice, properly signed app, make sure that the account appears under Users and roles, Sandbox testers and that the app and product are marked as available in the country of your test user).
Note: while fetching the list of products does work on the simulator, actually purchasing any product will fail due to internal encryption processes. You need to use a real device to test in-app purchases, even in sandbox.
Note on environments: Apple offer two separated environments to perform in-app purchases, the sandbox and production environment. Clan of the Cloud sandbox environment is configured to work with the Apple sandbox environment, while the Clan of the Cloud production environment is configured to work with the Apple production environment. Therefore, in order to perform test purchases, you need to use our sandbox environment as well.
If you want to test quickly on Android, you do not need to register your application as instructed below. This is only necessary to perform real life tests, and we highly recommend you register your application properly before you start doing extensive tests on your application. Therefore, to get started quickly you may simply use the product name android.test.purchased
when creating your product in the Clan of the Cloud backoffice. This is a reserved name for a product whose purchase always succeeds regardless of the application state or the credit card used. You do not need to have an account on the Google Play Developer Console or pay a membership either.
You must input information about the products to be sold in the Google Play Developer Console. Follow the URL below and connect using your Google account. Note that you need to pay a membership in order to access the Developer Console.
You need to prepare your application for purchase. First you need to input the list of products, which is described in Add Your Application to the Developer Console (external link). You do not need follow the later steps in this document (adding the AIDL and such).
Then comes the difficult part. You need to prepare your application so that the Google Play Store will list your products, and this can sometimes be a pain to get working unless you choose to publish your application. Also, we recommend starting to prepare your app for distribution long ahead of your plans to start testing, as some simple configuration may take hours until they propagate through Google servers. The steps that should be necessary are:
There are a few basic steps that you need to take in your applications in order to take advantage of in-app purchases. These are described below.
On iOS and Mac OS X, you only need to verify that your binary is linked against the StoreKit framework. This can be done from the project options, selecting your target, and under Build phases, Link binary with libraries, click the plus button and add StoreKit.framework
.
On Android, you do not need anything special. The supplied plugin in AAR form will automatically modify your manifest as required.
The InappPurchaseSampleScene scene and associated script, bundled with the plugin, are a very good start. The integration requires multiple steps, but they ensure a maximum of flexibility and security. The CotcSdk.GamerStore object is accessed through a logged in CotcSdk.Gamer and provide CotC backend related functions. The other is the CotcSdk.InappPurchase.CotcInappPurchaseGameObject which exposes functions that interact with the platform dependent store. You will need to mix the two in order to perform a purchase. Typically, we have:
Prior to starting any purchase, the first thing that a game should do is to query the products available for sale. This can be used to show an UI to the player indicating what he is able to purchase and possibly the price. Querying product information can be done as in the snippet below.
The CotcSdk.InappPurchase.ProductInfo object contains a lot of information about the product to be purchased, regardless of the current platform. This is what should be used to present a store front to the user.
The first 30 products are returned by default, and pagination may be used as described under CotcSdk.PagedList.
With FetchProductInfo
, the product information for each product configured on the Clan of the Cloud backoffice for this game is consolidated with the product information returned by the app store. If product information for a given product can not be fetched, the product is removed from the list. This means that even though products are configured on the backoffice, if something is wrong on the servers of the app store, in the above snippet backofficeProducts
will be non-empty but enrichedProducts
may be.
Launching a purchase brings a store front end to the customer, prompting him to purchase the product. The UI is the native one as expected by your players. The result handler is called whether the purchase is successful or failed.
Notes: While launching a purchase is straightforward using CotC, the process that does run on the device is not. Therefore, CotC can not guarantee that you will receive a response for a given call to LaunchPurchase
(that is, the handler is ever called). The application might have a shorter livecycle. CotC does however offer a guarantee that no product will have to be purchased twice by the client without the purchase being registered properly on the Clan of the Cloud servers, unless the application is uninstalled after a purchase that failed to be validated to Clan of the Cloud servers.
After having received a positive result from the purchase process, you should validate it through CotC. Purchases usually trigger a transaction as configured in the backoffice, that might give the player additional abilities or otherwise affect his inventory. It means that you should reload the balance after any purchase reported as successful. Also, it is very important that you close the transaction once CotC reports your purchase as taken in account successfully. Unless a completed transaction is closed, the product will be reported as purchased for any additional attempts to purchase it. This protection prevents that any crash happening between the purchase on the store and the validation on CotC servers makes the product "lost" (purchased, but not delivered).
Here is how you may drive a purchase flow once the CotcSdk.ConfiguredProduct is chosen:
You can query the history of purchased products for the logged in user. Note that this won't actually query the history of purchases made on the current device, but by the current Clan of the Cloud user. This distinction is important, and means that a player having purchased something must avoid losing his Clan of the Cloud account.
Querying the product list may be done as shown in the following snippet. Take a look at the CotcSdk.GamerStore.GetPurchaseHistory method for information about what is precisely returned.