# Web/WAP 

> Learn how to integrate the APM SDK into a web or WAP client.

In this topic, you'll learn how to integrate the pop-up payment experience SDK into a web or WAP client to accept APM (Alternative Payment Methods) payments. This will help you render APM cashier pages both in a computer and mobile browser.

---

## Prerequisites

Before the integration, ensure that you have completed the following tasks:

- Install npm (Node Package Manager).
- Properly handle compatibility issues: Provide corresponding polyfills for Internet Explorer and other old browser versions. We recommend that you use [babel-preset-env](https://babeljs.io/docs/babel-preset-env) to address browser compatibility issues when you build a project.
- Use the following recommended browser versions:
  - For mobile browsers:
    - iOS 11 and later
    - Android 5.0 and later
  - For computer browsers, use the following recommended versions:

| Edge | Firefox | Chrome | Safari | Opera | Electron |
|------|---------|--------|--------|-------|----------|
| Latest 2 versions | Latest 2 versions | Latest 2 versions | Latest 2 versions | Latest 2 versions | Last 2 versions |

---

## Key integration steps

Integrate the SDK by following these steps:

### Step 1: Integrate the SDK package

**Merchant client**

To integrate the SDK package, please refer to [Integrate the SDK Package for Web/WAP](https://docs.antom.com/ac/sdks/web.md).

### Step 2: Create the SDK instance by using the `AMSCashierPayment`

**Merchant client**

- Create a config object: Required. The object must contain all of the following configuration parameters:
  - **locale**: Optional. String type. It is used to pass in language information. Valid values are listed as follows. If other values are passed, English is used by default:
    - `en_US`: English
    - `es_ES`: Spanish
    - `fr_FR`: French
    - `nl_NL`: Dutch
    - `it_IT`: Italian
    - `de_DE`: German
    - `zh_CN`: Simplified Chinese
  - **environment**: Required string. It is used to pass in environmental information. Valid values are:
    - `sandbox`: Sandbox environment
    - `prod`: Production environment
  - **analytics**: Optional object. It is used to configure and analyze data. It contains a value below:
    - **enabled**: Optional Boolean. It defaults to `true`, which indicates that you allow the SDK to upload and analyze operational data to deliver a better service. If you do not allow the data to be uploaded and analyzed, specify it as `false`.
  - **onLog**: Optional. It is a callback method that is used to generate the error information about logs and API exceptions during the execution of the SDK.
  - **onEventCallback**: Optional. A callback function that returns various types of data or values, including event codes. See the [References](https://docs.antom.com/ac/sdks/ref.md) for further information.
  - **onError**: Optional. Triggered when an error occurs during SDK usage (initialization exception, network timeout, etc.). Refer to the error codes in the References.

- Instantiate `AMSCashierPayment`.

> **Note**: The environment set by environment should be consistent with the environment in which you call the [createPaymentSession](https://docs.antom.com/ac/apo/payment_session.md) API in step 3.

**Example - Obtain the browser language:**

```javascript
let language = navigator.language || navigator.userLanguage;
language = language.replace("-", "_"); // Replace "-" with "_"
```

**Example - Create an SDK instance (npm):**

```javascript
import { AMSCashierPayment } from '@alipay/ams-checkout' // Package management

const checkoutApp = new AMSCashierPayment({
  environment: "sandbox",
  locale: "en_US",
  analytics: {
    enabled: true
  },
  onLog: ({code, message})=>{},
  onEventCallback: ({code, result})=>{},
  onError: ({code, result})=>{}
});
```

**Example - Create an SDK instance (CDN):**

```javascript
const checkoutApp = new window.AMSCashierPayment({
  environment: "sandbox",
  locale: "en_US",
  analytics: {
    enabled: true
  },
  onLog: ({code, message})=>{},
  onEventCallback: ({code, result})=>{},
  onError: ({code, message})=>{}
});
```

### Step 3: Initiate a createPaymentSession request

**Merchant server**

The merchant client monitors the click events of the Payment button itself. When the buyer selects the APM payment method and clicks the Payment button, the merchant client initiates [createPaymentSession](https://docs.antom.com/ac/apo/payment_session.md) request to the APO server. Once the response to the [createPaymentSession](https://docs.antom.com/ac/apo/payment_session.md) request is received, the `paymentSessionData` value in the response is used in Step 4.

**Example - createPaymentSession request:**

```json
{
  "order": {
    "buyer": {
      "referenceBuyerId": "testbuyer123"
    },
    "orderAmount": {
      "currency": "EUR",
      "value": "100"
    },
    "orderDescription": "Test order",
    "referenceOrderId": "ORDER123456"
  },
  "paymentAmount": {
    "currency": "EUR",
    "value": "100"
  },
  "paymentMethod": {
    "paymentMethodType": "ALIPAYVN"
  },
  "paymentNotifyUrl": "https://www.yourdomain.com/notify",
  "paymentRedirectUrl": "https://www.yourdomain.com/return",
  "paymentRequestId": "PR123456789",
  "productCode": "CASHIER_PAYMENT",
  "settlementStrategy": {
    "settlementCurrency": "EUR"
  }
}
```

**Example - Response for createPaymentSession request:**

```json
{
  "paymentSessionData": "UNvjVWnWPXJA4BgW+vfjsQj7PbOraafHY19X+6EqMz6Kvvmsdk+akdLvoShW5avHX8e8J15P8uNVEf/PcCMyXg==&&SG&&111",
  "paymentSessionExpiryTime": "2023-04-06T03:28:49Z",
  "paymentSessionId": "UNvjVWnWPXJA4BgW+vfjsQj7PbOraafHY19X+6EqMz6Ikyj9FPVUOpv+DjiIZqMe",
  "result": {
    "resultCode": "SUCCESS",
    "resultMessage": "success.",
    "resultStatus": "S"
  }
}
```

### Step 4: Create an APM payment component using the createComponent method in the instance object

**Merchant client**

- Create a configuration object using the sessionData parameter: Pass in the complete data of the `paymentSessionData` field obtained in the response of the [createPaymentSession](https://docs.antom.com/ac/apo/payment_session.md) request into the `sessionData` parameter.

- Call the `createComponent()` method in the instance object, returning a `Promise` with the component instance object obtained using this method. The payment block DOM node can render a pop-up view on the current page based on parameters.
  - **notRedirectAfterComplete**: Optional, Boolean type. The default value is `false`, which means it will jump back to your page after the payment is completed. The same applies when the value is empty. `true` value means there will be no jump after the payment is completed. You need to use the customer event code to control the payment to complete the subsequent process. Please note that the payment result event code returned by the client is only used as a reference for the jump operation of the client page. For transaction status's updates, please refer to the results returned by the server's [notifyPayment](https://docs.antom.com/ac/apo/paymentrn_online.md) or [inquiryPayment](https://docs.antom.com/ac/apo/paymentri_online.md) API.

- Free resources of the SDK component by calling the `unmount()` method in the instance object. You need to unmount a component in the following cases:
  - When the user switches views to exit the checkout page, free the component resources created in the [createPaymentSession](https://docs.antom.com/ac/apo/payment_session.md).
  - When the user initiates multiple payments, free the component resources created in the previous [createPaymentSession](https://docs.antom.com/ac/apo/payment_session.md).
  - When the user completes the payment and sets `notRedirectAfterComplete` to `true`, free the component resources after obtaining specific payment result codes.

**Example - Call the createComponent() method:**

```javascript
async function create(sessionData) {
  await checkoutApp.createComponent({ 
    sessionData: sessionData, 
    notRedirectAfterComplete: true,
  });
}
// Free SDK component resources
checkoutApp.unmount();
```

---

## References

The References offers callback data and code samples for integration steps. For further details, refer to the [References](https://docs.antom.com/ac/sdks/ref.md).

---