Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgraded to Omnipay v3 #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
}
],
"require": {
"omnipay/common": "~2.0"
"omnipay/common": "^3.0",
"ext-json": "*"
},
"require-dev": {
"omnipay/tests": "~2.0",
"omnipay/tests": "^3.0",
"satooshi/php-coveralls": "^0.7.1"
},
"autoload": {
Expand All @@ -36,7 +37,8 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
"1.0.x-dev": "1.0-dev",
"dev-master": "3.0.x-dev"
}
}
}
}
27 changes: 16 additions & 11 deletions src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
namespace Omnipay\TwoCheckoutPlus;

use Omnipay\Common\AbstractGateway;
use Omnipay\Common\Message\AbstractRequest;
use Omnipay\TwoCheckoutPlus\Message\PurchaseRequest;
use Omnipay\TwoCheckoutPlus\Message\RefundRequest;
use Omnipay\TwoCheckoutPlus\Message\DetailSaleRequest;
use Omnipay\TwoCheckoutPlus\Message\StopRecurringRequest;
use Omnipay\TwoCheckoutPlus\Message\CompletePurchaseRequest;
use Omnipay\TwoCheckoutPlus\Message\NotificationRequest;

/**
* 2Checkout Gateway.
Expand Down Expand Up @@ -121,8 +128,6 @@ public function setLanguage($value)
/**
* Getter: purchase step.
*
* @param $value
*
* @return $this
*/
public function getPurchaseStep()
Expand Down Expand Up @@ -387,45 +392,45 @@ public function setAdminPassword($value)
/**
* @param array $parameters
*
* @return \Omnipay\Common\Message\AbstractRequest
* @return AbstractRequest
*/
public function purchase(array $parameters = array())
{
return $this->createRequest('\Omnipay\TwoCheckoutPlus\Message\PurchaseRequest', $parameters);
return $this->createRequest(PurchaseRequest::class, $parameters);
}

public function refund(array $parameters = array())
{
return $this->createRequest('\Omnipay\TwoCheckoutPlus\Message\RefundRequest', $parameters);
return $this->createRequest(RefundRequest::class, $parameters);
}

public function fetchSaleDetails(array $parameters = array())
{
return $this->createRequest('\Omnipay\TwoCheckoutPlus\Message\DetailSaleRequest', $parameters);
return $this->createRequest(DetailSaleRequest::class, $parameters);
}

public function stopRecurring(array $parameters = array())
{
return $this->createRequest('\Omnipay\TwoCheckoutPlus\Message\StopRecurringRequest', $parameters);
return $this->createRequest(StopRecurringRequest::class, $parameters);
}

/**
* @param array $parameters
*
* @return \Omnipay\Common\Message\AbstractRequest
* @return AbstractRequest
*/
public function completePurchase(array $parameters = array())
{
return $this->createRequest('\Omnipay\TwoCheckoutPlus\Message\CompletePurchaseRequest', $parameters);
return $this->createRequest(CompletePurchaseRequest::class, $parameters);
}

/**
* @param array $parameters
*
* @return \Omnipay\Common\Message\AbstractRequest
* @return AbstractRequest
*/
public function acceptNotification(array $parameters = array())
{
return $this->createRequest('\Omnipay\TwoCheckoutPlus\Message\NotificationRequest', $parameters);
return $this->createRequest(NotificationRequest::class, $parameters);
}
}
2 changes: 1 addition & 1 deletion src/Message/CompletePurchaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function getData()
}

$key = md5($this->getSecretWord().$this->getAccountNumber().$orderNo.$orderAmount);
if (strtolower($this->httpRequest->$request_type->get('key')) !== $key) {
if (!hash_equals($key, strtolower($this->httpRequest->$request_type->get('key')))) {
throw new InvalidResponseException('Invalid key');
}

Expand Down
6 changes: 3 additions & 3 deletions src/Message/CompletePurchaseResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ public function isSuccessful()
*/
public function getTransactionReference()
{
return isset($this->data['order_number']) ? $this->data['order_number'] : null;
return $this->data['order_number'] ?? null;
}

public function getTransactionInvoice()
{
return isset($this->data['invoice_id']) ? $this->data['invoice_id'] : null;
return $this->data['invoice_id'] ?? null;
}

/**
Expand All @@ -36,6 +36,6 @@ public function getTransactionInvoice()
*/
public function getTransactionId()
{
return isset($this->data['merchant_order_id']) ? $this->data['merchant_order_id'] : null;
return $this->data['merchant_order_id'] ?? null;
}
}
33 changes: 15 additions & 18 deletions src/Message/DetailSaleRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace Omnipay\TwoCheckoutPlus\Message;

use Guzzle\Http\Exception\BadResponseException;
use Omnipay\Common\Http\Exception\NetworkException;
use Omnipay\Common\Http\Exception\RequestException;

/**
* Purchase Request.
Expand Down Expand Up @@ -31,21 +32,17 @@ public function getEndPoint()
*/
public function getRequestHeaders()
{
return array(
return [
'Accept' => 'application/json',
);
}

public function isNotNull($value)
{
return !is_null($value);
'Authorization' => 'Basic ' . base64_encode($this->getAdminUsername() . ':' . $this->getAdminPassword())
];
}

public function getData()
{
$this->validate('adminUsername', 'adminPassword');

$data = array();
$data = [];
$data['admin_username'] = $this->getAdminUsername();
$data['admin_password'] = $this->getAdminPassword();

Expand All @@ -58,7 +55,7 @@ public function getData()
}

$data = array_filter($data, function ($value) {
return !is_null($value);
return $value !== null;
});

