From 1f95e5d44dfdca052a153e2accefc0245ea71bae Mon Sep 17 00:00:00 2001 From: sid Date: Mon, 28 Dec 2015 18:17:07 +1300 Subject: [PATCH] omnipay/common ~2.0 and omnipay/tests ~2.0 --- composer.json | 18 +++-- src/Gateway.php | 24 +++++- src/Message/CompletePurchaseRequest.php | 14 +++- src/Message/FetchCheckoutRequest.php | 67 ++++++++++++++++ src/Message/FetchCheckoutResponse.php | 100 ++++++++++++++++++++++++ src/Message/PurchaseRequest.php | 35 ++++++--- src/Message/PurchaseResponse.php | 75 +++++++++++++----- tests/GatewayTest.php | 36 +++++---- tests/Message/PurchaseRequestTest.php | 4 +- tests/Mock/PurchaseRequestSuccess.txt | 8 +- tests/bootstrap.php | 2 +- 11 files changed, 322 insertions(+), 61 deletions(-) create mode 100644 src/Message/FetchCheckoutRequest.php create mode 100644 src/Message/FetchCheckoutResponse.php diff --git a/composer.json b/composer.json index d33029f..8877b58 100644 --- a/composer.json +++ b/composer.json @@ -17,23 +17,31 @@ "name": "Jeremy Shipman", "email": "jeremy@burnbright.net", "homepage": "http://jeremyshipman.com" + }, + { + "name": "Sid Bachtiar", + "email": "sid.bachtiar@gmail.com", + "homepage": "http://onlinesid.com" } ], "autoload": { - "psr-4": { "Omnipay\\Poli\\" : "src/" } + "psr-4": { + "Omnipay\\Poli\\" : "src/" + } }, "require": { - "omnipay/omnipay": "~1.1" + "omnipay/common": "~2.0" }, "require-dev": { "guzzle/plugin-mock": "~3.1", "mockery/mockery": "~0.7", - "phpunit/phpunit": "~3.7.16", - "squizlabs/php_codesniffer": "~1.4" + "phpunit/phpunit": "~3.7.0", + "squizlabs/php_codesniffer": "~1.4", + "omnipay/tests": "~2.0" }, "extra": { "branch-alias": { - "dev-master": "1.0.x-dev" + "dev-master": "2.0.x-dev" } } } \ No newline at end of file diff --git a/src/Gateway.php b/src/Gateway.php index 98dde75..99e4532 100644 --- a/src/Gateway.php +++ b/src/Gateway.php @@ -4,14 +4,24 @@ use Omnipay\Common\AbstractGateway; +/** + * Class Gateway + * + * @package Omnipay\Poli + */ class Gateway extends AbstractGateway { - + /** + * @inheritdoc + */ public function getName() { return 'Poli'; } + /** + * @inheritdoc + */ public function getDefaultParameters() { return array( @@ -20,11 +30,18 @@ public function getDefaultParameters() ); } + /** + * @return string + */ public function getMerchantCode() { return $this->getParameter('merchantCode'); } + /** + * @param string $value + * @return $this + */ public function setMerchantCode($value) { return $this->setParameter('merchantCode', $value); @@ -49,4 +66,9 @@ public function completePurchase(array $parameters = array()) { return $this->createRequest('\Omnipay\Poli\Message\CompletePurchaseRequest', $parameters); } + + public function fetchCheckout(array $parameters = array()) + { + return $this->createRequest('\Omnipay\Poli\Message\FetchCheckoutRequest', $parameters); + } } diff --git a/src/Message/CompletePurchaseRequest.php b/src/Message/CompletePurchaseRequest.php index 1ad0608..d7aab5c 100644 --- a/src/Message/CompletePurchaseRequest.php +++ b/src/Message/CompletePurchaseRequest.php @@ -2,6 +2,7 @@ namespace Omnipay\Poli\Message; +use Omnipay\Common\Exception\InvalidRequestException; use SimpleXMLElement; use Omnipay\Common\Exception\InvalidResponseException; @@ -12,8 +13,8 @@ */ class CompletePurchaseRequest extends PurchaseRequest { - - protected $endpoint = "https://publicapi.apac.paywithpoli.com/api/Transaction/GetTransaction"; + //protected $endpoint = "https://publicapi.apac.paywithpoli.com/api/Transaction/GetTransaction"; + protected $endpoint = 'https://poliapi.apac.paywithpoli.com/api/v2/Transaction/GetTransaction'; public function getData() { @@ -38,10 +39,15 @@ public function getData() } public function send() + { + return $this->sendData($this->getData()); + } + + public function sendData($data) { $request = $this->httpClient->get($this->endpoint) - ->setAuth($this->getMerchantCode(), $this->getAuthenticationCode()); - $request->getQuery()->replace($this->getData()); + ->setAuth($this->getMerchantCode(), $this->getAuthenticationCode()); + $request->getQuery()->replace($data); $httpResponse = $request->send(); return $this->response = new CompletePurchaseResponse($this, $httpResponse->getBody()); diff --git a/src/Message/FetchCheckoutRequest.php b/src/Message/FetchCheckoutRequest.php new file mode 100644 index 0000000..15a4217 --- /dev/null +++ b/src/Message/FetchCheckoutRequest.php @@ -0,0 +1,67 @@ +getParameter('merchantCode'); + } + + public function setMerchantCode($value) + { + return $this->setParameter('merchantCode', $value); + } + + public function getAuthenticationCode() + { + return $this->getParameter('authenticationCode'); + } + + public function setAuthenticationCode($value) + { + return $this->setParameter('authenticationCode', $value); + } + + public function getData() + { + $this->validate( + 'merchantCode', + 'authenticationCode' + ); + $data = array(); + return $data; + } + + public function send() + { + $data = $this->getData(); + return $this->sendData($data); + } + + public function sendData($data) + { + $token = $this->httpRequest->query->get('token'); + $url = $this->endpoint.'?token='.urlencode($token); + + $merchantCode = $this->getMerchantCode(); + $authenticationCode = $this->getAuthenticationCode(); + $auth = base64_encode($merchantCode.":".$authenticationCode); //'S61xxxxx:AuthCode123'); + + $httpRequest = $this->httpClient->get( + $url, + array( + 'Content-Type'=>'application/json', + 'Authorization' => 'Basic '.$auth, + ) + ); + $httpResponse = $httpRequest->send(); + return $this->response = new FetchCheckoutResponse($this, $httpResponse->getBody(true)); + + } +} diff --git a/src/Message/FetchCheckoutResponse.php b/src/Message/FetchCheckoutResponse.php new file mode 100644 index 0000000..87752c1 --- /dev/null +++ b/src/Message/FetchCheckoutResponse.php @@ -0,0 +1,100 @@ +request = $request; + $this->data = json_decode($data, true); + } + + /** + * Is the result a success? + * + * @return bool + */ + public function isSuccessful() + { + return !$this->getCode(); + } + + /** + * Do we need to redirect? + * + * @return bool + */ + public function isRedirect() + { + return isset($this->data['NavigateURL']); + } + + /** + * Error message, e.g.: '' = no error + * + * @return string + */ + public function getMessage() + { + return $this->data['ErrorMessage']; + } + + /** + * Error code, e.g.: 0 or '' = no error + * + * @return int + */ + public function getCode() + { + return $this->data['ErrorCode']; + } + + /** + * Redirection URL + * + * @return string + */ + public function getRedirectUrl() + { + if ($this->isRedirect()) { + return $this->data['NavigateURL']; + } + } + + /** + * Redirection method + * + * @return string + */ + public function getRedirectMethod() + { + return 'GET'; + } + + /** + * Redirection data + * + * @return null + */ + public function getRedirectData() + { + return null; + } +} diff --git a/src/Message/PurchaseRequest.php b/src/Message/PurchaseRequest.php index 8e4137b..aca3191 100644 --- a/src/Message/PurchaseRequest.php +++ b/src/Message/PurchaseRequest.php @@ -11,8 +11,7 @@ */ class PurchaseRequest extends AbstractRequest { - - protected $endpoint = 'https://merchantapi.apac.paywithpoli.com/MerchantAPIService.svc/Xml/transaction/initiate'; + protected $endpoint = 'https://poliapi.apac.paywithpoli.com/api/v2/Transaction/Initiate'; public function getMerchantCode() { @@ -47,22 +46,20 @@ public function getData() ); $data = array(); - $data['AuthenticationCode'] = $this->getAuthenticationCode(); - $data['CurrencyAmount'] = $this->getAmount(); + $data['Amount'] = $this->getAmount(); $data['CurrencyCode'] = $this->getCurrency(); - $data['MerchantCheckoutURL'] = $this->getCancelUrl(); - $data['MerchantCode'] = $this->getMerchantCode(); + $data['CancellationURL'] = $this->getCancelUrl(); $data['MerchantData'] = $this->getTransactionId(); $data['MerchantDateTime'] = date('Y-m-d\TH:i:s'); $data['MerchantHomePageURL'] = $this->getCancelUrl(); - $data['MerchantRef'] = $this->getCombinedMerchantRef(); + $data['MerchantReference'] = $this->getCombinedMerchantRef(); $data['MerchantReferenceFormat'] = 1; $data['NotificationURL'] = $this->getNotifyUrl(); - $data['SuccessfulURL'] = $this->getReturnUrl(); + $data['SuccessURL'] = $this->getReturnUrl(); $data['Timeout'] = 0; - $data['UnsuccessfulURL'] = $this->getReturnUrl(); + $data['FailureURL'] = $this->getReturnUrl(); $data['UserIPAddress'] = $this->getClientIp(); - + return $data; } @@ -92,10 +89,24 @@ protected function cleanField($field) public function send() { - $postdata = $this->packageData($this->getData()); + return $this->sendData($this->getData()); + } + + public function sendData($data) + { + $merchantCode = $this->getMerchantCode(); + $authenticationCode = $this->getAuthenticationCode(); + $auth = base64_encode($merchantCode.":".$authenticationCode); //'S61xxxxx:AuthCode123'); + unset($data['MerchantCode'], $data['AuthenticationCode']); + + //$postdata = $this->packageData($data); + $postdata = json_encode($data); $httpRequest = $this->httpClient->post( $this->endpoint, - array('Content-Type'=>'text/xml'), + array( + 'Content-Type'=>'application/json', + 'Authorization' => 'Basic '.$auth, + ), $postdata ); $httpResponse = $httpRequest->send(); diff --git a/src/Message/PurchaseResponse.php b/src/Message/PurchaseResponse.php index c03510a..ba73340 100644 --- a/src/Message/PurchaseResponse.php +++ b/src/Message/PurchaseResponse.php @@ -3,6 +3,7 @@ namespace Omnipay\Poli\Message; use DOMDocument; +use Guzzle\Http\EntityBody; use SimpleXMLElement; use Omnipay\Common\Message\AbstractResponse; use Omnipay\Common\Message\RequestInterface; @@ -14,62 +15,96 @@ */ class PurchaseResponse extends AbstractResponse implements RedirectResponseInterface { - - protected $error; - + /** + * PurchaseResponse constructor. + * + * @param RequestInterface $request + * @param EntityBody $data + * @throws InvalidResponseException + */ public function __construct(RequestInterface $request, $data) { $this->request = $request; - $xml = new SimpleXMLElement($data); - if (!$xml->TransactionStatusCode) { - throw new InvalidResponseException; - } - $errors = $xml->Errors->children( - 'http://schemas.datacontract.org/2004/07/Centricom.POLi.Services.MerchantAPI.DCO' - ); - $this->error = $errors[0]; //only store first error - $this->data = $xml->Transaction->children( - 'http://schemas.datacontract.org/2004/07/Centricom.POLi.Services.MerchantAPI.DCO' - ); + $this->data = json_decode($data, true); } + /** + * Is the result a success? + * + * @return bool + */ public function isSuccessful() { - return false; + return isset($this->data['Success']) ? $this->data['Success'] : false; } + /** + * Do we need to redirect? + * + * @return bool + */ public function isRedirect() { - return isset($this->data->NavigateURL); + return isset($this->data['NavigateURL']); } + /** + * Transaction reference + * + * @return string + */ public function getTransactionReference() { - return isset($this->data->TransactionRefNo) ? (string)$this->data->TransactionRefNo : null; + return $this->data['TransactionRefNo']; } + /** + * Error message, e.g.: '' = no error + * + * @return string + */ public function getMessage() { - return isset($this->error->Message) ? (string)$this->error->Message : null; + return $this->data['ErrorMessage']; } + /** + * Error code, e.g.: 0 = no error + * + * @return int + */ public function getCode() { - return isset($this->error->Code) ? (string)$this->error->Code : null; + return $this->data['ErrorCode']; } + /** + * Redirection URL + * + * @return string + */ public function getRedirectUrl() { if ($this->isRedirect()) { - return (string)$this->data->NavigateURL; + return $this->data['NavigateURL']; } } + /** + * Redirection method + * + * @return string + */ public function getRedirectMethod() { return 'GET'; } + /** + * Redirection data + * + * @return null + */ public function getRedirectData() { return null; diff --git a/tests/GatewayTest.php b/tests/GatewayTest.php index a3c2cee..ba045cf 100644 --- a/tests/GatewayTest.php +++ b/tests/GatewayTest.php @@ -1,8 +1,9 @@ 'http://www.mytest.co.nz/shop/cancelpayment' ))->send(); - $this->assertFalse($response->isSuccessful()); + $this->assertTrue($response->isSuccessful()); $this->assertTrue($response->isRedirect()); $this->assertEquals('596464607245', $response->getTransactionReference()); @@ -44,19 +45,24 @@ public function testPurchaseFailure() { $this->setMockHttpResponse('PurchaseRequestFailure.txt'); - $response = $this->gateway->purchase(array( - 'amount' => '-12345.00', - 'currency' => 'NZD', - 'card' => $this->getValidCard(), - 'transactionID' => 12345, - 'returnUrl' => 'http://www.mytest.co.nz/shop/completepayment', - 'notifyUrl' => 'http://www.mytest.co.nz/shop/notifypayment', - 'cancelUrl' => 'http://www.mytest.co.nz/shop/cancelpayment' - ))->send(); - $this->assertFalse($response->isSuccessful()); - $this->assertFalse($response->isRedirect()); - $this->assertEquals('1025', $response->getCode()); - $this->assertEquals('The CurrencyAmount field is out of range.', $response->getMessage()); + try { + $response = $this->gateway->purchase(array( + 'amount' => '-12345.00', + 'currency' => 'NZD', + 'card' => $this->getValidCard(), + 'transactionID' => 12345, + 'returnUrl' => 'http://www.mytest.co.nz/shop/completepayment', + 'notifyUrl' => 'http://www.mytest.co.nz/shop/notifypayment', + 'cancelUrl' => 'http://www.mytest.co.nz/shop/cancelpayment' + ))->send(); + $this->assertFalse(true, 'Expecting an InvalidRequestException'); + } catch (InvalidRequestException $ire) { + $this->assertEquals('A negative amount is not allowed.', $ire->getMessage()); + } +// $this->assertFalse($response->isSuccessful()); +// $this->assertFalse($response->isRedirect()); +// $this->assertEquals('1025', $response->getCode()); +// $this->assertEquals('The CurrencyAmount field is out of range.', $response->getMessage()); } public function testCompletePurchaseSuccess() diff --git a/tests/Message/PurchaseRequestTest.php b/tests/Message/PurchaseRequestTest.php index 5c770c3..8f3d5be 100644 --- a/tests/Message/PurchaseRequestTest.php +++ b/tests/Message/PurchaseRequestTest.php @@ -1,9 +1,9 @@ assertEquals(123, $this->request->getCombinedMerchantRef()); $data = $this->request->getData(); - $this->assertEquals(123, $data['MerchantRef']); + $this->assertEquals(123, $data['MerchantReference']); $this->assertEquals(1, $data['MerchantReferenceFormat']); //valid card uses combined form diff --git a/tests/Mock/PurchaseRequestSuccess.txt b/tests/Mock/PurchaseRequestSuccess.txt index 58c7c88..870b007 100644 --- a/tests/Mock/PurchaseRequestSuccess.txt +++ b/tests/Mock/PurchaseRequestSuccess.txt @@ -9,4 +9,10 @@ X-AspNet-Version: 2.0.50727 X-Powered-By: ASP.NET Strict-Transport-Security: max-age=15768000 -Initiatedhttps://txn.apac.paywithpoli.com/?token=ix3dphBp7oPMGCKSjH%2flf0OQSFipYvej596464607245ix3dphBp7oPMGCKSjH/lf0OQSFipYvej \ No newline at end of file +{ + "Success": true, + "TransactionRefNo": "596464607245", + "NavigateURL": "https://txn.apac.paywithpoli.com/?token=ix3dphBp7oPMGCKSjH%2flf0OQSFipYvej", + "ErrorCode": 0, + "ErrorMessage": "" +} \ No newline at end of file diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 3af3d7c..19527fb 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -6,5 +6,5 @@ $autoloader = require __DIR__.'/../vendor/autoload.php'; // autoload abstract TestCase classes in test directory -$autoloader->add('Omnipay', __DIR__.'/../vendor/omnipay/omnipay/tests'); +$autoloader->add('Omnipay', __DIR__.'/../vendor/omnipay/tests/src');