Skip to content

Instantly share code, notes, and snippets.

@neo22s
Created February 20, 2016 14:15
Show Gist options
  • Save neo22s/c40e15c37d780c138a4b to your computer and use it in GitHub Desktop.
Save neo22s/c40e15c37d780c138a4b to your computer and use it in GitHub Desktop.
Securenet PHP integration
<?php
/**
* Securenet PHP library
* See docs https://apidocs.securenet.com/docs/
* You need PHP >= 5.5 and CURL installed
*
* @category Payments
* @author Chema <[email protected]>
* @copyright (c) 2009-2016 Open Classifieds Team
* @license GPL v3
*/
class SecureNet{
protected $api_version = '1.2';
protected $api_url = 'https://gwapi.securenet.com/api/';
protected $api_url_demo = 'https://gwapi.demo.securenet.com/api/';
protected $type_goods = 'DIGITAL'; // 'DIGITAL' OR 'PHYSICAL'
protected $securenet_id = NULL;
protected $secure_key = NULL;
protected $developer_id = NULL;
protected $sandbox = FALSE;
/**
* sets the values...nothing else
* @param string $securenet_id
* @param string $secure_key
* @param string $developer_id
* @param boolean $sandbox in case you are testing
*/
public function __construct($securenet_id, $secure_key, $developer_id, $sandbox = FALSE)
{
$this->securenet_id = $securenet_id;
$this->secure_key = $secure_key;
$this->developer_id = $developer_id;
$this->sandbox = $sandbox;
}
/**
* charges a card
* @param Array $data array with card info
* @return array ex: { ["result"]=> bool(false) ["message"]=> string(76) "American Express NUMBER IS INVALID. ACCURACY OF THE CARD CANNOT BE VALIDATED" }
*/
public function charge(Array $data)
{
$result = $this->execute('Payments/Charge',$data);
//process the result and simplify it
if (is_array($result) AND isset($result['responseCode']) AND isset($result['message']) )
{
return array('result'=>($result['responseCode']==1)?TRUE:FALSE,'message'=>$result['message']);
}
//probably curl error
else
{
return array('result'=>FALSE,'message'=>$result);
}
}
/**
* ads extra info to the data sent needed to charde
* @param Array $data
*/
protected function add_extra_data(Array $data)
{
$data+= array(
'extendedInformation' => array(
'typeOfGoods' => $this->type_goods,
),
'developerApplication' => array(
'developerId' => $this->developer_id,
'version' => $this->api_version
)
);
return $data;
}
/**
* makes the API call using curl
* @param string $api_point where we call
* @param Array $data data to process
* @return Array
*/
protected function execute($api_point, Array $data)
{
// where we make the API petition
$url = (($this->sandbox===TRUE)?$this->api_url_demo:$this->api_url).$api_point;
// data we send to the API
$data = $this->add_extra_data($data);
$data = json_encode($data);
// headers to authenticate
$header = array( 'Authorization: Basic ' . base64_encode($this->securenet_id . ':' . $this->secure_key),
'Content-Type: application/json',
'Content-Length: ' . strlen($data)
);
//open connection
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_TIMEOUT,15);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
if (curl_errno($ch))
return 'Execute error: ' . curl_error($ch);
curl_close($ch);
return json_decode($result,TRUE);
}
}
<?php
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
ini_set('display_errors', 1);
if ($_POST)
{
include 'securenet.php';
$securenet = new SecureNet('securenetid','securekey','developerid', TRUE);
$result = $securenet->charge(array('amount' => $_POST['amount'],
'card' => array(
'number' => $_POST['cc_number'],
'cvv' => $_POST['cc_cvv'],
'expirationDate' => $_POST['cc_expiration'],
'firstName' => $_POST['first_name'],
'lastName' => $_POST['last_name'],
'address' => array(
'line1' => $_POST['line1'],
'city' => $_POST['city'],
'state' => $_POST['state'],
'zip' => $_POST['zip']
),
),
)
);
d($result);
}
function d($v)
{
die(var_dump($v));
}
function dr($v)
{
die(print_r($v,1));
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="test.php" method="POST">
<table>
<tr>
<td>First Name: </td>
<td><input name="first_name" type="text"></input></td>
</tr>
<tr>
<td>Last Name: </td>
<td><input name="last_name" type="text"></input></td>
</tr>
<tr>
<td>Street Address: </td>
<td><input name="line1" type="text" value="456 Testing Way"></input></td>
</tr>
<tr>
<td>City: </td>
<td><input name="city" type="text" value="Rockville"></input></td>
</tr>
<tr>
<td>State: </td>
<td><input name="state" type="text" value="TX"></input></td>
</tr>
<tr>
<td>Zip code: </td>
<td><input name="zip" type="text" value="20704"></input></td>
</tr>
<tr>
<td>Credit Card Number: </td>
<td><input name="cc_number" type="text"></input></td>
</tr>
<tr>
<td>Expiration Date: </td>
<td><input name="cc_expiration" type="text" value="MM/YY"></input></td>
</tr>
<tr>
<td>CVV: </td>
<td><input name="cc_cvv" type="text" value="111"></input></td>
</tr>
<tr>
<td>Amount: </td>
<td><input name="amount" type="text" value="12.5"></input></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="Submit" value="Submit" /></td>
</tr>
</table>
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment