Skip to content

Commit

Permalink
Merge pull request #1 from trilobyte-my/master
Browse files Browse the repository at this point in the history
support omnipay v3
  • Loading branch information
yinsee authored Jul 22, 2019
2 parents 9aaebc6 + 3ccd153 commit f5047c9
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
28 changes: 20 additions & 8 deletions src/Message/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Omnipay\Billplz\Message;

use Omnipay\Common\Exception\InvalidRequestException;
use Omnipay\Common\Exception\InvalidResponseException;
use GuzzleHttp\Middleware;

/**
* Abstract Request
Expand All @@ -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)
);
Expand All @@ -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(
Expand Down
16 changes: 15 additions & 1 deletion src/Message/PurchaseResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function isSuccessful()
*/
public function isRedirect()
{
return true;
return isset($this->data['url'])?true:false;
}

/**
Expand Down Expand Up @@ -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;
}
}

0 comments on commit f5047c9

Please sign in to comment.