OPay Void API

In this page, you will learn how to use Void API. To void the successful transactions of the day.

OPay Void API

For the Bank Card payment, merchant can request this API to void the transaction so that no founds are transferred between the client and the merchant.The transaction is voided and it is not recorded on the client's statement. Voids can only be performed on transactions that have not yet been sent to the issuing bank for processing settlement at the end of day. Otherwise, merchants need to request Refund Payment API , to refund the payment to the client. For calling the API using the POST method you can use these endpoints URL:

In case you are still in development phase, you can call cashier payment status API using POST at the following staging endpoint API point URL

https://sandboxapi.opaycheckout.com/api/v1/international/payment/void/create

Once you are ready for production, you should use the following production API endpoint URL instead

https://api.opaycheckout.com/api/v1/international/payment/void/create

-Request Parameters:

  1. Header: Authorization(API Calls Signature), MerchantId
  2. Authorization    : Bearer {signature}
    MerchantId       : 256612345678901
  3. Json object containing the transaction information:
  4. {
        "country": "EG",
        "reference": "202111300001",
        "orderNo":"211220149663225819"
    }

HTTP POST parameters

-Here is a detailed description for the parameters you need to complete the POST request:

ParametertyperequiredDescription
countryStringrequiredCountry Code.See full list here
referenceStringrequiredThe unique void transaction number in merchant side
orderNoStringrequiredOPay transaction number, the orderNo field in the Bank Card payin API response

-An example call of Void API is as follows :

class CashierVoidController
{
    private $secretkey;
    private $merchantId;
    private $url;

    public function __construct() {
        $this->merchantId = '281822011134954';
        $this->secretkey = 'OPAYPRV16419*********993';
        $this->url = 'https://sandboxapi.opaycheckout.com/api/v1/international/payment/void/create';
    }

    public function test(){
        $data = [
            'country' => 'EG',
            'orderNo' => '220111140920511940',
            'reference' => '2021113000012331'
        ]
        ;
        $data2 = (string) json_encode($data,JSON_UNESCAPED_SLASHES);
        $auth = $this->auth($data2);
        $header = ['Content-Type:application/json', 'Authorization:Bearer '. $auth, 'MerchantId:'.$this->merchantId];
        $response = $this->http_post($this->url, $header, json_encode($data));
        $result = $response?$response:null;
        return $result;
    }

    private function http_post ($url, $header, $data) {
        if (!function_exists('curl_init')) {
            throw new Exception('php not found curl', 500);
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        $response = curl_exec($ch);
        $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $error=curl_error($ch);
        curl_close($ch);
        if (200 != $httpStatusCode) {
            print_r("invalid httpstatus:{$httpStatusCode} ,response:$response,detail_error:" . $error, $httpStatusCode);
        }
        return $response;
    }
    public function auth ( $data ) {
        $secretKey = $this->secretkey;
        $auth = hash_hmac('sha512', $data, $secretKey);
        return $auth;
    }

}
const request = require('request');
var sha512 = require('js-sha512');
const formData = {
  "country": "EG",
  "orderNo":"220111140947967800",
  "reference": "20211130000123312"
};

var privateKey = "OPAYPRV16419*********993"

var hash = sha512.hmac.create(privateKey);
hash.update(JSON.stringify(formData));
hmacsignature = hash.hex();
console.log(hmacsignature)
request({
    url: 'https://sandboxapi.opaycheckout.com/api/v1/international/payment/void/create',
    method: 'POST',
    headers: {
      'MerchantId': '281822011134954',
      'Authorization': 'Bearer '+hmacsignature
    },
    json: true,
    body: formData
  }, function (error, response, body) {
    console.log('body: ')
    console.log(body)
  }
)
curl --location --request POST 'https://sandboxapi.opaycheckout.com/api/v1/international/payment/void/create' \
--header 'MerchantId: 281822011134954' \
--header 'Authorization: Bearer ac5acb015b9087aede4d2e0a0b38c8ed1311686ea9ea9ef0d2c2009ba16fec3a11f4f39f95c8f8f9c76ead9dddc211d7a3ff2f7bcedcaf83995b0919e43713e6' \
--header 'Content-Type: application/json' \
--data-raw '{
    "country": "EG",
    "orderNo":"220111140920513723",
    "reference": "20211130000123311"
}'
import com.google.gson.Gson;
import org.apache.commons.codec.binary.Hex;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.TreeMap;
import java.util.UUID;

