Huawei Wallet Kit -Server Side Tickets and All-in-one pass Creation

sujith E
Huawei Developers
Published in
7 min readJul 10, 2020

Introduction

HUAWEI Wallet Kit providing users to claim passes of merchants, like loyalty cards, gift cards, coupons, boarding passes, event tickets, and transit passes. It provides easy-to-access digital passes on an integrated platform. It enables user save their cards into mobile phones for convenient. The interaction between apps and users via location based notifications.

Integration Process.

The Wallet Kit integration process consists following steps

1. Generating Public and Private Keys

2. Adding Pass type on the AGC

3. Running the Server Demo code

4. Running the Client Demo code

5. View card adding status

Step-1: Check Below Link to generate keys & App Configuration in AGC:

LINK: https://forums.developer.huawei.com/forumPortal/en/topicview?tid=0201242460338530108&fid=0101187876626530001

· Let’s J how to apply new wallet.

· Now will see how to create Flight ticket.

· Service type consists 3 types

1. Card

2. Coupon

3. Ticket

· Service item will change based on service type

· Service name should be Unique

· Service Id should be unique it will start will always hwpass

· Public key we need to get it from RSAUtils

· Click next button and save details. The service application procedure is now complete. After applying for the service, you can view, edit, and delete it on the HUAWEI Wallet Kit service application page

Step-2: Server Side Integration:

Download server demo code check below link.

Link: https://developer.huawei.com/consumer/en/doc/development/HMS-Examples/wallet-sample-code

1. Download maven dependency.

2. Configure your project into IntelliJ IDEA

3. Download required dependencies and import

4. Sync your project

5. Open release.config.properties and replace appId & secret Key

6. Compile project using terminal mvn clean compile

7. After completion of compilation automatically Target folder will generate

Follow below steps

Copy hmspass folder in the config directory, paste into the target/classes directory.

You can run the source code, you can run mainly java files in the Test folder.

Check below steps and modify accordingly.

· The file ends with ModelTest provides the examples of adding, modifying and pass models.

· The file name ends with InstanceTest provides the examples of adding, modifying and pass instances.

Step-3: Generate Pass Model:

· Open FlightModel.json file

· PassTypeIdentifier is unique which you mentioned service Id in AGC both needs to match

· You have to modify passTypeIdentifier and passStyleIdentifier when we add card types on the AGC PassStyleIdentifier field is unique.

· After completion of modify execute createFlightModel()

· Open the HwFlightModelTest file and Run CreateFlightModel() method

Test
public void createFlightModel() {
System.out.println("createFlightModel begin");

// Read an example flight model from a JSON file.
String objectJson = CommonUtil.readWalletObject("FlightModel.json");
HwWalletObject hwWalletObject = JSONObject.parseObject(objectJson, HwWalletObject.class);
String modelId = hwWalletObject.getPassStyleIdentifier();
System.out.println("modelId is: " + modelId);
String instanceId = hwWalletObject.getSerialNumber();

// Set parameters of the flight model you want to create. Flight instances belong to this model share these
// parameters.
WalletBuildService walletBuildService = new WalletBuildServiceImpl();
HwWalletObject requestData = walletBuildService.createHwWalletObject(objectJson, instanceId, modelId);

// Validate the parameters.
boolean validateModel = HwWalletObjectUtil.validateWalletModel(requestData);
if (validateModel) {
// Post the new flight model to the wallet server.
String urlParameter = "flight/model";
HwWalletObject flightModel = walletBuildService.postHwWalletObjectToWalletServer(urlParameter, requestData);
System.out.println("createFlightModel JSON is: " + CommonUtil.toJson(flightModel));
System.out.println("createFlightModel end");
}
}

FlightModel.json this file to be transferred to Huawei interfaces

Step-4: Generate pass instance:

· Open FlightInstance.json file

· Follow above procedure to modify the passType and passStyleIdentifier

· serialNumber and organizationPassId both are unique.

· OrganizationPassId replace with AppId

· Serial Number every Time needs to change it should be unique.

· Open HWFlightInstanceTest.java file.

· After Completion of modification execute CreateFlightInstance() to generate pass instance

