diff --git a/bootstrap.php b/bootstrap.php new file mode 100644 index 0000000..573fcda --- /dev/null +++ b/bootstrap.php @@ -0,0 +1,4 @@ + setParameter('merchantSecret', $value); } + /** + * @return mixed + */ + public function getCountryCode() + { + return $this->getParameter('countryCode'); + } + + /** + * @param mixed $value + * @return $this + */ + public function setCountryCode($value) + { + return $this->setParameter('countryCode', $value); + } + /** * Configuration Request. * @@ -97,4 +117,15 @@ public function completePurchase(array $options = array()) { return $this->createRequest('\Omnipay\AfterPay\Message\CompletePurchaseRequest', $options); } + + /** + * Handle partial/full refunds + * + * @param array $options + * @return \Omnipay\Common\Message\ResponseInterface + */ + public function refund(array $options = array()) + { + return $this->createRequest('\Omnipay\AfterPay\Message\RefundRequest', $options); + } } diff --git a/src/Message/AbstractRequest.php b/src/Message/AbstractRequest.php index 128b709..6b7d434 100755 --- a/src/Message/AbstractRequest.php +++ b/src/Message/AbstractRequest.php @@ -4,8 +4,8 @@ abstract class AbstractRequest extends \Omnipay\Common\Message\AbstractRequest { - protected $liveEndpoint = 'https://api.secure-afterpay.com.au/v1'; - protected $testEndpoint = 'https://api-sandbox.secure-afterpay.com.au/v1'; + protected $liveEndpoint = 'https://api.afterpay.com/v1'; + protected $testEndpoint = 'https://api-sandbox.afterpay.com/v1'; /** @@ -53,29 +53,6 @@ public function getHeaders() return $headers; } - - /** - * @param mixed $data - * @return \Omnipay\AfterPay\Message\Response - * @throws \Guzzle\Http\Exception\RequestException - */ - public function sendData($data) - { - $endpoint = $this->getEndpoint(); - $httpMethod = $this->getHttpMethod(); - $httpRequest = $this->httpClient->createRequest($httpMethod, $endpoint); - $httpRequest->getCurlOptions()->set(CURLOPT_SSLVERSION, 6); // CURL_SSLVERSION_TLSv1_2 - $httpRequest->addHeader('Authorization', $this->buildAuthorizationHeader()); - $httpRequest->addHeader('Content-type', 'application/json'); - $httpRequest->addHeader('Accept', 'application/json'); - $httpRequest->setBody(json_encode($data)); - $httpResponse = $httpRequest->send(); - $this->response = $this->createResponse( - $this->parseResponseData($httpResponse) - ); - return $this->response; - } - /** * @return string */ @@ -92,27 +69,74 @@ protected function getEndpoint() return $this->getTestMode() ? $this->testEndpoint : $this->liveEndpoint; } - /** - * @param \Guzzle\Http\Message\Response $httpResponse - * @return array - */ - protected function parseResponseData(GuzzleResponse $httpResponse) + public function sendData($data) { - return $httpResponse->json(); + // don't throw exceptions for 4xx errors + $this->httpClient->getEventDispatcher()->addListener( + 'request.error', + function ($event) { + if ($event['response']->isClientError()) { + $event->stopPropagation(); + } + } + ); + + // Guzzle HTTP Client createRequest does funny things when a GET request + // has attached data, so don't send the data if the method is GET. + if ($this->getHttpMethod() == 'GET') { + $httpRequest = $this->httpClient->createRequest( + $this->getHttpMethod(), + $this->getEndpoint() . '?' . http_build_query($data), + array( + 'User-Agent' => $this->getUserAgent(), + 'Accept' => 'application/json', + 'Authorization' => $this->buildAuthorizationHeader(), + 'Content-type' => 'application/json', + ) + ); + } else { + $httpRequest = $this->httpClient->createRequest( + $this->getHttpMethod(), + $this->getEndpoint(), + array( + 'User-Agent' => $this->getUserAgent(), + 'Accept' => 'application/json', + 'Authorization' => $this->buildAuthorizationHeader(), + 'Content-type' => 'application/json', + ), + $this->toJSON($data) + ); + } + + try { + $httpRequest->getCurlOptions()->set(CURLOPT_SSLVERSION, 6); // CURL_SSLVERSION_TLSv1_2 for libcurl < 7.35 + $httpResponse = $httpRequest->send(); + $responseBody = (string) $httpResponse->getBody(); + $response = json_decode($responseBody, true) ?? []; + + return $this->response = $this->createResponse($response); + } catch (\Exception $e) { + throw new InvalidResponseException( + 'Error communicating with payment gateway: ' . $e->getMessage(), + $e->getCode() + ); + } + + } + + public function toJSON($data, $options = 0) + { + if (version_compare(phpversion(), '5.4.0', '>=') === true) { + return json_encode($data, $options | 64); + } + return str_replace('\\/', '/', json_encode($data, $options)); } - /** - * @param mixed $data - * @return \Omnipay\AfterPay\Message\Response - */ protected function createResponse($data) { - return new Response($this, $data); + return $this->response = new Response($this, $data); } - /** - * @return string - */ protected function buildAuthorizationHeader() { $merchantId = $this->getMerchantId(); @@ -120,4 +144,8 @@ protected function buildAuthorizationHeader() return 'Basic ' . base64_encode($merchantId . ':' . $merchantSecret); } + + protected function getUserAgent() { + return 'Omnipay (Omnipay-Afterpay/'.PHP_VERSION.' ; '.$this->getMerchantId().')'; + } } diff --git a/src/Message/ConfigurationRequest.php b/src/Message/ConfigurationRequest.php index 6a31050..a771d90 100755 --- a/src/Message/ConfigurationRequest.php +++ b/src/Message/ConfigurationRequest.php @@ -12,7 +12,7 @@ class ConfigurationRequest extends AbstractRequest */ public function getData() { - return null; + return array(); } /** diff --git a/src/Message/PurchaseResponse.php b/src/Message/PurchaseResponse.php index 8a76a8b..d59d9ca 100755 --- a/src/Message/PurchaseResponse.php +++ b/src/Message/PurchaseResponse.php @@ -6,8 +6,7 @@ class PurchaseResponse extends Response { - protected $liveScript = 'https://www.secure-afterpay.com.au/afterpay.js'; - protected $testScript = 'https://www-sandbox.secure-afterpay.com.au/afterpay.js'; + protected $script = 'https://portal.sandbox.afterpay.com/afterpay.js'; public function getRedirectMethod() { @@ -27,7 +26,6 @@ public function isRedirect() */ public function getRedirectResponse() { - echo '
'; print_r($this->data); echo '
'; die(); $output = << @@ -37,7 +35,7 @@ public function getRedirectResponse() @@ -45,7 +43,7 @@ public function getRedirectResponse() EOF; - $output = sprintf($output, $this->getScriptUrl(), $this->getToken()); + $output = sprintf($output, $this->getScriptUrl(), $this->getCountryCode(), $this->getToken()); return HttpResponse::create($output); } @@ -55,13 +53,7 @@ public function getRedirectResponse() */ public function getScriptUrl() { - $request = $this->getRequest(); - - if ($request instanceof PurchaseRequest && $request->getTestMode()) { - return $this->testScript; - } - - return $this->liveScript; + return $this->script; } /** diff --git a/src/Message/RefundRequest.php b/src/Message/RefundRequest.php new file mode 100644 index 0000000..cd9c7d0 --- /dev/null +++ b/src/Message/RefundRequest.php @@ -0,0 +1,30 @@ + array( + 'amount' => $this->getAmount(), + 'currency' => $this->getCurrency() + ) + ); + } + + /** + * @return string + */ + public function getEndpoint() + { + return parent::getEndpoint() . '/payments/' . $this->getTransactionReference() . '/refund'; + } +} diff --git a/tests/GatewayTest.php b/tests/GatewayTest.php index 60386a7..168e1bc 100755 --- a/tests/GatewayTest.php +++ b/tests/GatewayTest.php @@ -67,7 +67,6 @@ public function configurationRequest() public function testPurchase() { $request = $this->gateway->purchase(array('amount' => '10.00')); - $this->assertInstanceOf('Omnipay\AfterPay\Message\PurchaseRequest', $request); $this->assertSame('10.00', $request->getAmount()); } @@ -75,10 +74,17 @@ public function testPurchase() public function testPurchaseReturn() { $request = $this->gateway->completePurchase(array('amount' => '10.00')); - $this->assertInstanceOf('Omnipay\AfterPay\Message\CompletePurchaseRequest', $request); $this->assertSame('10.00', $request->getAmount()); } + public function testRefund() + { + $options = array('amount' => '10.00', 'currency' => 'NZD'); + $request = $this->gateway->refund($options); + $this->assertInstanceOf('Omnipay\Afterpay\Message\RefundRequest', $request); + $this->assertSame('10.00', $request->getAmount()); + $this->assertSame('NZD', $request->getCurrency()); + } }