目录

Preparing the Certificates

After integrating open APIs of Huawei Pay, you need to use certificates to sign requests and responses and verify the signatures to ensure the security and reliability of the requests.

This section describes the functions and obtaining methods of related certificates during the integration. Before preparing certificates, register with the Huawei Pay merchant platform.

Certificates to be prepared: merchant certificate and Huawei Pay certificate (public key)

Certificate Description

The following figure shows how to use the certificates.

Merchant Certificate

A merchant certificate is generated by the merchant (refer to Generating a Merchant Certificate) or applied for from a qualified CA. It contains the public and private keys of the merchant.

The private key is used by the merchant to sign requests to open APIs of Huawei Pay. The public key is provided to Huawei Pay for verifying the request signature.

The certificate file must be in PEM format. RSA and SM2 certificates are supported.

Certificate Private Key

When a merchant generates or applies for a merchant certificate, a paired merchant private key is generated.

Certificate Public Key

When a merchant generates or applies for a merchant certificate, a paired merchant public key is generated.

[h2]Generating a Merchant Certificate

(Recommended) Method 1: Using the JavaScript Library to Generate a Certificate Offline (Using the RSA Algorithm as an Example)

  1. Configure the Node.js execution environment. The following uses Windows as an example to show how to download and install Node.js.

    Log in to the Node.js official website and download the Node.js software package. Select the LTS version, and download the corresponding software package for your OS.

    Double-click the downloaded software package to perform installation. Retain the default settings and click Next until Finish is displayed. During the installation, Node.js will automatically add the path of node.exe to the Path environment variable.

    If the default installation directory is not selected during Node.js installation, you will need to manually set the system Path environment variable (My Computer > Properties > Advanced System Settings > Environment Variables).

    Open the CLI and run the node -v command. If the Node.js version number is displayed, the Node.js execution environment is configured successfully.

  2. Use the text editor to create a file (encoded in UTF-8), copy the following code to the file, and save the file as generateKeyPair.js.

    const crypto = require('crypto');
    // Generate a key pair.
    const { publicKey, privateKey } = crypto.generateKeyPairSync('rsa', {
      modulusLength: 3072, // Key length, which must be greater than or equal to 3072 characters.
      publicKeyEncoding: {
        type: 'spki', // Public key encoding format.
        format: 'pem' // Public key output format.
      },
      privateKeyEncoding: {
        type: 'pkcs8', // Private key encoding format.
        format: 'pem' // Private key output format.
      }
    });
    console.info('Generated public key:');
    console.info(publicKey);
    console.info('Generated private key:');
    console.info(privateKey);
  3. Open the CLI, go to the directory where generateKeyPair.js is located, and run the node generateKeyPair.js command.

  4. Copy the generated public and private keys from the result and save them. The following figure shows the result.

Method 2: Using an Online Tool to Generate a Certificate

You can search for and use related open-source online tools or websites to generate the public/private key pair for the certificate.

Generating the public and private keys is a sensitive operation. Exercise caution when performing this operation. It is recommended that you generate the key pair by yourself.

[h2]Uploading the Merchant Certificate

After the certificate public key is generated, the merchant must manually upload it to the Huawei Pay merchant platform for the Huawei Pay server to verify the signature of the payment request.

  1. The private key is used to sign the content of the request to be sent. Keep the private key secure and do not disclose it.

  2. After signing in to the Huawei Pay merchant platform, the merchant needs to upload the merchant certificate in certificate management. The merchant can perform transactions only after the merchant certificate is uploaded. The certificate must match the merchant ID and be valid.

The certificate public key to be uploaded must meet the following requirements:

Sign in to the Huawei Pay merchant platform and upload the public key of the merchant certificate in the certificate management > merchant certificate upload module.

The following is an example of the public key to be uploaded:

-----BEGIN PUBLIC KEY-----
MIIBIjANBgkq*********************************vW7gQTM8BHFTezQjdRI
A7xka2TaVHt***********************************rOA3P5rew9cO96q/7Z
kQ6lRd3oVsf************************************rGYCrA2RVgr79mRx+
s22qfA5FdTC*************************************i6I2cRVb1grBQphR
yFBxCGC/NeV*************************************K1QM1SC4GCORHocS
MQvApBQwQF9*************************************eEQvwpVfFxg4dGBz
DQIDAQAB
-----END PUBLIC KEY-----

Huawei Pay Certificate

The Huawei Pay certificate is provided by Huawei Pay and contains the Huawei Pay platform identifier and public key information. The certificate algorithm is SM2.

[h2]Downloading the Huawei Pay Certificate

Sign in to the Huawei Pay merchant platform and download the Huawei Pay certificate from the merchant center > certificate management > Huawei Pay certificate page. This certificate is used to verify the information sent by Huawei Pay to the merchant's service system, such as the payment result.