Skip to content

Added authorize request. #17

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

Open
wants to merge 7 commits 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
24 changes: 19 additions & 5 deletions src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
98 changes: 98 additions & 0 deletions src/Message/AuthorizeRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace Omnipay\Netaxept\Message;

use Omnipay\Common\Message\AbstractRequest;

/**
* Netaxept Authorize Request
*/
class AuthorizeRequest extends AbstractRequest
{
protected $liveEndpoint = 'https://epayment.nets.eu';
protected $testEndpoint = 'https://test.epayment.nets.eu';

public function getMerchantId()
{
return $this->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;
}
}
4 changes: 2 additions & 2 deletions src/Message/CaptureRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
39 changes: 39 additions & 0 deletions src/Message/CompleteAuthorizeRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Omnipay\Netaxept\Message;

use Omnipay\Common\Exception\InvalidResponseException;

/**
* Netaxept Complete Purchase Request
*/
class CompleteAuthorizeRequest extends PurchaseRequest
{
public function getData()
{
$data = array();
$data['responseCode'] = $this->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()));
}
}
12 changes: 12 additions & 0 deletions src/Message/PurchaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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();
Expand Down
81 changes: 81 additions & 0 deletions tests/GatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
68 changes: 68 additions & 0 deletions tests/Message/AuthorizeRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Omnipay\Netaxept\Message;

use Omnipay\Common\CreditCard;
use Omnipay\Tests\TestCase;

class AuthorizeRequestTest extends TestCase
{
/**
* @var \Omnipay\Netaxept\Message\AuthorizeRequest
*/
private $request;

public function setUp()
{
$client = $this->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());
}
}
Loading