|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dynamic\FoxyStripe\API\Client; |
|
4
|
|
|
|
|
5
|
|
|
use Dynamic\FoxyStripe\Model\FoxyStripeClient; |
|
6
|
|
|
use Foxy\FoxyClient\FoxyClient; |
|
7
|
|
|
use SilverStripe\Core\Config\Configurable; |
|
8
|
|
|
use SilverStripe\Core\Extensible; |
|
9
|
|
|
use SilverStripe\Core\Injector\Injectable; |
|
10
|
|
|
use SilverStripe\Security\Member; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class CustomerClient |
|
14
|
|
|
* @package Dynamic\FoxyStripe\API\Client |
|
15
|
|
|
*/ |
|
16
|
|
|
class CustomerClient extends FoxyStripeClient |
|
17
|
|
|
{ |
|
18
|
|
|
use Configurable; |
|
19
|
|
|
use Extensible; |
|
20
|
|
|
use Injectable; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
private static $customer_map = [ |
|
26
|
|
|
'Customer_ID' => 'id', |
|
27
|
|
|
'FirstName' => 'first_name', |
|
28
|
|
|
'Surname' => 'last_name', |
|
29
|
|
|
'Email' => 'email', |
|
30
|
|
|
'Salt' => 'password_salt', |
|
31
|
|
|
'Password' => 'password_hash', |
|
32
|
|
|
]; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
private static $foxy_password_hash_type = 'bcrypt'; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var Member |
|
41
|
|
|
*/ |
|
42
|
|
|
private $customer; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* CustomerClient constructor. |
|
46
|
|
|
*/ |
|
47
|
49 |
|
public function __construct(Member $customer) |
|
48
|
|
|
{ |
|
49
|
49 |
|
parent::__construct(); |
|
50
|
|
|
|
|
51
|
49 |
|
$this->setCustomer($customer); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param $customer |
|
56
|
|
|
* @return $this |
|
57
|
|
|
*/ |
|
58
|
49 |
|
public function setCustomer($customer) |
|
59
|
|
|
{ |
|
60
|
49 |
|
$this->customer = $customer; |
|
61
|
|
|
|
|
62
|
49 |
|
return $this; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return Member |
|
67
|
|
|
*/ |
|
68
|
49 |
|
public function getCustomer() |
|
69
|
|
|
{ |
|
70
|
49 |
|
return $this->customer; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @return string |
|
75
|
|
|
*/ |
|
76
|
|
|
private function getAPIURI($single = false) |
|
77
|
|
|
{ |
|
78
|
|
|
$parts = [FoxyClient::PRODUCTION_API_HOME, 'customers']; |
|
79
|
|
|
|
|
80
|
|
|
if ($single) { |
|
81
|
|
|
$parts[] = $this->getCustomer()->Customer_ID; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return implode('/', $parts); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
49 |
|
private function getNewCustomerAPIURI() |
|
88
|
|
|
{ |
|
89
|
49 |
|
return implode('/', [$this->getCurrentStore(), 'customers']); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return mixed |
|
94
|
|
|
*/ |
|
95
|
49 |
|
public function putCustomer() |
|
96
|
|
|
{ |
|
97
|
49 |
|
$data = $this->getSendData(); |
|
|
|
|
|
|
98
|
49 |
|
$client = $this->getClient(); |
|
99
|
|
|
|
|
100
|
49 |
|
if (!$this->getCustomer()->Customer_ID) { |
|
101
|
49 |
|
$response = $client->post($this->getNewCustomerAPIURI(), $this->getSendData()); |
|
102
|
|
|
} else { |
|
103
|
|
|
$response = $client->patch($this->getAPIURI(true), $this->getSendData()); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
49 |
|
return $response; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @return mixed |
|
111
|
|
|
*/ |
|
112
|
|
|
public function fetchCustomer() |
|
113
|
|
|
{ |
|
114
|
|
|
$client = $this->getClient(); |
|
115
|
|
|
|
|
116
|
|
|
$result = $client->get($this->getAPIURI(true)); |
|
117
|
|
|
|
|
118
|
|
|
return $result; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @return mixed |
|
123
|
|
|
*/ |
|
124
|
|
|
public function fetchCustomers() |
|
125
|
|
|
{ |
|
126
|
|
|
$client = $this->getClient(); |
|
127
|
|
|
|
|
128
|
|
|
$result = $client->get($this->getAPIURI(true)); |
|
129
|
|
|
|
|
130
|
|
|
return $result; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* |
|
135
|
|
|
*/ |
|
136
|
|
|
public function deleteCustomer() |
|
137
|
|
|
{ |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @return array |
|
142
|
|
|
*/ |
|
143
|
49 |
|
protected function getSendData() |
|
144
|
|
|
{ |
|
145
|
49 |
|
$data = []; |
|
146
|
|
|
|
|
147
|
49 |
|
if ($customer = $this->getCustomer()) { |
|
148
|
49 |
|
foreach ($this->config()->get('customer_map') as $localField => $remoteField) { |
|
149
|
49 |
|
if ($customer->{$localField}) { |
|
150
|
49 |
|
$data[$remoteField] = $customer->{$localField}; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
49 |
|
return $data; |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|