HUAWEI Wallet Kit allows users to claim passes of merchants, including loyalty cards, gift cards, coupons, boarding passes, event tickets, and transit passes, and add them to HUAWEI Wallet. You can provide a button or link in your app for users to add passes to their HUAWEI Wallet and view and manage them.
You need to perform the following operations:

What You Will Create

In this codelab, you will use the created demo project to:
Call the HMS Core Wallet SDK to construct instances so that passes can be added to HUAWEI Wallet.

What You Will Learn

Hardware Requirements

Software Requirements

HUAWEI HMS Core integration requires the following preparations

For details, see the HUAWEI HMS Core Integration Preparation.

Enabling the HUAWEI Wallet Kit API

You have now successfully enabled the HUAWEI Wallet Kit API for your app.

Generating a Public-Private Key Pair

Registering HUAWEI Wallet Kit

Obtaining the Configuration File

Configuring Dependency for the HMS Core Wallet SDK

Configuring Obfuscation Scripts

Open the obfuscation script file proguard-rules.pro of your Android Studio project and add obfuscation configurations to the file.

-ignorewarning -keepattributes *Annotation* -keepattributes Exceptions -keepattributes InnerClasses -keepattributes Signature -keepattributes SourceFile,LineNumberTable -keep class com.hianalytics.android.**{*;} -keep class com.huawei.updatesdk.**{*;} -keep class com.huawei.hms.**{*;}

If you have used AndResGuard, add it to the whitelist in the obfuscation script file.

"R.string.hms*", "R.string.connect_server_fail_prompt_toast", "R.string.getting_message_fail_prompt_toast", "R.string.no_available_network_prompt_toast", "R.string.third_app_*", "R.string.upsdk_*", "R.layout.hms*", "R.layout.upsdk_*", "R.drawable.upsdk*", "R.color.upsdk*", "R.dimen.upsdk*", "R.style.upsdk*"

The HUAWEI Wallet Kit API is used to add passes to HUAWEI Wallet. You need to configure pass information when calling this API. For example, to add a loyalty card through this API, you need to configure the loyalty card number, loyalty points, and member name on the loyalty card. In this codelab, you can create a layout page in your Android Studio project and design the UI according to the following figure.

Display the Add to HUAWEI Wallet button.Display the Add to HUAWEI Wallet button in an app. For details about button specifications, please refer to Huawei Icon Specifications

Construct a loyalty card instance.

PassObject.Builder passBuilder = PassObject.getBuilder(); passBuilder.setStatus(status) .setOrganizationPassId(cardNumber) .setPassStyleIdentifier(mPassStyleIdentifier.getText().toString()) .setPassTypeIdentifier(typeId) .setSerialNumber(serinumber) .addAppendFields(appendFields) .addCommonFields(commonField) .passBuilder.addLocationList(locationList) .addImageList(imageList); // Generate a loyalty card instance. PassObject passObject = passBuilder.build();

Convert the instance to a JWT.

