# Web SDK

## Javascript SDK for NeoX Payment Gateway

**Tutorial & Code sample**: [NeoX SDK(javascript)](https://github.com/neopayvn/neopay-pg-js-sdk)

**Use**

```
<!-- example-payment.html -->
<!DOCTYPE html>
<html lang="vi-VN">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>NEOPAY SDK Example</title>
  </head>

  <body>
    <button onclick="onPayWithPopup()">Thanh toán (Popup)</button>
    <br />
    <br />
    <button onclick="onPayWithRedirect()">Thanh toán (Redirect)</button>
  </body>

  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  <script
    neopay-sdk
    type="text/javascript"
    src="https://cdn.jsdelivr.net/gh/neopayvn/neopay-pg-js-sdk/sdk/neopay-sdk.min.js"
  ></script>
  <script>
    const MERCHANT_CODE = "RZRGNY";
    const HASH_KEY = "4F99C21FE8A14FD198FA00D18662A63B";
    $(document).ready(function () {
      const neo_PaymmentBaseUrl = "https://sandbox-api.neopay.vn/pg";
      const configUI = {
        neo_HiddenHeader: false,
        neo_HiddenFooter: true,
        neo_HiddenPaymentMethod: false,
        neo_HiddenOrderInfo: false,
      };
      const callbacks = {
        onSuccess: (data) => {
          console.log("success", data);
        },
        onFailure: (data) => {
          console.log("failure:", data);
        },
        onClose: (data) => {
          console.log(data);
        },
      };
      neopaySDK.init(neo_PaymmentBaseUrl, configUI, callbacks);
    });
    function onPayWithPopup() {
      const config = {
        neo_MerchantCode: MERCHANT_CODE,
        neo_PaymentMethod: ["WALLET", "ATM", "CC", "QR"],
        neo_Currency: "VND",
        neo_Locale: "vi",
        neo_Version: "1",
        neo_Command: "PAY",
        neo_Amount: Math.floor(Math.random() * 600000) + 100000,
        neo_MerchantTxnID: `T${`${Date.now()}`.slice(-8)}`,
        neo_OrderID: `DH${`${Date.now()}`.slice(-8)}`,
        neo_OrderInfo: `Thanh toán ĐH Test`,
        neo_Title: "Thanh toán",
        neo_ReturnURL: "https://sandbox-api.neopay.vn/pg/paygate/tryitnow",
        neo_ViewType: "POPUP", //POPUP | REDIRECT
      };
      neopaySDK.pay(config, HASH_KEY);
    }

    function onPayWithRedirect() {
      const config = {
        neo_MerchantCode: MERCHANT_CODE,
        neo_PaymentMethod: ["WALLET", "ATM", "CC", "QR"],
        neo_Currency: "VND",
        neo_Locale: "vi",
        neo_Version: "1",
        neo_Command: "PAY",
        neo_Amount: Math.floor(Math.random() * 600000) + 100000,
        neo_MerchantTxnID: `T${`${Date.now()}`.slice(-8)}`,
        neo_OrderID: `DH${`${Date.now()}`.slice(-8)}`,
        neo_OrderInfo: `Thanh toán ĐH Test`,
        neo_Title: "Thanh toán",
        neo_ReturnURL: "https://sandbox-api.neopay.vn/pg/paygate/tryitnow",
        neo_ViewType: "REDIRECT", //POPUP | REDIRECT
      };
      neopaySDK.pay(config, HASH_KEY);
    }
  </script>
</html>
```

```
<!DOCTYPE html>
<html lang="vi-VN">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>NEOPAY SDK Example Checkout</title>
  </head>

  <body>
    <span id="neopay-checkout">Checkout</span>
  </body>
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  <script
    neopay-sdk
    type="text/javascript"
    src="https://cdn.jsdelivr.net/gh/neopayvn/neopay-pg-js-sdk/sdk/neopay-sdk.min.js"
  ></script>
  <script>
    $(document).ready(function () {
      const neo_PaymmentBaseUrl = "https://sandbox-api.neopay.vn/pg";
      const configUI = {
        neo_HiddenHeader: false,
        neo_HiddenFooter: true,
      };
      const callbacks = {
        onSuccess: (data) => {
          console.log("success", data);
        },
        onFailure: (data) => {
          console.log("failure:", data);
        },
        onClose: (data) => {
          console.log(data);
        },
      };
      neopaySDK.init(neo_PaymmentBaseUrl, configUI, callbacks);
    });
  </script>
</html>
```

#### Methods

<table><thead><tr><th width="179">Method</th><th>Description</th><th data-hidden></th></tr></thead><tbody><tr><td>init</td><td>Initialize the necessary configurations for the SDK and UI</td><td></td></tr><tr><td>pay</td><td>Payment</td><td></td></tr><tr><td>close</td><td>Close payment popup</td><td></td></tr></tbody></table>

**neopaySDK.init (neo\_PaymmentBaseUrl, configUI, callbacks)**<br>

**neo\_PaymmentBaseUrl**

Sandbox environment testing URL: *<mark style="color:blue;"><https://sandbox-api.neopay.vn/pg></mark>*

#### configUI

<table><thead><tr><th width="265">Parammetter</th><th width="112">Data Type</th><th width="136">Default Value</th><th>Description</th></tr></thead><tbody><tr><td>neo_HiddenHeader</td><td>boolean</td><td>false</td><td>Hide header</td></tr><tr><td>neo_HiddenFooter</td><td>boolean</td><td>false</td><td>Hide footer</td></tr><tr><td>neo_HiddenPaymentMethod</td><td>boolean</td><td>false</td><td>Hide payment method</td></tr><tr><td>neo_HiddenOrderInfo</td><td>boolean</td><td>false</td><td>Hide order information</td></tr></tbody></table>

#### neopaySDK.pay (config, hashKey)

<table><thead><tr><th width="200">Parameter</th><th width="109">Data Type</th><th width="130">Default Value</th><th width="102">Required</th><th>Description</th></tr></thead><tbody><tr><td>neo_MerchantCode</td><td>string</td><td></td><td>Y</td><td>Merchant code is provided by NeoX</td></tr><tr><td>neo_Currency</td><td>string</td><td>VND</td><td>Y</td><td>Transaction currency, VND as default</td></tr><tr><td>neo_Locale</td><td>string</td><td>vi</td><td>Y</td><td>Language will display on the checkout page. Support: “vi”, “en”.</td></tr></tbody></table>

#### neopaySDK.pay (config, hashKey)

<table><thead><tr><th width="212">Parameter</th><th>Data Type</th><th width="133">Default Value</th><th width="103">Required</th><th>Descript</th></tr></thead><tbody><tr><td>neo_MerchantCode</td><td>string</td><td></td><td>Y</td><td>Merchant code is provided by NeoX</td></tr><tr><td>neo_Currency</td><td>string</td><td>VND</td><td>Y</td><td>Transaction currency, VND as default</td></tr><tr><td>neo_Locale</td><td>string</td><td>vi</td><td>Y</td><td>Language will display on the checkout page. Support: “vi”, “en”.</td></tr><tr><td>neo_Version</td><td>string</td><td>1</td><td>Y</td><td>Payment gateway version, “1” as default</td></tr><tr><td>neo_Command</td><td>string</td><td>PAY</td><td>Y</td><td>“PAY” as default</td></tr><tr><td>neo_Amount</td><td>number</td><td></td><td>Y</td><td>Payment amount</td></tr><tr><td>neo_MerchantTxnID</td><td>string</td><td></td><td>Y</td><td>Unique - Transaction ID is provided by merchant</td></tr><tr><td>neo_OrderID</td><td>string</td><td></td><td>Y</td><td>Oder ID</td></tr><tr><td>neo_OrderInfo</td><td>string</td><td></td><td>Y</td><td>Oder information</td></tr><tr><td>neo_Title</td><td>string</td><td></td><td>Y</td><td>Thông tin tiêu đề sẽ hiển thị trên trang thanh toán</td></tr><tr><td>neo_ReturnURL</td><td>string</td><td></td><td>Y</td><td>The website URL of the merchant</td></tr><tr><td>neo_ViewType</td><td>string</td><td>"POPUP" / "REDIRECT"</td><td>Y</td><td>Choose to open payment gateway as popup or redirect</td></tr><tr><td>neo_PaymentMethod</td><td>string</td><td>["WALLET", "ATM", "CC", "QR"]</td><td>N</td><td>Allows to choose to display direct or list payment channels. If this field is not transmitted, all channels will be displayed.</td></tr></tbody></table>

#### Request sample

```
{
  neo_MerchantCode: "RZRGNY",
  neo_PaymentMethod: ["WALLET", "ATM", "CC", "QR"],
  neo_Currency: "VND",
  neo_Locale: "vi",
  neo_Version: "1",
  neo_Command: "PAY",
  neo_Amount: Math.floor(Math.random() * 600000) + 100000,
  neo_MerchantTxnID: `T${`${Date.now()}`.slice(-8)}`,
  neo_OrderID: `DH${`${Date.now()}`.slice(-8)}`,
  neo_OrderInfo: `Payment for Test Order`,
  neo_Title: "Payment",
  neo_ReturnURL: "https://sandbox-api.neopay.vn/pg/paygate/tryitnow",
  neo_ViewType: "POPUP",
}
```

#### callback

<table><thead><tr><th width="208">Event</th><th>Description</th></tr></thead><tbody><tr><td>onSuccess</td><td>The event occurs when payment is successful.</td></tr><tr><td>onFailure</td><td>The event occurs when payment fails.</td></tr><tr><td>onClose</td><td>The event occurs when the popup is closed.</td></tr></tbody></table>

**Callback data from onSuccess and onFailure**

<table><thead><tr><th width="218">Parameter</th><th width="109.00000000000003">Data Type</th><th>Description</th></tr></thead><tbody><tr><td>neo_MerchantCode</td><td>string</td><td>Merchant code is provided by NeoX</td></tr><tr><td>neo_Currency</td><td>string</td><td>Transaction currency, VND as default</td></tr><tr><td>neo_Locale</td><td>string</td><td>Language will display on the checkout page. Support: “vi”, “en”.</td></tr><tr><td>neo_Version</td><td>string</td><td>Payment gateway version, “1” as default</td></tr><tr><td>neo_Command</td><td>string</td><td>“PAY” as default</td></tr><tr><td>neo_Amount</td><td>string</td><td>Payment amount</td></tr><tr><td>neo_MerchantTxnID</td><td>string</td><td>Unique - Transaction ID is provided by merchant</td></tr><tr><td>neo_OrderID</td><td>string</td><td>Oder ID</td></tr><tr><td>neo_OrderInfo</td><td>string</td><td>Oder Information</td></tr><tr><td>neo_TransactionID</td><td>string</td><td>Unique - Transaction ID is generated by the NeoX system,  for reconciling.</td></tr><tr><td>neo_ResponseCode</td><td>number</td><td>The error code is returned by NeoX, indicates the transaction result.</td></tr><tr><td>neo_ResponseMsg</td><td>string</td><td>Error description.</td></tr><tr><td>neo_CustomerID</td><td>string</td><td>The customer ID on the merchant system.</td></tr><tr><td>neo_ResponseData</td><td>string</td><td>The data response of customer's payment.</td></tr><tr><td>neo_SecureHash</td><td>string</td><td>Use SHA256 to hash the above parameters (sort the key by Alphabet order) + Secret Key (will be provided by NeoX after actual integration)</td></tr></tbody></table>

#### Response sample

```
{
  "neo_Amount": "112214",
  "neo_Command": "PAY",
  "neo_Currency": "VND",
  "neo_Locale": "vi",
  "neo_MerchantCode": "RZRGNY",
  "neo_MerchantTxnID": "T23343243",
  "neo_OrderID": "DH28900084",
  "neo_OrderInfo": "Thanh toán ĐH Test",
  "neo_ResponseCode": "0",
  "neo_ResponseData": "",
  "neo_ResponseMsg": "Success",
  "neo_SecureHash": "80A669689BEE56D02211F8C762D828194C5B3AD121420433D280696B952F3A19",
  "neo_TransactionID": "5226",
  "neo_Version": "1"
}
```
