- Create In-App Purchase items in the Google Play Console website
- Host a JSON file on your website with a list of purchasable items
- Display a web UI listing purchasable items
- Initiate a purchase when requested by a user
- Verify and fulfill purchase through Google Play
- Provide users with subscription management
Configure IAP in Google Play Console
Create your app in the Google Play Console. The package name of your app is a globally unique identifier that identifies your app on Google Play. Your account “claims” that package name the first time you upload your app to the Google Play Console. Make sure that any pages with gray checkmarks are completed so that the checkmark turns green. The app must be fully configured in Google Play before you can create and test in-app purchases. In the Google Play Console, select your app, expand the Products menu. Create either an in-app product, or a subscription. Take note of the product id as it will be used later in code to identify the item. Title and description are used in Google Play listings, and will also be passed to your app. If you wish to have different tiers of subscriptions (e.g. a monthly and annual subscription), they need to be put under the same subscription with different base plans. Each base plan can have its own pricing, offers and renewal cycles.Host a JSON file with available products
Create a JSON file that lists the product IDs you would like to offer for sale. This allows you to in real time add or remove products without publishing a new version of your app. The following example shows three products for sale: a regular in-app purchase, and two subscription products. The product IDs for each must be placed in the correct section:inappProducts vs subProducts.
App configuration
Now you are ready to enable and configure the plugin within the Native Plugins tab of App Studio. You need to enterproductsUrl. The productsUrl should point to the JSON file on your website. When your app launches, it will make an HTTP GET request to your productsUrl.
IAP products and status
The app will verify the list of products with Google Play and retrieve each product’s information. Then it will execute a JavaScript function on your website defined asmedian_info_ready with a single object parameter. Alternatively you can use the Median JavaScript Bridge to obtain this data at runtime by calling the method median.iap.info().
You define this function on your page, but don’t call it. If present on a page, it will be called by the app when the page is loaded.
productsData will be of the form:
-
productID: the identifier set in Google Play that matches those in theproductsUrl. -
type: “inapp” or “subs” -
title: from Google Play Console -
name: from Google Play Console -
description: from Google Play Console -
purchaseOfferDetails(if the type is “inapp”):formattedPrice: the price you should display to your user. It will be localized to their currency.priceAmountMicros: the price in micro-units, e.g. 1 USD = 1000000 micros.priceCurrencyCode: the 3-letter currency code
-
subscriptionOfferDetails(If the type is “subs”):-
offerId: offer id associated with the subscription product. -
basePlanId: base plan id associated with the subscription product. -
offerTags: offer tags associated with this Subscription Offer. -
offerToken: offer token required to pass in launchBillingFlow to purchase the subscription product with these pricing phases. -
pricingPhases: pricing phases for purchasing an item through a offer. Pricing phases contains time ordered list of PricingPhase-
formattedPrice: the price you should display to your user. It will be localized to their currency. -
priceAmountMicros: the price in micro-units, e.g. 1 USD = 1000000 micros. -
priceCurrencyCode: the 3-letter currency code -
billingPeriod: billing period for which the given price applies, specified in ISO 8601 format. -
billingCycleCount: number of cycles for which the billing period is applied. -
recurrenceMode: recurrence mode of the pricing phase. Can be one of 3 integer values.- 1: Infinitely recurring
- 2: Finitely recurring
- 3: Non recurring
-
-
Display web UI for purchases
Create a page on your website that shows the available items for purchase. It should wait for themedian_info_ready function to be called and then populate the items for purchase. The price must be shown using the price string, as different users may have different language and currency settings.
Initiate purchase
When a user decides to purchase an IAP, run the JavaScript function below.productID and offerToken can be passed as parameters and are available from productsData above.
purchasesData in the format listed further below. If the purchase fails an error will be thrown with the error provided by Google.
The median.iap.purchase() method supports these additional parameters for auto-renewing subscriptions:
previousProductID– the product ID we are replacing. This is required to support subscription upgrades and downgrades (i.e. converting a monthly subscription to annual, or changing a subscription tier from basic to premium membership).
previousProductIDis no longer accepted for purchase. Instead, usepreviousPurchaseToken. If this field is empty, theprorationModeis also ignored- The history/details of purchase now contain a list of Product IDs. Like how the library kept the
productIDfield (which is the first item of the list), we also kept theproductIDin our response data for compatibility support. Hence, the web component shall get bothproductIDandproductIDs formedian_iap_purchases
-
replacementMode– determines how to credit the user for a mid-period subscription change. Valid values include: -
CHARGE_FULL_PRICE- The new plan takes effect immediately, and the user is charged full price of new plan and is given a full billing cycle of subscription, plus remaining prorated time from the old plan. -
CHARGE_PRORATED_PRICE- The new plan takes effect immediately, and the billing cycle remains the same. -
DEFERRED- The new purchase takes effect immediately, the new plan will take effect when the old item expires. -
UNKNOWN_REPLACEMENT_MODE- (no details provided) -
WITHOUT_PRORATION- The new plan takes effect immediately, and the new price will be charged on next recurrence time. -
WITH_TIME_PRORATION- The new plan takes effect immediately, and the remaining time will be prorated and credited to the user.
Purchase verification and fulfillment, and purchase restoration
On app launch, and after any purchases are made, the app will call a JavaScript function on your page namedmedian_iap_purchases with a single object parameter. This function can be used to determine the purchases previously made by the current user and the functionality/subscriptions that they can access.
Alternatively you can use the Median JavaScript Bridge to obtain this data at runtime by calling the method median.iap.purchases().
You define this function on your page, but don’t call it. If present on a page, it will be called by the app when the page is loaded.
purchasesData will be of the form:
allPurchases array may have these fields:
orderId: identifies the purchase transactionpackageName: should match thepackageNameof your appproductID: the identifier for what was purchasedpurchaseTime: milliseconds since the unix epoch (Jan 1 1970)purchaseTimeString: formatted as a stringpurchaseState: 0 – purchased, 1 – canceled, 2 – pendingpurchaseString: “purchased”, “canceled”, “pending”, or “unknown”acknowledged: indicates the app has confirmed the purchase with Google PlayautoRenewing: indicates the purchase will auto-renew. Note that this will be set to false if the user cancels their subscription.purchaseToken: a string that can be used to verify the purchase with Google
allPurchases array will list all current subscriptions. Any expired subscriptions will not longer appear.
Subscription management
You may wish to provide links for your users to manage their subscriptions. To open the Google Play page that lists all subscriptions for all of the user’s apps (not just your app), open the URL:Ongoing subscription management
Google Play can provide real-time notifications of subscription changes so that you can manage subscription status on your backend. You will need to configure and subscribe to a Google Cloud Pub/Sub topic per the Google Play Developer documentation: Real-time developer notifications reference guideTesting process
Testing your in-app purchase flow requires your app be properly set up in Google Play to be distributed. Your test devices (including simulator devices) must have Google Play Store installed and be signed into a Gmail or Google Apps for Business account. You may use your day-to-day account. In your Developer Account -> Account details, go to the License Testing section. Add the email addresses for the test accounts. On your app’s management page, create an internal test track. Add the test user email to a user list. Once added, each test user go to the Opt-in URL (looks likehttps://play.google.com/apps/internaltest/1234321234...) and accept the Opt-In. Create a release build (or use the Median-built apk) and upload it to the internal test track. The test users should then be able to find the app in the Google Play Store on their devices and install it.
Subsequent releases to the internal test track will immediately be available to the test devices by checking for new updates in the Google Play Store app.
Any in-app purchases can be tested without actual payment being exchanged. Auto-renewing subscriptions will renew every 5 minutes until they are canceled.
Upgrading the Google Play Billing Library
If you were previously using the Median Google IAP plugin, your app is currently configured in legacy mode to support the previous Google Play Billing Library 4. To upgrade your app to support the latest Google Play Billing Library 6 you will need to update your website code and submit a new build of your app to Google Play withlegacyMode disabled.
To transition your website code, check the libraryVersion in the median_info_ready callback or the median.iap.info() function call. Version 6 or above means the new API will be used. A lower version number or missing value means the old API being used. Make sure your website code can work with both versions of the API during the transition period while you have both old and new versions of your app in use.
Note that you may also differentiate the version of your app being used by adding custom headers or the appVersion returned in Device info.
Once your website code has been updated to support Version 6, you must generate a new build of your Android app with legacyMode disabled. From your app management page, go to the Native Plugins tab and in Settings for In-App Purchases disable legacyMode. Rebuild your app, complete testing (see below), and then release through Google Play.