1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\FoxyStripe\Controller; |
4
|
|
|
|
5
|
|
|
use Dynamic\FoxyStripe\Model\FoxyCart; |
6
|
|
|
use Dynamic\FoxyStripe\Model\FoxyStripeClient; |
7
|
|
|
use Dynamic\FoxyStripe\Model\FoxyStripeSetting; |
8
|
|
|
use Dynamic\FoxyStripe\Model\OptionItem; |
9
|
|
|
use Dynamic\FoxyStripe\Model\Order; |
10
|
|
|
use Dynamic\FoxyStripe\Model\OrderDetail; |
11
|
|
|
use Dynamic\FoxyStripe\Page\ProductPage; |
12
|
|
|
use SilverStripe\Control\HTTPRequest; |
13
|
|
|
use SilverStripe\ORM\Queries\SQLUpdate; |
14
|
|
|
use SilverStripe\Security\Member; |
15
|
|
|
use SilverStripe\Security\Security; |
16
|
|
|
|
17
|
|
|
class FoxyStripeController extends \PageController |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* |
21
|
|
|
*/ |
22
|
|
|
const URLSEGMENT = 'foxystripe'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
private static $allowed_actions = [ |
|
|
|
|
28
|
|
|
'index', |
29
|
|
|
'sso', |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return string |
34
|
|
|
*/ |
35
|
|
|
public function getURLSegment() |
36
|
|
|
{ |
37
|
|
|
return self::URLSEGMENT; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return string |
42
|
|
|
* |
43
|
|
|
* @throws \SilverStripe\ORM\ValidationException |
44
|
|
|
*/ |
45
|
|
|
public function index() |
46
|
|
|
{ |
47
|
|
|
$request = $this->getRequest(); |
48
|
|
|
|
49
|
|
|
$this->processFoxyRequest($request); |
50
|
|
|
|
51
|
|
|
if ($request->postVar('FoxyData') || $request->postVar('FoxySubscriptionData')) { |
52
|
|
|
$this->processFoxyRequest($request); |
53
|
|
|
|
54
|
|
|
return 'foxy'; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return 'No FoxyData or FoxySubscriptionData received.'; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Process a request after a transaction is completed via Foxy |
62
|
|
|
* |
63
|
|
|
* @param HTTPRequest $request |
64
|
|
|
*/ |
65
|
|
|
protected function processFoxyRequest(HTTPRequest $request) |
66
|
|
|
{ |
67
|
|
|
$encryptedData = $request->postVar('FoxyData') ?: $request->postVar('FoxySubscriptionData'); |
|
|
|
|
68
|
|
|
|
69
|
|
|
$encryptedData = \rc4crypt::encrypt(FoxyCart::getStoreKey(), |
|
|
|
|
70
|
|
|
file_get_contents('/Users/nichorstmeier/Sites/foxystripe.test/vendor/dynamic/foxystripe/sample_data/non-multiship.xml')); |
71
|
|
|
|
72
|
|
|
$decryptedData = $this->decryptFeedData($encryptedData); |
73
|
|
|
|
74
|
|
|
$this->parseFeedData($encryptedData, $decryptedData); |
75
|
|
|
|
76
|
|
|
$this->extend('addIntegrations', $encryptedData); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Decrypt the XML data feed from Foxy |
81
|
|
|
* |
82
|
|
|
* @param $data |
83
|
|
|
* @return string |
84
|
|
|
* @throws \SilverStripe\ORM\ValidationException |
85
|
|
|
*/ |
86
|
|
|
private function decryptFeedData($data) |
87
|
|
|
{ |
88
|
|
|
return \rc4crypt::decrypt(FoxyCart::getStoreKey(), $data); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Parse the XML data feed from Foxy to a SimpleXMLElement object |
93
|
|
|
* |
94
|
|
|
* @param $encrypted |
95
|
|
|
* @param $decrypted |
96
|
|
|
* |
97
|
|
|
* @throws \SilverStripe\ORM\ValidationException |
98
|
|
|
*/ |
99
|
|
|
private function parseFeedData($encryptedData, $decryptedData) |
100
|
|
|
{ |
101
|
|
|
$orders = new \SimpleXMLElement($decryptedData); |
102
|
|
|
|
103
|
|
|
// loop over each transaction to find FoxyCart Order ID |
104
|
|
|
foreach ($orders->transactions->transaction as $transaction) { |
105
|
|
|
$this->processTransaction($transaction, $encryptedData); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param $transaction |
111
|
|
|
* @return bool |
112
|
|
|
* @throws \SilverStripe\ORM\ValidationException |
113
|
|
|
*/ |
114
|
|
|
private function processTransaction($transaction, $encryptedData) |
115
|
|
|
{ |
116
|
|
|
if (!isset($transaction->id)) { |
117
|
|
|
return false; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
if (!$order = Order::get()->filter('Order_ID', (int)$transaction->id)->first()) { |
|
|
|
|
121
|
|
|
$order = Order::create(); |
122
|
|
|
$order->Order_ID = (int)$transaction->id; |
123
|
|
|
$order->Response = urlencode($encryptedData); |
124
|
|
|
$order->write(); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Single Sign on integration with FoxyCart. |
130
|
|
|
*/ |
131
|
|
|
public function sso() |
132
|
|
|
{ |
133
|
|
|
// GET variables from FoxyCart Request |
134
|
|
|
$fcsid = $this->request->getVar('fcsid'); |
135
|
|
|
$timestampNew = strtotime('+30 days'); |
136
|
|
|
|
137
|
|
|
// get current member if logged in. If not, create a 'fake' user with Customer_ID = 0 |
138
|
|
|
// fake user will redirect to FC checkout, ask customer to log in |
139
|
|
|
// to do: consider a login/registration form here if not logged in |
140
|
|
|
if (!$Member = Security::getCurrentUser()) { |
141
|
|
|
$Member = new Member(); |
142
|
|
|
$Member->Customer_ID = 0; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$auth_token = sha1($Member->Customer_ID . '|' . $timestampNew . '|' . FoxyCart::getStoreKey()); |
|
|
|
|
146
|
|
|
|
147
|
|
|
$config = FoxyStripeSetting::current_foxystripe_setting(); |
148
|
|
|
if ($config->CustomSSL) { |
|
|
|
|
149
|
|
|
$link = FoxyCart::getFoxyCartStoreName(); |
150
|
|
|
} else { |
151
|
|
|
$link = FoxyCart::getFoxyCartStoreName() . '.foxycart.com'; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$redirect_complete = 'https://' . $link . '/checkout?fc_auth_token=' . $auth_token . '&fcsid=' . $fcsid . |
155
|
|
|
'&fc_customer_id=' . $Member->Customer_ID . '×tamp=' . $timestampNew; |
156
|
|
|
|
157
|
|
|
$this->redirect($redirect_complete); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|