It’s Show Time app using Huawei Wallet Kit Server API ( Node js Server )

Sanghati Mukherjee
Huawei Developers
Published in
5 min readOct 22, 2020

The article is the continuity of Huawei kit series. The series contain four parts. Below are the links to follow them:

1) It’s Show Time using Huawei Auth Service and Account kit.

2) It’s Show Time using Huawei Location and Site kit.

3) It’s Show Time using Huawei Wallet kit.

The last part of the series that is the fourth part would be explained here. In this article, we would learn how to implement the server side of Huawei wallet kit using local Node js server and how to call them in client side in order to create ticket and save the ticket on user wallet app for future use.

Why having your own server?

Using our own server to communicate with app is generally the best option for creating cards or tickets in our application. Why? Because our app will recognize and trust our own server, allowing us to control all transactions between our server and user devices. That is what we are going to learn today.

Setting up the server

If you are not familiar with setting up your own local server and connecting the server with the device, I would strongly recommend you to go through my previous article i.e. “Build your own server from scratch to send push notification“. My previous article will help you setting up local server from scratch also it is healthy to learn something new every day as our brain can store more information than a computer.

Prerequisite

  1. We must have latest version of Node installed.
  2. We must have latest version of Visual Studio Code installed.
  3. We must have latest version of MongoDB database installed.
  4. Laptop/desktop and Huawei mobile device must share same Wi-Fi connection.
  5. We must have a working app integrate with HMS Wallet Kit.

We will divide this article into two parts

1) Server Side: The server side contains Node, Express, Request and JavaScript.

2) Client Side: The client side contains Android Native, Java, Retrofit and HMS Wallet Kit.

Note: Since there is no wallet kit APIs for booking movie tickets, so we have to use wallet kit event APIs to create movie ticket.

Demo

Server Side

Obtaining app-level access token API

The request header uses AccessToken for authentication, which is obtained using the service API provided by the open platform. The service API provides two modes to obtain AccessToken: authorization code mode and client password mode. This API uses the client password mode.

Do not apply for a new app-level access token each time before the server API is called. Frequent application requests may trigger rejection, leading to application failure within a specified period. Each access token has a validity period.

Within the validity period, it can be used repeatedly. You are advised to apply for an access token again only when the server API is accessed and HTTP result code 401 is returned.

Wallet server address

Set the {url} variable based on the region where the server is located. For details, please refer to Wallet Server Address as shown below.

Creating a HwWalletObject

We need to set HwWalletObject as body parameter in order to call wallet event ticket APIs. To know more about HwWalletObject follow the link below:

https://developer.huawei.com/consumer/en/doc/HMSCore-References-V5/def-0000001050160319-V5

Creating an event ticket model API

We call this method to add an event ticket model to the Huawei server.

URL: https://passentrust-dra.wallet.hicloud.com/hmspass/v1/eventticket/model

API

The value of passTypeIdentifier parameter is the Service ID, which can be obtained when we apply for HUAWEI Wallet Kit service in AGC and the value of passStyleIdentifier is the Model ID, which can also be obtained from wallet kit service in AGC. Both are mandatory.

Adding an event ticket instance API

We call this method to add the event ticket instance of a user to the Huawei server. After the instance is added using this API, a thin JWE should be used as well to link the instance to the user’s HUAWEI ID. Alternatively, a JWE can be used, rather this API, to directly add the instance to the Huawei server and link it to the user’s HUAWEI ID.

URL: https://passentrust-dra.wallet.hicloud.com/hmspass/v1/eventticket/instance

API

The value of organizationPassId parameter is the APP ID, which can be obtained from our AGC.

Create ticket instance API for client

We need an API for client to call the above two APIs on server in order to fetch wallet instance and use it to create a movie ticket.

Client Side (Android Native)

We need Retrofit in order to call our restful Apis. Include the following dependencies in app build.gradle file.

implementation 'com.squareup.retrofit2:retrofit:2.8.1'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'

Create retrofit interface

Create a new Interface class and name it GetDataService. Open the class, copy and paste below code:

Create retrofit instance

Create a new class and name it RetrofitClientInstance. Open the class, copy and paste below code:

NOTE: The BASE_URL is important here. We will put our IPv4 Address of our server machine instead localhost. To find our machine IPv4 Address, we will go to command prompt and type ipconfig. Also make sure that the device is connected to the same Wi-Fi the server machine is connected too.

Use retrofit instance in the activity

Create an object of retrofit instance in onCreate() method of the activity as show below:

service = RetrofitClientInstance.getRetrofitInstance().create(GetDataService.class);

After that use this retrofit instance object to call the create ticket instance API of server in order to fetch instance Id and use it to create thin JWE which will be used to create a ticket.

When JWE is created the Huawei server will call browser to create ticket as shown below:

Now we need to add the ticket in our wallet app for future use which in this case is a movie ticket for INOX cinemas.

GitHub

Client Side:

https://github.com/DTSE-India-Community/ItsShowTime

Server Side:

https://github.com/DTSE-India-Community/Huawei-In-App-Purchase-Push-Kit-Server_Side-And-Wallet-Kit-Server-Side-Implementation

For More Information

  1. https://developer.huawei.com/consumer/en/doc/development/HMSCore-References-V5/create-model-0000001050158460-V5
  2. https://developer.huawei.com/consumer/en/doc/development/HMSCore-References-V5/add-instance-0000001050158466-V5

--

--