diff --git a/README.md b/README.md index be2797e..66a81c1 100644 --- a/README.md +++ b/README.md @@ -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('test@payfort.com') +// ->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' => 'test@payfort.com', 'order_description' => 'iPhone 6-S', ])->send(); diff --git a/src/Gateway.php b/src/Gateway.php index 5756a37..9bc3ace 100644 --- a/src/Gateway.php +++ b/src/Gateway.php @@ -46,8 +46,6 @@ public function getDefaultParameters(): array { return [ 'testMode' => FALSE, - 'currency' => 'AED', - 'language' => 'en', ]; } @@ -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); diff --git a/src/Message/Request/APSAbstractRequest.php b/src/Message/Request/APSAbstractRequest.php index 48ec351..f428bb0 100644 --- a/src/Message/Request/APSAbstractRequest.php +++ b/src/Message/Request/APSAbstractRequest.php @@ -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', @@ -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.'); @@ -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); } @@ -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); @@ -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']); + } } \ No newline at end of file diff --git a/src/Message/Request/AuthorizationRequest.php b/src/Message/Request/AuthorizationRequest.php index 403e060..13dc5c7 100644 --- a/src/Message/Request/AuthorizationRequest.php +++ b/src/Message/Request/AuthorizationRequest.php @@ -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'), ]; } diff --git a/src/Message/Request/CaptureRequest.php b/src/Message/Request/CaptureRequest.php index 8d48f70..c52135e 100644 --- a/src/Message/Request/CaptureRequest.php +++ b/src/Message/Request/CaptureRequest.php @@ -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'), ]; } diff --git a/src/Message/Response/AbstractResponse.php b/src/Message/Response/AbstractResponse.php index f6abfeb..65eed67 100644 --- a/src/Message/Response/AbstractResponse.php +++ b/src/Message/Response/AbstractResponse.php @@ -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; } /** diff --git a/tests/GatewayTest.php b/tests/GatewayTest.php index 73f404c..1b0532b 100644 --- a/tests/GatewayTest.php +++ b/tests/GatewayTest.php @@ -1,98 +1,94 @@ -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' => 'test@payfort.com', -<<<<<<< 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' => 'test@payfort.com', + '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()); } @@ -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()); diff --git a/tests/Mock/AuthorizationFailure.txt b/tests/Mock/AuthorizationFailure.txt index deaac2b..8e18076 100644 --- a/tests/Mock/AuthorizationFailure.txt +++ b/tests/Mock/AuthorizationFailure.txt @@ -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":"test@payfort.com","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"} \ No newline at end of file diff --git a/tests/Mock/AuthorizationSuccess.txt b/tests/Mock/AuthorizationSuccess.txt index cb85c05..5c4d797 100644 --- a/tests/Mock/AuthorizationSuccess.txt +++ b/tests/Mock/AuthorizationSuccess.txt @@ -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":"test@payfort.com","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"} \ No newline at end of file diff --git a/tests/Mock/PurchaseFailure.txt b/tests/Mock/PurchaseFailure.txt index 3c10cb2..252fa3d 100644 --- a/tests/Mock/PurchaseFailure.txt +++ b/tests/Mock/PurchaseFailure.txt @@ -1 +1,12 @@ -{"command":"AUTHORIZATION","access_code":"zx0IPmPy5jp1vAz8Kpg7","merchant_identifier":"CycHZxVj","merchant_reference":"XYZ9239-yu898","amount":"10000","currency":"AED","language":"en","customer_email":"test@payfort.com","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"} \ No newline at end of file +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":"test@payfort.com","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"} \ No newline at end of file diff --git a/tests/Mock/PurchaseSuccess.txt b/tests/Mock/PurchaseSuccess.txt index 70f43d2..286522b 100644 --- a/tests/Mock/PurchaseSuccess.txt +++ b/tests/Mock/PurchaseSuccess.txt @@ -1 +1,12 @@ -{"command":"AUTHORIZATION","access_code":"zx0IPmPy5jp1vAz8Kpg7","merchant_identifier":"CycHZxVj","merchant_reference":"XYZ9239-yu898","amount":"10000","currency":"AED","language":"en","customer_email":"test@payfort.com","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":"13064","status":"13","card_holder_name":"John Smith","expiry_date":"2105","card_number":"400555******0001"} \ No newline at end of file +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":"test@payfort.com","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":"13064","status":"14","card_holder_name":"John Smith","expiry_date":"2105","card_number":"400555******0001"} \ No newline at end of file