furkanaydgn / hms-iap-clientdemo-android-studio

This demo app provides all 3 types of product to demonstrate the procedure and capability of Huawei IAP.

Home Page:https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/introduction-0000001050033062?ha_source=hms1

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

HMS Core In-App Purchases Client Sample Code

English | 中文

This section describes how to develop a client for accessing In-App Purchases (IAP) of HMS Core, helping you quickly understand the client APIs provided by IAP and how to use them.

Click here to learn more about the kit.

Contents

Introduction

IAP allows your users to buy three types of products, namely consumables, non-consumables, and subscriptions, in your app.

  • Consumables: Such products are depleted once they are used and can be purchased again.

  • Non-consumables: Such products are purchased once and never expire.

  • Subscriptions: Such products provide users with ongoing access to content or services in your app. Users are charged on a recurring basis until they decide to cancel.

This demo showcases the integration process and functions of IAP using the three products.

The products in the demo are only used to demonstrate the purchase process, and will not be put into commercial use.

You can use HMS Toolkit to quickly run the sample code. HMS Toolkit supports one-stop kit integration, and provides functions such as free app debugging on remote real devices. For details about HMS Toolkit, please refer to the HMS Toolkit documentation.

Preparations

  1. Install Android Studio on your computer. Use Android Studio to open the project-level build.gradle file of the sample code.

  2. Create an app in AppGallery Connect and configure the app information. For details, please refer to Configuring App Information in AppGallery Connect.

  3. Create and configure your products in AppGallery Connect. For details, please refer to Configuring Your Products.

  4. Import the demo to Android Studio 3.0 or later and build the demo.

  5. Configure the sample code:

    • Download the agconnect-services.json file of your app from AppGallery Connect, and add the file to the app-level directory of the demo.
    • Add the signing certificate and add configurations to the app-level build.gradle file.
    • Open the AndroidManifest file and change the package name to your app package name.
    • Replace PUBLIC_KEY in the CipherUtil class with the public key of your app. For details about how to obtain the public key, please refer to Querying IAP Information.
    • Replace the products in this demo with your products.
  6. Run the sample code on an Android device or simulator.

Environment Requirements

Android SDK 22 or later and JDK 1.8 or later are recommended.

Result

The following screen will be displayed when the demo is running.

demo home page

Consumables

We use gems in the game to showcase how to purchase consumables.

  1. Tap Consumable products to view the home page for purchasing consumables. The obtainProductInfo API is called to obtain the detailed information about this type of products.

consumable demo page

  1. Tap 5 gems, and the createPurchaseIntent API will be called to jump to the payment screen, which is supported by IAP.

    consumable payment selection
  2. After the payment is successful, you can own more gems. The consumeOwnedPurchase API will be called to notify the IAP server that the products have been consumed.

    gem purchase result

Note: If an exception (such as network error or process termination) occurs after the payment is made, the demo will update the gem quantity when you enter the screen again. (The obtainOwnedPurchases API is called to obtain information about the purchased consumables, and the consumeOwnedPurchase API is called to consume the products.)

  1. Tap History, and the obtainOwnedPurchaseRecord API will be called to obtain the purchase history.

    consumable purchase history

Non-consumables

This demo uses the hidden level product as an example to demonstrate how to purchase a non-consumable product.

  1. Tap Non-consumable product to view the home page for purchasing non-consumables. The obtainOwnedPurchases API will be called to obtain information about the non-consumables you have purchased.

  2. If you have not purchased hidden level, the screen below will be displayed. Tap hidden level to start purchase. The purchase process is the same as that of consumables.

    hidden level not purchased
  3. After the purchase is successful or the hidden level has been purchased before, the demo will display the hidden level that you have purchased.

    hidden level have been purchased

Subscriptions

The demo uses the subscription groups Service-One and Service-Two as examples to demonstrate the purchase process of subscriptions. Each subscription group contains two products and each product contains two options: BUY and ACTIVE.

(For more information about subscriptions and subscription groups, please refer to Subscription Functions.

  1. Tap Auto-renewable subscription to view the home page for purchasing subscriptions. The obtainOwnedPurchase API will be called to obtain information about the subscriptions. If you have subscribed to a product, the status of the product is displayed as ACTIVE.

    subscription homepage with active subscription
  2. Tap the BUY button of the product to be subscribed to. The demo will call the createPurchaseIntent API to start purchase.

    subscription payment selection

    Note: Currently, only Alipay is supported for subscription purchase.

  3. You will be asked to authorize the automatic renewal agreement. After the purchase is successful, the purchase result will be displayed on the screen, which is supported by IAP.

    subscription payment selection
  4. Tap MANAGE SUBSCRIPTION, and the subscription management screen will be displayed. This screen shows all the products you have subscribed to before, including expired ones.

    subscription manage
  5. Tap Happy Subscribe to edit the subscription. You can select another subscription in the same subscription group, or click UNSUBSCRIBE to cancel the subscription. The subscription is available until it expires.

    edit subscription

Technical Support

You can visit the Reddit community to obtain the latest information about HMS Core and communicate with other developers.

If you have any questions about the sample code, try the following:

  • Visit Stack Overflow, submit your questions, and tag them with huawei-mobile-services. Huawei experts will answer your questions.
  • Visit the HMS Core section in the HUAWE Developer Forum and communicate with other developers.

If you encounter any issues when using the sample code, submit your issues or submit a pull request.

License

The sample code is licensed under Apache License 2.0.

About

This demo app provides all 3 types of product to demonstrate the procedure and capability of Huawei IAP.

https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/introduction-0000001050033062?ha_source=hms1

License:Apache License 2.0


Languages

Language:Java 100.0%