Skip to content

Instantly share code, notes, and snippets.

@homaily
Created February 22, 2016 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save homaily/f6811daf0353af29cef3 to your computer and use it in GitHub Desktop.
Save homaily/f6811daf0353af29cef3 to your computer and use it in GitHub Desktop.
#Pay api client demo in PHP
<?php
include_once 'functions.php';
$data = array(
"title" => "New Invoice",
"price" => 10,
"currency" => "SAR",
"description" => "Create new invoice.",
"quantity" => 1,
"is_stock" => true,
"is_address" => false,
"is_active" => true,
"date_end" => null,
"reference" => "Api test",
"reference_private" => null,
"customer" => array(
"name" => "John Doe",
"mobile" => "966505330609",
"email" => null
)
);
// Create invoice
$result = call('POST', 'invoices', $data);
echo '<pre>';
print_r($result);
echo '</pre>';
<?php
function call ($method = 'GET', $path, $data = array()) {
$key = 'YOUR_API_KEY_HERE';
if (!$path) {
die('$path are required');
}
$ch = curl_init();
if ($method === 'POST') {
$data = json_encode($data);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
} else {
$data = '';
}
$headers = array(
'Content-Type: application/json',
'x-key: '.$key,
'x-lang: ar', // Allowd values: [ar, en]
'Content-Length: ' . strlen($data)
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, 'https://api_test.hashpay.co/'. $path);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
$response = curl_exec($ch);
$error = curl_errno($ch);
curl_close($ch);
if($error) {
die("cURL error (". $error ."):\n ". curl_strerror($error) ."");
}
try {
$response = json_decode($response);
} catch (Exception $e) {
die("Invalid JSON response:\n ". $response);
}
return $response;
}
<?php
include_once 'functions.php';
$invoice_id = 'TST';
// Get invoice by id
$result = call('GET', 'invoices/'.$invoice_id);
echo '<pre>';
print_r($result);
echo '</pre>';
<?php
include_once 'functions.php';
// List all invoices
$result = call('GET', 'invoices');
echo '<pre>';
print_r($result);
echo '</pre>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment