Last active
December 15, 2015 06:08
-
-
Save spaomalley/5213677 to your computer and use it in GitHub Desktop.
These 2 files are necessary for many of the examples listed on the PPS API examples page in relation to the API Documentation.
OAuth.php constructs the OAuth request. enterCreds2.php holds basic information such as your credentials, merchantId and endpoints.
This file contains hidden or 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 | |
class Creds{ | |
function creds(){ | |
//You're using 2-Legged OAuth so you're consumer secret is a SHA1 hash of your password into beta.mxmerchant.com | |
//put that between password between the second 2 single quotes. | |
return array( | |
'', | |
base64_encode(sha1('',true))); | |
} | |
function merchantId(){ | |
$merchantId = ""; | |
return "-". $merchantId; | |
} | |
function links(){ | |
$baseURL = 'https://sandbox.api.prioritypaymentsystems.com/checkout/v1.1/'; | |
$links = array( | |
'request'=> array($baseURL.'OAuth/1A/RequestToken' | |
,"POST" | |
), | |
'access'=> array($baseURL.'OAuth/1A/AccessToken' | |
, "POST", | |
), | |
'payment'=>array($baseURL.'payment' | |
, "POST" | |
), | |
'deletePaymentId'=>array( | |
$baseURL.'payment/INITIAL_ID', | |
"DELETE" | |
), | |
'postOrder'=> array( | |
$baseURL.'order', | |
"POST" | |
), | |
'postCustomer'=> array( | |
$baseURL.'customer', | |
"POST" | |
), | |
'getPaymentId'=>array( | |
$baseURL.'payment/INITIAL_ID', | |
"GET" | |
), | |
'getCustomerSearch'=> array( | |
$baseURL.'customer', | |
"GET" | |
) | |
); | |
return $links; | |
} | |
} | |
?> |
This file contains hidden or 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 | |
class OAuthRequest | |
{ | |
protected $params; | |
public $linkArray; | |
protected $query; | |
protected $creds; | |
protected $base_string; | |
public $headers; | |
protected $response; | |
protected $tokenArray; | |
public $queryParams; | |
protected $newArray; | |
protected $id; | |
protected $subId; | |
public $image; | |
public $testDescriptor; | |
public $uniqueIdentifier; | |
public $test; | |
public $oauthHeader; | |
function __construct($linkArray = null, $creds = null,$tokenArray = null, $queryParams = null,$id = null, $subId = null, $image = null) | |
{ | |
if(@$linkArray){ | |
$this->linkArray=$linkArray; | |
} | |
if(@$creds){ | |
$this->creds=$creds; | |
} | |
if(@$tokenArray){ | |
$this->tokenArray=$tokenArray; | |
} | |
if(@$queryParams){ | |
$this->queryParams=$queryParams; | |
} | |
if(@$id){ | |
$this->id=$id; | |
} | |
if(@$subId){ | |
$this->subId=$subId; | |
} | |
if(@$image){ | |
$this->image=$image; | |
} | |
} | |
function oauthParams(){ | |
$params = array( | |
'oauth_consumer_key' => $this->creds[0], | |
'oauth_nonce' => $this->getGUID(), | |
'oauth_signature_method' => 'HMAC-SHA1', | |
'oauth_timestamp' => $this->generateTimestamp(), | |
'oauth_version' => '1.0' | |
); | |
if($this->tokenArray) | |
{ | |
$params['oauth_token']=$this->tokenArray['oauth_token']; | |
} | |
return $params; | |
} | |
function getGUID(){ | |
mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up. | |
$charid = strtoupper(md5(uniqid(rand(), true))); | |
$hyphen = chr(45);// "-" | |
$uuid = substr($charid, 0, 8).$hyphen | |
.substr($charid, 8, 4).$hyphen | |
.substr($charid,12, 4).$hyphen | |
.substr($charid,16, 4).$hyphen | |
.substr($charid,20,12); | |
$this->uniqueIdentifier = $uuid; | |
return $uuid; | |
} | |
function generateTimestamp(){ | |
return time(); | |
} | |
function determineParams(){ | |
if($this->queryParams){ | |
$this->params = array_merge($this->oauthParams(), $this->queryParams); | |
ksort($this->params); | |
return $this->params; | |
} | |
else{ | |
$this->params=$this->oauthParams(); | |
ksort($this->params); | |
return $this->params; | |
} | |
} | |
function urlencode_oauth($str) | |
{ | |
return str_replace('+',' ',str_replace('%7E','~',rawurlencode($str))); | |
} | |
function makeQuery($allParams) | |
{ | |
foreach ($allParams as $key=>$value) | |
{ | |
if(strpos($value, "/") != - 1){ | |
$query[] = $this->urlencode_oauth($key).'='.$value; | |
}else{ | |
$query[] = $this->urlencode_oauth($key).'='. $this->urlencode_oauth($value); | |
} | |
} | |
$query = implode('&', $query); | |
return $query; | |
} | |
function getLink() | |
{ | |
if($this->id) | |
{ | |
$this->linkArray[0]= str_replace("...", $this->id, $this->linkArray[0]); | |
if($this->subId) | |
{ | |
$this->linkArray[0]= str_replace("222", $this->subId, $this->linkArray[0]); | |
} | |
return $this->linkArray[0]; | |
} | |
else | |
{ | |
return $this->linkArray[0]; | |
} | |
} | |
function makeBaseString() | |
{ | |
$parts = array( | |
$this->urlencode_oauth($this->linkArray[1]), | |
$this->urlencode_oauth($this->getLink()), | |
$this->urlencode_oauth($this->makeQuery($this->params)) | |
); | |
$base_string = implode('&',$parts); | |
return $base_string; | |
} | |
function makeKey() | |
{ | |
$key = $this->urlencode_oauth($this->creds[1]) . '&'; | |
if ($this->tokenArray) | |
{ | |
$key .= $this->urlencode_oauth($this->tokenArray['oauth_token_secret']); | |
return $key; | |
} | |
else{ | |
return $key; | |
} | |
} | |
function recaptureParams(){ | |
$neededParams= $this->determineParams(); | |
$neededParams['oauth_signature']= $this->sign($this->makeKey(), $this->makeBaseString()); | |
$temp = $neededParams; | |
$this->test = $this->makeQuery($temp); | |
// echo $this->makeQuery($temp). "\n\n"; | |
return $neededParams; | |
} | |
function sign($key,$basestring){ | |
$signature = base64_encode(hash_hmac('sha1', $basestring, $key, true)); | |
return $signature; | |
} | |
function createHeader($JSON = null) | |
{ | |
foreach ($this->recaptureParams() as $key=>$value) | |
{ | |
$str[] = $key . '='. $this->urlencode_oauth($value); | |
} | |
$str = implode(',',$str); | |
$headers = array( | |
'Authorization: OAuth '.$str | |
); | |
$this->oauthHeader = 'Authorization: OAuth '.$str; | |
if(!@$JSON) | |
{ | |
array_push($headers, "Content-Length: 0"); | |
}else{ | |
if($this->image!=null){ | |
array_push($headers, "Content-Type: application/octet-stream"); | |
}else{ | |
array_push($headers, "Content-Type: application/json"); | |
} | |
} | |
$this->headers = $headers; | |
return $this->headers; | |
} | |
function sendRequest($JSON=null, $image=null){ | |
$options = array( | |
CURLOPT_HTTPHEADER => $this->headers, | |
CURLOPT_URL => $this->linkArray[0], | |
CURLOPT_SSL_VERIFYPEER => false, | |
CURLOPT_HEADER=>true, | |
CURLOPT_RETURNTRANSFER=>true | |
); | |
switch($this->linkArray[1]) | |
{ | |
case'GET': | |
{ | |
if($this->queryParams) | |
{ | |
$options[CURLOPT_URL] .= "?".urldecode($this->makeQuery($this->queryParams)); | |
} | |
} | |
break; | |
case 'POST': | |
{ | |
$options[CURLOPT_POST] = true; | |
if(@$JSON) | |
{ | |
$options[CURLOPT_POSTFIELDS]= json_encode($JSON); | |
$options[CURLOPT_HEADER]=true; | |
} | |
if (@$image) | |
{ | |
$options[CURLOPT_POSTFIELDS]= $image; | |
$options[CURLOPT_HEADER]=true; | |
} | |
if($this->queryParams) | |
{ | |
$options[CURLOPT_URL] .= "?".urldecode($this->makeQuery($this->queryParams)); | |
} | |
} | |
break; | |
case 'PUT': | |
{ | |
$options[CURLOPT_CUSTOMREQUEST] = 'PUT'; | |
$options[CURLOPT_POSTFIELDS]= json_encode(@$JSON); | |
if($this->queryParams) | |
{ | |
$options[CURLOPT_URL] .= "?".urldecode($this->makeQuery($this->queryParams)); | |
} | |
} | |
break; | |
case 'DELETE': | |
{ | |
$options[CURLOPT_CUSTOMREQUEST] = 'DELETE'; | |
if($this->queryParams) | |
{ | |
$options[CURLOPT_URL] .= "?".urldecode($this->makeQuery($this->queryParams)); | |
} | |
} | |
break; | |
} | |
echo "\n\n".$options[CURLOPT_URL]."\n\n"; | |
$ch = curl_init(); | |
curl_setopt_array($ch, $options); | |
$res = curl_exec($ch); | |
curl_close($ch); | |
return $res; | |
} | |
function sendRequestForContent($JSON=null, $image=null) | |
{ | |
$options = array( | |
CURLOPT_HTTPHEADER => $this->headers, | |
CURLOPT_URL => $this->linkArray[0], | |
CURLOPT_SSL_VERIFYPEER => false, | |
//CURLOPT_HEADER=>true, | |
CURLOPT_RETURNTRANSFER=>true | |
); | |
switch($this->linkArray[1]) | |
{ | |
case'GET': | |
{ | |
if($this->queryParams) | |
{ | |
$options[CURLOPT_URL] .= "?".urldecode($this->makeQuery($this->queryParams)); | |
} | |
} | |
break; | |
case 'POST': | |
{ | |
$options[CURLOPT_POST] = true; | |
if(@$JSON) | |
{ | |
$options[CURLOPT_POSTFIELDS]= json_encode($JSON); | |
//$options[CURLOPT_HEADER]=true; | |
} | |
if (@$image) | |
{ | |
$options[CURLOPT_POSTFIELDS]= $image; | |
$options[CURLOPT_HEADER]=true; | |
} | |
if($this->queryParams) | |
{ | |
$options[CURLOPT_URL] .= "?".urldecode($this->makeQuery($this->queryParams)); | |
} | |
} | |
break; | |
case 'PUT': | |
{ | |
$options[CURLOPT_CUSTOMREQUEST] = 'PUT'; | |
$options[CURLOPT_POSTFIELDS]= json_encode(@$JSON); | |
$options[CURLOPT_HEADER]=true; | |
if($this->queryParams) | |
{ | |
$options[CURLOPT_URL] .= "?".urldecode($this->makeQuery($this->queryParams)); | |
} | |
} | |
break; | |
case 'DELETE': | |
{ | |
$options[CURLOPT_CUSTOMREQUEST] = 'DELETE'; | |
$options[CURLOPT_HEADER]=true; | |
if($this->queryParams) | |
{ | |
$options[CURLOPT_URL] .= "?".urldecode($this->makeQuery($this->queryParams)); | |
} | |
} | |
break; | |
} | |
echo "\n\n".$options[CURLOPT_URL]."\n\n"; | |
$ch = curl_init(); | |
curl_setopt_array($ch, $options); | |
$res = curl_exec($ch); | |
$httpCode= curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
curl_close($ch); | |
echo "\n\n"."Logging"; | |
// $Logger = new Logger; | |
// $Logger->logHeaders($res, $this->linkArray, $httpCode, $this->returnEnv()); | |
// $CSV=new CSV; | |
// $CSV->logResults($this->linkArray, $httpCode, $this->testDescriptor, $this->uniqueIdentifier, $this->returnEnv()); | |
return $res; | |
} | |
} | |
class Utility | |
{ | |
function getListFromJSON($JSON){ | |
$tempArray=array(); | |
foreach($JSON['records'] as $item){ | |
array_push($tempArray, $item['id']); | |
} | |
return $tempArray; | |
} | |
} | |
function getAccesstoken($links, $creds) { | |
$getRequestToken=new OAuthRequest($links['request'], $creds); | |
$getRequestToken->createHeader(); | |
parse_str(sendRequestForTokens($getRequestToken->headers, $getRequestToken->linkArray),$initCall); | |
if(count($initCall) < 1){ | |
echo "No Tokens Acquired!\n\n"; | |
} | |
$getAccessToken= new OAuthRequest($links['access'],$creds,$initCall); | |
$getAccessToken->createHeader(); | |
parse_str(sendRequestForTokens($getAccessToken->headers,$getAccessToken->linkArray),$accessData); | |
return $accessData; | |
} | |
function createHeaderAndSendRequest($object,$JSON=null){ | |
$object->createHeader(@$JSON); | |
$object->sendRequest(@$JSON); | |
} | |
function sendRequestForTokens($headers,$linkArray,$JSON=null,$queryParams=null, $image=null){ | |
$options = array( | |
CURLOPT_HTTPHEADER => $headers, | |
CURLOPT_URL => $linkArray[0], | |
CURLOPT_SSL_VERIFYPEER => false, | |
CURLOPT_RETURNTRANSFER=>true | |
); | |
switch($linkArray[1]) | |
{ | |
case'GET': | |
{ | |
if(@$queryParams) | |
{ | |
$options[CURLOPT_URL] .= "?".urldecode($queryParams); | |
} | |
} | |
break; | |
case 'POST': | |
{ | |
$options[CURLOPT_POST] = true; | |
if(@$JSON) | |
{ | |
$options[CURLOPT_POSTFIELDS]= json_encode($JSON); | |
//$options[CURLOPT_HEADER]=true; | |
} | |
if (@$image) | |
{ | |
$options[CURLOPT_POSTFIELDS]= $image; | |
$options[CURLOPT_HEADER]=true; | |
} | |
if(@$queryParams) | |
{ | |
$options[CURLOPT_URL] .= "?".urldecode($queryParams); | |
} | |
} | |
break; | |
case 'PUT': | |
{ | |
$options[CURLOPT_CUSTOMREQUEST] = 'PUT'; | |
$options[CURLOPT_POSTFIELDS]= json_encode(@$JSON); | |
$options[CURLOPT_HEADER]=true; | |
if(@$queryParams) | |
{ | |
$options[CURLOPT_URL] .= "?".urldecode($queryParams); | |
} | |
} | |
break; | |
case 'DELETE': | |
{ | |
$options[CURLOPT_CUSTOMREQUEST] = 'DELETE'; | |
$options[CURLOPT_HEADER]=true; | |
} | |
break; | |
} | |
$ch = curl_init(); | |
curl_setopt_array($ch, $options); | |
$res = curl_exec($ch); | |
curl_close($ch); | |
return $res; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment