1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\FoxyStripe\Controller; |
4
|
|
|
|
5
|
|
|
use Dynamic\FoxyStripe\Model\FoxyCart; |
6
|
|
|
use Dynamic\FoxyStripe\Model\Order; |
7
|
|
|
use SilverStripe\Control\Controller; |
8
|
|
|
use SilverStripe\Control\Director; |
9
|
|
|
use SilverStripe\Core\Injector\Injector; |
10
|
|
|
use SilverStripe\Dev\DebugView; |
11
|
|
|
use SilverStripe\ORM\ArrayList; |
12
|
|
|
use SilverStripe\Security\Member; |
13
|
|
|
use SilverStripe\Security\PasswordEncryptor; |
14
|
|
|
use SilverStripe\View\ArrayData; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class DataTestController |
18
|
|
|
* @package Dynamic\FoxyStripe\Controller |
19
|
|
|
*/ |
20
|
|
|
class DataTestController extends Controller |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
private static $data = [ |
|
|
|
|
27
|
|
|
"TransactionDate" => "now", |
28
|
|
|
"OrderID" => "auto", |
29
|
|
|
"Email" => "auto", |
30
|
|
|
"Password" => "password", |
31
|
|
|
"OrderDetails" => [], |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @throws \SilverStripe\ORM\ValidationException |
36
|
|
|
*/ |
37
|
|
|
public function index() |
38
|
|
|
{ |
39
|
|
|
$rules = Director::config()->get('rules'); |
40
|
|
|
$rule = array_search(FoxyStripeController::class, $rules); |
41
|
|
|
$myURL = Director::absoluteBaseURL() . explode('//', $rule)[0]; |
|
|
|
|
42
|
|
|
$myKey = FoxyCart::getStoreKey(); |
43
|
|
|
|
44
|
|
|
$this->updateConfig(); |
45
|
|
|
$config = static::config()->get('data'); |
46
|
|
|
$config['OrderDetails'] = ArrayList::create($config['OrderDetails']); |
47
|
|
|
$xml = $this->renderWith('TestData', $config); |
48
|
|
|
$XMLOutput = $xml->RAW(); |
49
|
|
|
|
50
|
|
|
$XMLOutput_encrypted = \rc4crypt::encrypt($myKey, $XMLOutput); |
|
|
|
|
51
|
|
|
$XMLOutput_encrypted = urlencode($XMLOutput_encrypted); |
52
|
|
|
|
53
|
|
|
$ch = curl_init(); |
54
|
|
|
curl_setopt($ch, CURLOPT_URL, $myURL); |
55
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, array("FoxyData" => $XMLOutput_encrypted)); |
56
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
57
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 30); |
58
|
|
|
|
59
|
|
|
$response = curl_exec($ch); |
60
|
|
|
$responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
|
|
|
|
61
|
|
|
curl_close($ch); |
62
|
|
|
|
63
|
|
|
$configString = print_r(static::config()->get('data'), true); |
64
|
|
|
/** @var DebugView $view */ |
65
|
|
|
$view = Injector::inst()->create(DebugView::class); |
66
|
|
|
echo $view->renderHeader(); |
67
|
|
|
echo '<div class="info">'; |
68
|
|
|
echo "<h2>Config:</h2><pre>$configString</pre>"; |
69
|
|
|
if ($this->getRequest()->getVar('data')) { |
70
|
|
|
echo "<h2>Data:</h2><pre>{$xml->HTML()}</pre>"; |
71
|
|
|
} |
72
|
|
|
echo "<h2>Response:</h2><pre>$response</pre>"; |
73
|
|
|
echo '<p></p>'; |
74
|
|
|
echo '</div>'; |
75
|
|
|
echo $view->renderFooter(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* |
80
|
|
|
*/ |
81
|
|
|
private function updateConfig() |
82
|
|
|
{ |
83
|
|
|
$data = static::config()->get('data'); |
84
|
|
|
$transactionDate = $data['TransactionDate']; |
85
|
|
|
static::config()->merge('data', [ |
86
|
|
|
'TransactionDate' => strtotime($transactionDate), |
87
|
|
|
]); |
88
|
|
|
|
89
|
|
|
$order_id = $data['OrderID']; |
90
|
|
|
if ($order_id === 'auto' || $order_id < 1) { |
91
|
|
|
$lastOrderID = Order::get()->sort('Order_ID')->last()->Order_ID; |
92
|
|
|
static::config()->merge('data', [ |
93
|
|
|
'OrderID' => $lastOrderID + 1, |
94
|
|
|
]); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$email = $data['Email']; |
98
|
|
|
if ($email === 'auto') { |
99
|
|
|
static::config()->merge('data', [ |
100
|
|
|
'Email' => $this->generateEmail(), |
101
|
|
|
]); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$orderDetails = $data['OrderDetails']; |
105
|
|
|
if (count($orderDetails) === 0) { |
106
|
|
|
static::config()->merge('data', [ |
107
|
|
|
'OrderDetails' => [ |
108
|
|
|
$this->generateOrderDetail() |
109
|
|
|
], |
110
|
|
|
]); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if (!array_key_exists('Salt', $data)) { |
114
|
|
|
static::config()->merge('data', [ |
115
|
|
|
'Salt' => 'faGgWXUTdZ7i42lpA6cljzKeGBeUwShBSNHECwsJmt', |
116
|
|
|
]); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
if (!array_key_exists('HashType', $data)) { |
120
|
|
|
static::config()->merge('data', [ |
121
|
|
|
'HashType' => 'sha1_v2.4', |
122
|
|
|
]); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$data = static::config()->get('data'); |
126
|
|
|
if (!array_key_exists('HashedPassword', $data)) { |
127
|
|
|
$encryptor = PasswordEncryptor::create_for_algorithm($data['HashType']); |
128
|
|
|
static::config()->merge('data', [ |
129
|
|
|
'HashedPassword' => $encryptor->encrypt($data['Password'], $data['Salt']), |
130
|
|
|
]); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
|
|
private function generateEmail() |
138
|
|
|
{ |
139
|
|
|
$emails = Member::get()->filter([ |
140
|
|
|
'Email:EndsWith' => '@example.com', |
141
|
|
|
])->column('Email'); |
142
|
|
|
|
143
|
|
|
if ($emails && count($emails)) { |
144
|
|
|
$email = $emails[count($emails) - 1]; |
145
|
|
|
return preg_replace_callback( |
146
|
|
|
"|(\d+)|", |
147
|
|
|
function ($mathces) { |
148
|
|
|
return ++$mathces[1]; |
149
|
|
|
}, |
150
|
|
|
$email |
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
return '[email protected]'; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return array |
158
|
|
|
*/ |
159
|
|
|
private function generateOrderDetail() |
160
|
|
|
{ |
161
|
|
|
return [ |
162
|
|
|
'Title' => 'foo', |
163
|
|
|
'Price' => 20.00, |
164
|
|
|
'Quantity' => 1, |
165
|
|
|
'Weight' => 0.1, |
166
|
|
|
'DeliveryType' => 'shipped', |
167
|
|
|
'CategoryDescription' => 'Default cateogry', |
168
|
|
|
'CategoryCode' => 'DEFAULT', |
169
|
|
|
'Options' => [ |
170
|
|
|
'Name' => 'color', |
171
|
|
|
'OptionValue' => 'blue', |
172
|
|
|
'PriceMod' => '', |
173
|
|
|
'WeightMod' => '', |
174
|
|
|
], |
175
|
|
|
]; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|