|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bytesfield\SimpleKyc\Pipes; |
|
4
|
|
|
|
|
5
|
|
|
use Closure; |
|
6
|
|
|
use Noodlehaus\Config; |
|
7
|
|
|
use Bytesfield\SimpleKyc\HttpProcessor; |
|
8
|
|
|
use Bytesfield\SimpleKyc\Classes\IdFilter; |
|
9
|
|
|
|
|
10
|
|
|
class Smile |
|
11
|
|
|
{ |
|
12
|
|
|
public function __construct() |
|
13
|
|
|
{ |
|
14
|
|
|
$config = new Config(__DIR__ . '/../config'); |
|
15
|
|
|
|
|
16
|
|
|
$this->handler = $config->get('smile.handler'); |
|
|
|
|
|
|
17
|
|
|
$this->apiKey = $config->get('smile.api_key'); |
|
|
|
|
|
|
18
|
|
|
$this->baseUrl = $config->get('smile.api_url'); |
|
|
|
|
|
|
19
|
|
|
$this->partnerId = $config->get('smile.partner_id'); |
|
|
|
|
|
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Process http calls |
|
24
|
|
|
* |
|
25
|
|
|
* @param string $method The call method get|post|put|delete|patch |
|
26
|
|
|
* @param string $url The url to call |
|
27
|
|
|
* @param array payload The payload data to send with the call |
|
|
|
|
|
|
28
|
|
|
* |
|
29
|
|
|
* @return \Bytesfield\SimpleKyc\HttpProcessor |
|
30
|
|
|
*/ |
|
31
|
|
|
private function process($method, $url, $payload): HttpProcessor |
|
32
|
|
|
{ |
|
33
|
|
|
//HttpProcessor class to handle http request |
|
34
|
|
|
$processor = new HttpProcessor($this->baseUrl, $this->apiKey, $this->handler); |
|
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
return $processor->process($method, $url, $payload); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Handles the ID request |
|
41
|
|
|
* |
|
42
|
|
|
* @param \Bytesfield\SimpleKyc\Classes\IdFilter |
|
43
|
|
|
* @param Closure $next |
|
44
|
|
|
* |
|
45
|
|
|
* @return array |
|
46
|
|
|
*/ |
|
47
|
|
|
public function handle(IdFilter $IdFilter, Closure $next) |
|
48
|
|
|
{ |
|
49
|
|
|
if (!$IdFilter->isSuccessful()) { |
|
50
|
|
|
|
|
51
|
|
|
$secKey = $this->generateKey($this->partnerId, $this->apiKey); |
|
|
|
|
|
|
52
|
|
|
$jobId = md5(time() . rand()); |
|
53
|
|
|
|
|
54
|
|
|
$body = [ |
|
55
|
|
|
'partner_id' => $this->partnerId, |
|
56
|
|
|
'sec_key' => $secKey[0], |
|
57
|
|
|
'timestamp' => $secKey[1], |
|
58
|
|
|
'country' => strtoupper($IdFilter->getCountry()), |
|
|
|
|
|
|
59
|
|
|
'id_type' => $IdFilter->getIdType(), |
|
60
|
|
|
'id_number' => $IdFilter->getIdNumber(), |
|
61
|
|
|
'first_name' => $IdFilter->getFirstName(), |
|
62
|
|
|
'last_name' => $IdFilter->getLastName(), |
|
63
|
|
|
'dob' => $IdFilter->getDOB(), |
|
64
|
|
|
'phone_number' => $IdFilter->getPhoneNumber(), |
|
65
|
|
|
'company' => $IdFilter->getCompany(), |
|
66
|
|
|
'partner_params' => [ |
|
67
|
|
|
'job_type' => 5, |
|
68
|
|
|
'job_id' => $jobId, |
|
69
|
|
|
'user_id' => $IdFilter->getUserId() |
|
70
|
|
|
] |
|
71
|
|
|
]; |
|
72
|
|
|
|
|
73
|
|
|
try { |
|
74
|
|
|
$response = $this->process('POST', '/v1/id_verification', array_filter($body)); |
|
75
|
|
|
|
|
76
|
|
|
$result = $response->getResponse(); |
|
77
|
|
|
|
|
78
|
|
|
if ($result['IsFinalResult']) { |
|
79
|
|
|
$IdFilter->confirmSuccess(); |
|
80
|
|
|
$IdFilter->setHandler($this->handler); |
|
81
|
|
|
|
|
82
|
|
|
$IdFilter->setData([ |
|
83
|
|
|
'handler' => $IdFilter->getHandler(), |
|
84
|
|
|
'country' => $IdFilter->getCountry(), |
|
85
|
|
|
'message' => $IdFilter->getIDType() . ' Verified' . ' Successfully', |
|
86
|
|
|
'data' => $result |
|
87
|
|
|
]); |
|
88
|
|
|
|
|
89
|
|
|
return $IdFilter->getData(); |
|
90
|
|
|
} |
|
91
|
|
|
} catch (\Exception $e) { |
|
92
|
|
|
$IdFilter->setError(['error' => $e->getMessage()]); |
|
93
|
|
|
|
|
94
|
|
|
return $next($IdFilter); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
return $next($IdFilter); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Generate Secret Key |
|
102
|
|
|
* |
|
103
|
|
|
* @param string $partnerId |
|
104
|
|
|
* @param string $apiKey |
|
105
|
|
|
* @return array |
|
106
|
|
|
*/ |
|
107
|
|
|
private function generateKey(string $partnerId, string $apiKey): array |
|
108
|
|
|
{ |
|
109
|
|
|
$timestamp = time(); |
|
110
|
|
|
$plaintext = intval($partnerId) . ":" . $timestamp; |
|
111
|
|
|
$hashSignature = hash('sha256', $plaintext); |
|
112
|
|
|
|
|
113
|
|
|
openssl_public_encrypt($hashSignature, $secKey, base64_decode($apiKey), OPENSSL_PKCS1_PADDING); |
|
114
|
|
|
|
|
115
|
|
|
$secKey = base64_encode($secKey); |
|
116
|
|
|
$secKey = $secKey . "|" . $hashSignature; |
|
117
|
|
|
|
|
118
|
|
|
return array($secKey, $timestamp); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|