diff --git a/composer.json b/composer.json index e56186c..a758d7e 100644 --- a/composer.json +++ b/composer.json @@ -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": { @@ -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" } } -} \ No newline at end of file +} diff --git a/src/Gateway.php b/src/Gateway.php index 4d56e48..370a3f0 100644 --- a/src/Gateway.php +++ b/src/Gateway.php @@ -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. @@ -121,8 +128,6 @@ public function setLanguage($value) /** * Getter: purchase step. * - * @param $value - * * @return $this */ public function getPurchaseStep() @@ -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); } } diff --git a/src/Message/CompletePurchaseRequest.php b/src/Message/CompletePurchaseRequest.php index 566e675..105bf40 100644 --- a/src/Message/CompletePurchaseRequest.php +++ b/src/Message/CompletePurchaseRequest.php @@ -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'); } diff --git a/src/Message/CompletePurchaseResponse.php b/src/Message/CompletePurchaseResponse.php index 41eda3a..7b1c9b2 100644 --- a/src/Message/CompletePurchaseResponse.php +++ b/src/Message/CompletePurchaseResponse.php @@ -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; } /** @@ -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; } } diff --git a/src/Message/DetailSaleRequest.php b/src/Message/DetailSaleRequest.php index 5257e4a..37a855c 100644 --- a/src/Message/DetailSaleRequest.php +++ b/src/Message/DetailSaleRequest.php @@ -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. @@ -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(); @@ -58,7 +55,7 @@ public function getData() } $data = array_filter($data, function ($value) { - return !is_null($value); + return $value !== null; }); // remove unwanted data @@ -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'])) { @@ -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()]); } } } diff --git a/src/Message/DetailSaleResponse.php b/src/Message/DetailSaleResponse.php index e32bb3b..ce2f893 100644 --- a/src/Message/DetailSaleResponse.php +++ b/src/Message/DetailSaleResponse.php @@ -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() @@ -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']); } } diff --git a/src/Message/NotificationResponse.php b/src/Message/NotificationResponse.php index 920b5aa..c3c42b7 100644 --- a/src/Message/NotificationResponse.php +++ b/src/Message/NotificationResponse.php @@ -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']; } /** diff --git a/src/Message/PurchaseRequest.php b/src/Message/PurchaseRequest.php index 7b049d5..a097a34 100644 --- a/src/Message/PurchaseRequest.php +++ b/src/Message/PurchaseRequest.php @@ -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(); @@ -87,7 +87,7 @@ public function getData() } $data = array_filter($data, function ($value) { - return !is_null($value); + return $value !== null; }); return $data; diff --git a/src/Message/PurchaseResponse.php b/src/Message/PurchaseResponse.php index e95022e..8359bc4 100644 --- a/src/Message/PurchaseResponse.php +++ b/src/Message/PurchaseResponse.php @@ -22,9 +22,9 @@ public function getEndPoint() { if ($this->data['sandbox']) { return $this->testEndpoint; - } else { - return $this->liveEndpoint; } + + return $this->liveEndpoint; } /** diff --git a/src/Message/RefundRequest.php b/src/Message/RefundRequest.php index da9dccc..bbb2965 100644 --- a/src/Message/RefundRequest.php +++ b/src/Message/RefundRequest.php @@ -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. @@ -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(); @@ -78,7 +77,7 @@ public function getData() } $data = array_filter($data, function ($value) { - return !is_null($value); + return $value !== null; }); // remove unwanted data @@ -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()]); } } } diff --git a/src/Message/RefundResponse.php b/src/Message/RefundResponse.php index 3487646..d9ee325 100644 --- a/src/Message/RefundResponse.php +++ b/src/Message/RefundResponse.php @@ -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; } /** @@ -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; } /** @@ -45,9 +45,7 @@ public function getCode() */ 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'] ?? []); } /** diff --git a/src/Message/StopRecurringRequest.php b/src/Message/StopRecurringRequest.php index 2f466c6..c4950e8 100644 --- a/src/Message/StopRecurringRequest.php +++ b/src/Message/StopRecurringRequest.php @@ -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. @@ -31,14 +33,11 @@ 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() @@ -57,7 +56,7 @@ public function getData() } $data = array_filter($data, function ($value) { - return !is_null($value); + return $value !== null; }); // remove unwanted data @@ -75,21 +74,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 StopRecurringResponse($this, $response->json()); - } catch (BadResponseException $e) { - $response = $e->getResponse(); + json_encode($payload) + ); + $json = json_decode($response->getBody()->getContents(), true); - return new StopRecurringResponse($this, $response->json()); + return new StopRecurringResponse($this, $json ?? []); + } catch (RequestException|NetworkException $e) { + return new StopRecurringResponse($this, ['error' => $e->getMessage()]); } } } diff --git a/src/Message/StopRecurringResponse.php b/src/Message/StopRecurringResponse.php index 822e7fe..0a2ed94 100644 --- a/src/Message/StopRecurringResponse.php +++ b/src/Message/StopRecurringResponse.php @@ -17,7 +17,7 @@ class StopRecurringResponse extends AbstractResponse implements ResponseInterfac */ public function isSuccessful() { - return !isset($this->data['errors']) && $this->data['response_code'] == 'OK'; + return !isset($this->data['errors']) && $this->data['response_code'] === 'OK'; } /** @@ -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() @@ -50,8 +50,6 @@ public function getLineItems() */ 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']); } } diff --git a/src/Message/TokenPurchaseRequest.php b/src/Message/TokenPurchaseRequest.php index bcb75e5..bbd9c4f 100644 --- a/src/Message/TokenPurchaseRequest.php +++ b/src/Message/TokenPurchaseRequest.php @@ -3,6 +3,9 @@ namespace Omnipay\TwoCheckoutPlus\Message; use Guzzle\Http\Exception\BadResponseException; +use Omnipay\Common\Exception\InvalidRequestException; +use Omnipay\Common\Http\Exception\NetworkException; +use Omnipay\Common\Http\Exception\RequestException; /** * Purchase Request. @@ -28,7 +31,7 @@ public function getEndpoint() public function isNotNull($value) { - return !is_null($value); + return $value !== null; } /** @@ -38,22 +41,23 @@ public function isNotNull($value) */ public function getRequestHeaders() { - return array( + return [ 'Accept' => 'application/json', 'Content-Type' => 'application/json', - ); + 'Authorization' => 'Basic ' . base64_encode($this->getAdminUsername() . ':' . $this->getAdminPassword()), + ]; } /** * @return array * - * @throws \Omnipay\Common\Exception\InvalidRequestException + * @throws InvalidRequestException */ public function getData() { $this->validate('accountNumber', 'privateKey', 'token', 'amount', 'transactionId'); - $data = array(); + $data = []; $data['sellerId'] = $this->getAccountNumber(); $data['privateKey'] = $this->getPrivateKey(); $data['merchantOrderId'] = $this->getTransactionId(); @@ -98,17 +102,18 @@ public function getData() public function sendData($data) { try { - $response = $this->httpClient->post( + $response = $this->httpClient->request( + 'POST', $this->getEndpoint(), $this->getRequestHeaders(), json_encode($data) - )->send(); + ); - return new TokenPurchaseResponse($this, $response->json()); - } catch (BadResponseException $e) { - $response = $e->getResponse(); + $json = json_decode($response->getBody()->getContents(), true); - return new TokenPurchaseResponse($this, $response->json()); + return new TokenPurchaseResponse($this, $json ?? []); + } catch (RequestException|NetworkException $e) { + return new TokenPurchaseResponse($this, ['error' => $e->getMessage()]); } } } diff --git a/src/Message/TokenPurchaseResponse.php b/src/Message/TokenPurchaseResponse.php index 0e662a1..5061216 100644 --- a/src/Message/TokenPurchaseResponse.php +++ b/src/Message/TokenPurchaseResponse.php @@ -17,9 +17,9 @@ class TokenPurchaseResponse extends AbstractResponse implements ResponseInterfac */ public function isSuccessful() { - $responseCode = $this->data['response']['responseCode']; + $responseCode = $this->data['response']['responseCode'] ?? null; - return isset($responseCode) ? $responseCode == 'APPROVED' : false; + return $responseCode === 'APPROVED'; } /** @@ -55,7 +55,7 @@ public function getMessage() */ public function getTransactionReference() { - return isset($this->data['response']['orderNumber']) ? $this->data['response']['orderNumber'] : null; + return $this->data['response']['orderNumber'] ?? null; } /** @@ -63,6 +63,6 @@ public function getTransactionReference() */ public function getTransactionId() { - return isset($this->data['response']['merchantOrderId']) ? $this->data['response']['merchantOrderId'] : null; + return $this->data['response']['merchantOrderId'] ?? null; } } diff --git a/src/TokenGateway.php b/src/TokenGateway.php index 225812d..4dc791c 100644 --- a/src/TokenGateway.php +++ b/src/TokenGateway.php @@ -3,6 +3,8 @@ namespace Omnipay\TwoCheckoutPlus; use Omnipay\Common\AbstractGateway; +use Omnipay\Common\Message\AbstractRequest; +use Omnipay\TwoCheckoutPlus\Message\TokenPurchaseRequest; /** * 2Checkout Token Gateway. @@ -94,10 +96,10 @@ public function setPrivateKey($value) /** * @param array $parameters * - * @return \Omnipay\Common\Message\AbstractRequest + * @return AbstractRequest */ public function purchase(array $parameters = array()) { - return $this->createRequest('\Omnipay\TwoCheckoutPlus\Message\TokenPurchaseRequest', $parameters); + return $this->createRequest(TokenPurchaseRequest::class, $parameters); } } diff --git a/tests/GatewayTest.php b/tests/GatewayTest.php index d519ca5..7a46947 100644 --- a/tests/GatewayTest.php +++ b/tests/GatewayTest.php @@ -84,17 +84,17 @@ public function testPurchase() public function testRefund() { - $request = $this->gateway->refund(array( + $request = $this->gateway->refund([ 'adminUsername' => 'username', 'adminPassword' => 'password', 'saleId' => 106235469964, 'comment' => 'Buyer deserved a refund', 'category' => 13 - ))->send(); + ])->send(); $this->assertSame(false, $request->isSuccessful()); $this->assertSame(false, $request->isRedirect()); - $this->assertSame('[{"code":"UNAUTHORIZED","message":"Authentication failed"}]', $request->getMessage()); + $this->assertSame('[]', $request->getMessage()); $this->assertNull($request->getCode()); } @@ -157,7 +157,7 @@ public function testAcceptNotificationFail() $this->getHttpRequest()->initialize( array(), - $this->getMockHttpResponse('FraudChangeNotificationFail.txt')->json() + json_decode($this->getMockHttpResponse('FraudChangeNotificationFail.txt')->getBody()->getContents(), true), ); $response = $this->gateway->acceptNotification()->send(); diff --git a/tests/Message/NotificationRequestTest.php b/tests/Message/NotificationRequestTest.php index c00800f..5312e50 100644 --- a/tests/Message/NotificationRequestTest.php +++ b/tests/Message/NotificationRequestTest.php @@ -18,7 +18,7 @@ public function setUp() // directly passing an array of the POSTed data would do but to prevent // duplicate array in test, i made it seem like an API response then // get the response as an array using json() method. - $this->getMockHttpResponse('FraudChangeNotificationFail.txt')->json() + json_decode($this->getMockHttpResponse('FraudChangeNotificationFail.txt')->getBody()->getContents(), true) ) ) ->setMethods(null) @@ -43,7 +43,7 @@ public function testSendData() { $data = $this->request->getData(); $response = $this->request->sendData($data); - $this->assertSame('Omnipay\TwoCheckoutPlus\Message\NotificationResponse', get_class($response)); + $this->assertInstanceOf(NotificationResponse::class, $response); } -} \ No newline at end of file +} diff --git a/tests/Message/NotificationResponseTest.php b/tests/Message/NotificationResponseTest.php index fe67ee7..b2c65cc 100644 --- a/tests/Message/NotificationResponseTest.php +++ b/tests/Message/NotificationResponseTest.php @@ -8,7 +8,7 @@ class NotificationResponseTest extends TestCase { public function testResponseFail() { - $data = $this->getMockHttpResponse('FraudChangeNotificationFail.txt')->json(); + $data = json_decode($this->getMockHttpResponse('FraudChangeNotificationFail.txt')->getBody()->getContents(), true); $data['accountNumber'] = '901290261'; $data['secretWord'] = 'MzBjODg5YTUtNzcwMS00N2NlLWFkODMtNzQ2YzllZWRjMzBj'; $response = new NotificationResponse($this->getMockRequest(), $data); @@ -22,7 +22,7 @@ public function testResponseFail() } public function testResponsePass() { - $data = $this->getMockHttpResponse('FraudChangeNotificationPass.txt')->json(); + $data = json_decode($this->getMockHttpResponse('FraudChangeNotificationPass.txt')->getBody()->getContents(), true); $data['accountNumber'] = '901290261'; $data['secretWord'] = 'MzBjODg5YTUtNzcwMS00N2NlLWFkODMtNzQ2YzllZWRjMzBj'; $response = new NotificationResponse($this->getMockRequest(), $data); @@ -36,7 +36,7 @@ public function testResponsePass() } public function testForResponseOtherThanFraudReview() { - $data = $this->getMockHttpResponse('FraudChangeNotificationPass.txt')->json(); + $data = json_decode($this->getMockHttpResponse('FraudChangeNotificationPass.txt')->getBody()->getContents(), true); $data['accountNumber'] = '901290261'; $data['secretWord'] = 'MzBjODg5YTUtNzcwMS00N2NlLWFkODMtNzQ2YzllZWRjMzBj'; $data['message_type'] = 'INVOICE_STATUS_CHANGED'; @@ -44,4 +44,4 @@ public function testForResponseOtherThanFraudReview() { $this->assertTrue($response->getTransactionStatus()); } -} \ No newline at end of file +} diff --git a/tests/Message/TokenPurchaseRequestTest.php b/tests/Message/TokenPurchaseRequestTest.php index ca3a23b..f134cde 100644 --- a/tests/Message/TokenPurchaseRequestTest.php +++ b/tests/Message/TokenPurchaseRequestTest.php @@ -1,6 +1,7 @@ addResponse($this->getMockHttpResponse('TokenPurchaseFailure.txt')); - + /** @var Client $httpClient */ $httpClient = $this->getHttpClient(); - $httpClient->addSubscriber($mock); $this->request = new TokenPurchaseRequest($httpClient, $this->getHttpRequest()); $this->request->initialize(array( @@ -35,24 +33,24 @@ public function setUp() public function testGetSetCart() { $this->request->setCart( - array( - array( + [ + [ "name" => "Demo Item", "price" => "4.99", "type" => "product", "quantity" => "1", "recurrence" => "4 Year", "startupFee" => "9.99" - ), - array( + ], + [ "name" => "Demo Item 2", "price" => "6.99", "type" => "product", "quantity" => "2", "recurrence" => "8 Year", "startupFee" => "19.99" - ) - ) + ] + ] ); $cart = $this->request->getCart(); @@ -72,7 +70,7 @@ public function testSendData() { $data = $this->request->getData(); $response = $this->request->sendData($data); - $this->assertSame('Omnipay\TwoCheckoutPlus\Message\TokenPurchaseResponse', get_class($response)); + $this->assertInstanceOf(\Omnipay\TwoCheckoutPlus\Message\TokenPurchaseResponse::class, $response); } } diff --git a/tests/Message/TokenPurchaseResponseTest.php b/tests/Message/TokenPurchaseResponseTest.php index d80dd71..2040703 100644 --- a/tests/Message/TokenPurchaseResponseTest.php +++ b/tests/Message/TokenPurchaseResponseTest.php @@ -8,7 +8,7 @@ class TokenPurchaseResponseTest extends TestCase { public function testSuccess() { - $body = file_get_contents(dirname(dirname(__FILE__)) . '/Mock/TokenPurchaseSuccess.txt'); + $body = file_get_contents(dirname(__FILE__, 2) . '/Mock/TokenPurchaseSuccess.txt'); $httpResponse = new Response(200, array('Content-Type' => 'application/json'), $body); $response = new TokenPurchaseResponse($this->getMockRequest(), $httpResponse->json()); @@ -22,7 +22,7 @@ public function testSuccess() public function testFailure() { $httpResponse = $this->getMockHttpResponse('TokenPurchaseFailure.txt'); - $response = new TokenPurchaseResponse($this->getMockRequest(), $httpResponse->json()); + $response = new TokenPurchaseResponse($this->getMockRequest(), json_decode($httpResponse->getBody()->getContents(), true)); $this->assertFalse($response->isSuccessful()); $this->assertFalse($response->isRedirect()); @@ -31,4 +31,4 @@ public function testFailure() $response->getMessage()); $this->assertNull($response->getTransactionReference()); } -} \ No newline at end of file +} diff --git a/tests/TokenGatewayTest.php b/tests/TokenGatewayTest.php index 5e46824..12b8f7f 100644 --- a/tests/TokenGatewayTest.php +++ b/tests/TokenGatewayTest.php @@ -2,10 +2,8 @@ namespace Omnipay\TwoCheckoutPlus; +use GuzzleHttp\Psr7\Response; use Omnipay\Tests\GatewayTestCase; -use \Guzzle\Plugin\Mock\MockPlugin; -use Guzzle\Http\Message\Response; -use Omnipay\TwoCheckoutPlus\Message\TokenPurchaseRequest; class TokenGatewayTest extends GatewayTestCase { @@ -17,48 +15,46 @@ public function setUp() { parent::setUp(); - $mock = new MockPlugin(); - $body = file_get_contents(dirname(__FILE__) . '/Mock/TokenPurchaseSuccess.txt'); - $mock->addResponse(new Response(200, array('Content-Type' => 'application/json'), $body)) - // alternate style of getting mock response. See how both txt file varies. - ->addResponse($this->getMockHttpResponse('TokenPurchaseFailure.txt')); + $mock = $this->getMockClient(); + $body = file_get_contents(__DIR__ . '/Mock/TokenPurchaseSuccess.txt'); + $mock->addResponse(new Response(200, ['Content-Type' => 'application/json'], $body)); + $mock->addResponse($this->getMockHttpResponse('TokenPurchaseFailure.txt')); // Add the mock plugin to the client object $httpClient = $this->getHttpClient(); - $httpClient->addSubscriber($mock); $this->gateway = new TokenGateway($httpClient, $this->getHttpRequest()); - $this->options = array( + $this->options = [ 'card' => $this->getValidCard(), 'token' => 'Y2RkZDdjN2EtNjFmZS00ZGYzLWI4NmEtNGZhMjI3NmExMzQ0', 'transactionId' => '123456', 'currency' => 'USD', 'amount' => '20.5' - ); + ]; $this->gateway->setAccountNumber('801290261'); $this->gateway->setTestMode(true); $this->gateway->setPrivateKey('5F876A36-D506-4E1F-8EE9-DA2358500F9C'); $this->gateway->setCart( - array( - array( + [ + [ "name" => "Demo Item", "price" => "4.99", "type" => "product", "quantity" => "1", "recurrence" => "4 Year", "startupFee" => "9.99" - ), - array( + ], + [ "name" => "Demo Item 2", "price" => "6.99", "type" => "product", "quantity" => "2", "recurrence" => "8 Year", "startupFee" => "19.99" - ) - ) + ] + ] ); }