-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_me_first.php
129 lines (100 loc) · 3.46 KB
/
run_me_first.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
129
<?php
require __DIR__ . '/vendor/autoload.php';
use splitbrain\phpcli\CLI;
use splitbrain\phpcli\Options;
use GuzzleHttp\Client;
class Winstant extends CLI
{
private $clientId=0;
private $kycClient;
private $config;
private $token;
// register options and arguments
protected function setup(Options $options)
{
$options->setHelp('A very small example how to work with the WinstantPay API');
$options->registerOption('version', 'print version', 'v');
$options->registerOption('config', 'print configuration', 'c');
}
private function login()
{
$client = new GuzzleHttp\Client(['base_uri' => $this->config->url]);
$response = $client->request('POST', 'client/auth/', [
'json' => ['username'=>$this->config->username , 'password'=> $this->config->password]
]);
$resp = json_decode($response->getBody());
$this->token = $resp->token;
}
private function registerClient($nextRequirementId, $requirements)
{
$data = [];
$client = new GuzzleHttp\Client(['base_uri' => $this->config->url]);
$uri = 'client/signup/?service_caller_id='.$this->config->APIKey;
if ($nextRequirementId) {
$uri .= "&requirement_id=".$nextRequirementId;
}
if ($this->clientId > 0 ) {
$uri .= "&client_id=".$this->clientId;
$this->info("Client_id: ".$this->clientId);
}
foreach ($requirements->field_names as $key) {
echo ":: $key -> ".$this->kycClient->$key."\n";
$data[$key] = $this->kycClient->$key;
}
$json_string = json_encode($data, JSON_PRETTY_PRINT);
echo "---------------------\n";
echo $json_string."\n";
echo "---------------------\n";
$this->info($uri);
$response = $client->request('POST',$uri, [
'json' => $data,
'headers' => [
'Authorization' => 'token ' . $this->token,
],
]);
$response = json_decode($response->getBody());
var_dump($response);
if (isset($response->id)) {
$this->clientId = $response->id;
}
}
private function handleStep($nextRequirementId,$requirements)
{
$newKYCClient = $this->registerClient($nextRequirementId, $requirements);
$nextRequirementId = $requirements->requirement_id;
return $nextRequirementId;
}
protected function main(Options $options)
{
$this->config = include(__DIR__ . '/config.php');
$this->kycClient = include(__DIR__ . '/client.php');
if ($options->getOpt('version')) {
$this->info('1.0.0');
}
if ($options->getOpt('config')) {
var_dump($config);
}
$this->login();
$nextRequirementId = null;
$client = new GuzzleHttp\Client(['base_uri' => $this->config->url]);
do {
$uri = '/client/schema/?service_caller_id='.$this->config->APIKey;
if ($nextRequirementId) {
$uri .= '&requirement_id='.$nextRequirementId;
}
$this->info($this->config->url.$uri);
$response = $client->request('GET', $uri);
if($response->getStatusCode() == 200) {
$requirement = json_decode($response->getBody());
//var_dump($requirement);
$nextRequirementId = $this->handleStep($nextRequirementId,$requirement);
}
else {
$nextRequirementId = null;
$this->error("Fatal step machine error. Please contact WKYC support");
}
} while ($nextRequirementId != null);
}
}
$cli = new Winstant();
$cli->run();