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