Skip to content

Commit

Permalink
Wordpress compliance
Browse files Browse the repository at this point in the history
  • Loading branch information
AppelsDev committed Feb 28, 2018
1 parent 0c79830 commit 924911d
Showing 1 changed file with 50 additions and 27 deletions.
77 changes: 50 additions & 27 deletions src/API/PaymentGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

/**
* Wrapper class for the payment gateway.
* Compliant to WordPress' code standards.
*/
class PaymentGateway
{
Expand Down Expand Up @@ -55,7 +56,7 @@ public function __construct($apiKey)
* @return array $result The payment that is created, the user should be
* redirected to links.paymentUrl
*/
public function openPayment($amount, $description, $redirectUrl, $webhookUrl, $metadata = [])
public function openPayment($amount, $description, $redirectUrl, $webhookUrl, $metadata = array())
{
if (!is_float($amount)) {
throw new \InvalidArgumentException('$amount should be a float');
Expand All @@ -77,13 +78,13 @@ public function openPayment($amount, $description, $redirectUrl, $webhookUrl, $m
throw new \Exception('$metadata should be a array');
}

$response = $this->call('/payments', [
$response = $this->call('/payments', array(
'amount' => $amount,
'redirectUrl' => $redirectUrl,
'webhookUrl' => $webhookUrl,
'description' => $description,
'metadata' => json_encode($metadata),
], 'POST');
), 'POST');

return $response;
}
Expand Down Expand Up @@ -123,30 +124,52 @@ public function getPayment($id)
*
* @return array $response
*/
private function call($url, array $parameters = [], $method = 'GET')
private function call($url, array $parameters = array(), $method = 'GET')
{
$headers = [
'X-AUTH-TOKEN: '.$this->apiKey,
];

$curl = curl_init('https://wallet.getadcoin.com/api'.$url);

curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

if ($method === 'POST') {
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($parameters));
}

$response = curl_exec($curl);

$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

if ($status < 200 || $status >= 300) {
throw new ClientException($response, $status);
}

return json_decode($response, true);
if (function_exists('wp_remote_post')) {
// Use WordPress' HTTP API
$response = wp_remote_post(
'https://wallet.getadcoin.com/api'.$url,
array(
'method' => $method,
'timeout' => 20,
'redirection' => 5,
'httpversion' => '1.0',
'blocking' => true,
'headers' => array('X-AUTH-TOKEN' => $this->apiKey),
'body' => $parameters,
'cookies' => array()
)
);
$body = wp_remote_retrieve_body($response);
$status = wp_remote_retrieve_response_code($response);

} else {
// Use CURL
$headers = [
'X-AUTH-TOKEN: '.$this->apiKey,
];

$curl = curl_init('https://wallet.getadcoin.com/api'.$url);

curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

if ($method === 'POST') {
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($parameters));
}

$body = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
}

// Check HTTP status
if ($status < 200 || $status >= 300) {
throw new ClientException($body, $status);
}

// Decode and return response body
return json_decode($body, true);
}
}

0 comments on commit 924911d

Please sign in to comment.