public class VoidDoc {

    private static final String privateKey = "OPAYPRV16419*********993";

    private static final String endpoint = "https://sandboxapi.opaycheckout.com";

    private static final String merchantId = "281822011134954";

    public static void main(String[] args) throws Exception {
        String addr = endpoint + "/api/v1/international/payment/void/create";
        Gson gson = new Gson();
        TreeMap order = new TreeMap<>();
        order.put("country","EG");
        order.put("orderNo","220111140920513723");
        order.put("reference", UUID.randomUUID().toString());

        String requestBody = gson.toJson(order);
        System.out.println("--request:");
        System.out.println(requestBody);
        String oPaySignature = hmacSHA512(requestBody, privateKey);
        System.out.println("--signature:");
        System.out.println(oPaySignature);

        URL url = new URL(addr);
        HttpURLConnection con = (HttpURLConnection)url.openConnection();
        con.setRequestMethod("POST");
        con.setRequestProperty("Content-Type", "application/json; utf-8");
        con.setRequestProperty("Authorization", "Bearer "+oPaySignature);
        con.setRequestProperty("MerchantId", merchantId);
        con.setDoOutput(true);
        OutputStream os = con.getOutputStream();
        byte[] input = requestBody.getBytes(StandardCharsets.UTF_8);
        os.write(input, 0, input.length);
        BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8));
        StringBuilder response = new StringBuilder();
        String responseLine = null;
        while ((responseLine = br.readLine()) != null) {
            response.append(responseLine.trim());
        }

        System.out.println("--response:");
        System.out.println(response.toString());
        //close your stream and connection
    }

    public static String hmacSHA512(final String data, final String secureKey) throws Exception{
        byte[] bytesKey = secureKey.getBytes();
        final SecretKeySpec secretKey = new SecretKeySpec(bytesKey, "HmacSHA512");
        Mac mac = Mac.getInstance("HmacSHA512");
        mac.init(secretKey);
        final byte[] macData = mac.doFinal(data.getBytes());
        byte[] hex = new Hex().encode(macData);
        return new String(hex, StandardCharsets.UTF_8);
    }
}

Header must contain the "Signature" , "MerchantId"

Signature is calculated using SHA-512 HMAC signed with your Private Key. See API Calls Signature section for more details.

Void API Response

-Response Parameters:

the parameters contained in the response received whenever you call the Transaction Create API as a JSON Object.

{
    "code": "00000",
    "message": "SUCCESSFUL",
    "data": {
        "reference": "202111300001",
        "orderNo": "211220149663225819",
        "voidOrderNo": "211220149663225822",
        "orderStatus": "SUCCESS",
        "voidAmount": {
            "total": 10000,
            "currency": "EGP"
        },
        "errorCode": "null",
        "errorMsg": "null"
     }
}

-Here is a detailed description for the parameters received in the response:

ParametertypeDescriptionexample
referenceStringThe unique void transaction number in merchant side.202111300001
orderNoStringOPay transaction number, the orderNo field in the Bank Card payin API response.211220149663225819
voidOrderNoStringThe void transaction number in OPay side.211220149663225822
orderStatusStringThis status of void order.[PENDING, SUCCESS, FAIL, CLOSE]
voidAmount
totalLongThe void amount,it is same as the transaction amount.10000(cent unit)
currencyStringCurrencyEGP
errorCodeStringError code for Void API.02000
errorMsgStringError message for Void API.authentication failed.

Error Handling

After submitting an API call to OPay, you receive a response back to inform you that your request was received and processed. A successful OPay API should return a status code 00, meanwhile, in a situation where any payment processing error occurred, you will receive an error code with a message to describe the reason of the error. A sample error response can be found below.

{
    "code": "02006",
    "message": "payment not found."
}

Depending on the HTTP status code of the response, you should build some logic to handle any errors that a request or the system may return. A list of possible potential error codes that you may receive can be found below. A full list of all possible error codes can be found in the Error Codes section.

Error CodeError Message
02000authentication failed.
02001request parameters not valid.
02006not found.

What's Next?