1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\FoxyStripe\Model; |
4
|
|
|
|
5
|
|
|
use Psr\Log\LoggerInterface; |
6
|
|
|
use SilverStripe\Core\Injector\Injector; |
7
|
|
|
use SilverStripe\Dev\Debug; |
8
|
|
|
use SilverStripe\SiteConfig\SiteConfig; |
9
|
|
|
use SilverStripe\Security\Member; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* |
13
|
|
|
*/ |
14
|
|
|
class FoxyCart |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private static $keyPrefix = 'dYnm1c'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param int $length |
23
|
|
|
* @param int $count |
24
|
|
|
* |
25
|
|
|
* @return string |
26
|
|
|
*/ |
27
|
3 |
|
public static function setStoreKey($length = 54, $count = 0) |
28
|
|
|
{ |
29
|
3 |
|
$charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'.strtotime('now'); |
30
|
3 |
|
$strLength = strlen($charset); |
31
|
3 |
|
$str = ''; |
32
|
3 |
|
while ($count < $length) { |
33
|
3 |
|
$str .= $charset[mt_rand(0, $strLength - 1)]; |
34
|
3 |
|
++$count; |
35
|
|
|
} |
36
|
|
|
|
37
|
3 |
|
return self::getKeyPrefix().substr(base64_encode($str), 0, $length); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return mixed|null |
42
|
|
|
* @throws \SilverStripe\ORM\ValidationException |
43
|
|
|
*/ |
44
|
|
|
public static function getStoreKey() |
45
|
|
|
{ |
46
|
|
|
$config = FoxyStripeSetting::current_foxystripe_setting(); |
47
|
|
|
if ($config->StoreKey) { |
48
|
|
|
return $config->StoreKey; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return false; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return null|string |
56
|
|
|
* @throws \SilverStripe\ORM\ValidationException |
57
|
|
|
*/ |
58
|
1 |
|
public static function store_name_warning() |
59
|
|
|
{ |
60
|
1 |
|
$warning = null; |
61
|
1 |
|
if (self::getFoxyCartStoreName() === null) { |
62
|
|
|
$warning = 'Must define FoxyCart Store Name or Store Remote Domain in your site settings in the cms'; |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
return $warning; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return mixed|null |
70
|
|
|
* @throws \SilverStripe\ORM\ValidationException |
71
|
|
|
*/ |
72
|
1 |
|
public static function getFoxyCartStoreName() |
73
|
|
|
{ |
74
|
1 |
|
$config = FoxyStripeSetting::current_foxystripe_setting(); |
75
|
1 |
|
if ($config->CustomSSL) { |
|
|
|
|
76
|
|
|
if ($config->RemoteDomain) { |
|
|
|
|
77
|
|
|
return $config->RemoteDomain; |
78
|
|
|
} |
79
|
|
|
} else { |
80
|
1 |
|
if ($config->StoreName) { |
81
|
|
|
return $config->StoreName; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
return false; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return string |
90
|
|
|
* @throws \SilverStripe\ORM\ValidationException |
91
|
|
|
*/ |
92
|
|
|
public static function FormActionURL() |
93
|
|
|
{ |
94
|
|
|
$config = FoxyStripeSetting::current_foxystripe_setting(); |
95
|
|
|
if ($config->CustomSSL) { |
|
|
|
|
96
|
|
|
return sprintf('https://%s/cart', self::getFoxyCartStoreName()); |
97
|
|
|
} else { |
98
|
|
|
return sprintf('https://%s.foxycart.com/cart', self::getFoxyCartStoreName()); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* FoxyCart API v1.1 functions. |
104
|
|
|
*/ |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param array $foxyData |
108
|
|
|
* @return string |
109
|
|
|
* @throws \SilverStripe\ORM\ValidationException |
110
|
|
|
*/ |
111
|
|
|
private static function getAPIRequest($foxyData = array()) |
112
|
|
|
{ |
113
|
|
|
if (self::getStoreKey() && self::getFoxyCartStoreName()) { |
114
|
|
|
$foxy_domain = self::getFoxyCartStoreName().'.foxycart.com'; |
115
|
|
|
$foxyData['api_token'] = self::getStoreKey(); |
116
|
|
|
|
117
|
|
|
$ch = curl_init(); |
118
|
|
|
curl_setopt($ch, CURLOPT_URL, 'https://'.$foxy_domain.'/api'); |
119
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $foxyData); |
120
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
121
|
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); |
122
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 15); |
123
|
|
|
// If you get SSL errors, you can uncomment the following, or ask your host to add the appropriate CA bundle |
124
|
|
|
// curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
|
|
|
|
125
|
|
|
$response = trim(curl_exec($ch)); |
126
|
|
|
|
127
|
|
|
// The following if block will print any CURL errors you might have |
128
|
|
|
if ($response == false) { |
|
|
|
|
129
|
|
|
//trigger_error("Could not connect to FoxyCart API", E_USER_ERROR); |
|
|
|
|
130
|
|
|
Injector::inst()->get(LoggerInterface::class)->error('Could not connect to FoxyCart API'); |
131
|
|
|
} |
132
|
|
|
curl_close($ch); |
133
|
|
|
|
134
|
|
|
return $response; |
135
|
|
|
} |
136
|
|
|
return false; |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param null $Member |
|
|
|
|
141
|
|
|
* @return string |
142
|
|
|
* @throws \SilverStripe\ORM\ValidationException |
143
|
|
|
*/ |
144
|
|
|
public static function getCustomer(Member $Member = null) |
145
|
|
|
{ |
146
|
|
|
|
147
|
|
|
// throw error if no $Member Object |
148
|
|
|
if (!isset($Member)) { |
149
|
|
|
trigger_error('No Member set', E_USER_ERROR); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
// grab customer record from API |
153
|
|
|
|
154
|
|
|
$foxyData = array(); |
155
|
|
|
$foxyData['api_action'] = 'customer_get'; |
156
|
|
|
if ($Member->Customer_ID) { |
157
|
|
|
$foxyData['customer_id'] = $Member->Customer_ID; |
158
|
|
|
} |
159
|
|
|
$foxyData['customer_email'] = $Member->Email; |
160
|
|
|
|
161
|
|
|
return self::getAPIRequest($foxyData); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param null $Member |
|
|
|
|
166
|
|
|
* @return string |
167
|
|
|
* @throws \SilverStripe\ORM\ValidationException |
168
|
|
|
*/ |
169
|
|
|
public static function putCustomer(Member $Member = null) |
170
|
|
|
{ |
171
|
|
|
// throw error if no $Member Object |
172
|
|
|
if ($Member === null) { |
173
|
|
|
//trigger_error('No Member set', E_USER_ERROR); |
|
|
|
|
174
|
|
|
return false; |
|
|
|
|
175
|
|
|
} |
176
|
|
|
// send updated customer record from API |
177
|
|
|
$foxyData = array(); |
178
|
|
|
$foxyData['api_action'] = 'customer_save'; |
179
|
|
|
// customer_id will be 0 if created in SilverStripe. |
180
|
|
|
if ($Member->Customer_ID) { |
181
|
|
|
$foxyData['customer_id'] = $Member->Customer_ID; |
182
|
|
|
} |
183
|
|
|
$foxyData['customer_email'] = $Member->Email; |
184
|
|
|
$foxyData['customer_password_hash'] = $Member->Password; |
185
|
|
|
$foxyData['customer_password_salt'] = $Member->Salt; |
186
|
|
|
$foxyData['customer_first_name'] = $Member->FirstName; |
187
|
|
|
$foxyData['customer_last_name'] = $Member->Surname; |
188
|
|
|
|
189
|
|
|
return self::getAPIRequest($foxyData); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @return string |
194
|
|
|
*/ |
195
|
3 |
|
public static function getKeyPrefix() |
196
|
|
|
{ |
197
|
3 |
|
return self::$keyPrefix; |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|