Skip to content

Commit

Permalink
Add ConnectException
Browse files Browse the repository at this point in the history
  • Loading branch information
yidas committed Sep 29, 2019
1 parent 060868b commit 13cc960
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 13 deletions.
55 changes: 42 additions & 13 deletions src/linePay/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Exception;
use yidas\linePay\Response;
use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\Psr7\Request;

/**
* LINE Pay Client
Expand Down Expand Up @@ -55,6 +56,13 @@ class Client
*/
protected $httpClient;

/**
* PSR-7 Request
*
* @var GuzzleHttp\Psr7\Request
*/
protected $request;

/**
* Saved LINE Pay Channel Secret for v3 API Authentication
*
Expand Down Expand Up @@ -109,11 +117,22 @@ function __construct($optParams)
'base_uri' => $baseUri,
// 'timeout' => 6.0,
'headers' => $headers,
'http_errors' => false,
]);

return $this;
}

/**
* Get Request object
*
* @return GuzzleHttp\Psr7\Request
*/
public function getRequest()
{
return $this->request;
}

/**
* Client request handler with version
*
Expand All @@ -127,13 +146,13 @@ function __construct($optParams)
*/
protected function requestHandler($version, $method, $uri, $queryParams=null, $bodyParams=null, $options=[])
{
// Request options
if ($queryParams) {
$options['query'] = $queryParams;
}
if ($bodyParams) {
$options['body'] = json_encode($bodyParams);
}
// Headers
$headers = [];
// Query String
$queryString = ($queryParams) ? http_build_query($queryParams) : null;
$url = ($queryParams) ? "{$uri}?{$queryString}" : $uri ;
// Body
$body = ($bodyParams) ? json_encode($bodyParams) : '';

// Guzzle on_stats
$stats = null;
Expand All @@ -147,22 +166,32 @@ protected function requestHandler($version, $method, $uri, $queryParams=null, $b
switch ($version) {
case 'v2':
// V2 API Authentication
$options['headers']['X-LINE-ChannelSecret'] = $this->channelSecret;
$response = $this->httpClient->request($method, $uri, $options);
$headers['X-LINE-ChannelSecret'] = $this->channelSecret;
break;

case 'v3':
default:
// V3 API Authentication
$authNonce = date('c'); // ISO 8601 date
$authParams = ($method=='GET' && $queryParams) ? http_build_query($queryParams) : (($bodyParams) ? $options['body'] : null);
$authParams = ($method=='GET' && $queryParams) ? $queryString : (($bodyParams) ? $body : null);
$authMacText = $this->channelSecret . $uri . $authParams . $authNonce;
$options['headers']['X-LINE-Authorization'] = base64_encode(hash_hmac('sha256', $authMacText, $this->channelSecret, true));
$options['headers']['X-LINE-Authorization-Nonce'] = $authNonce;
$response = $this->httpClient->request($method, $uri, $options);
$headers['X-LINE-Authorization'] = base64_encode(hash_hmac('sha256', $authMacText, $this->channelSecret, true));
$headers['X-LINE-Authorization-Nonce'] = $authNonce;
break;
}

// Send request with PSR-7 pattern
$this->request = new Request($method, $url, $headers, $body);
$this->request->timestamp = microtime(true);
try {

$response = $this->httpClient->send($this->request, $options);

} catch (\GuzzleHttp\Exception\ConnectException $e) {

throw new \yidas\linePay\exception\ConnectException($e->getMessage(), $this->request);
}

return new Response($response, $stats);
}

Expand Down
15 changes: 15 additions & 0 deletions src/linePay/exception/ConnectException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace yidas\linePay\exception;

use GuzzleHttp\Exception\ConnectException as BassException;

/**
* Exception thrown when a connection cannot be established.
*
* Note that no response is present for a ConnectException
*/
class ConnectException extends BassException
{

}

0 comments on commit 13cc960

Please sign in to comment.