Skip to content

Commit

Permalink
updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cave committed Sep 28, 2021
1 parent 540efcd commit b7b21df
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 85 deletions.
22 changes: 14 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,26 @@ The result will be a redirect to the gateway or bank.
```php
use Omnipay\Omnipay;

<<<<<<< HEAD
$gateway = Omnipay::create('APS');
=======
$gateway = Omnipay::create('Amazon Payment Service');
>>>>>>> 24428a5b7399bc46a68f08187fb62fe42a45d455

// Send purchase request (don't get so excited... params below are just fake :))
$gateway->setTestMode(TRUE);
$gateway->setRequestPhrase('PASS');

//$gateway->setAccessCode('zx0IPmPy5jp1vAz8Kpg7')
// ->setAmount(10000) // amount * 100
// ->setMerchantIdentifier(123)
// ->setMerchantReference('random')
// ->setCustomerEmail('[email protected]')
// ->setOrderDescription('iPhone 6-S');
// .... OR ....

$response = $gateway->purchase([
'access_code' => 'zx0IPmPy5jp1vAz8Kpg7',
'amount' => '10000', // amount * 100
'merchant_identifier' => 'CycHZxVj',
'merchant_reference' => 'XYZ9239-yu898',
'amount' => '10000',
'currency' => 'AED',
'language' => 'en',
'currency' => 'AED', // default AED
'language' => 'en', // default en
'customer_email' => '[email protected]',
'order_description' => 'iPhone 6-S',
])->send();
Expand Down
11 changes: 7 additions & 4 deletions src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ public function getDefaultParameters(): array
{
return [
'testMode' => FALSE,
'currency' => 'AED',
'language' => 'en',
];
}

Expand Down Expand Up @@ -117,20 +115,25 @@ public function setAmount($amount)
return $this;
}

public function setCurrency($currency = 'AED')
public function setCurrency($currency)
{
$this->setParameter('currency', $currency);

return $this;
}

public function setLanguage($language = 'en')
public function setLanguage($language)
{
$this->setParameter('language', $language);

return $this;
}

public function getLanguage()
{
return $this->getParameter('language');
}

public function setCustomerEmail($email)
{
$this->setParameter('customer_email', $email);
Expand Down
27 changes: 22 additions & 5 deletions src/Message/Request/APSAbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ public function sendData($data): ResponseInterface
// Sort array by key ascending
ksort($data);

echo $data['signature'] = $this->_createSignature($data);
$this->_prepare_data($data);

$data['signature'] = $this->_createSignature($data);

$httpResponse = $this->httpClient->request(
'POST',
Expand Down Expand Up @@ -70,9 +72,6 @@ protected function getEndpoint(): string
*/
private function _createSignature(array $data): string
{
unset($data['testMode']);
unset($data['Mode']);

if (empty($this->getParameter('request_phrase')))
throw new RequestPhraseException('Request phrase is missing.');

Expand All @@ -97,7 +96,7 @@ private function _createSignature(array $data): string
*
* @return mixed
*/
public function setRequestPhrase(string $requestPhrase)
public function setRequestPhrase($requestPhrase)
{
return $this->setParameter('request_phrase', $requestPhrase);
}
Expand Down Expand Up @@ -137,11 +136,21 @@ public function setCurrency($currency = 'AED')
return $this->setParameter('currency', $currency);
}

public function getCurrency($currency = 'AED')
{
return $this->getParameter('currency');
}

public function setLanguage($language = 'en')
{
return $this->setParameter('language', $language);
}

public function getLanguage()
{
return $this->getParameter('language');
}

public function setCustomerEmail($email)
{
return $this->setParameter('customer_email', $email);
Expand All @@ -161,4 +170,12 @@ protected function _is_json_response($string) {
json_decode($string);
return json_last_error() === JSON_ERROR_NONE;
}

protected function _prepare_data(&$data)
{
unset($data['testMode']);
unset($data['Mode']);
unset($data['request_phrase']);
unset($data['sha_type']);
}
}
1 change: 1 addition & 0 deletions src/Message/Request/AuthorizationRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function getData(): array
'language' => $this->getParameter('language'),
'customer_email' => $this->getParameter('customer_email'),
'order_description' => $this->getParameter('order_description'),
'request_phrase' => $this->getParameter('request_phrase'),
];
}

Expand Down
1 change: 1 addition & 0 deletions src/Message/Request/CaptureRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function getData(): array
'language' => $this->getParameter('language'),
'order_description' => $this->getParameter('order_description'),
'fort_id' => $this->getParameter('fort_id'),
'request_phrase' => $this->getParameter('request_phrase'),
];
}

Expand Down
6 changes: 4 additions & 2 deletions src/Message/Response/AbstractResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,20 +239,22 @@ abstract class AbstractResponse extends OmnipayAbstractResponse
public function __construct(RequestInterface $request, $data)
{
$this->request = $request;
$this->processResponse($data);
$this->response = $this->processResponse($data);
}

/**
* @param $response
* @throws InvalidResponseException
*/
public function processResponse($response): void
public function processResponse($response): array
{
if ( ! is_array($response))
throw new InvalidResponseException('Wrong response format.');

if (empty($response))
throw new InvalidResponseException('Response is empty.');

return $response;
}

/**
Expand Down
124 changes: 60 additions & 64 deletions tests/GatewayTest.php
Original file line number Diff line number Diff line change
@@ -1,98 +1,94 @@
<?php
<?php namespace Omnipay\APS;

namespace Omnipay\APS\Tests;

use Omnipay\APS\Message\Response\AuthorizationResponse;
use Omnipay\Stripe\Message\AuthorizeRequest;
use Omnipay\Tests\GatewayTestCase;
use Omnipay\APS\Message\Response\PurchaseResponse;

class GatewayTest extends GatewayTestCase
{
/**
* @var Gateway
*/
protected $gateway;

/**
* @var array
*/
protected $options;

protected function setUp()
{
parent::setUp();

$this->gateway = new Gateway($this->getHttpClient(), $this->getHttpRequest());
$this->gateway->setTestMode(TRUE);

$this->options = [
'access_code' => 'zx0IPmPy5jp1vAz8Kpg7',
'merchant_identifier' => 'CycHZxVj',
'merchant_reference' => 'XYZ9239-yu898',
'currency' => 'AED',
'language' => 'en',
'customer_email' => '[email protected]',
<<<<<<< HEAD
=======
'signature' => '7cad05f0212ed933c9a5d5dffa31661acf2c827a',
>>>>>>> 24428a5b7399bc46a68f08187fb62fe42a45d455
'order_description' => 'iPhone 6-S',
];
}
/**
* @var Gateway
*/
protected $gateway;

/**
* @var array
*/
protected $options;

protected function setUp(): void
{
parent::setUp();

$this->gateway = new Gateway($this->getHttpClient(), $this->getHttpRequest());
$this->gateway->setTestMode(TRUE);
$this->gateway->setRequestPhrase('PASS');

$this->options = [
'access_code' => 'zx0IPmPy5jp1vAz8Kpg7',
'merchant_identifier' => 'CycHZxVj',
'merchant_reference' => 'XYZ9239-yu898',
'currency' => 'AED',
'language' => 'en',
'customer_email' => '[email protected]',
'order_description' => 'iPhone 6-S',
'request_phrase' => 'iPhone 6-S',
];
}

/**
* Authorization success
*/
public function testAuthorizationSuccess()
{
$this->setMockHttpResponse('AuthorizationSuccess.txt');
public function testAuthorizationSuccess()
{
$this->setMockHttpResponse('AuthorizationSuccess.txt');

$this->options['amount'] = '10000';
$this->options['amount'] = '10000';

/** @var AuthorizationResponse $response */
$response = $this->gateway
->authorize($this->options)
->send();
/** @var AuthorizationResponse $response */
$response = $this->gateway
->authorize($this->options)
->send();

$this->assertTrue($response->isSuccessful());
$this->assertEquals('Success.', $response->getMessage());
}
$this->assertTrue($response->isSuccessful());
$this->assertStringContainsString('Success', $response->getMessage());
}

/**
* Authorization failure
*/
public function testAuthorizationFailure()
{
$this->setMockHttpResponse('AuthorizationFailure.txt');
/**
* Authorization failure
*/
public function testAuthorizationFailure()
{
$this->setMockHttpResponse('AuthorizationFailure.txt');

$this->options['amount'] = NULL;
$this->options['amount'] = 100;

/** @var AuthorizationResponse $response */
$response = $this->gateway
->authorize($this->options)
->send();
/** @var AuthorizationResponse $response */
$response = $this->gateway
->authorize($this->options)
->send();

$this->assertFalse($response->isSuccessful());
$this->assertSame('Invalid amount.', $response->getMessage());
}
$this->assertFalse($response->isSuccessful());
$this->assertSame('Invalid amount.', $response->getMessage());
}

/**
* Purchase success. Purchase combines authorization and capture requests
*/
public function testPurchaseSuccess()
{
$this->setMockHttpResponse('PurchaseFailure.txt');
$this->setMockHttpResponse('PurchaseSuccess.txt');

$this->options['amount'] = '10000';

/** @var PurchaseResponse $response */
$response = $this->gateway
->authorize($this->options)
->purchase($this->options)
->send();

$this->assertTrue($response->isSuccessful());
$this->assertEquals('Success.', $response->getMessage());
$this->assertStringContainsString('Success', $response->getMessage());
}


Expand All @@ -101,13 +97,13 @@ public function testPurchaseSuccess()
*/
public function testPurchaseFailure()
{
$this->setMockHttpResponse('PurchaseSuccess.txt');
$this->setMockHttpResponse('PurchaseFailure.txt');

$this->options['amount'] = '10001';

/** @var PurchaseResponse $response */
$response = $this->gateway
->authorize($this->options)
->purchase($this->options)
->send();

$this->assertFalse($response->isSuccessful());
Expand Down
11 changes: 11 additions & 0 deletions tests/Mock/AuthorizationFailure.txt
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
HTTP/2 200
server: nginx
date: Fri, 30 Aug 2019 17:13:47 GMT
content-type: application/json
vary: Accept-Encoding
vary: Accept-Encoding
x-powered-by: PHP/7.0.7
cache-control: private, must-revalidate
pragma: no-cache
expires: -1

{"command":"AUTHORIZATION","access_code":"zx0IPmPy5jp1vAz8Kpg7","merchant_identifier":"CycHZxVj","merchant_reference":"XYZ9239-yu898","amount":"10000","currency":"AED","language":"en","customer_email":"[email protected]","signature":"7cad05f0212ed933c9a5d5dffa31661acf2c827a","fort_id":"149295435400084008","payment_option":"VISA","eci":"ECOMMERCE","order_description":"iPhone6-S","customer_ip":"192.178.1.10","customer_name":"John","response_message":"Invalid amount.","response_code":"20064","status":"03","card_holder_name":"John Smith","expiry_date":"2105","card_number":"400555******0001"}
11 changes: 11 additions & 0 deletions tests/Mock/AuthorizationSuccess.txt
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
HTTP/2 200
server: nginx
date: Fri, 30 Aug 2019 17:13:47 GMT
content-type: application/json
vary: Accept-Encoding
vary: Accept-Encoding
x-powered-by: PHP/7.0.7
cache-control: private, must-revalidate
pragma: no-cache
expires: -1

{"command":"AUTHORIZATION","access_code":"zx0IPmPy5jp1vAz8Kpg7","merchant_identifier":"CycHZxVj","merchant_reference":"XYZ9239-yu898","amount":"10000","currency":"AED","language":"en","customer_email":"[email protected]","signature":"7cad05f0212ed933c9a5d5dffa31661acf2c827a","fort_id":"149295435400084008","payment_option":"VISA","eci":"ECOMMERCE","order_description":"iPhone6-S","customer_ip":"192.178.1.10","customer_name":"John","response_message":"Success","response_code":"20064","status":"02","card_holder_name":"John Smith","expiry_date":"2105","card_number":"400555******0001"}
13 changes: 12 additions & 1 deletion tests/Mock/PurchaseFailure.txt
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
{"command":"AUTHORIZATION","access_code":"zx0IPmPy5jp1vAz8Kpg7","merchant_identifier":"CycHZxVj","merchant_reference":"XYZ9239-yu898","amount":"10000","currency":"AED","language":"en","customer_email":"[email protected]","signature":"7cad05f0212ed933c9a5d5dffa31661acf2c827a","fort_id":"149295435400084008","payment_option":"VISA","eci":"ECOMMERCE","order_description":"iPhone6-S","customer_ip":"192.178.1.10","customer_name":"John","response_message":"Operation amount exceeds the authorized amount.","response_code":"14042","status":"14","card_holder_name":"John Smith","expiry_date":"2105","card_number":"400555******0001"}
HTTP/2 200
server: nginx
date: Fri, 30 Aug 2019 17:13:47 GMT
content-type: application/json
vary: Accept-Encoding
vary: Accept-Encoding
x-powered-by: PHP/7.0.7
cache-control: private, must-revalidate
pragma: no-cache
expires: -1

{"command":"AUTHORIZATION","access_code":"zx0IPmPy5jp1vAz8Kpg7","merchant_identifier":"CycHZxVj","merchant_reference":"XYZ9239-yu898","amount":"10000","currency":"AED","language":"en","customer_email":"[email protected]","signature":"7cad05f0212ed933c9a5d5dffa31661acf2c827a","fort_id":"149295435400084008","payment_option":"VISA","eci":"ECOMMERCE","order_description":"iPhone6-S","customer_ip":"192.178.1.10","customer_name":"John","response_message":"Operation amount exceeds the authorized amount.","response_code":"14042","status":"13","card_holder_name":"John Smith","expiry_date":"2105","card_number":"400555******0001"}
Loading

0 comments on commit b7b21df

Please sign in to comment.