diff --git a/src/Gateway.php b/src/Gateway.php index 5044e98..50c3ef6 100644 --- a/src/Gateway.php +++ b/src/Gateway.php @@ -3,8 +3,12 @@ namespace Omnipay\Netaxept; use Omnipay\Common\AbstractGateway; +use Omnipay\Netaxept\Message\AnnulRequest; +use Omnipay\Netaxept\Message\AuthorizeRequest; +use Omnipay\Netaxept\Message\CompleteAuthorizeRequest; use Omnipay\Netaxept\Message\PurchaseRequest; use Omnipay\Netaxept\Message\CompletePurchaseRequest; +use Omnipay\Netaxept\Message\CreditRequest; /** * Netaxept Gateway @@ -47,28 +51,38 @@ public function setPassword($value) return $this->setParameter('password', $value); } + public function authorize($parameters = array()) + { + return $this->createRequest(AuthorizeRequest::class, $parameters); + } + + public function completeAuthorize($parameters = array()) + { + return $this->createRequest(CompleteAuthorizeRequest::class, $parameters); + } + public function purchase(array $parameters = array()) { - return $this->createRequest('\Omnipay\Netaxept\Message\PurchaseRequest', $parameters); + return $this->createRequest(PurchaseRequest::class, $parameters); } public function completePurchase(array $parameters = array()) { - return $this->createRequest('\Omnipay\Netaxept\Message\CompletePurchaseRequest', $parameters); + return $this->createRequest(CompletePurchaseRequest::class, $parameters); } public function capture(array $parameters = array()) { - return $this->createRequest('\Omnipay\Netaxept\Message\CaptureRequest', $parameters); + return $this->createRequest(CaptureRequest::class, $parameters); } public function void(array $parameters = array()) { - return $this->createRequest('\Omnipay\Netaxept\Message\AnnulRequest', $parameters); + return $this->createRequest(AnnulRequest::class, $parameters); } public function credit(array $parameters = array()) { - return $this->createRequest('\Omnipay\Netaxept\Message\CreditRequest', $parameters); + return $this->createRequest(CreditRequest::class, $parameters); } } diff --git a/src/Message/AuthorizeRequest.php b/src/Message/AuthorizeRequest.php new file mode 100644 index 0000000..232082e --- /dev/null +++ b/src/Message/AuthorizeRequest.php @@ -0,0 +1,98 @@ +getParameter('merchantId'); + } + + public function setMerchantId($value) + { + return $this->setParameter('merchantId', $value); + } + + public function getPassword() + { + return $this->getParameter('password'); + } + + public function setPassword($value) + { + return $this->setParameter('password', $value); + } + + public function getLanguage() + { + return $this->getParameter('language'); + } + + public function setLanguage($value) + { + return $this->setParameter('language', $value); + } + + public function setPaymentMethodActionList($value) + { + return $this->setParameter('paymentMethodActionList', $value); + } + + public function getPaymentMethodActionList() + { + return $this->getParameter('paymentMethodActionList'); + } + + public function getData() + { + $this->validate('amount', 'currency', 'transactionId', 'returnUrl'); + + $data = array(); + $data['merchantId'] = $this->getMerchantId(); + $data['token'] = $this->getPassword(); + $data['serviceType'] = 'B'; + $data['orderNumber'] = $this->getTransactionId(); + $data['transactionId'] = $this->getTransactionReference(); + $data['currencyCode'] = $this->getCurrency(); + $data['amount'] = $this->getAmountInteger(); + $data['redirectUrl'] = $this->getReturnUrl(); + $data['language'] = $this->getLanguage(); + $data['paymentMethodActionList'] = $this->getPaymentMethodActionList(); + + if ($this->getCard()) { + $data['customerFirstName'] = $this->getCard()->getFirstName(); + $data['customerLastName'] = $this->getCard()->getLastName(); + $data['customerEmail'] = $this->getCard()->getEmail(); + $data['customerPhoneNumber'] = $this->getCard()->getPhone(); + $data['customerAddress1'] = $this->getCard()->getAddress1(); + $data['customerAddress2'] = $this->getCard()->getAddress2(); + $data['customerPostcode'] = $this->getCard()->getPostcode(); + $data['customerTown'] = $this->getCard()->getCity(); + $data['customerCountry'] = $this->getCard()->getCountry(); + } + + return $data; + } + + public function sendData($data) + { + $url = $this->getEndpoint().'/Netaxept/Register.aspx?'; + $httpResponse = $this->httpClient->request('GET', $url.http_build_query($data)); + + return $this->response = new Response($this, simplexml_load_string($httpResponse->getBody()->getContents())); + } + + public function getEndpoint() + { + return $this->getTestMode() ? $this->testEndpoint : $this->liveEndpoint; + } +} diff --git a/src/Message/CaptureRequest.php b/src/Message/CaptureRequest.php index fe88400..470d6cc 100644 --- a/src/Message/CaptureRequest.php +++ b/src/Message/CaptureRequest.php @@ -15,13 +15,13 @@ public function getData() { $data = array(); $data['transactionAmount'] = $this->getAmountInteger(); - $data['transactionId'] = $this->getTransactionId(); + $data['transactionId'] = $this->getTransactionReference(); $data['merchantId'] = $this->getMerchantId(); $data['token'] = $this->getPassword(); $data['operation'] = 'CAPTURE'; if (empty($data['transactionAmount']) || empty($data['transactionId'])) { - throw new InvalidResponseException; + throw new InvalidResponseException(); } return $data; diff --git a/src/Message/CompleteAuthorizeRequest.php b/src/Message/CompleteAuthorizeRequest.php new file mode 100644 index 0000000..5d80991 --- /dev/null +++ b/src/Message/CompleteAuthorizeRequest.php @@ -0,0 +1,39 @@ +httpRequest->query->get('responseCode'); + $data['transactionId'] = $this->httpRequest->query->get('transactionId'); + $data['merchantId'] = $this->getMerchantId(); + $data['token'] = $this->getPassword(); + $data['operation'] = 'AUTH'; + + if (empty($data['responseCode']) || empty($data['transactionId'])) { + throw new InvalidResponseException(); + } + + return $data; + } + + public function sendData($data) + { + if ('OK' !== $data['responseCode']) { + return $this->response = new ErrorResponse($this, $data); + } + + $url = $this->getEndpoint().'/Netaxept/Process.aspx?'; + $httpResponse = $this->httpClient->request('GET', $url.http_build_query($data)); + + return $this->response = new Response($this, simplexml_load_string($httpResponse->getBody()->getContents())); + } +} diff --git a/src/Message/PurchaseRequest.php b/src/Message/PurchaseRequest.php index 0419912..55913fa 100644 --- a/src/Message/PurchaseRequest.php +++ b/src/Message/PurchaseRequest.php @@ -42,6 +42,16 @@ public function setLanguage($value) return $this->setParameter('language', $value); } + public function setPaymentMethodActionList($value) + { + return $this->setParameter('paymentMethodActionList', $value); + } + + public function getPaymentMethodActionList() + { + return $this->getParameter('paymentMethodActionList'); + } + public function getData() { $this->validate('amount', 'currency', 'transactionId', 'returnUrl'); @@ -51,10 +61,12 @@ public function getData() $data['token'] = $this->getPassword(); $data['serviceType'] = 'B'; $data['orderNumber'] = $this->getTransactionId(); + $data['transactionId'] = $this->getTransactionReference(); $data['currencyCode'] = $this->getCurrency(); $data['amount'] = $this->getAmountInteger(); $data['redirectUrl'] = $this->getReturnUrl(); $data['language'] = $this->getLanguage(); + $data['paymentMethodActionList'] = $this->getPaymentMethodActionList(); if ($this->getCard()) { $data['customerFirstName'] = $this->getCard()->getFirstName(); diff --git a/tests/GatewayTest.php b/tests/GatewayTest.php index 7cfa850..fe3f07e 100644 --- a/tests/GatewayTest.php +++ b/tests/GatewayTest.php @@ -103,6 +103,87 @@ public function testCompletePurchaseFailure() $this->assertSame('Unable to find transaction', $response->getMessage()); } + public function testAuthorizeSuccess() + { + $this->setMockHttpResponse('AuthorizeSuccess.txt'); + + $response = $this->gateway->authorize($this->options)->send(); + + $this->assertFalse($response->isSuccessful()); + $this->assertTrue($response->isRedirect()); + $this->assertEquals('f3d94dd5c0f743a788fc943402757c58', $response->getTransactionReference()); + $this->assertSame('GET', $response->getRedirectMethod()); + $this->assertSame('https://epayment.nets.eu/Terminal/Default.aspx?merchantId=foo&transactionId=f3d94dd5c0f743a788fc943402757c58', $response->getRedirectUrl()); + } + + public function testAuthorizeFailure() + { + $this->setMockHttpResponse('AuthorizeFailure.txt'); + + $response = $this->gateway->authorize($this->options)->send(); + + $this->assertFalse($response->isSuccessful()); + $this->assertFalse($response->isRedirect()); + $this->assertNull($response->getTransactionReference()); + $this->assertSame("Missing parameter: 'Order Number'", $response->getMessage()); + } + + public function testCompleteAuthorizeSuccess() + { + $this->getHttpRequest()->query->replace( + array( + 'responseCode' => 'OK', + 'transactionId' => 'abc123', + ) + ); + + $this->setMockHttpResponse('CompleteAuthorizeSuccess.txt'); + + $response = $this->gateway->completeAuthorize($this->options)->send(); + + $this->assertTrue($response->isSuccessful()); + $this->assertFalse($response->isRedirect()); + $this->assertEquals('8a88d40cab5b47fab25e24d6228180a7', $response->getTransactionReference()); + $this->assertSame('OK', $response->getMessage()); + } + + public function testCompleteAuthorizeCancel() + { + $this->getHttpRequest()->query->replace( + array( + 'transactionId' => '1de59458487344759832716abf48109b', + 'responseCode' => 'Cancel', + ) + ); + + $response = $this->gateway->completeAuthorize($this->options)->send(); + + $this->assertFalse($response->isSuccessful()); + $this->assertFalse($response->isRedirect()); + $this->assertEquals('1de59458487344759832716abf48109b', $response->getTransactionReference()); + $this->assertEquals('Cancel', $response->getMessage()); + } + + public function testCompleteAuthorizeFailure() + { + $this->getHttpRequest()->query->replace( + array( + 'responseCode' => 'OK', + 'transactionId' => 'abc123', + ) + ); + + $this->setMockHttpResponse('CompleteAuthorizeFailure.txt'); + + $response = $this->gateway->completeAuthorize($this->options)->send(); + + $this->assertFalse($response->isSuccessful()); + $this->assertFalse($response->isRedirect()); + $this->assertNull($response->getTransactionReference()); + + $this->assertSame('Unable to find transaction', $response->getMessage()); + } + public function testCaptureSuccess() { $this->setMockHttpResponse('CaptureSuccess.txt'); diff --git a/tests/Message/AuthorizeRequestTest.php b/tests/Message/AuthorizeRequestTest.php new file mode 100644 index 0000000..548f043 --- /dev/null +++ b/tests/Message/AuthorizeRequestTest.php @@ -0,0 +1,68 @@ +getHttpClient(); + $request = $this->getHttpRequest(); + + $this->request = new AuthorizeRequest($client, $request); + } + + public function testGetDataWithCard() + { + $this->request->setMerchantId('MERCH-123'); + $this->request->setPassword('PASSWORD-123'); + $this->request->setAmount('1.23'); + $this->request->setCurrency('USD'); + $this->request->setTransactionId('ABC-123'); + $this->request->setReturnUrl('http://return.domain.com/'); + $this->request->setLanguage('en_GB'); + + $card = new CreditCard(array( + 'firstName' => 'John', + 'lastName' => 'Doe', + 'email' => 'test@email.com', + 'phone' => '555-555-5555', + 'address1' => '123 NW Blvd', + 'address2' => 'Lynx Lane', + 'postcode' => '66605', + 'city' => 'Topeka', + 'country' => 'USA', + )); + $this->request->setCard($card); + + $expected = array( + 'merchantId' => 'MERCH-123', + 'token' => 'PASSWORD-123', + 'serviceType' => 'B', + 'orderNumber' => 'ABC-123', + 'currencyCode' => 'USD', + 'amount' => 123, + 'redirectUrl' => 'http://return.domain.com/', + 'language' => 'en_GB', + 'customerFirstName' => 'John', + 'customerLastName' => 'Doe', + 'customerEmail' => 'test@email.com', + 'customerPhoneNumber' => '555-555-5555', + 'customerAddress1' => '123 NW Blvd', + 'customerAddress2' => 'Lynx Lane', + 'customerPostcode' => '66605', + 'customerTown' => 'Topeka', + 'customerCountry' => 'USA', + ); + + $this->assertEquals($expected, $this->request->getData()); + } +} diff --git a/tests/Message/CompleteAuthorizeRequestTest.php b/tests/Message/CompleteAuthorizeRequestTest.php new file mode 100644 index 0000000..04a355d --- /dev/null +++ b/tests/Message/CompleteAuthorizeRequestTest.php @@ -0,0 +1,46 @@ +getHttpClient(); + $this->httpRequest = $this->getHttpRequest(); + + $this->request = new CompleteAuthorizeRequest($client, $this->httpRequest); + } + + /** + * @expectedException \Omnipay\Common\Exception\InvalidResponseException + */ + public function testGetDataThrowsExceptionWithoutResponseCode() + { + $this->httpRequest->query->set('transactionId', 'TRANS-123'); + + $this->request->getData(); + } + + /** + * @expectedException \Omnipay\Common\Exception\InvalidResponseException + */ + public function testGetDataThrowsExceptionWithoutTransactionId() + { + $this->httpRequest->query->set('responseCode', 'ABC-123'); + + $this->request->getData(); + } +} diff --git a/tests/Mock/AuthorizeFailure.txt b/tests/Mock/AuthorizeFailure.txt new file mode 100644 index 0000000..6ed37ee --- /dev/null +++ b/tests/Mock/AuthorizeFailure.txt @@ -0,0 +1,15 @@ +HTTP/1.1 200 OK +Cache-Control: private +Content-Type: text/xml +Server: Microsoft-IIS/7.5 +X-AspNet-Version: 4.0.30319 +X-Powered-By: ASP.NET +Date: Fri, 22 Feb 2013 15:53:42 GMT +Content-Length: 259 + + + + + Missing parameter: 'Order Number' + + diff --git a/tests/Mock/AuthorizeSuccess.txt b/tests/Mock/AuthorizeSuccess.txt new file mode 100644 index 0000000..966cbe4 --- /dev/null +++ b/tests/Mock/AuthorizeSuccess.txt @@ -0,0 +1,13 @@ +HTTP/1.1 200 OK +Cache-Control: private +Content-Type: text/xml +Server: Microsoft-IIS/7.5 +X-AspNet-Version: 4.0.30319 +X-Powered-By: ASP.NET +Date: Fri, 22 Feb 2013 15:55:07 GMT +Content-Length: 228 + + + + f3d94dd5c0f743a788fc943402757c58 + diff --git a/tests/Mock/CompleteAuthorizeFailure.txt b/tests/Mock/CompleteAuthorizeFailure.txt new file mode 100644 index 0000000..f9481fb --- /dev/null +++ b/tests/Mock/CompleteAuthorizeFailure.txt @@ -0,0 +1,15 @@ +HTTP/1.1 200 OK +Cache-Control: private +Content-Type: text/xml +Server: Microsoft-IIS/7.5 +X-AspNet-Version: 4.0.30319 +X-Powered-By: ASP.NET +Date: Fri, 22 Feb 2013 16:55:41 GMT +Content-Length: 245 + + + + + Unable to find transaction + + diff --git a/tests/Mock/CompleteAuthorizeSuccess.txt b/tests/Mock/CompleteAuthorizeSuccess.txt new file mode 100644 index 0000000..062ae13 --- /dev/null +++ b/tests/Mock/CompleteAuthorizeSuccess.txt @@ -0,0 +1,19 @@ +HTTP/1.1 200 OK +Cache-Control: private +Content-Type: text/xml +Server: Microsoft-IIS/7.5 +X-AspNet-Version: 4.0.30319 +X-Powered-By: ASP.NET +Date: Fri, 22 Feb 2013 16:53:34 GMT +Content-Length: 474 + + + + AUTH + OK + 090896 + 8a88d40cab5b47fab25e24d6228180a7 + 2013-02-22T17:53:33.9906245+01:00 + 424278 + 246 +