@Test
public void createFlightInstance() {
System.out.println("createFlightInstance begin");

// Read an example flight instance from a JSON file.
String objectJson = CommonUtil.readWalletObject("FlightInstance.json");
HwWalletObject hwWalletObject = JSONObject.parseObject(objectJson, HwWalletObject.class);

// Every flight instance has a style, which is a flight model. This model ID indicates which model the new
// flight instance belongs to. Before creating a flight instance, its associated flight model should already
// exist.
String modelId = hwWalletObject.getPassStyleIdentifier();

// Set the ID of the new flight instance.
String instanceId = hwWalletObject.getSerialNumber();
System.out.println("instanceId is: " + instanceId);

WalletBuildService walletBuildService = new WalletBuildServiceImpl();

// Set the flight instance's parameters.
HwWalletObject requestData = walletBuildService.createHwWalletObject(objectJson, instanceId, modelId);

// Validate the parameters.
boolean validateInstance = HwWalletObjectUtil.validateWalletInstance(requestData);
if (validateInstance) {
// Post requestData to the wallet server to create a new flight instance.
String urlParameter = "flight/instance";
HwWalletObject flightInstance =
walletBuildService.postHwWalletObjectToWalletServer(urlParameter, requestData);
System.out.println("flightInstance JSON is: " + CommonUtil.toJson(flightInstance));
System.out.println("createFlightInstance end");
}
}

Step-5: Generating JWE character strings

· Open HWFlightInstanceTest file execute below methods.

· Before executing methods change AppId, jweSignPrivateKey (privateKey take from RSAUtil.zip) and InstanceIdListJson

· generateThinJWEToBindUser() ->this method will generate JWES are used to bind gift card instance to users.it generates a character string.

· Replace your AppId and modify instance Id which you mentioned serial Number in FlightInstance.java

· Replace private key, You generated a pair of keys while applying for services on AGC. use that private key

· After replacing required data execute Now generateThinJWEToBindUser()

@Test
public void generateThinJWEToBindUser() {
System.out.println("generateThinJWEToBindUser begin.");

// The app ID registered on the Huawei AppGallery Connect website.
String appId = "*******";

// Bind existing flight instances to users.
// Construct a list of flight-instance IDs to be bound.
String instanceIdListJson = "{\"instanceIds\": [\"20039\"]}";
JSONObject instanceIdListJsonObject = JSONObject.parseObject(instanceIdListJson);
instanceIdListJsonObject.put("iss", appId);

// Generate a session key to encrypt payload data. A session key is a string of random hex numbers.
String sessionKey = RandomUtils.generateSecureRandomFactor(16);
System.out.println("sessionKey: " + sessionKey);

// Huawei's fixed public key to encrypt session key.
String sessionKeyPublicKey =*************";

System.out.println("sessionKeyPublicKey: " + sessionKeyPublicKey);

// You generated a pair of keys while applying for services on AGC. Use that private key here.
String jweSignPrivateKey = "*******************";

// Generate JWEs.
String jweStrByInstanceIds = JweUtil.generateJwe(sessionKey, jweSignPrivateKey,
instanceIdListJsonObject.toJSONString(), sessionKeyPublicKey);
System.out.println("JWE String: " + jweStrByInstanceIds);

//Url Encoded
try {
String encodedString = URLEncoder.encode(jweStrByInstanceIds, StandardCharsets.UTF_8.toString());
System.out.println("JWE EncodeString: " + encodedString);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}

· Url-encoded code is as follows:

URLEncoder.encode(jwtStrInstanceIds,StandardCharactersets.UTF_8.toString())

· JWE string generated.

Step-6: Binding pass by URL

· Huawei provides an interface for binding pass through URL

· Follow below URL format

https://{walletkit_website_url}/walletkit/consumer/pass/save?jwt={jwt-content}

Below are the area Wallet server URL’s

· After you enter the URL on the browser the Huawei Login page is displayed.

· After successfully login it will redirect to next screen

Output:

· Accept permission then click add button. Now flight ticket card add into Huawei Wallet.

Generate Other Cards Like Coupon Card:

Note: You have to start from Step1

· We can check cards Huawei wallet app.search AppGallery in mobile download Huawei wallet app.

FAQs….

  1. How can I view an added pass?

Download the latest version of HUAWEI Wallet (the earliest version supported by HUAWEI Wallet Kit is 9.0.7.300) to your phone and sign in with the account that was used to add the pass. After successfully signing in, you will be able to view the pass.

2. What will happen if a user attempts to add a pass to HUAWEI Wallet without signing in using a HUAWEI ID?

HUAWEI Wallet Kit displays the HUAWEI ID registration or sign-in page first. The user can add the pass to HUAWEI Wallet only after signing in using a registered HUAWEI ID.

Now Then….!

That’s it for this time. Go check out the below links for your reference.

1. https://developer.huawei.com/consumer/en/doc/development/HMS-Guides/wallet-guide-introduction

2. https://developer.huawei.com/consumer/en/doc/development/HMS-Examples/wallet-sample-code-android

3. https://developer.huawei.com/consumer/en/doc/development/HMS-References/wallet-api-client-2

--

--