|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dynamic\FoxyStripe\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Dynamic\FoxyStripe\Model\FoxyCart; |
|
6
|
|
|
use Dynamic\FoxyStripe\Model\FoxyStripeSetting; |
|
7
|
|
|
use Dynamic\FoxyStripe\Model\OptionItem; |
|
8
|
|
|
use Dynamic\FoxyStripe\Model\Order; |
|
9
|
|
|
use Dynamic\FoxyStripe\Model\OrderDetail; |
|
10
|
|
|
use Dynamic\FoxyStripe\Page\ProductPage; |
|
11
|
|
|
use SilverStripe\Security\Member; |
|
12
|
|
|
use SilverStripe\Security\Security; |
|
13
|
|
|
|
|
14
|
|
|
class FoxyStripeController extends \PageController |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* |
|
18
|
|
|
*/ |
|
19
|
|
|
const URLSEGMENT = 'foxystripe'; |
|
20
|
|
|
/** |
|
21
|
|
|
* @var array |
|
22
|
|
|
*/ |
|
23
|
|
|
private static $allowed_actions = array( |
|
|
|
|
|
|
24
|
|
|
'index', |
|
25
|
|
|
'sso', |
|
26
|
|
|
); |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @return string |
|
30
|
|
|
*/ |
|
31
|
|
|
public function getURLSegment() |
|
32
|
|
|
{ |
|
33
|
|
|
return self::URLSEGMENT; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @return string |
|
38
|
|
|
* |
|
39
|
|
|
* @throws \SilverStripe\ORM\ValidationException |
|
40
|
|
|
*/ |
|
41
|
|
|
public function index() |
|
42
|
|
|
{ |
|
43
|
|
|
// handle POST from FoxyCart API transaction |
|
44
|
|
|
if ((isset($_POST['FoxyData']) or isset($_POST['FoxySubscriptionData']))) { |
|
|
|
|
|
|
45
|
|
|
$FoxyData_encrypted = (isset($_POST['FoxyData'])) ? |
|
46
|
|
|
urldecode($_POST['FoxyData']) : |
|
47
|
|
|
urldecode($_POST['FoxySubscriptionData']); |
|
48
|
|
|
$FoxyData_decrypted = \rc4crypt::decrypt(FoxyCart::getStoreKey(), $FoxyData_encrypted); |
|
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
// parse the response and save the order |
|
51
|
|
|
self::handleDataFeed($FoxyData_encrypted, $FoxyData_decrypted); |
|
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
// extend to allow for additional integrations with Datafeed |
|
54
|
|
|
$this->extend('addIntegrations', $FoxyData_encrypted); |
|
55
|
|
|
|
|
56
|
|
|
return 'foxy'; |
|
57
|
|
|
} else { |
|
58
|
|
|
return 'No FoxyData or FoxySubscriptionData received.'; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param $encrypted |
|
64
|
|
|
* @param $decrypted |
|
65
|
|
|
* |
|
66
|
|
|
* @throws \SilverStripe\ORM\ValidationException |
|
67
|
|
|
*/ |
|
68
|
|
|
public function handleDataFeed($encrypted, $decrypted) |
|
69
|
|
|
{ |
|
70
|
|
|
$orders = new \SimpleXMLElement($decrypted); |
|
71
|
|
|
|
|
72
|
|
|
// loop over each transaction to find FoxyCart Order ID |
|
73
|
|
|
foreach ($orders->transactions->transaction as $transaction) { |
|
74
|
|
|
// if FoxyCart order id, then parse order |
|
75
|
|
|
if (isset($transaction->id)) { |
|
76
|
|
|
($order = Order::get()->filter('Order_ID', (int)$transaction->id)->First()) ? |
|
|
|
|
|
|
77
|
|
|
$order = Order::get()->filter('Order_ID', (int)$transaction->id)->First() : |
|
78
|
|
|
$order = Order::create(); |
|
79
|
|
|
|
|
80
|
|
|
// save base order info |
|
81
|
|
|
$order->Order_ID = (int)$transaction->id; |
|
82
|
|
|
$order->Response = urlencode($encrypted); |
|
83
|
|
|
$this->parseOrder($orders, $order); |
|
|
|
|
|
|
84
|
|
|
$order->write(); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param array $transactions |
|
91
|
|
|
* @param Order $order |
|
92
|
|
|
*/ |
|
93
|
|
|
public function parseOrder($transactions, $order) |
|
94
|
|
|
{ |
|
95
|
|
|
$this->parseOrderInfo($transactions, $order); |
|
96
|
|
|
$this->parseOrderCustomer($transactions, $order); |
|
97
|
|
|
$this->parseOrderDetails($transactions, $order); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param array $orders |
|
102
|
|
|
* @param Order $transaction |
|
103
|
|
|
*/ |
|
104
|
|
|
public function parseOrderInfo($orders, $transaction) |
|
105
|
|
|
{ |
|
106
|
|
|
foreach ($orders->transactions->transaction as $order) { |
|
107
|
|
|
// Record transaction data from FoxyCart Datafeed: |
|
108
|
|
|
$transaction->Store_ID = (int)$order->store_id; |
|
|
|
|
|
|
109
|
|
|
$transaction->TransactionDate = (string)$order->transaction_date; |
|
|
|
|
|
|
110
|
|
|
$transaction->ProductTotal = (float)$order->product_total; |
|
|
|
|
|
|
111
|
|
|
$transaction->TaxTotal = (float)$order->tax_total; |
|
|
|
|
|
|
112
|
|
|
$transaction->ShippingTotal = (float)$order->shipping_total; |
|
|
|
|
|
|
113
|
|
|
$transaction->OrderTotal = (float)$order->order_total; |
|
|
|
|
|
|
114
|
|
|
$transaction->ReceiptURL = (string)$order->receipt_url; |
|
|
|
|
|
|
115
|
|
|
$transaction->OrderStatus = (string)$order->status; |
|
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param array $orders |
|
121
|
|
|
* @param Order $transaction |
|
122
|
|
|
* @throws \SilverStripe\ORM\ValidationException |
|
123
|
|
|
*/ |
|
124
|
|
|
public function parseOrderCustomer($orders, $transaction) |
|
125
|
|
|
{ |
|
126
|
|
|
foreach ($orders->transactions->transaction as $order) { |
|
127
|
|
|
if (!isset($order->customer_email) || $order->is_anonymous != 0) { |
|
128
|
|
|
continue; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
// if Customer is existing member, associate with current order |
|
132
|
|
|
if (Member::get()->filter('Email', $order->customer_email)->First()) { |
|
133
|
|
|
$customer = Member::get()->filter('Email', $order->customer_email)->First(); |
|
134
|
|
|
/* todo: make sure local password is updated if changed on FoxyCart |
|
135
|
|
|
$this->updatePasswordFromData($customer, $order); |
|
136
|
|
|
*/ |
|
137
|
|
|
} else { |
|
138
|
|
|
// create new Member, set password info from FoxyCart |
|
139
|
|
|
$customer = Member::create(); |
|
140
|
|
|
$customer->Customer_ID = (int)$order->customer_id; |
|
141
|
|
|
$customer->FirstName = (string)$order->customer_first_name; |
|
142
|
|
|
$customer->Surname = (string)$order->customer_last_name; |
|
143
|
|
|
$customer->Email = (string)$order->customer_email; |
|
144
|
|
|
$this->updatePasswordFromData($customer, $order); |
|
145
|
|
|
} |
|
146
|
|
|
$customer->write(); |
|
147
|
|
|
// set Order MemberID |
|
148
|
|
|
$transaction->MemberID = $customer->ID; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Updates a customer's password. Sets password encryption to 'none' to avoid encryting it again. |
|
154
|
|
|
* |
|
155
|
|
|
* @param Member $customer |
|
156
|
|
|
* @param $order |
|
157
|
|
|
*/ |
|
158
|
|
|
public function updatePasswordFromData($customer, $order) |
|
159
|
|
|
{ |
|
160
|
|
|
$password_encryption_algorithm = Security::config()->get('password_encryption_algorithm'); |
|
161
|
|
|
Security::config()->update('password_encryption_algorithm', 'none'); |
|
162
|
|
|
|
|
163
|
|
|
$customer->PasswordEncryption = 'none'; |
|
164
|
|
|
$customer->Password = (string)$order->customer_password; |
|
165
|
|
|
$customer->Salt = (string)$order->customer_password_salt; |
|
166
|
|
|
|
|
167
|
|
|
Security::config()->update('password_encryption_algorithm', $password_encryption_algorithm); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @param array $orders |
|
172
|
|
|
* @param Order $transaction |
|
173
|
|
|
*/ |
|
174
|
|
|
public function parseOrderDetails($orders, $transaction) |
|
175
|
|
|
{ |
|
176
|
|
|
// remove previous OrderDetails so we don't end up with duplicates |
|
177
|
|
|
foreach ($transaction->Details() as $detail) { |
|
178
|
|
|
$detail->delete(); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
foreach ($orders->transactions->transaction as $order) { |
|
182
|
|
|
// Associate ProductPages, Options, Quanity with Order |
|
183
|
|
|
foreach ($order->transaction_details->transaction_detail as $product) { |
|
184
|
|
|
$this->orderDetailFromProduct($product, $transaction); |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* @param $product |
|
191
|
|
|
* @param $transaction |
|
192
|
|
|
*/ |
|
193
|
|
|
public function orderDetailFromProduct($product, $transaction) |
|
194
|
|
|
{ |
|
195
|
|
|
$OrderDetail = OrderDetail::create(); |
|
196
|
|
|
$OrderDetail->Quantity = (int)$product->product_quantity; |
|
197
|
|
|
$OrderDetail->Price = (float)$product->product_price; |
|
198
|
|
|
// Find product via product_id custom variable |
|
199
|
|
|
|
|
200
|
|
|
foreach ($this->getTransactionOptions($product) as $productID) { |
|
201
|
|
|
$productPage = $this->getProductPage($product); |
|
202
|
|
|
$this->modifyOrderDetailPrice($productPage, $OrderDetail, $product); |
|
203
|
|
|
// associate with this order |
|
204
|
|
|
$OrderDetail->OrderID = $transaction->ID; |
|
205
|
|
|
// extend OrderDetail parsing, allowing for recording custom fields from FoxyCart |
|
206
|
|
|
$this->extend('handleOrderItem', $decrypted, $product, $OrderDetail); |
|
207
|
|
|
// write |
|
208
|
|
|
$OrderDetail->write(); |
|
209
|
|
|
} |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* @param $product |
|
214
|
|
|
* @return \Generator |
|
215
|
|
|
*/ |
|
216
|
|
|
public function getTransactionOptions($product) { |
|
217
|
|
|
foreach ($product->transaction_detail_options->transaction_detail_option as $productOption) { |
|
218
|
|
|
yield $productOption; |
|
219
|
|
|
} |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* @param $product |
|
224
|
|
|
* @return bool|ProductPage |
|
225
|
|
|
*/ |
|
226
|
|
|
public function getProductPage($product) |
|
227
|
|
|
{ |
|
228
|
|
|
foreach ($this->getTransactionOptions($product) as $productOptions) { |
|
229
|
|
|
if ($productOptions->product_option_name != 'product_id') { |
|
230
|
|
|
continue; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
return ProductPage::get() |
|
234
|
|
|
->filter('ID', (int) $productOptions->product_option_value) |
|
235
|
|
|
->First(); |
|
236
|
|
|
} |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* @param bool|ProductPage $OrderProduct |
|
241
|
|
|
* @param OrderDetail $OrderDetail |
|
242
|
|
|
*/ |
|
243
|
|
|
public function modifyOrderDetailPrice($OrderProduct, $OrderDetail, $product) |
|
244
|
|
|
{ |
|
245
|
|
|
if (!$OrderProduct) { |
|
246
|
|
|
return; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
$OrderDetail->ProductID = $OrderProduct->ID; |
|
250
|
|
|
|
|
251
|
|
|
foreach ($this->getTransactionOptions($product) as $option) { |
|
252
|
|
|
$OptionItem = OptionItem::get()->filter(array( |
|
253
|
|
|
'ProductID' => (string)$OrderProduct->ID, |
|
254
|
|
|
'Title' => (string)$option->product_option_value |
|
255
|
|
|
))->First(); |
|
256
|
|
|
|
|
257
|
|
|
if (!$OptionItem) { |
|
258
|
|
|
continue; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
$OrderDetail->OptionItems()->add($OptionItem); |
|
262
|
|
|
// modify product price |
|
263
|
|
|
if ($priceMod = $option->price_mod) { |
|
264
|
|
|
$OrderDetail->Price += $priceMod; |
|
265
|
|
|
} |
|
266
|
|
|
} |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* Single Sign on integration with FoxyCart. |
|
271
|
|
|
*/ |
|
272
|
|
|
public function sso() |
|
273
|
|
|
{ |
|
274
|
|
|
|
|
275
|
|
|
// GET variables from FoxyCart Request |
|
276
|
|
|
$fcsid = $this->request->getVar('fcsid'); |
|
277
|
|
|
$timestampNew = strtotime('+30 days'); |
|
278
|
|
|
|
|
279
|
|
|
// get current member if logged in. If not, create a 'fake' user with Customer_ID = 0 |
|
280
|
|
|
// fake user will redirect to FC checkout, ask customer to log in |
|
281
|
|
|
// to do: consider a login/registration form here if not logged in |
|
282
|
|
|
if ($Member = Security::getCurrentUser()) { |
|
|
|
|
|
|
283
|
|
|
$Member = Security::getCurrentUser(); |
|
284
|
|
|
} else { |
|
285
|
|
|
$Member = new Member(); |
|
286
|
|
|
$Member->Customer_ID = 0; |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
$auth_token = sha1($Member->Customer_ID . '|' . $timestampNew . '|' . FoxyCart::getStoreKey()); |
|
|
|
|
|
|
290
|
|
|
|
|
291
|
|
|
$config = FoxyStripeSetting::current_foxystripe_setting(); |
|
292
|
|
|
if ($config->CustomSSL) { |
|
|
|
|
|
|
293
|
|
|
$link = FoxyCart::getFoxyCartStoreName(); |
|
294
|
|
|
} else { |
|
295
|
|
|
$link = FoxyCart::getFoxyCartStoreName() . '.foxycart.com'; |
|
|
|
|
|
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
$redirect_complete = 'https://'.$link.'.foxycart.com/checkout?fc_auth_token='. |
|
|
|
|
|
|
299
|
|
|
$auth_token.'&fcsid='.$fcsid.'&fc_customer_id='.$Member->Customer_ID.'×tamp='.$timestampNew; |
|
300
|
|
|
|
|
301
|
|
|
$this->redirect($redirect_complete); |
|
302
|
|
|
} |
|
303
|
|
|
} |
|
304
|
|
|
|