# getPaymentCode

`POST /aps/api/v1/codes/getPaymentCode`

In the User-presented Mode Payment scenario, this API is used by the Mobile Payment Provider (MPP) to obtain payment codes from Alipay+. If the request succeeds, the MPP receives payment codes and can then display payment codes on the MPP app.

# Structure

A message consists of a header and body. The following sections are focused on the body structure. For the header structure, see:

-   [Request header](api_overview#3mLq0) 
-   [Response header](api_overview#YdmVS) 

> **Note**:
>
> 1.  Set the data type of each parameter (except array) as String. This means that you must use double quotation marks (" ") to enclose the parameter value. Examples:
>
> -   If the data type of a parameter is Integer and its value is 20, set it as "20".
> -   If the data type of a parameter is Boolean and its value is `true`, set it as "true".
>
> 2.  For optional parameters that are not required in your case, you can take one of the following actions:
>
> -   Exclude the parameters from the request body.
> -   Set the parameter values as `null` (without the double quotation marks).
>
> Do NOT leave the optional parameters empty by setting their values as `""`; otherwise, an error might occur.

## Request parameters

#### region (String)

The region that is selected by the user as where the payment occurs. The value of this parameter must be a 2-character country/region code that follows the [ISO 3166](https://www.iso.org/iso-3166-country-codes.html) standard.

This field is required when the region is China (value: `CN`).

More information:

- Maximum length: 2 characters

#### customerId (String, REQUIRED)

The unique ID that is assigned by the MPP to identify a user.

More information:

- Maximum length: 64 characters

#### codeQuantity (Integer)

The number of payment codes to be returned. The default value of this parameter is 1. The MPP can specify this parameter to obtain multiple payment codes at a time. With multiple payment codes, the MPP can display a new payment code in case a previous one is expired, as the validity period of a payment code is only 1 minute.

More information:

- Maximum length: 2 characters
- Value range: 1-10

#### env (Env, REQUIRED)

The environment information about the device that is used by the user to request the payment code.

##### terminalType (String)

Terminal type. Valid values are:

-   `APP`: Mobile terminal
-   `WEB`: Web
-   `WAP`: Wap
-   `MINI_APP`: Mini program

##### osType (String)

Operation system type. Valid values are:

-   `IOS`: iOS
-   `ANDROID`: Android

##### deviceTokenId (String, REQUIRED)

Token ID of the device. This parameter is used for risk control. 

For more information about this parameter, see [Obtain the device token ID](../integration_user_mode_mpp/obtain_device_token_id) .

More information:

- Maximum length: 64 characters

##### clientIp (String)

IP address of the client device.

More information:

- Maximum length: 64 characters

##### cookieId (String)

User cookie ID.

More information:

- Maximum length: 64 characters

##### userAgent (String)

User agent.

More information:

- Maximum length: 1024 characters

#### indirectMpp (IndirectMpp)

Information about the indirect MPP.

Specify this parameter when the payment involves an indirect MPP, for example, when the payment is processed through a special payment network.

##### indirectMppId (String, REQUIRED)

The indirect MPP ID.

More information:

- Maximum length: 64 characters

##### indirectMppName (String)

The legal name of the indirect MPP.

More information:

- Maximum length: 256 characters

## Response parameters

#### result (Result, REQUIRED)

The result of the business processing, including the result code, result status, and result message. For more information about how to handle the result, see _How to handle the result_.

##### resultCode (String, REQUIRED)

The result code that indicates the detailed processing result.

More information:

- Maximum length: 64 characters

##### resultStatus (String, REQUIRED)

The result status that indicates the processing result. 

-   `S`: Successful
-   `F`: Failed
-   `U`: Unknown

##### resultMessage (String)

The result message that describes the result code in detail.

More information:

- Maximum length: 256 characters

#### paymentCodeInfoList (Array<PaymentCodeInfo>)

A list of payment codes. The quantity of the payment codes is determined by the _codeQuantity_ parameter in the request.

This parameter is returned by Alipay+ if the value of the _result.resultStatus_ parameter is `S`, which means that the request is successful. The default validity period of a code is one minute and has a 30-second overlap with other codes.

##### paymentCode (String, REQUIRED)

The value of the payment code.

More information:

- Maximum length: 128 characters

##### codeValidityStartTime (Datetime, REQUIRED)

The time when the payment code takes effect.

More information:

- The value follows the [ISO 8601](https://www.iso.org/iso-8601-date-and-time-format.html) standard format. For example, "2019-11-27T12:01:01+08:00".

##### codeExpiryTime (Datetime, REQUIRED)

The time when the payment code expires.

More information:

- The value follows the [ISO 8601](https://www.iso.org/iso-8601-date-and-time-format.html) standard format. For example, "2019-11-27T12:01:01+08:00".

## More information

**How to handle the result**

You might receive different results from Alipay+. Follow the instructions below to handle the result.

| result.resultStatus | result.resultCode                                           | Request status             | Actions                                                                                                                                |
|---------------------|------------------------------------------------------------|----------------------------|----------------------------------------------------------------------------------------------------------------------------------------|
| `S`                 | `SUCCESS`                                                  | Successful                 | Display the payment code.                                                                                                               |
| `F`                 | Multiple possible values exist, such as `ACCESS_DENIED`, `INVALID_CLIENT`, etc. | Failed                     | Take actions according to the result code (specified on the *result.resultCode* parameter). For more information, see the Result codes section below. |
| `U`                 | Multiple possible values exist, such as `REQUEST_TRAFFIC_EXCEED_LIMIT`, `UNKNOWN_EXCEPTION`, etc. | Unknown                    | Retry the same request.                                                                                                                 |
| No result received  | No result received                                         | Unknown                    | Retry the same request.                                                                                                                 |

## Result/Error codes

| Code | Value | Message | Further action |
| --- | --- | --- | --- |
| SUCCESS | S | Success | Display the payment code. |
| ACCESS_DENIED | F | Access is denied. | It is recommended that you contact connect_support@service.alipay.com to troubleshoot the issue. |
| INVALID_CLIENT | F | The client is invalid. | It is recommended that you contact connect_support@service.alipay.com to troubleshoot the issue. |
| INVALID_SIGNATURE | F | The signature is invalid. | Check whether the public key, signed message, and signature algorithm are as expected. |
| KEY_NOT_FOUND | F | The key is not found. | It is recommended that you contact connect_support@service.alipay.com to troubleshoot the issue. |
| MEDIA_TYPE_NOT_ACCEPTABLE | F | The server does not implement the media type that is acceptable to the client. | Use a media type that is accepted by Alipay+. |
| METHOD_NOT_SUPPORTED | F | The server does not implement the requested HTTPS method. | Ensure the HTTP method is POST. |
| NO_INTERFACE_DEF | F | API is not defined. | Check whether the request URL is correct. Ensure that the endpoint of the called API is correct. |
| PARAM_ILLEGAL | F | Illegal parameters. For example, non-numeric input, invalid date. | Check whether the request parameters, including the header parameters and body parameters, are correct and valid. For more information about the parameters of each API, see the Structure section of the specific API reference topic. |
| PROCESS_FAIL | F | A general business failure occurred. Do not retry. | It is recommended that you contact connect_support@service.alipay.com to troubleshoot the issue. |
| REQUEST_TRAFFIC_EXCEED_LIMIT | U | The request traffic exceeds the limit. | Reduce the frequency of API calls. |
| UNKNOWN_EXCEPTION | U | An API call failed, which is caused by unknown reasons. | Try to recall the API. |

## Request

### ALIPAY, ALIPAY_HK, DANA, H5, DIRECT

```json
{
  "region": "PH",
  "customerId": "21281****",
  "codeQuantity": "3",
  "env": {
    "deviceTokenId": "21212****"
  },
  "indirectMpp": {
    "indirectMppId": "xxxMppId",
    "indirectMppName": "xxxMppName"
  }
}
```

## Response

```json
{
  "result": {
    "resultCode": "SUCCESS",
    "resultMessage": "Success",
    "resultStatus": "S"
  },
  "paymentCodeInfoList": [
    {
      "paymentCode": "2892889331822611998",
      "codeValidityStartTime": "2024-01-16T15:19:20+08:00",
      "codeExpiryTime": "2024-01-16T15:20:20+08:00"
    },
    {
      "paymentCode": "2892889221822611928",
      "codeValidityStartTime": "2024-01-16T15:19:50+08:00",
      "codeExpiryTime": "2024-01-16T15:20:50+08:00"
    },
    {
      "paymentCode": "2892889441822611221",
      "codeValidityStartTime": "2024-01-16T15:20:20+08:00",
      "codeExpiryTime": "2024-01-16T15:21:20+08:00"
    }
  ]
}
```