Created
February 22, 2016 10:59
-
-
Save homaily/f6811daf0353af29cef3 to your computer and use it in GitHub Desktop.
#Pay api client demo in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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>'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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>'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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