// remove unwanted data
Expand All @@ -76,8 +73,7 @@ public function getData()
public function sendData($data)
{
$payload = $data;
unset($payload['admin_username']);
unset($payload['admin_password']);
unset($payload['admin_username'], $payload['admin_password']);

$query = '';
if (!empty($payload['invoice_id'])) {
Expand All @@ -89,16 +85,17 @@ public function sendData($data)
}

try {
$response = $this->httpClient->get(
$response = $this->httpClient->request(
'GET',
$this->getEndpoint() . $query,
$this->getRequestHeaders()
)->setAuth($data['admin_username'], $data['admin_password'])->send();
);

return new DetailSaleResponse($this, $response->json());
} catch (BadResponseException $e) {
$response = $e->getResponse();
$json = json_decode($response->getBody()->getContents(), true);

return new DetailSaleResponse($this, $response->json());
return new DetailSaleResponse($this, $json ?? []);
} catch (RequestException|NetworkException $e) {
return new DetailSaleResponse($this, ['error' => $e->getMessage()]);
}
}
}
4 changes: 2 additions & 2 deletions src/Message/DetailSaleResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function isRedirect()
*/
public function getCode()
{
return isset($this->data['response_code']) ? $this->data['response_code'] : null;
return $this->data['response_code'] ?? null;
}

public function getLineItems()
Expand All @@ -50,6 +50,6 @@ public function getLineItems()
*/
public function getMessage()
{
return isset($this->data['sale']) ? $this->data['sale'] : json_encode($this->data['errors']);
return $this->data['sale'] ?? json_encode($this->data['errors']);
}
}
2 changes: 1 addition & 1 deletion src/Message/NotificationResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function isSuccessful()
$hashInvoice = $this->data['invoice_id'];
$StringToHash = strtoupper(md5($hashOrder.$hashSid.$hashInvoice.$hashSecretWord));

return $StringToHash == $this->data['md5_hash'];
return $StringToHash === $this->data['md5_hash'];
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Message/PurchaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function getData()
{
$this->validate('accountNumber', 'returnUrl');

$data = array();
$data = [];
$data['sid'] = $this->getAccountNumber();
$data['mode'] = '2CO';
$data['merchant_order_id'] = $this->getTransactionId();
Expand Down Expand Up @@ -87,7 +87,7 @@ public function getData()
}

$data = array_filter($data, function ($value) {
return !is_null($value);
return $value !== null;
});

return $data;
Expand Down
4 changes: 2 additions & 2 deletions src/Message/PurchaseResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public function getEndPoint()
{
if ($this->data['sandbox']) {
return $this->testEndpoint;
} else {
return $this->liveEndpoint;
}

return $this->liveEndpoint;
}

/**
Expand Down
36 changes: 17 additions & 19 deletions src/Message/RefundRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Omnipay\TwoCheckoutPlus\Message;

use Guzzle\Http\Exception\BadResponseException;
use Omnipay\Common\Http\Exception\NetworkException;
use Omnipay\Common\Http\Exception\RequestException;

/**
* Purchase Request.
Expand Down Expand Up @@ -31,21 +33,18 @@ public function getEndPoint()
*/
public function getRequestHeaders()
{
return array(
return [
'Accept' => 'application/json',
);
}

public function isNotNull($value)
{
return !is_null($value);
'Content-Type' => 'application/json',
'Authorization' => 'Basic ' . base64_encode($this->getAdminUsername() . ':' . $this->getAdminPassword())
];
}

public function getData()
{
$this->validate('adminUsername', 'adminPassword', 'saleId', 'comment');

$data = array();
$data = [];
$data['admin_username'] = $this->getAdminUsername();
$data['admin_password'] = $this->getAdminPassword();

Expand Down Expand Up @@ -78,7 +77,7 @@ public function getData()
}

$data = array_filter($data, function ($value) {
return !is_null($value);
return $value !== null;
});

// remove unwanted data
Expand All @@ -96,21 +95,20 @@ public function getData()
public function sendData($data)
{
$payload = $data;
unset($payload['admin_username']);
unset($payload['admin_password']);
unset($payload['admin_username'], $payload['admin_password']);

try {
$response = $this->httpClient->post(
$response = $this->httpClient->request(
'POST',
$this->getEndpoint(),
$this->getRequestHeaders(),
$payload
)->setAuth($data['admin_username'], $data['admin_password'])->send();

return new RefundResponse($this, $response->json());
} catch (BadResponseException $e) {
$response = $e->getResponse();
json_encode($payload)
);
$json = json_decode($response->getBody()->getContents(), true);

return new RefundResponse($this, $response->json());
return new RefundResponse($this, $json ?? []);
} catch (RequestException|NetworkException $e) {
return new RefundResponse($this, ['error' => $e->getMessage()]);
}
}
}
8 changes: 3 additions & 5 deletions src/Message/RefundResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class RefundResponse extends AbstractResponse implements ResponseInterface
*/
public function isSuccessful()
{
return isset($this->data['response_code']) ? $this->data['response_code'] == 'OK' : false;
return isset($this->data['response_code']) ? $this->data['response_code'] === 'OK' : false;
}

/**
Expand All @@ -37,17 +37,15 @@ public function isRedirect()
*/
public function getCode()
{
return isset($this->data['response_code']) ? $this->data['response_code'] : null;
return $this->data['response_code'] ?? null;
}

/**
* {@inheritdoc}
*/
public function getMessage()
{
return isset($this->data['response_message']) ?
$this->data['response_message'] :
json_encode($this->data['errors']);
return $this->data['response_message'] ?? json_encode($this->data['errors'] ?? []);
}

/**
Expand Down
Loading