Example of PHP code for integration with the gateway

Example of PHP code for integration with the gateway as per REST:

php
        <?php
 
/**
 * DATA FOR CONNECTION TO THE PAYMENT GATEWAY
 *
 * USERNAME         The store's login received when connecting.
 * PASSWORD         The store's password received when connecting.
 * GATEWAY_URL      Address of the payment gateway.
 * RETURN_URL       The address to redirect the user to
 *                  in case of successful payment.
 */
define('USERNAME', 'USERNAME');
define('PASSWORD', 'PASSWORD');
define('GATEWAY_URL', 'https://server/payment/rest/');
define('RETURN_URL', 'http://your.site/rest.php');
 
/**
 * FUNCTION FOR INTERACTION WITH THE PAYMENT GATEWAY
 *
 * cURL standard library is used to send POST requests
 * to the payment gateway.
 *
 * PARAMETERS
 *      method      Method from the API.
 *      data        Array of data.
 *
 * RESPONSE
 *      response    Response.
 */

function gateway($method, $data) {
    $curl = curl_init(); // Initializing the request
    curl_setopt_array($curl, array(
        CURLOPT_URL => GATEWAY_URL.$method, // Full address of the method
        CURLOPT_RETURNTRANSFER => true, // Return responce
        CURLOPT_POST => true, // POST method
        CURLOPT_POSTFIELDS => http_build_query($data) // Data in the request
    ));
    $response = curl_exec($curl); // Executing the request
     
    $response = json_decode($response, true); // Decoding from JSON to array
    curl_close($curl); // Closing the connection
    return $response; // Returning the response
}
 
/**
 * DISPLAYING THE FORM ON THE SCREEN
 */

if ($_SERVER['REQUEST_METHOD'] == 'GET' && !isset($_GET['orderId'])) {
    echo '
        <form method="post" action="/rest.php">
            <label>Order number</label><br />
            <input type="text" name="orderNumber" /><br />
            <label>Amount</label><br />
            <input type="text" name="amount" /><br />
            <button type="submit">Submit</button>
        </form>
    ';
}
 
/**
 * PROCESSING DATA FROM THE FORM
 */

else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $data = array(
        'userName' => USERNAME,
        'password' => PASSWORD,
        'orderNumber' => urlencode($_POST['orderNumber']),
        'amount' => urlencode($_POST['amount']),
        'returnUrl' => RETURN_URL
    );
 
    /**
     * REQUEST FOR REGISTRATION OF A ONE-PHASE PAYMENT IN THE PAYMENT GATEWAY
     *      register.do
     *
     * PARAMETERS
     *       userName       Store's login.
     *       password       Store's password.
     *       OrderNumber   Unique identifier of the order in the store.
     *       amount       The order amount in kopecks.
     *       returnUrl    The address to which the user must be redirected in case of successful payment.
     *
     * RESPONSE
     *      In case of an error:
     *      errorCode      Error code. The list of possible values is given in the table below.
     *      ErrorMessage   Error description.
     *
     *      In case of successful registration:
     *      orderId      The order number in the payment system. It is unique within the system.
     *      formUrl       URL of the payment form to which the client's browser must be redirected.
     *
     * Error code       Description
     *     0     Request processing took place without system errors.
     *     1      Order with this number is already registered in the system.
     *     3      Unknown (prohibited) currency.
     *     4      Mandatory request parameter is missing.
     *     5      Request parameter value error.
     *     7      System error.
     */

    $response = gateway('register.do', $data);
     
    /**
     * REQUEST FOR REGISTRATION OF A TWO-PHASE PAYMENT IN THE PAYMENT GATEWAY
     *      registerPreAuth.do
     *
     * The parameters and response are exactly the same as in the previous method.
     * It is necessary to call either register.do, or registerPreAuth.do.
     */

//    $response = gateway('registerPreAuth.do', $data);
     
    if (isset($response['errorCode'])) { // In case of an error, output it
        echo 'Error #' . $response['errorCode'] . ': ' . $response['errorMessage'];
    } else { // If successful, redirect the user to the payment form
        header('Location: ' . $response['formUrl']);
        die();
    }
}
 
/**
 * PROCESSING OF DATA AFTER THE PAYMENT FORM
 */
else if ($_SERVER['REQUEST_METHOD'] == 'GET' && isset($_GET['orderId'])){
    $data = array(
        'userName' => USERNAME,
        'password' => PASSWORD,
        'orderId' => $_GET['orderId']
    );
     
    /**
     * REQUEST FOR ORDER STATUS
     *      getOrderStatus.do
     *
     * PARAMETERS
     *       userName       Store's login.
     *       password       Store's password.
     *       orderId      The order number in the payment system. It is unique within the system.
     *
     * RESPONSE
     *      ErrorCode      Error code. The list of possible values is given in the table below.
     *      OrderStatus    The value of this parameter determines the order status in the payment system.
     *                     The list of possible values is given in the table below. It is missing if the order has not been found.
     *
     * Error code       Description
     *     0     Request processing took place without system errors.
     *     2     The order was rejected due to an error in the payment details.
     *     5     Access denied;
     *           The user must change password;
     *           The order number is not specified.
     *     6     Unknown order number.
     *     7     System error.
     *
     *  Order status  Description
     *      0      The order is registered, but not paid for.
     *      1      The pre-authorized amount is reserved (for two-phase payments).
     *      2      Full authorization of the order amount has been carried out.
     *      3      Authorization canceled.
     *      4      Refund operation was performed on the transaction.
     *      5      Authorization has been initiated through the issuing bank's ACS.
     *      6      Authorization denied.
     */

    $response = gateway('getOrderStatus.do', $data);
     
    // Error code output and order status
    echo '
        <b>Error code:</b> ' . $response['ErrorCode'] . '<br />
        <b>Order status:</b> ' . $response['OrderStatus'] . '<br />
    ';
}
?>