1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\FoxyStripe\Controller; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Dynamic\FoxyStripe\Model\FoxyCart; |
7
|
|
|
use Dynamic\FoxyStripe\Model\Order; |
8
|
|
|
use SilverStripe\Control\Controller; |
9
|
|
|
use SilverStripe\Control\Director; |
10
|
|
|
use SilverStripe\Core\Config\Config; |
11
|
|
|
use SilverStripe\Core\Injector\Injector; |
12
|
|
|
use SilverStripe\Dev\DebugView; |
13
|
|
|
use SilverStripe\Security\Member; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class DataTestController |
17
|
|
|
* @package Dynamic\FoxyStripe\Controller |
18
|
|
|
*/ |
19
|
|
|
class DataTestController extends Controller |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
private static $data = [ |
|
|
|
|
23
|
|
|
"TransactionDate" => "now", |
24
|
|
|
"OrderID" => "auto", |
25
|
|
|
"Email"=> "auto", |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @throws \SilverStripe\ORM\ValidationException |
30
|
|
|
*/ |
31
|
|
|
public function index() |
32
|
|
|
{ |
33
|
|
|
$rules = Director::config()->get('rules'); |
34
|
|
|
$rule = array_search(FoxyStripeController::class, $rules); |
35
|
|
|
$myURL = Director::absoluteBaseURL() . explode('//', $rule)[0]; |
|
|
|
|
36
|
|
|
$myKey = FoxyCart::getStoreKey(); |
37
|
|
|
|
38
|
|
|
$this->updateConfig(); |
39
|
|
|
|
40
|
|
|
$config = static::config()->get('data'); |
41
|
|
|
$XMLOutput = $this->renderWith( |
42
|
|
|
'TestData', |
43
|
|
|
$config |
44
|
|
|
)->RAW(); |
45
|
|
|
|
46
|
|
|
$XMLOutput_encrypted = \rc4crypt::encrypt($myKey, $XMLOutput); |
|
|
|
|
47
|
|
|
$XMLOutput_encrypted = urlencode($XMLOutput_encrypted); |
48
|
|
|
|
49
|
|
|
$ch = curl_init(); |
50
|
|
|
curl_setopt($ch, CURLOPT_URL, $myURL); |
51
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, array("FoxyData" => $XMLOutput_encrypted)); |
52
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
53
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 30); |
54
|
|
|
|
55
|
|
|
$response = curl_exec($ch); |
56
|
|
|
$responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
|
|
|
|
57
|
|
|
curl_close($ch); |
58
|
|
|
|
59
|
|
|
$configString = print_r($config, true); |
60
|
|
|
/** @var DebugView $view */ |
61
|
|
|
$view = Injector::inst()->create(DebugView::class); |
62
|
|
|
echo $view->renderHeader(); |
63
|
|
|
echo '<div class="info">'; |
64
|
|
|
echo "<h2>Data: </h2><pre>{$configString}</pre>"; |
65
|
|
|
echo "<h2>Response: </h2><pre>$response</pre>"; |
66
|
|
|
echo '<p></p>'; |
67
|
|
|
echo '</div>'; |
68
|
|
|
echo $view->renderFooter(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* |
73
|
|
|
*/ |
74
|
|
|
private function updateConfig() |
75
|
|
|
{ |
76
|
|
|
$transaction_date = static::config()->get('data')['TransactionDate']; |
77
|
|
|
static::config()->merge('data', [ |
78
|
|
|
'TransactionDate' => strtotime($transaction_date), |
79
|
|
|
]); |
80
|
|
|
|
81
|
|
|
$order_id = static::config()->get('data')['OrderID']; |
82
|
|
|
if ($order_id === 'auto' || $order_id < 1) { |
83
|
|
|
$lastOrderID = Order::get()->sort('Order_ID')->last()->Order_ID; |
84
|
|
|
static::config()->merge('data', [ |
85
|
|
|
'OrderID' => $lastOrderID + 1, |
86
|
|
|
]); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$email = static::config()->get('data')['Email']; |
90
|
|
|
if ($email === 'auto') { |
91
|
|
|
static::config()->merge('data', [ |
92
|
|
|
'Email' => $this->generateEmail(), |
93
|
|
|
]); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
private function generateEmail() |
101
|
|
|
{ |
102
|
|
|
$emails = Member::get()->filter([ |
103
|
|
|
'Email:EndsWith' => '@example.com', |
104
|
|
|
])->column('Email'); |
105
|
|
|
|
106
|
|
|
if ($emails && count($emails)) { |
107
|
|
|
$email = $emails[count($emails) - 1]; |
108
|
|
|
return preg_replace_callback( |
109
|
|
|
"|(\d+)|", |
110
|
|
|
function($mathces) { |
111
|
|
|
return ++$mathces[1]; |
112
|
|
|
}, |
113
|
|
|
$email); |
114
|
|
|
} |
115
|
|
|
return '[email protected]'; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|