From 3ccd1535dfae53b9bf163c21c009d0a197fecf70 Mon Sep 17 00:00:00 2001 From: Eric Khoo Date: Mon, 22 Jul 2019 15:04:35 +0800 Subject: [PATCH] support omnipay v3 --- composer.json | 7 ++++--- src/Message/AbstractRequest.php | 28 ++++++++++++++++++++-------- src/Message/PurchaseResponse.php | 16 +++++++++++++++- 3 files changed, 39 insertions(+), 12 deletions(-) diff --git a/composer.json b/composer.json index 21a8994..665bb63 100755 --- a/composer.json +++ b/composer.json @@ -23,9 +23,10 @@ "psr-4": { "Omnipay\\Billplz\\" : "src/" } }, "require": { - "omnipay/common": "~2.0" + "omnipay/common": "^3", + "php-http/guzzle6-adapter": "^2" }, "require-dev": { - "omnipay/tests": "~2.0" + "omnipay/tests": "^3" } -} +} \ No newline at end of file diff --git a/src/Message/AbstractRequest.php b/src/Message/AbstractRequest.php index 7355671..99b09a2 100755 --- a/src/Message/AbstractRequest.php +++ b/src/Message/AbstractRequest.php @@ -3,6 +3,8 @@ namespace Omnipay\Billplz\Message; use Omnipay\Common\Exception\InvalidRequestException; +use Omnipay\Common\Exception\InvalidResponseException; +use GuzzleHttp\Middleware; /** * Abstract Request @@ -22,35 +24,45 @@ private function getEndpoint() public function sendData($data) { // don't throw exceptions for 4xx errors - $this->httpClient->getEventDispatcher()->addListener( + /*$this->httpClient->getEventDispatcher()->addListener( 'request.error', function ($event) { if ($event['response']->isClientError()) { $event->stopPropagation(); } } - ); + );*/ + + try { // 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( + $httpResponse = $this->httpClient->request( $this->getHttpMethod(), $this->getEndpoint() . '?' . http_build_query($data), array( 'Accept' => 'application/json', 'Authorization' => 'Basic ' . $this->getToken(), 'Content-type' => 'application/json', + 'curl.options' => array( + CURLOPT_SSLVERSION => 6 + ), + 'http_errors' => false ) ); } else { - $httpRequest = $this->httpClient->createRequest( + $httpResponse = $this->httpClient->request( $this->getHttpMethod(), $this->getEndpoint(), array( 'Accept' => 'application/json', 'Authorization' => 'Basic ' . $this->getToken(), 'Content-type' => 'application/json', + 'curl.options' => array( + CURLOPT_SSLVERSION => 6 + ), + 'http_errors' => false ), $this->toJSON($data) ); @@ -61,12 +73,12 @@ function ($event) { // logging engine is being used. // echo "Data == " . json_encode($data) . "\n"; - try { - $httpRequest->getCurlOptions()->set(CURLOPT_SSLVERSION, 6); // CURL_SSLVERSION_TLSv1_2 for libcurl < 7.35 - $httpResponse = $httpRequest->send(); + + //$httpRequest->getOptions()->set(CURLOPT_SSLVERSION, 6); // CURL_SSLVERSION_TLSv1_2 for libcurl < 7.35 + //$httpResponse = $httpRequest->send(); // Empty response body should be parsed also as and empty array $body = $httpResponse->getBody(true); - $jsonToArrayResponse = !empty($body) ? $httpResponse->json() : array(); + $jsonToArrayResponse = !empty($body) ? json_decode($body, true) : array(); return $this->response = $this->createResponse($jsonToArrayResponse, $httpResponse->getStatusCode()); } catch (\Exception $e) { throw new InvalidResponseException( diff --git a/src/Message/PurchaseResponse.php b/src/Message/PurchaseResponse.php index 46fe6c8..5260224 100755 --- a/src/Message/PurchaseResponse.php +++ b/src/Message/PurchaseResponse.php @@ -37,7 +37,7 @@ public function isSuccessful() */ public function isRedirect() { - return true; + return isset($this->data['url'])?true:false; } /** @@ -69,4 +69,18 @@ public function getRedirectData() { return $this->getData(); } + + public function getMessage() + { + if(is_array($this->data['error'])){ + return implode(", ",$this->data['error']['message']); + }else{ + return $this->data['error']; + } + } + + public function getTransactionReference() + { + return isset($this->data['id'])?$this->data['id']:null; + } }