Show: Object Pascal C++
Display Preferences

Adding In-App Payments to Your Mobile Apps

From Appmethod Topics
Jump to: navigation, search

Go Up to Creating an Android App

Go Up to Creating an iOS App


Note: This topic describes how to add support for in-app payments to an application, regardless of the target in-app payment service. For service-specific documentation that covers prerequisites, testing, deployment and other aspects of adding in-app payments support to your application, read the following pages:

In-app payment services let you sell digital content directly within your applications. Google and Apple provide in-app payment services for Google Play (Android) and iTunes (iOS), respectively.

FireMonkey provides the TInAppPurchase component that abstracts your applications from the particularities of different in-app payment services.

You need to add an instance of this component to your application, and establish a connection to the target in-app payment service either when your application starts or at any other point. Then you can use the service. Once you establish a connection to your in-app payment service, you can:

TInAppPurchase also provides some service-specific features that you can use as well.

Establishing a Connection to Your In-App Payment Service

Before you can use your in-app payment service, you must configure TInAppPurchase to connect to your in-app payment service and establish a connection.

Configuring the Connection Data for Your In-App Payment Service

To connect to the Google Play In-app Billing service on Android, you must copy your license key into the ApplicationLicenseKey property of TInAppPurchase. You obtain a license key after configuring your application for In-app Billing in Google Play; for example:

InAppPurchase1.ApplicationLicenseKey := 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgEAlGKUp9KCAQjzJdrjDaCC2Z2P0K751w1KAL3cBLhDqRSYDnug+nZKzeg2+R3KOIDVpJU7+PXz8jyf07m/30Y0J1CmOBf6NK/MIsXq3BmWb4/UIUW1aKzlHiiVtpcm26n3hOSmnnfkhasbfQo75puBBTbURvumOFfwUAJ2BM9FzAOPWzsAqT1brYI8iu+NHDT+gGQl7uOTgEN3jbBD/la5qAtevOGbgP1DhfKtsi91+/oyy3+lphWNn2A+RE1KOx4lnHp0wZyOEvDP/CPZneT0YYgqG6MtawM+Mij/75XwhJEdeOSXYya7BN6hg49JX2WJxCRc7JqbgzN9vsRn6hSceQ5MuQIDAQAB';

To connect to the iOS In-App Purchase service on iOS, TInAppPurchase does not need any special configuration and should work, provided that your application:

Connecting to Your In-App Payment Service

To connect to the target in-app payment service, call SetupInAppPurchase. This procedure is asynchronous, and once it finishes it triggers the OnSetupComplete event. If you want to associate an event handler to OnSetupComplete, do it before you call SetupInAppPurchase:

InAppPurchase1.OnSetupComplete := InAppPurchase1SetupComplete;
InAppPurchase1.SetupInAppPurchase;

You usually want to associate OnSetupComplete to an event handler, to request the list of products as soon as you are connected to the target service. Alternatively, you can call IsSetupComplete to check whether you can use the service at a particular time (True) or not (False).

Using Your In-App Payment Service

Once you have established a connection to your in-app payment service, you can do any of the following:

Obtaining Information About a List of Products

Your application should keep a list with the IDs of the products that can be purchased from your application. Keep this list in the TInAppPurchase.ProductIDs property. You can call QueryProducts at any time to request information about the products with these IDs, such as whether these products have been purchased or not.

If there is an error, TInAppPurchase.OnError is triggered with ProductsRequest as <FailureKind>.

If the request is successful, TInAppPurchase.OnProductsRequestResponse is triggered instead. This event provides two lists of products:

  • <Products>, a TList of instances of TProduct that provides the list of valid available and purchased products.
  • <InvalidProductIDs>, a TStrings that provides a list with the IDs of products that are invalid.

To tell apart products that your user purchased from products that are simply available for purchase, you can call IsProductPurchased providing the ID of a product:

if InAppPurchase1.IsProductPurchased(ProductID) then { Do something. }
Note: If the list of products contains a consumable product that has been purchased, now is the time to consume this product.

TProduct provides many properties that you can read to obtain information about each product.

Purchasing a Product

Before you allow your user to purchase products, call CanMakeInAppPurchases to check whether your user is allowed to make purchases (True) or not (False):

if not InAppPurchase1.CanMakeInAppPurchases then { Disable or hide the in-app payments GUI elements of your application. }

To request the purchase of a product in the list of available products from your in-app payment service, call PurchaseProduct with the product ID:

InAppPurchase1.PurchaseProduct(ProductID);

If there is an error, TInAppPurchase.OnError is triggered with Purchase as <FailureKind>.

If the purchase is successful, TInAppPurchase.OnPurchaseCompleted is triggered instead. This event provides the ID of the purchased product, and its <NewTransaction> parameter is True if the product has just been purchased or False if it has been restored. Use this event handler to manage purchased products; for example, if the purchased product is a consumable product, consume it.

Consuming a Product

Note: TInAppPurchase does not provide any method to find out whether a product is consumable or non-consumable. Your application must be able to tell them apart on its own. For example, you can hardcode a list of consumable or non-consumable product IDs in your application.

Consumable products are products that can be purchased more than once. When your user purchases a consumable product, you must call ConsumeProduct to inform your in-app payment service that this product has been consumed:

InAppPurchase1.ConsumeProduct(ProductID);
Notes:
  • Alternatively, you can call ConsumeProducts with a list of product IDs.
  • iOS In-App Purchase does not require to be informed of consumed products.

After you call one of the methods to consume a product, one of the following events is eventually triggered:

  • TInAppPurchase.OnConsumeCompleted if the product is consumed successfully. Handle this event to process this purchase in your application. For example, if your application keeps a counter of cookies and your user buys a "5 cookies" product, you can increase the count of cookies in your application by 5.
  • TInAppPurchase.OnConsumeFailed if an error occurs that prevented the product from being consumed. The <ErrorMessage> argument of this event provides a detailed description of the error.

Restoring Products Purchased from a Different Device on the Current Device

In Google Play In-app Billing, the non-consumable products of a user always appear as purchased on any device of this user. You do not need to do anything to restore these purchases.

In iOS In-App Purchase, the non-consumable products of a user do not appear as purchased by default when your user switches to a different device. To restore products purchased from a different device on the current device, you must call RestorePurchasedProducts. Restoring a product, which is similar to purchasing this product for free, triggers TInAppPurchase.OnPurchaseCompleted with False as the value of the <NewTransaction> parameter. Handle this event to keep track of restored purchases.

Notes:
  • If your user attempts to buy a previously purchased product again, the purchase comes at no cost for your user.
  • Restoring purchases prompts for the user's App Store credentials. You should not automatically restore purchases. Instead, you can let users start a restoring operation manually, for example with a "Restore" button.

Adding Support for Service-Specific Features

The following table shows features exclusive to a specific in-app payment service that TInAppPurchase provides:

Service Features

Google Play In-app Billing

iOS In-App Purchase

See Also

Personal tools
In other languages