// Fixed public key provided by HUAWEI used to encrypt the sessionKey String sessionKeyPublicKey = "MIIBojANBgkqhkiG9w0BAQEFAAOCAY8AMIIBigKCAYEAgBJB4usbO33Xg5vhJqfHJsMZj44f7rxpjRuPhGy37bUBjSLXN+dS6HpxnZwSVJCtmiydjl3Inq3Mzu4SCGxfb9RIjqRRfHA7ab5p3JnJVQfTEHMHy8XcABl6EPYIJMh26kztPOKU2Mkn6yhRaCurhVUD3n9bD8omiNrR4rg442AJlNamA7vgKs65AoqBuU4NBkGHg0VWWpEHCUx/xyX6hIwqc1aD7P2f62ZHsKpNZBOek/riWhaVx3dTAa9ZS+Av3IGLOZiplhYIow9f8dlWyqs8nff9FZoJO03QhXLvOORT+lPAkW6gFzaoeMaGb40HakkZn3uvlAEKrKrtR0rZEok+N1hnboaAu8oaKK0rF1W6iNrXcFrO0rcrCsFTVF8qCa/1dFmIXwUd2M6cUzT9W0YkNyb6ZBbwEhjwBL4DNW4JfeF2Dzj0eZYlSuYV7e7e1e+XEO8lwPLAiy4bEFAWCaeuDVIhbIoBaU6xHNVQoyzct98gaOYxE4mVDqAUVmhfAgMBAAE="; // sessionkey is used to encrypt the payload data. String sessionKey = RandomUtils.generateSecureRandomFactor(16); // Encrypt JSON as payload of JWT byte[] sessionKeyBytes = EncodeUtil.hex2Byte(sessionKey); String objectJson = passObject.toJson(); String payLoadEncrypt = AESUtils.encryptAESCBC(objectJson.getBytes(EncodeUtil.UTF_8), sessionKeyBytes); // Encrypt sessionKey and set into header of JWT Map<String, Object> headerMap = new HashMap<>(); try { String sessionKeyPlaintext = RSA.encrypt(sessionKey.getBytes(), sessionKeyPublicKey, "RSA/ECB/OAEPwithSHA-256andMGF1Padding", "UTF-8"); headerMap.put("sessionKey", sessionKeyPlaintext); } catch (Exception e) { System.out.println(e.getMessage()); } JwtBuilder builder = Jwts.builder().setHeader(headerMap).setPayload(payLoadEncrypt); String userToken = builder.compact(); String content = userToken.substring(0, userToken.length() - 1); String sign = ""; String charset = "utf-8"; try { // Use the private key generated during service registration to sign the JWT body and header to obtain a JWT and encode the JWT using Base64. PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(privateKey)); KeyFactory keyf = KeyFactory.getInstance("RSA"); PrivateKey priKey = keyf.generatePrivate(priPKCS8); java.security.Signature signatureObj = java.security.Signature.getInstance(SIGN_ALGORITHMS256); signatureObj.initSign(priKey); signatureObj.update(content.getBytes(charset)); byte[] signed = signatureObj.sign(); sign = Base64.getEncoder().encodeToString(signed); } catch (Exception ex) { ... }

Create CreateWalletPassRequest.

CreateWalletPassRequest request = CreateWalletPassRequest.getBuilder() .setJwt(jwtStr) .build();

Create a Client instance.

WalletPassClient walletObjectsClient = Wallet.getWalletPassClient(PassTestActivity.this);

Create a task.

Task<AutoResolvableForegroundIntentResult> task = walletObjectsClient.createWalletPass(request);

Execute the task.

// SAVE_FLAG is a task execution flag used to receive the task execution result. ResolveTaskHelper.excuteTask(task, XXXActivity.this, SAVE_FLAG);

Receive the loyalty card adding result.

protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { switch (requestCode) { case SAVE_TO_ANDROID: switch (resultCode) { case Activity.RESULT_OK: Toast.makeText(this, "save success", Toast.LENGTH_LONG).show(); break; case Activity.RESULT_CANCELED: Toast.makeText(this, "cancel by user", Toast.LENGTH_LONG).show(); break; default: if (data != null) { int errorCode = data.getIntExtra( WalletCommonConstants.EXTRA_ERROR_CODE, -1); Toast.makeText(this, "fail, [" + errorCode + "]:" + errorCode, Toast.LENGTH_LONG).show(); } else { Toast.makeText(this, "fail:data is null", Toast.LENGTH_LONG).show(); } break; } } }

Registering a Huawei ID

Testing the Function of Adding a Loyalty Card

to run the project you have created in Android Studio to generate an APK. Then install the APK on the test phone. The screen is shown in the following figure.

Well done. You have successfully completed this codelab and learned:

HMS Core Wallet SDK APIs

HUAWEI Wallet Kit Demo

Code copied