Create Single Invoice
You can issue invoices programmatically just like you do through OPay merchant dashboard.
In this page, you will learn how to issue single invoice.
Create Single Invoice
To create single invoice, you need to request using the POST method in our sandbox environment.
-Here is the request URL:
https://sandboxapi.opaycheckout.com/api/v1/international/invoice/createSingleInvoice
-Once you have a fully tested payment flow and you are ready for production, use the following production API endpoint URL instead
https://api.opaycheckout.com/api/v1/international/invoice/createSingleInvoice
-Request Parameters:
- Header: Authorization(API Calls Signature), MerchantId
- Json object containing the request information:
Authorization : Bearer {signature}
MerchantId : 256612345678901
{
"amount": {
"currency": "EGP",
"total": 1000
},
"country": "EG",
"customer": {
"customerEmail": "test@email.com",
"customerMobile": "+201066668888",
"customerName": "OPay Test Name"
},
"description": "test invoice description",
"dueTime": 1649973600,
"lang": "EN",
"name": "OPay Test Invoice",
"notifyMethod": "SMS",
"productList": [{
"merchantProductNo": "1000000570",
"quantity": 1
}, {
"merchantProductNo": "1000000571",
"quantity": 2
}],
"relationProduct": false,
"serviceFee": {
"currency": "EGP",
"feeRate":"1",
"fixedFee": 1
}
}
HTTP POST Parameters
-Here is a detailed description for the parameters you need to complete the POST request:
-An example of invoice create request is as follows :
class CreateSingleInvoiceController
{
private $secretkey;
private $merchantId;
private $url;
public function __construct() {
$this->merchantId = '256612345678901';
$this->secretkey = 'OPAYPRV16357783346230.07653446106051542';
$this->url = 'https://sandboxapi.opaycheckout.com/api/v1/international/invoice/createSingleInvoice';
}
public function test(){
$data = [
'amount' => [
'currency' => 'EGP',
'total' => 1100
],
'country' => 'EG',
'customer' => [
'customerEmail' => '103790xxxxx@qq.com',
'customerMobile' => '+201110024320',
'customerName' => 'customerName'
],
'description' => 'description',
'dueTime' => 1698363138,
'lang' => 'AR',
'name' => 'invoice_test',
'notifyMethod' => 'BOTH',
'productList' => [
[
'merchantProductNo' => '1010',
'quantity' => 2
]
],
'relationProduct' => true,
'customer' => [
'currency' => 'EGP',
'feeRate' => '1',
'fixedFee' => 1
],
]
;
$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": "EGP",
"total": 1100
},
"country": "EG",
"customer": {
"customerEmail": "10379xxxx@xx.com",
"customerMobile": "+201110024320",
"customerName": "customerName"
},
"description": "test description",
"dueTime": 1698363138,
"lang": "AR",
"name": "invoice_test",
"notifyMethod": "BOTH",
"productList": [
{
"merchantProductNo": "1010",
"quantity": 2
}
],
"relationProduct": true,
"serviceFee": {
"currency": "EGP",
"feeRate":"1",
"fixedFee": 1
}
};
var privateKey = "OPAYPRV16357783346230.07653446106051542"
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/invoice/createSingleInvoice',
method: 'POST',
headers: {
'MerchantId': '256612345678901',
'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/invoice/createSingleInvoice' \
--header 'MerchantId: 256612345678901' \
--header 'Authorization: Bearer 2f239b9cc89c7eba736b7001cfe70092e5f825a96c333a04fd17762162ff6403eb05227f41796c373e0b8bbf5760d2513bc80cb6eabfdbec1dee2b7d7cee0edf' \
--header 'Content-Type: application/json' \
--data-raw '{
"amount": {
"currency": "EGP",
"total": 1100
},
"country": "EG",
"customer": {
"customerEmail": "10379xxxx@xx.com",
"customerMobile": "+201110024320",
"customerName": "customerName"
},
"description": "test description",
"dueTime": 1698363138,
"lang": "AR",
"name": "invoice_test",
"notifyMethod": "BOTH",
"productList": [
{
"merchantProductNo": "1010",
"quantity": 2
}
],
"relationProduct": true,
"serviceFee": {
"currency": "EGP",
"feeRate":"1",
"fixedFee": 1
}
}'
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.ArrayList;
import java.util.TreeMap;
public class CreateSingleInvoice {
private static final String privateKey = "OPAYPRV16357783346230.07653446106051542";
private static final String endpoint = "https://sandboxapi.opaycheckout.com";
private static final String merchantId = "256612345678901";
public static void main(String[] args) throws Exception {
String addr = endpoint + "/api/v1/international/invoice/createSingleInvoice";
Gson gson = new Gson();
TreeMap order = new TreeMap<>();
TreeMap amount = new TreeMap<>();
amount.put("currency","EGP");
amount.put("total",new Integer(400));
order.put("amount",amount);
order.put("country","EG");
TreeMap customer = new TreeMap<>();
customer.put("customerEmail","10379xxxx@xx.com");
customer.put("customerMobile","+201110024320");
customer.put("customerName","customerName");
order.put("customer",customer);
order.put("description","description");
order.put("dueTime",1698363138);
order.put("lang","AR");
order.put("name","invoice_name");
order.put("notifyMethod","BOTH");
ArrayList productList = new ArrayList<>();
TreeMap product = new TreeMap<>();
product.put("merchantProductNo","1010");
product.put("quantity",new Integer(2));
productList.add(product);
order.put("productList",productList);
order.put("relationProduct",true);
TreeMap serviceFee = new TreeMap<>();
customer.put("currency","EGP");
customer.put("feeRate","1");
customer.put("fixedFee","1");
order.put("serviceFee",serviceFee);
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);
}
}
Create Invoice API Response
-Response Parameters:
the parameters contained in the response received whenever you call create invoice API as a JSON Object.
{
"code":"00000",
"message":"SUCCESSFUL",
"data": {
"invoiceNo": "20944923",
"name": "OPay Test Invoice",
"amount": {
"total": 1000,
"currency": "EGP"
},
"payLink": "https://cashier.opaycheckout.com?vid=8d0abb66f8e34ed3bd8daa68bac16eef",
"dueTime": 1635772665,
"description": "test description",
"country": "EG",
"lang": "EN",
"notifyMethod": "SMS",
"totalServiceFee": {
"total": 21,
"currency": "EGP"
}
}
}
-Here is a detailed description for the parameters received in the response:
Parameter | type | Description | example | |
---|---|---|---|---|
code | String |
Transaction response code. | 00000 | |
message | String |
Transaction response information. | SUCCESSFUL | |
invoiceNo | String |
Invoice ID. | 20459544 | |
name | String |
Invoice Name. | OPay Test Invoice | |
amount Json Object |
||||
total | Long |
Invoice Amount. | 10000 (cents) | |
currency | String |
Currency type. | EGP | |
paylink | String |
Invoice link. | https://cashier.opaycheckout.com?vid=1d400b9adbdd42c7825847bc5ed8ff50 | |
duetime | Long |
Invoice due date. | Invoice due date. Timestamp. Unit: second. e.g.1635772665. | |
description | String |
Invoice description. | OPay test invoice description. | |
country | String |
Merchant country code. | EG. | |
lang | String |
Language of Email and SMS . | "EN" or "AR". | |
notifyMethod | String |
Invoice notification method | BOTH,EMAIL or SMS .Default is BOTH. | |
totalServiceFee
|
||||
total | Long |
The calculated total service fee. | 100 piastres = 1 EGP (cent unit). | |
currency | String |
Currency type. | EGP |
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": "02001",
"message": "Invalid characters in mobile 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 |
---|---|
02001 | |
Invalid characters in mobile number. | |
Mobile number format should be like this +201088889999 | |
Merchant Product ID does not exist, please create it in merchant dashboard first. | |
Please merge the same products. | |
Products is required. | |
Invoice amount can not be 0 |