Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make whitespace consistent
Browse files Browse the repository at this point in the history
To make future diffs cleaner.
Also inserts explicit braces around oneline if statements.
ejegg committed Jan 11, 2018
1 parent 0b96d1e commit a1548b7
Showing 11 changed files with 591 additions and 548 deletions.
441 changes: 232 additions & 209 deletions AmazonPay/Client.php

Large diffs are not rendered by default.

13 changes: 7 additions & 6 deletions AmazonPay/HttpCurl.php
Original file line number Diff line number Diff line change
@@ -47,7 +47,7 @@ public function setAccessToken($accesstoken)
* config['proxy_password']
*/

protected function commonCurlParams($url,$userAgent)
protected function commonCurlParams($url, $userAgent)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
@@ -60,8 +60,9 @@ protected function commonCurlParams($url,$userAgent)
curl_setopt($ch, CURLOPT_CAINFO, $this->config['cabundle_file']);
}

if (!empty($userAgent))
if (!empty($userAgent)) {
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
}

if ($this->config['proxy_host'] != null && $this->config['proxy_port'] != -1) {
curl_setopt($ch, CURLOPT_PROXY, $this->config['proxy_host'] . ':' . $this->config['proxy_port']);
@@ -82,12 +83,12 @@ protected function commonCurlParams($url,$userAgent)

public function httpPost($url, $userAgent = null, $parameters = null)
{
$ch = $this->commonCurlParams($url,$userAgent);
$ch = $this->commonCurlParams($url, $userAgent);

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
curl_setopt($ch, CURLOPT_HEADER, false);

$response = $this->execute($ch);
return $response;
}
@@ -99,7 +100,7 @@ public function httpPost($url, $userAgent = null, $parameters = null)

public function httpGet($url, $userAgent = null)
{
$ch = $this->commonCurlParams($url,$userAgent);
$ch = $this->commonCurlParams($url, $userAgent);

// Setting the HTTP header with the Access Token only for Getting user info
if ($this->header) {
16 changes: 8 additions & 8 deletions AmazonPay/HttpCurlInterface.php
Original file line number Diff line number Diff line change
@@ -4,27 +4,27 @@
/* Interface for HttpCurl.php */

interface HttpCurlInterface
{
{
/* Set Http header for Access token for the GetUserInfo call */

public function setHttpHeader();

/* Setter for Access token to get the user info */

public function setAccessToken($accesstoken);

/* POST using curl for the following situations
* 1. API calls
* 2. IPN certificate retrieval
* 3. Get User Info
*/

public function httpPost($url, $userAgent = null, $parameters = null);

/* GET using curl for the following situations
* 1. IPN certificate retrieval
* 3. Get User Info
*/

public function httpGet($url, $userAgent = null);
}
95 changes: 50 additions & 45 deletions AmazonPay/IpnHandler.php
Original file line number Diff line number Diff line change
@@ -9,11 +9,12 @@
require_once 'HttpCurl.php';
require_once 'IpnHandlerInterface.php';
if (!interface_exists('\Psr\Log\LoggerAwareInterface')) {
require_once(__DIR__.'/../Psr/Log/LoggerAwareInterface.php');
require_once(__DIR__ . '/../Psr/Log/LoggerAwareInterface.php');
}
if (!interface_exists('\Psr\Log\LoggerInterface')) {
require_once(__DIR__.'/../Psr/Log/LoggerInterface.php');
require_once(__DIR__ . '/../Psr/Log/LoggerInterface.php');
}

use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;

@@ -32,11 +33,13 @@ class IpnHandler implements IpnHandlerInterface, LoggerAwareInterface
// Implement a logging library that utilizes the PSR 3 logger interface
private $logger = null;

private $ipnConfig = array('cabundle_file' => null,
'proxy_host' => null,
'proxy_port' => -1,
'proxy_username' => null,
'proxy_password' => null);
private $ipnConfig = array(
'cabundle_file' => null,
'proxy_host' => null,
'proxy_port' => -1,
'proxy_username' => null,
'proxy_password' => null
);


public function __construct($headers, $body, $ipnConfig = null)
@@ -51,11 +54,11 @@ public function __construct($headers, $body, $ipnConfig = null)
// Get the list of fields that we are interested in
$this->fields = array(
"Timestamp" => true,
"Message" => true,
"Message" => true,
"MessageId" => true,
"Subject" => false,
"TopicArn" => true,
"Type" => true
"Subject" => false,
"TopicArn" => true,
"Type" => true
);

// Validate the IPN message header [x-amz-sns-message-type]
@@ -74,7 +77,7 @@ public function __construct($headers, $body, $ipnConfig = null)
private function checkConfigKeys($ipnConfig)
{
$ipnConfig = array_change_key_case($ipnConfig, CASE_LOWER);
$ipnConfig = $this->trimArray($ipnConfig);
$ipnConfig = $this->trimArray($ipnConfig);

foreach ($ipnConfig as $key => $value) {
if (array_key_exists($key, $this->ipnConfig)) {
@@ -86,13 +89,15 @@ private function checkConfigKeys($ipnConfig)
}
}

public function setLogger(LoggerInterface $logger = null) {
public function setLogger(LoggerInterface $logger = null)
{
$this->logger = $logger;
}

/* Helper function to log data within the Client */

private function logMessage($message) {
private function logMessage($message)
{
if ($this->logger) {
$this->logger->debug($message);
}
@@ -101,7 +106,7 @@ private function logMessage($message) {
/* Setter function
* Sets the value for the key if the key exists in ipnConfig
*/

public function __set($name, $value)
{
if (array_key_exists(strtolower($name), $this->ipnConfig)) {
@@ -114,7 +119,7 @@ public function __set($name, $value)
/* Getter function
* Returns the value for the key if the key exists in ipnConfig
*/

public function __get($name)
{
if (array_key_exists(strtolower($name), $this->ipnConfig)) {
@@ -125,16 +130,15 @@ public function __get($name)
}

/* Trim the input Array key values */

private function trimArray($array)
{
foreach ($array as $key => $value)
{
$array[$key] = trim($value);
}
return $array;
foreach ($array as $key => $value) {
$array[$key] = trim($value);
}
return $array;
}

private function validateHeaders()
{
// Quickly check that this is a sns message
@@ -165,7 +169,7 @@ private function getMessage()
*
* @return string error message
*/

private function getErrorMessageForJsonError($json_error)
{
switch ($json_error) {
@@ -263,10 +267,10 @@ private function validateUrl($url)
*
* @return bool true if valid
*/

private function constructAndVerifySignature()
{
$signature = base64_decode($this->getMandatoryField("Signature"));
$signature = base64_decode($this->getMandatoryField("Signature"));
$certificatePath = $this->getMandatoryField("SigningCertURL");
$this->validateUrl($certificatePath);
$this->certificate = $this->getCertificate($certificatePath);
@@ -281,12 +285,12 @@ private function constructAndVerifySignature()
*
* gets the certificate from the $certificatePath using Curl
*/

private function getCertificate($certificatePath)
{
$httpCurlRequest = new HttpCurl($this->ipnConfig);
$httpCurlRequest = new HttpCurl($this->ipnConfig);

$response = $httpCurlRequest->httpGet($certificatePath);
$response = $httpCurlRequest->httpGet($certificatePath);

return $response;
}
@@ -302,12 +306,12 @@ public function verifySignatureIsCorrectFromCertificate($signature)
{
$certKey = openssl_get_publickey($this->certificate);

if ($certKey === False) {
if ($certKey === false) {
throw new \Exception("Unable to extract public key from cert");
}

try {
$certInfo = openssl_x509_parse($this->certificate, true);
$certInfo = openssl_x509_parse($this->certificate, true);
$certSubject = $certInfo["subject"];

if (is_null($certSubject)) {
@@ -340,7 +344,7 @@ public function verifySignatureIsCorrectFromCertificate($signature)
*
* @return string field contents if found
*/

private function getMandatoryField($fieldName)
{
$value = $this->getField($fieldName);
@@ -356,7 +360,7 @@ private function getMandatoryField($fieldName)
*
* @return string field contents if found, null otherwise
*/

private function getField($fieldName)
{
if (array_key_exists($fieldName, $this->snsMessage)) {
@@ -367,7 +371,7 @@ private function getField($fieldName)
}

/* returnMessage() - JSON decode the raw [Message] portion of the IPN */

public function returnMessage()
{
return json_decode($this->snsMessage['Message'], true);
@@ -383,14 +387,14 @@ public function returnMessage()
* Topic ARN - Topic of the IPN
* @return response in JSON format
*/

public function toJson()
{
$response = $this->simpleXmlObject();

// Merging the remaining fields with the response
$remainingFields = $this->getRemainingIpnFields();
$responseArray = array_merge($remainingFields,(array)$response);
$responseArray = array_merge($remainingFields, (array)$response);

// Converting to JSON format
$response = json_encode($responseArray);
@@ -401,7 +405,7 @@ public function toJson()
/* toArray() - Converts IPN [Message] field to associative array
* @return response in array format
*/

public function toArray()
{
$response = $this->simpleXmlObject();
@@ -412,7 +416,7 @@ public function toArray()

// Merging the remaining fields with the response array
$remainingFields = $this->getRemainingIpnFields();
$response = array_merge($remainingFields,$response);
$response = array_merge($remainingFields, $response);

return $response;
}
@@ -436,7 +440,7 @@ private function simpleXmlObject()
$this->logMessage($this->sanitizeResponseData($ipnMessage['NotificationData']));

// Getting the Simple XML element object of the IPN XML Response Body
$response = simplexml_load_string((string) $ipnMessage['NotificationData']);
$response = simplexml_load_string((string)$ipnMessage['NotificationData']);

// Adding the Type, MessageId, TopicArn details of the IPN to the Simple XML element Object
$response->addChild('Type', $this->snsMessage['Type']);
@@ -449,16 +453,17 @@ private function simpleXmlObject()
/* getRemainingIpnFields()
* Gets the remaining fields of the IPN to be later appended to the return message
*/

private function getRemainingIpnFields()
{
$ipnMessage = $this->returnMessage();

$remainingFields = array(
'NotificationReferenceId' =>$ipnMessage['NotificationReferenceId'],
'NotificationType' =>$ipnMessage['NotificationType'],
'SellerId' =>$ipnMessage['SellerId'],
'ReleaseEnvironment' =>$ipnMessage['ReleaseEnvironment'] );
'NotificationReferenceId' => $ipnMessage['NotificationReferenceId'],
'NotificationType' => $ipnMessage['NotificationType'],
'SellerId' => $ipnMessage['SellerId'],
'ReleaseEnvironment' => $ipnMessage['ReleaseEnvironment']
);

return $remainingFields;
}
8 changes: 4 additions & 4 deletions AmazonPay/IpnHandlerInterface.php
Original file line number Diff line number Diff line change
@@ -4,9 +4,9 @@
/* Interface for IpnHandler.php */

interface IpnHandlerInterface
{
{
/* returnMessage() - JSON decode the raw [Message] portion of the IPN */

public function returnMessage();

/* toJson() - Converts IPN [Message] field to JSON
@@ -19,12 +19,12 @@ public function returnMessage();
* Topic ARN - Topic of the IPN
* @return response in JSON format
*/

public function toJson();

/* toArray() - Converts IPN [Message] field to associative array
* @return response in array format
*/

public function toArray();
}
Loading

0 comments on commit a1548b7

Please sign in to comment.