Skip to content

Commit

Permalink
Minor code clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
stymiee committed Jul 29, 2021
1 parent 2719bd9 commit b1ab224
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 111 deletions.
24 changes: 12 additions & 12 deletions src/Authnetjson/AuthnetApiFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ class AuthnetApiFactory
*/
public static function getJsonApiHandler(string $login, string $transaction_key, ?int $endpoint = null): object
{
$login = trim($login);
$login = trim($login);
$transaction_key = trim($transaction_key);
$endpoint = $endpoint ?? self::USE_CDN_SERVER;
$api_url = static::getWebServiceURL($endpoint);
$endpoint = $endpoint ?? self::USE_CDN_SERVER;
$api_url = static::getWebServiceURL($endpoint);

if (empty($login) || empty($transaction_key)) {
throw new AuthnetInvalidCredentialsException('You have not configured your login credentials properly.');
Expand All @@ -83,7 +83,7 @@ public static function getJsonApiHandler(string $login, string $transaction_key,
/**
* Gets the API endpoint to be used for a JSON API call.
*
* @param int $server ID of which server to use
* @param int $server ID of which server to use
* @return string The URL endpoint the request is to be sent to
* @throws AuthnetInvalidServerException
*/
Expand Down Expand Up @@ -113,10 +113,10 @@ protected static function getWebServiceURL(int $server): string
*/
public static function getSimHandler(string $login, string $transaction_key, ?int $server = null): object
{
$login = trim($login);
$login = trim($login);
$transaction_key = trim($transaction_key);
$server = $server ?? self::USE_PRODUCTION_SERVER;
$api_url = static::getSimURL($server);
$server = $server ?? self::USE_PRODUCTION_SERVER;
$api_url = static::getSimURL($server);

if (empty($login) || empty($transaction_key)) {
throw new AuthnetInvalidCredentialsException('You have not configured your login credentials properly.');
Expand All @@ -128,7 +128,7 @@ public static function getSimHandler(string $login, string $transaction_key, ?in
/**
* Gets the API endpoint to be used for a SIM API call.
*
* @param int $server ID of which server to use
* @param int $server ID of which server to use
* @return string The URL endpoint the request is to be sent to
* @throws AuthnetInvalidServerException
*/
Expand Down Expand Up @@ -156,10 +156,10 @@ protected static function getSimURL(int $server): string
*/
public static function getWebhooksHandler(string $login, string $transaction_key, ?int $server = null): object
{
$login = trim($login);
$login = trim($login);
$transaction_key = trim($transaction_key);
$server = $server ?? self::USE_PRODUCTION_SERVER;
$api_url = static::getWebhooksURL($server);
$server = $server ?? self::USE_PRODUCTION_SERVER;
$api_url = static::getWebhooksURL($server);

if (empty($login) || empty($transaction_key)) {
throw new AuthnetInvalidCredentialsException('You have not configured your login credentials properly.');
Expand All @@ -185,7 +185,7 @@ public static function getWebhooksHandler(string $login, string $transaction_key
/**
* Gets the API endpoint to be used for a SIM API call.
*
* @param int $server ID of which server to use
* @param int $server ID of which server to use
* @return string The URL endpoint the request is to be sent to
* @throws AuthnetInvalidServerException
*/
Expand Down
32 changes: 16 additions & 16 deletions src/Authnetjson/AuthnetJsonRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,15 @@ class AuthnetJsonRequest
* Creates the request object by setting the Authorize.Net credentials and URL of the endpoint to be used
* for the API call.
*
* @param string $login Authorize.Net API login ID
* @param string $login Authorize.Net API login ID
* @param string $transactionKey Authorize.Net API Transaction Key
* @param string $api_url URL endpoint for processing a transaction
* @param string $api_url URL endpoint for processing a transaction
*/
public function __construct(string $login, string $transactionKey, string $api_url)
{
$this->login = $login;
$this->login = $login;
$this->transactionKey = $transactionKey;
$this->url = $api_url;
$this->url = $api_url;
}

/**
Expand All @@ -126,17 +126,17 @@ public function __construct(string $login, string $transactionKey, string $api_u
*/
public function __toString()
{
$output = '<table id="authnet-request">'."\n";
$output .= '<caption>Authorize.Net Request</caption>'."\n";
$output .= '<tr><th colspan="2"><b>Class Parameters</b></th></tr>'."\n";
$output .= '<tr><td><b>API Login ID</b></td><td>'.$this->login.'</td></tr>'."\n";
$output .= '<tr><td><b>Transaction Key</b></td><td>'.$this->transactionKey.'</td></tr>'."\n";
$output .= '<tr><td><b>Authnet Server URL</b></td><td>'.$this->url.'</td></tr>'."\n";
$output .= '<tr><th colspan="2"><b>Request JSON</b></th></tr>'."\n";
$output = '<table id="authnet-request">' . "\n";
$output .= '<caption>Authorize.Net Request</caption>' . "\n";
$output .= '<tr><th colspan="2"><b>Class Parameters</b></th></tr>' . "\n";
$output .= '<tr><td><b>API Login ID</b></td><td>' . $this->login . '</td></tr>' . "\n";
$output .= '<tr><td><b>Transaction Key</b></td><td>' . $this->transactionKey . '</td></tr>' . "\n";
$output .= '<tr><td><b>Authnet Server URL</b></td><td>' . $this->url . '</td></tr>' . "\n";
$output .= '<tr><th colspan="2"><b>Request JSON</b></th></tr>' . "\n";
if (!empty($this->requestJson)) {
$output .= '<tr><td colspan="2"><pre>'."\n";
$output .= $this->requestJson."\n";
$output .= '</pre></td></tr>'."\n";
$output .= '<tr><td colspan="2"><pre>' . "\n";
$output .= $this->requestJson . "\n";
$output .= '</pre></td></tr>' . "\n";
}
$output .= '</table>';

Expand Down Expand Up @@ -214,10 +214,10 @@ private function process(): string
return $this->processor->response;
}
$error_message = null;
$error_code = null;
$error_code = null;
if ($this->processor->error_code || $this->processor->error_message) {
$error_message = $this->processor->error_message;
$error_code = $this->processor->error_code;
$error_code = $this->processor->error_code;
}
throw new AuthnetCurlException(sprintf('Connection error: %s (%s)', $error_message, $error_code));
}
Expand Down
43 changes: 23 additions & 20 deletions src/Authnetjson/AuthnetJsonResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
* @link https://github.com/stymiee/authnetjson
* @see https://developer.authorize.net/api/reference/
*
* @property object $messages
* @property string $directResponse
* @property string $validationDirectResponseList
* @property object $transactionResponse
* @property object $messages
* @property string $directResponse
* @property string $validationDirectResponseList
* @property object $transactionResponse
*
* @method null createTransactionRequest(array $array) process a payment
* @method null sendCustomerTransactionReceiptRequest(array $array) get a list of unsettled transactions
Expand Down Expand Up @@ -115,7 +115,7 @@ class AuthnetJsonResponse
/**
* Creates the response object with the response json returned from the API call
*
* @param string $responseJson Response from Authorize.Net
* @param string $responseJson Response from Authorize.Net
* @throws AuthnetInvalidJsonException
*/
public function __construct(string $responseJson)
Expand All @@ -128,9 +128,12 @@ public function __construct(string $responseJson)
if ($this->directResponse || $this->validationDirectResponseList || isset($this->response->validationDirectResponse)) {
$response = $this->directResponse ?: $this->validationDirectResponseList ?: $this->response->validationDirectResponse;
if (is_array($response)) {
$this->transactionInfoArray = array_map(static function ($r) {
return new TransactionResponse($r);
}, $response);
$this->transactionInfoArray = array_map(
static function ($r) {
return new TransactionResponse($r);
},
$response
);
} else {
$this->transactionInfo = new TransactionResponse($response);
$this->transactionInfoArray = [$this->transactionInfo];
Expand All @@ -145,12 +148,12 @@ public function __construct(string $responseJson)
*/
public function __toString()
{
$output = '<table id="authnet-response">'."\n";
$output .= '<caption>Authorize.Net Response</caption>'."\n";
$output .= '<tr><th colspan="2"><b>Response JSON</b></th></tr>'."\n";
$output .= '<tr><td colspan="2"><pre>'."\n";
$output .= $this->responseJson."\n";
$output .= '</pre></td></tr>'."\n";
$output = '<table id="authnet-response">' . "\n";
$output .= '<caption>Authorize.Net Response</caption>' . "\n";
$output .= '<tr><th colspan="2"><b>Response JSON</b></th></tr>' . "\n";
$output .= '<tr><td colspan="2"><pre>' . "\n";
$output .= $this->responseJson . "\n";
$output .= '</pre></td></tr>' . "\n";
$output .= '</table>';

return $output;
Expand All @@ -159,7 +162,7 @@ public function __toString()
/**
* Gets a response variable from the API response
*
* @param string $var unused
* @param string $var unused
* @return string requested variable from the API call response
*/
public function __get(string $var)
Expand Down Expand Up @@ -220,23 +223,23 @@ public function isDeclined(): bool
/**
* Check to see if the ResponseCode matches the expected value
*
* @param int $status
* @param int $status
* @return bool Check to see if the ResponseCode matches the expected value
*/
protected function checkTransactionStatus(int $status): bool
{
if ($this->transactionInfo instanceof TransactionResponse) {
$match = (int) $this->transactionInfo->getTransactionResponseField('ResponseCode') === $status;
$match = (int)$this->transactionInfo->getTransactionResponseField('ResponseCode') === $status;
} else {
$match = (int) $this->transactionResponse->responseCode === $status;
$match = (int)$this->transactionResponse->responseCode === $status;
}
return $match;
}

/**
* Gets the transaction response field for AIM and CIM transactions.
*
* @param mixed $field Name or key of the transaction field to be retrieved
* @param mixed $field Name or key of the transaction field to be retrieved
* @return null|string Transaction field to be retrieved
* @throws AuthnetTransactionResponseCallException
*/
Expand Down Expand Up @@ -299,7 +302,7 @@ public function getErrorCode(): string
}

/**
* @param string $type Whether to get the error code or text
* @param string $type Whether to get the error code or text
* @return string
*/
private function getError(string $type): string
Expand Down
30 changes: 15 additions & 15 deletions src/Authnetjson/AuthnetWebhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,17 @@ class AuthnetWebhook
/**
* Creates the response object with the response json returned from the API call
*
* @param string $signature Authorize.Net Signature Key
* @param string $payload Webhook Notification sent by Authorize.Net
* @param array $headers HTTP headers sent with Webhook. Optional if PHP is run as an Apache module
* @param string $signature Authorize.Net Signature Key
* @param string $payload Webhook Notification sent by Authorize.Net
* @param array $headers HTTP headers sent with Webhook. Optional if PHP is run as an Apache module
* @throws AuthnetInvalidCredentialsException
* @throws AuthnetInvalidJsonException
*/
public function __construct(string $signature, string $payload, array $headers = [])
{
$this->signature = $signature;
$this->signature = $signature;
$this->webhookJson = $payload;
$this->headers = $headers;
$this->headers = $headers;
if (empty($this->headers)) {
$this->headers = $this->getAllHeaders();
}
Expand All @@ -81,16 +81,16 @@ public function __construct(string $signature, string $payload, array $headers =
*/
public function __toString()
{
$output = '<table id="authnet-webhook">'."\n";
$output .= '<caption>Authorize.Net Webhook</caption>'."\n";
$output .= '<tr><th colspan="2"><b>Response HTTP Headers</b></th></tr>'."\n";
$output .= '<tr><td colspan="2"><pre>'."\n";
$output .= var_export($this->headers)."\n";
$output .= '</pre></td></tr>'."\n";
$output .= '<tr><th colspan="2"><b>Response JSON</b></th></tr>'."\n";
$output .= '<tr><td colspan="2"><pre>'."\n";
$output .= $this->webhookJson."\n";
$output .= '</pre></td></tr>'."\n";
$output = '<table id="authnet-webhook">' . "\n";
$output .= '<caption>Authorize.Net Webhook</caption>' . "\n";
$output .= '<tr><th colspan="2"><b>Response HTTP Headers</b></th></tr>' . "\n";
$output .= '<tr><td colspan="2"><pre>' . "\n";
$output .= var_export($this->headers) . "\n";
$output .= '</pre></td></tr>' . "\n";
$output .= '<tr><th colspan="2"><b>Response JSON</b></th></tr>' . "\n";
$output .= '<tr><td colspan="2"><pre>' . "\n";
$output .= $this->webhookJson . "\n";
$output .= '</pre></td></tr>' . "\n";
$output .= '</table>';

return $output;
Expand Down
Loading

0 comments on commit b1ab224

Please sign in to comment.