-
Notifications
You must be signed in to change notification settings - Fork 0
/
Base.php
128 lines (98 loc) · 3.56 KB
/
Base.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
namespace App\Lib\Pay;
use App\Models\ActivityRequest;
use GuzzleHttp\Client;
abstract class Base
{
protected $END_POINT;
protected $HEADER;
protected $API_TOKEN;
protected $CALLBACK;
//for enum -> log
protected $LOG_METHOD_REQUEST;
private $tempLogModel;
abstract public function SET_ENDPOINT();
abstract public function SET_API_TOKEN();
abstract public function SET_CALLBACK();
abstract public function SET_HEADER();
abstract public function createPaymentGateway($order_id, $amount, $phone, $name = null, $desc = null, $mail = null, $reseller = null): array;
abstract public function verifyPayment($idPay, $order_id);
abstract public static function GET_LOG_ID_CLASS(): int;
public function __construct()
{
$this->registerLogProperty();
$this->SET_ENDPOINT();
$this->SET_API_TOKEN();
$this->SET_CALLBACK();
$this->SET_HEADER();
}
/**
* @param $params
* @param $header
* exam url ---> payment/verify _ Without base and [/]
* @param $url
* POST OR GET
* @param string $method
* @return \Psr\Http\Message\ResponseInterface
* @throws \GuzzleHttp\Exception\GuzzleException
*/
protected function requestHttp(array $params, array $header, string $url, $method = 'POST'): array
{
$this->logRequest($params, $header, $url, $method);
$url = $this->END_POINT . $url;
$client = new Client();
$starttime = microtime(1);
try {
$response = $client->request($method, $url,
[
'json' => $params,
'headers' => $header,
'http_errors' => false
]);
$elapsed = microtime(1) - $starttime;
$elapsed = number_format((float)$elapsed, 3, '.', '');
$response->elapsed = $elapsed;
$this->logResponse($response->getStatusCode(),$params,json_decode($response->getBody(), true),$response->elapsed);
return [
'http_code' => $response->getStatusCode(),
'request' => $params,
'response' => json_decode($response->getBody(), true),
'request_time' => $response->elapsed
];
} catch (\Exception $exception) {
$this->logResponse($exception->getCode(),$params,$exception->getMessage(),0);
return [
'http_code' => $exception->getCode(),
'request' => $params,
'response' => env('APP_DEBUG') ? $exception->getMessage() : 'error'
];
}
}
//-------------log Request and Response
//just Log in db
protected function registerLogProperty()
{
$this->LOG_METHOD_REQUEST = [
'createPaymentGateway' => 1,
'verifyPayment' => 2
];
}
protected function logRequest($params, $header, $url, $method)
{
unset($params['callback'], $params['reseller']);
$this->tempLogModel = ActivityRequest::create([
'order_id' => $params['order_id'],
'method' => $this->LOG_METHOD_REQUEST[debug_backtrace()[2]['function']],
'gateway' => call_user_func([get_called_class(), 'GET_LOG_ID_CLASS']),
'request' => json_encode($params)
]);
}
protected function logResponse($httpCode, $param,$response,$request_time)
{
$this->tempLogModel->update([
'http_code'=>$httpCode,
'request_time'=>$request_time,
'response'=>$response
]);
}
}