### JavaScript Example for OPay Send OTP Source: https://documentation.opaycheckout.com/send-otp A Node.js example using the 'request' and 'js-sha512' libraries to send an OTP request. Ensure these packages are installed. ```javascript const request = require('request'); var sha512 = require('js-sha512'); const formData = { "country":"NG", "orderNo":"220524147840658585383" }; var privateKey = "OPAYPRV1641***********926" 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/payment/action/resend-otp', method: 'POST', headers: { 'MerchantId': '278421062352020', 'Authorization': 'Bearer '+hmacsignature }, json: true, body: formData }, function (error, response, body) { console.log('body: ') console.log(body) } ) ``` -------------------------------- ### Payment Refund Status Query Examples Source: https://documentation.opaycheckout.com/payment-refund-status Code examples demonstrating how to query the status of a payment refund using different programming languages. ```Python import requests import json import hashlib private_key = "OPAYPRV*******0187828" merchant_id = "256621051120756" form_data = { "country": "NG", "reference": "04123398911111" } hash_obj = hashlib.sha512(private_key.encode('utf-8')) hmac_signature = hash_obj.hexdigest() headers = { 'MerchantId': merchant_id, 'Authorization': f'Bearer {hmac_signature}', 'Content-Type': 'application/json' } response = requests.post('https://testapi.opaycheckout.com/api/v1/international/payment/refund/query', headers=headers, json=form_data) print(response.json()) ``` ```JavaScript const request = require('request'); var sha512 = require('js-sha512'); const formData = { "country": "NG", "reference": "04123398911111" }; var privateKey = "OPAYPRV*******0187828"; var hash = sha512.hmac.create(privateKey); hash.update(JSON.stringify(formData)); hmacsignature = hash.hex(); request({ url: 'https://testapi.opaycheckout.com/api/v1/international/payment/refund/query', method: 'POST', headers: { 'MerchantId': '256621051120756', 'Authorization': 'Bearer '+hmacsignature }, json: true, body: formData }, function (error, response, body) { console.log('body: ') console.log(body) } ); ``` ```Bash curl --location --request POST 'https://testapi.opaycheckout.com/api/v1/international/payment/refund/query' \ --header 'MerchantId: 256621051120756' \ --header 'Authorization: Bearer 38017496e*******2ab0297d1de8905cb7d85a043d9084254e61a8da6093b6f155550ff51891dca9f9889a' \ --header 'Content-Type: application/json' \ --data-raw '{ "reference": "04123398911111", "country": "NG" }' ``` ```Java 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; public class PaymentRefundStatus { 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/refund/query"; Gson gson = new Gson(); TreeMap order = new TreeMap<>(); order.put("reference", "56ed8ea1-ea00-48bf-8a71-a9cd5700b345"); order.put("country", "NG"); 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()); } 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); } } ``` -------------------------------- ### Python Example for Cashier Reversal Status API Call Source: https://documentation.opaycheckout.com/cashier-reversal-status A Python example using the requests library to make a POST request to the Cashier Reversal Status API. Ensure 'Public Key' is replaced with your actual Bearer token. ```Python # importing the requests library import requests # CashierAPI Endpoint URL = "https://testapi.opaycheckout.com/api/v1/international/transaction/reversal/status " # defining a params dict for the parameters to be sent to the API PaymentData = { 'reference' : '12312321324', 'reversalNo' : '11212134400034123', } # Prepare Headers headers = { 'Content-Type' : 'application/json', 'Accept' : 'application/json', 'Authorization': 'Bearer Public Key', 'MerchantId' : '256612345678901' } # sending post request and saving the response as response object status_request = requests.post(url = URL, params = json.dumps(PaymentData)) ``` -------------------------------- ### Python Example for Query Refund Status Source: https://documentation.opaycheckout.com/payment-refund-status This Python code snippet shows how to set up the request parameters for querying a refund status using the OPay API. ```python # importing the requests library import requests # CashierAPI Endpoint URL = "https://testapi.opaycheckout.com/api/v1/international/payment/refund/query" # defining a params dict for the parameters to be sent to the API PaymentData = { 'reference': '12312321324', 'country': 'NG' } ``` -------------------------------- ### Example Callback Object Structure Source: https://documentation.opaycheckout.com/payment-notifications-callbacks This is an example of the JSON object received when a transaction status changes. It includes payload details and a security signature. ```json { "payload":{ "amount":"49160", "channel":"Web", "country":"NG", "currency":"NGN", "displayedFailure":"", "fee":"737", "feeCurrency":"NGN", "instrumentType":"BankCard", "reference":"10023", "refunded":false, "status":"SUCCESS", "timestamp":"2022-05-07T06:20:46Z", "token":"220507145660712931829", "transactionId":"220507145660712931829", "updated_at":"2022-05-07T07:20:46Z" }, "sha512":"9f605d69f04e94172875dc156537071cead060bbcaeaca94a7b8805af9f89611e2fdf6836713c9c90b028ca7e4470b1356e996975f2abc862315aaa9b7f2ae2d", "type":"transaction-status" } ``` -------------------------------- ### PHP Example for OPay Send OTP Source: https://documentation.opaycheckout.com/send-otp A PHP implementation demonstrating how to send an OTP request using cURL. Ensure cURL is enabled in your PHP environment. ```php class SendOtpController { private $secretkey; private $merchantId; private $url; public function __construct() { $this->merchantId = '278421062352020'; $this->secretkey = 'OPAYPRV1641***********926'; $this->url = 'https://testapi.opaycheckout.com/api/v1/payment/action/resend-otp'; } public function test(){ $data = [ "country" => "NG", "orderNo" => "220524147840658585383" ]; $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; } } ``` -------------------------------- ### Java Setup for OPay Input PIN API Source: https://documentation.opaycheckout.com/input-pin Imports required for a Java implementation of the OPay Input PIN API, including JSON handling and cryptographic operations. ```java 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; ``` -------------------------------- ### PHP Example for Cashier Reversal Status API Call Source: https://documentation.opaycheckout.com/cashier-reversal-status A PHP example using Guzzle HTTP client to call the Cashier Reversal Status API. Remember to replace '{signature}' with your actual Bearer token. ```PHP $reference = '12312321324'; $reversalNo = '11212134400034123'; $httpClient = new \GuzzleHttp\Client(); // guzzle 6.3 $response = $httpClient->request('POST', 'https://testapi.opaycheckout.com/api/v1/international/transaction/reversal/status ', [ 'headers' => [ 'Content-Type' => 'application/json', 'Accept' => 'application/json', 'Authorization' : 'Bearer {signature}', 'MerchantId' : '256612345678901' ], 'body' => json_encode( [ 'reference' => $reference, 'reversalNo' => $orderNo, ] , true) ]); $response = json_decode($response->getBody()->getContents(), true); $paymentStatus = $response['type']; // get response values ``` -------------------------------- ### Python Cancel Payment API Request Setup Source: https://documentation.opaycheckout.com/cashier-close Initializes the requests library and defines the parameters for a Cancel Payment API call in Python. ```Python # importing the requests library import requests # CashierAPI Endpoint URL = "https://testapi.opaycheckout.com/api/v1/international/payment/close" # defining a params dict for the parameters to be sent to the API PaymentData = { 'reference' : '1001000', 'country' : 'NG' } ``` -------------------------------- ### cURL Example for OPay Send OTP Source: https://documentation.opaycheckout.com/send-otp A cURL command to send an OTP request. This example demonstrates the HTTP POST request with necessary headers and data payload. ```bash curl --location --request POST 'https://testapi.opaycheckout.com/api/v1/payment/action/resend-otp' \ --header 'MerchantId: 278421062352020' \ --header 'Authorization: Bearer c76b6906b728d1adfd0f9bef05758c7b640348b4c734a73b5b83db4fb5f87dec2835e1a974c8d458241ab811707662769ed276b7a809c8413d174fd158b06060' \ --header 'Content-Type: application/json' \ --data-raw '{ "country":"NG", "orderNo":"220209140595649129" }' ``` -------------------------------- ### Shopify App Installation Link Source: https://documentation.opaycheckout.com/shopify-plugin Use this link to install the OPay custom app on your Shopify store. Ensure you have received this link from the OPay technical team. ```html https://your-store.myshopify.com/admin/oauth/install_custom_app?client_id=37c5001d47da504d6cc193e2345438bc&signature=eyJfcmFpbHMiOnsibWVzc2FnZSI6ImV5SmxlSEJwY21WelgyRjBJam94TmpJNU56a3hNRGN5TENKd1pYSnRZVzVsYm5SZlpHOXRZV2x1SWpvaWJXbHRiMk52WkdWekxXUmxkaTV0ZVhOb2IzQnBabmt1WTI5dElpd2lZMnhwWlc1MFgybGtJam9pTXpkak5UQXdNV1EwTjJSaE5UQTBaRFpqWXpFNU0yVXlZV1V3Tm1JNFltTWlMQ0p3ZFhKd2IzTmxJam9pWTNWemRHOXRYMkZ3Y0NKOSIsImV4cCI6IjIwMjEtMDgtMzFUMDc6NDQ6MzIuNTUwWiIsInB1ciI6bnVsbH19--f182b86e2dbf3d417e58616801bd08e236272302 ``` -------------------------------- ### Install and Cache Magento 2 OPay Plugin Source: https://documentation.opaycheckout.com/magento-plugin Run these commands in your Magento app root directory to upgrade and flush the cache after installing the OPay plugin. ```bash php bin/magento setup:upgrade php bin/magento cache:flush php bin/magento cache:clean ``` -------------------------------- ### cURL Example for OPay Input PIN API Source: https://documentation.opaycheckout.com/input-pin A cURL command to demonstrate a POST request to the Input PIN API, including necessary headers and the JSON payload. ```bash curl --location --request POST 'https://testapi.opaycheckout.com/api/v1/payment/action/input-pin' \ --header 'MerchantId: 256622011275597' \ --header 'Authorization: Bearer c5b5f86cd1d3a0965671df08101ef521cd2a87a9593f4a46e6631bc7305959ad5ec9f452605b1f7b0516d349ced0686e8d179dc49dd4a2ae2ab509cedb016b20' \ --header 'Content-Type: application/json' \ --data-raw '{ "country":"NG", "orderNo":"220209140595656300", "pin":"1109" }' ``` -------------------------------- ### Python Example for OPay Payment Status Request Source: https://documentation.opaycheckout.com/query-payment-status Python code snippet demonstrating how to set up parameters and headers for a request to the OPay Cashier Payment Status API using the requests library. ```python # importing the requests library import requests # CashierAPI Endpoint URL = "https://testapi.opaycheckout.com/api/v1/international/cashier/status" # defining a params dict for the parameters to be sent to the API PaymentData = { 'reference' : '12312321324', 'country' : 'NG', } # Prepare Headers headers = { 'Content-Type' : 'application/json', 'Accept' : 'application/json', 'Authorization': 'Bearer Public Key', 'MerchantId' : '256612345678901' } ``` -------------------------------- ### Create Payment Request (cURL) Source: https://documentation.opaycheckout.com/3DS-API Example of creating a payment request using cURL. This demonstrates the raw HTTP request structure, including headers and JSON payload. ```Bash 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":400 \ }, \ "bankcard":{ \ "cardHolderName":"DAVID", \ "cardNumber":"450875*******", \ "cvv": "100", \ "enable3DS":true, \ "expiryMonth":"02", \ "expiryYear":"26" \ }, \ "callbackUrl":"https://your-call-back-url.com", \ "country":"NG", \ "payMethod":"BankCard", \ "product":{ \ "description":"description", \ "name":"name" \ }, \ "reference":"041233989103331", \ "returnUrl":"https://your-return-url.com" \ }' ``` -------------------------------- ### PHP Example for OPay Cashier Create Payment Source: https://documentation.opaycheckout.com/cashier-create A PHP class demonstrating how to initialize and prepare data for the OPay Cashier Create Payment API. Ensure your API credentials and URLs are correctly set. ```php class CashierCreateController { private $publickey; private $merchantId; private $url; public function __construct() { $this->merchantId = '256621051120756'; $this->publickey = 'OPAYPUB*************54133'; $this->url = 'https://testapi.opaycheckout.com/api/v1/international/cashier/create'; } public function test(){ $data = [ 'country' => 'NG', 'reference' => '9835413542121', 'amount' => [ "total"=> "400", "currency"=> 'NGN', ], 'returnUrl' => 'https://your-return-url', 'callbackUrl'=> 'https://your-call-back-url', 'cancelUrl' => 'https://your-cacel-url', ``` -------------------------------- ### Java Setup for OPay Send OTP Source: https://documentation.opaycheckout.com/send-otp Initial Java imports required for making HTTP requests and handling encryption for the OPay Send OTP API. This snippet sets up the necessary classes and libraries. ```java 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 SendOtp { ``` -------------------------------- ### React Example for Cashier Reversal Status API Call Source: https://documentation.opaycheckout.com/cashier-reversal-status An example of how to call the Cashier Reversal Status API using JavaScript's fetch API in a React environment. Ensure you replace '{signature}' with your actual Bearer token. ```JavaScript function Cashier(transaction_data) { const PaymentData = { reference: transaction_data.reference, reversalNo : transaction_data.reversalNo, }; const response = await fetch('https://testapi.opaycheckout.com/api/v1/international/transaction/reversal/status ', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization' : 'Bearer {signature}', 'MerchantId' : '256612345678901' }, body: JSON.stringify(PaymentData), }); // Return and display the result of the charge. return response.json(); } ``` -------------------------------- ### PHP OPay POS Payment Integration Example Source: https://documentation.opaycheckout.com/pos A PHP class demonstrating how to construct and send a POS payment request to the OPay API. It includes methods for authentication and making the HTTP POST request. ```php class PosController { private $secretkey; private $merchantId; private $url; public function __construct() { $this->merchantId = '256622011275597'; $this->secretkey = 'OPAYPRV1641***********926'; $this->url = 'https://testapi.opaycheckout.com/api/v1/international/payment/create'; } public function test(){ $data = [ 'amount'=> [ 'currency'=>'NGN', 'total'=>400 ], 'callbackUrl'=>'https://testapi.opaycheckout.com/api/v1/international/print', 'country'=>'NG', 'customerName'=>'customerName', 'payMethod'=>'Pos', 'product'=> [ 'description'=>'dd', 'name'=>'name' ], 'reference'=>"12345a", 'sn'=>"N78101818218", 'userPhone'=>'+1234567879' ]; $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 ) { ``` -------------------------------- ### PHP Example for Query Refund Status Source: https://documentation.opaycheckout.com/payment-refund-status This PHP code demonstrates how to construct and send a request to the OPay Query Refund Status API, including authentication and HTTP POST request handling. ```php class PaymentRefundStatusController { 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/refund/query'; } public function test(){ $data = [ 'country' => 'NG', 'reference' => '04123398910033621' ] ; $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; } } ``` -------------------------------- ### JavaScript Implementation of OPay Input PIN API Source: https://documentation.opaycheckout.com/input-pin A JavaScript example using the 'request' library to send a POST request to the Input PIN API, including HMAC-SHA512 signature generation. ```javascript const request = require('request'); var sha512 = require('js-sha512'); const formData = { "country":"NG", "orderNo":"220209140595656300", "pin":"1109" }; var privateKey = "OPAYPRV1641***********926" 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/payment/action/input-pin', method: 'POST', headers: { 'MerchantId': '256622011275597', 'Authorization': 'Bearer '+hmacsignature }, json: true, body: formData }, function (error, response, body) { console.log('body: ') console.log(body) } ) ``` -------------------------------- ### PHP Bank Account Payment API Call Source: https://documentation.opaycheckout.com/bank-account Example PHP code demonstrating how to construct and send a request to the OPay Bank Account Payment API. ```php class BankAccountController { private $secretkey; private $merchantId; private $url; public function __construct() { $this->merchantId = '256622011275597'; $this->secretkey = 'OPAYPRV1641***********926'; $this->url = 'https://testapi.opaycheckout.com/api/v1/international/payment/create'; } public function test(){ $data = [ 'amount'=> [ 'currency'=>'NGN', 'total'=>400 ], 'bankAccountNumber'=>'2215381176', 'bankCode'=>'033', 'bvn'=>'01234567899', 'callbackUrl'=>'https://testapi.opaycheckout.com/api/v1/international/print', 'country'=>'NG', 'customerName'=>'customerName', 'dobDay'=>'12', 'dobMonth'=>'12', 'dobYear'=>'2000', 'payMethod'=>'BankAccount', 'product'=>['description'=>'dd','name'=>'name'], 'reference'=>"12345c", 'returnUrl'=>'https://testapi.opaycheckout.com/api/v1/international/print', 'userPhone'=>'+1234567879' ]; $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); ``` -------------------------------- ### Node.js: Create Payment Request with HMAC Signature Source: https://documentation.opaycheckout.com/bank-transfer This Node.js example shows how to construct a payment request body, generate an HMAC-SHA512 signature using `js-sha512`, and make a POST request to the Opay API. ```JavaScript const request = require('request'); var sha512 = require('js-sha512'); const formData = { "amount":{ "currency":"NGN", "total":400 }, "callbackUrl":"https://testapi.opaycheckout.com/api/v1/international/print", "country":"NG", "customerName":"customerName", "payMethod":"BankTransfer", "product":{ "description":"dd", "name":"name" }, "reference":"123456", "userInfo":{ "userEmail":"xxx@xxx.com", "userId":"userid001", "userMobile":"13056288895", "userName":"xxx" }, "userPhone":"+1234567879" }; var privateKey = "OPAYPRV1641***********926" 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': '256622011275597', 'Authorization': 'Bearer '+hmacsignature }, json: true, body: formData }, function (error, response, body) { console.log('body: ') console.log(body) } ) ``` -------------------------------- ### PHP Example for Querying OPay Payment Status Source: https://documentation.opaycheckout.com/query-payment-status A PHP class demonstrating how to construct and send a request to the OPay Cashier Payment Status API, including authentication and HTTP POST request handling. ```php class QueryPaymentStatusController { 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/cashier/status'; } public function test(){ $data = [ 'country' => 'NG', '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; } } ``` -------------------------------- ### Handling Callbacks Source: https://documentation.opaycheckout.com/payment-notifications-callbacks This section details how to set up your callback endpoint, the expected request format, and how to respond to OPay's notifications. ```APIDOC ## POST /callback ### Description This endpoint is designed to receive asynchronous transaction status updates from OPay. You need to configure this URL in your OPay account settings under 'webhook URL'. ### Method POST ### Endpoint `/callback` ### Parameters #### Request Body - **payload** (object) - Required - Contains detailed transaction information. - **amount** (String) - Required - Transaction Amount in NGN. - **channel** (String) - Required - The payment channel used (e.g., 'Web'). - **country** (String) - Required - Transaction belongs country. - **currency** (String) - Required - Transaction currency. - **displayedFailure** (String) - Optional - Transaction reason for failure, if any. - **fee** (String) - Required - Transaction fee Amount in NGN. - **feeCurrency** (String) - Required - Transaction Fee currency. - **instrumentType** (String) - Required - The payment method used (e.g., 'BankCard', 'BankTransfer'). - **reference** (String) - Required - Partner transaction number. - **refunded** (String) - Required - Indicates if the transaction is a refund type ('true' or 'false'). - **status** (String) - Required - Transaction status ('SUCCESS' or 'FAILED'). - **timestamp** (String) - Required - Transaction initiation time in ISO 8601 format. - **token** (String) - Required - A unique token for the transaction. - **transactionId** (String) - Required - Opay transaction number. - **updated_at** (String) - Required - Transaction update time in ISO 8601 format. - **sha512** (String) - Required - HMAC SHA3-512 signature of the callback payload, signed using your Private Key. - **type** (String) - Required - The type of notification, typically 'transaction-status'. ### Request Example ```json { "payload":{ "amount":"49160", "channel":"Web", "country":"NG", "currency":"NGN", "displayedFailure":"", "fee":"737", "feeCurrency":"NGN", "instrumentType":"BankCard", "reference":"10023", "refunded":false, "status":"SUCCESS", "timestamp":"2022-05-07T06:20:46Z", "token":"220507145660712931829", "transactionId":"220507145660712931829", "updated_at":"2022-05-07T07:20:46Z" }, "sha512":"9f605d69f04e94172875dc156537071cead060bbcaeaca94a7b8805af9f89611e2fdf6836713c9c90b028ca7e4470b1356e996975f2abc862315aaa9b7f2ae2d", "type":"transaction-status" } ``` ### Response #### Success Response (200) - **Status Code**: 200 OK ### Response Example (No response body is required. A 200 OK status code acknowledges receipt.) ``` -------------------------------- ### Node.js Payment Creation with HMAC Signature Source: https://documentation.opaycheckout.com/bank-account This Node.js example shows how to create a payment request using the 'request' library and generate an HMAC-SHA512 signature for authorization. Ensure you have 'request' and 'js-sha512' installed. ```JavaScript const request = require('request'); var sha512 = require('js-sha512'); const formData = { "amount":{ "currency":"NGN", "total":400 }, "bankAccountNumber":"2215381176", "bankCode":"033", "bvn":"01234567899", "callbackUrl":"https://testapi.opaycheckout.com/api/v1/international/print", "country":"NG", "customerName":"customerName", "dobDay":"12", "dobMonth":"12", "dobYear":"2000", "payMethod":"BankAccount", "product":{ "description":"dd", "name":"name" }, "reference":"12348", "returnUrl":"https://testapi.opaycheckout.com/api/v1/international/print", "userPhone":"+1234567879" }; var privateKey = "OPAYPRV1641***********926" 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': '256622011275597', 'Authorization': 'Bearer '+hmacsignature }, json: true, body: formData }, function (error, response, body) { console.log('body: ') console.log(body) } ) ``` -------------------------------- ### Node.js Payment Creation with Opay Checkout Source: https://documentation.opaycheckout.com/reference-code This Node.js example shows how to create a payment request using the 'request' library and 'js-sha512' for HMAC signature generation. It defines the payment form data and sends a POST request to the Opay Checkout API. Ensure you have the 'request' and 'js-sha512' packages installed. ```JavaScript 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) } ) ``` -------------------------------- ### OPay Input OTP API - Java Setup (Incomplete) Source: https://documentation.opaycheckout.com/input-otp This Java code snippet shows the initial imports for interacting with the OPay Input OTP API, including libraries for JSON parsing and cryptographic operations. The full implementation is not provided. ```Java 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; ``` -------------------------------- ### Java Example for Inputting PIN Source: https://documentation.opaycheckout.com/input-pin This Java code demonstrates how to construct and send a request to the Opay API to input a PIN for a transaction. It includes generating the SHA-512 HMAC signature required for authorization. Ensure you have the Gson library for JSON processing and Hex encoding. ```Java import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.nio.charset.StandardCharsets; import java.util.TreeMap; public class InputPin { private static final String privateKey = "OPAYPRV1641***********926"; private static final String endpoint = "https://testapi.opaycheckout.com"; private static final String merchantId = "256622011275597"; public static void main(String[] args) throws Exception { String addr = endpoint + "/api/v1/payment/action/input-pin"; Gson gson = new Gson(); TreeMap order = new TreeMap<>(); order.put("country","NG"); order.put("orderNo","220209140595621505"); order.put("pin","1109"); 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); } } ``` -------------------------------- ### Python POST Request for Opay Cashier API Source: https://documentation.opaycheckout.com/cashier-create This Python script demonstrates how to make a POST request to the Opay Cashier API using the `requests` library. It includes setting up payment data, headers, and sending the request. Ensure you have the `requests` library installed (`pip install requests`). ```Python # importing the requests library import requests # CashierAPI Endpoint URL = "https://testapi.opaycheckout.com/api/v1/international/cashier/create" # defining a params dict for the parameters to be sent to the API PaymentData = { 'country' : 'NG', 'reference' : '12312321324', 'amount': { "total": '400', "currency": 'NGN', }, 'payMethod': 'BankCard', 'product': { "name": 'Iphone X', "description": 'xxxxxxxxxxxxxxxxx', }, 'returnUrl' : 'your returnUrl', 'callbackUrl' : 'your callbackUrl', 'cancelUrl' : 'your cancelUrl', 'userClientIP' : '1.1.1.1', 'sn' : 'PE462xxxxxxxx', 'evokeOpay' : true, 'customerVisitSource' : 'IOS', 'expireAt' : 30 } # Prepare Headers headers = { 'Content-Type' : 'application/json', 'Accept' : 'application/json', 'Authorization': 'Bearer Public Key', 'MerchantId' : '256612345678901' } # sending post request and saving the response as response object status_request = requests.post(url = URL, params = json.dumps(PaymentData) , headers=headers) ``` -------------------------------- ### Error Response Example Source: https://documentation.opaycheckout.com/3DS-API This JSON object shows a typical error response from the OPay API, indicating a specific issue with the payment request. ```json { "code": "02004", "message": "the payment reference already exists." } ```