Reference Code Payment


On this page, we'll show you how to generate reference codes for your clients through our Reference Code API in a few steps:

  • Collect your client's payment Information.
  • Trigger OPay Reference Code payment API with your collected information.
  • OPay will respond with the reference code.

Collect your Client's Payment Information

Once your client's Payment information is ready, you will need to trigger the reference code API with the collected information. This form should collect:

  1. Product Information:
    • Name
    • Description

  2. Preferred Payment Method

  3. Client Information:
    • User Name
    • User Email
    • User Mobile
    • Language

Create Reference Code Payment


To test your Reference Code Payment, you need to request using the POST method in our sandbox environment.
Here is the request URL:

Note

Staging

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


-Once you have a fully tested payment flow and you are ready for production, use the following production API endpoint URL instead

Note

Production

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


Request Parameters:

  1. Header: Authorization(API Calls Signature), MerchantId
  2.                         
                                Authorization    : Bearer {signature}
                                MerchantId       : 256612345678901
                            
                        

  3. Json object containing the transaction information:
  4.         
                                {
                                    "amount": {
                                        "currency": "NGN",
                                        "total": 500
                                    },
                                    "callbackUrl": "https://your-call-back-url.com",
                                    "country": "NG",
                                    "expireAt": 300,
                                    "merchantName": "beijing NGN",
                                    "notify": {
                                        "notifyLanguage": "English",
                                        "notifyMethod":"EMAIL",
                                        "notifyUserEmail": "test@email.com",
                                        "notifyUserMobile": "+23488889999",
                                        "notifyUserName": "David"
                                    },
                                    "payMethod": "ReferenceCode",
                                    "product": {
                                        "description": "description",
                                        "name": "name"
                                    },
                                    "reference": "4567898765678"
                                }
            
        

HTTP POST Parameters

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

Parameter type required Description
reference String required reference Id.
merchantName String optional Merchant Name to be displayed in cashier checkout form.
amount
total String required payment total amount.
currency String required currency type.
callbackUrl String optional If you have sent callbackUrl through API, OPay will send callback notification to this callbackUrl. If you didn't send callbackUrl through API, you need to configure webhook url on the merchant dashboard, and OPay will send callback notification to this webhook url. See callback here
product
name String required product name.
description String required Product description.
country String required Country Code.
payMethod String required The preferred payment method to be presented to your customer.
expireAt String required Payment expiration time in minutes. Default 30.
notify
notifyUserName String required Notify user name.
notifyLanguage String required Notify message language. Value: English or Arabic.
notifyUserEmail String required Notify user email. If notifyMethod is Both or Email, this parameter is required.
notifyUserMobile String required Notify user mobile. If notifyMethod is Both or SMS, this parameter is required.
notifyMethod String optional Reference Code notification method. Value: BOTH,EMAIL or SMS. Default is BOTH.

An example of Reference Code payment request for an amount of 400 NGN is as follows :

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

                                    public function __construct() {
                                        $this->merchantId = '281821110129700';
                                        $this->secretkey = 'OPAYPRV*******0187828';
                                        $this->url = 'https://testapi.opaycheckout.com/api/v1/international/payment/create';
                                    }

                                    public function test(){
                                        $data = [
                                            'amount' => [
                                                'currency' => 'NGN',
                                                'total' => 500
                                            ],
                                            'callbackUrl' => 'https://your-call-back-url.com',
                                            'country' => 'NG',
                                            'expireAt' => 300,
                                            'merchantName' => 'beijing NGN',
                                            'notify' => [
                                                'notifyLanguage' => 'English',
                                                'notifyMethod' => 'EMAIL',
                                                'notifyUserEmail' => 'xxx@xxx.com',
                                                'notifyUserMobile' => '121312312xxx',
                                                'notifyUserName' => 'your customer name '
                                            ],
                                            'payMethod' => 'ReferenceCode',
                                            'product' => [
                                                'description' => 'description',
                                                'name' => 'name'
                                            ],
                                            'reference' => '041233981115'
                                        ];
                                        $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 = {
                                        "amount": {
                                            "currency": "NGN",
                                            "total": 500
                                        },
                                        "callbackUrl": "https://your-call-back-url.com",
                                        "country": "NG",
                                        "expireAt": 300,
                                        "merchantName": "beijing NGN",
                                        "notify": {
                                            "notifyLanguage": "English",
                                            "notifyMethod":"EMAIL",
                                            "notifyUserEmail": "xxx@xxx.com",
                                            "notifyUserMobile": "121312312xxx",
                                            "notifyUserName": "your customer name"
                                        },
                                        "payMethod": "ReferenceCode",
                                        "product": {
                                            "description": "description",
                                            "name": "name"
                                        },
                                        "reference": "4567898765678"
                                    };

                                    var privateKey = "OPAYPRV*******0187828"

                                    var hash = sha512.hmac.create(privateKey);
                                    hash.update(JSON.stringify(formData));
                                    hmacsignature = hash.hex();
                                    console.log(hmacsignature)
                                    request({
                                        url: 'https://testapi.opaycheckout.com/api/v1/international/payment/create',
                                        method: 'POST',
                                        headers: {
                                          'MerchantId': '256621051120756',
                                          'Authorization': 'Bearer '+hmacsignature
                                        },
                                        json: true,
                                        body: formData
                                      }, function (error, response, body) {
                                        console.log('body: ')
                                        console.log(body)
                                      }
                                    )

                                
                            
                                
                                    curl --location --request POST 'https://testapi.opaycheckout.com/api/v1/international/payment/create' \
                                    --header 'MerchantId: 256621051120756' \
                                    --header 'Authorization: Bearer 38017496e*******2ab0297d1de8905cb7d85a043d9084254e61a8da6093b6f155550ff51891dca9f9889a' \
                                    --header 'Content-Type: application/json' \
                                    --data-raw '{
                                        "amount": {
                                            "currency": "NGN",
                                            "total": 500
                                        },
                                        "callbackUrl": "https://your-call-back-url.com",
                                        "country": "NG",
                                        "expireAt": 300,
                                        "merchantName": "beijing NGN",
                                        "notify": {
                                            "notifyLanguage": "English",
                                            "notifyMethod":"EMAIL",
                                            "notifyUserEmail": "xxx@xxx.com",
                                            "notifyUserMobile": "121312312xxx",
                                            "notifyUserName": "your customer name"
                                        },
                                        "payMethod": "ReferenceCode",
                                        "product": {
                                            "description": "description",
                                            "name": "name"
                                        },
                                        "reference": "4567898765678"
                                    }'
                                
                            
                            
                                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 ReferenceCodePayment {

                                    private static final String privateKey = "OPAYPRV*******0187828";

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

                                    private static final String merchantId = "256621051120756";

                                    public static void main(String[] args) throws Exception {
                                        String addr = endpoint + "/api/v1/international/payment/create";
                                        Gson gson = new Gson();
                                        TreeMap order = new TreeMap<>();
                                        TreeMap amount = new TreeMap<>();
                                        amount.put("currency","NGN");
                                        amount.put("total",new Integer(400));
                                        order.put("amount",amount);
                                        TreeMap product = new TreeMap<>();
                                        product.put("name","your product name");
                                        product.put("description","your product description");
                                        order.put("product",product);
                                        order.put("callbackUrl","https://your-call-back-url.com");
                                        order.put("country","NG");
                                        order.put("expireAt",30);
                                        order.put("payMethod","ReferenceCode");
                                        order.put("reference", UUID.randomUUID().toString());
                                        order.put("returnUrl","https://your-return-url.com");
                                        TreeMap notify = new TreeMap<>();
                                        notify.put("notifyLanguage","English");
                                        notify.put("notifyMethod","EMAIL");
                                        notify.put("notifyUserEmail","xxx@xxx.com");
                                        notify.put("notifyUserMobile","121312312xxx");
                                        notify.put("notifyUserName","your customer name");
                                        order.put("notify",notify);

                                        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);
                                    }
                                }

                            
                        
Note

Note

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.

Reference Code Payment Response


Response Parameters:

The parameters contained in the response received whenever you call the Reference Code payment API as a JSON Object.

                    
                        {
                          "code": "00000",
                          "message": "SUCCESSFUL",
                          "data": {
                            "reference": "1001000",
                            "orderNo": "10212100000034000"
                            "status":"PENDING",
                            "amount":{
                                "total":400,
                                "currency":"NGN"
                            },
                            "referenceCode":"674016496"
                          }
                        }
                
                        

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

Parameter type Description example
reference String reference Id. 1001000
orderNo String order id. 10212100000034000
status String Order Status. Success
amount
total Integer payment total amount. 400
currency String currency type. NGN
referenceCode String The Generated Reference Code. 674016496

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": "20000",
                                "message": "Duplicate merchant order number",
                                "data": null
                            }
                        
                    

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 Code Error Message
09 Time out.
90 System failure.
91 Refund error, please try again later.
96 Search order error, please try again later.
97 Create checkout session failed.
98 Invalid request header with requestId.
99 Request channel parameters are not valid.
20000 Duplicate merchant order number.

What's Next?


Reference Code Payin Verification

You can verify the 9 digits code for Reference Code Payin. After verification, you will receive a callback notification.

User Profile 12 messages

James Jones
Application Developer
Recent Notifications
Another purpose persuade Due in 2 Days
+28%
Would be to people Due in 2 Days
+50%
-27%
The best product Due in 2 Days
+8%