1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use SilverStripe\Omnipay\GatewayInfo; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Perform actions on placed orders |
7
|
|
|
* |
8
|
|
|
* @package shop |
9
|
|
|
* @subpackage forms |
10
|
|
|
*/ |
11
|
|
|
class OrderActionsForm extends Form |
12
|
|
|
{ |
13
|
|
|
private static $allowed_actions = array( |
|
|
|
|
14
|
|
|
'docancel', |
15
|
|
|
'dopayment', |
16
|
|
|
'httpsubmission', |
17
|
|
|
); |
18
|
|
|
|
19
|
|
|
private static $email_notification = false; |
20
|
|
|
|
21
|
|
|
private static $allow_paying = true; |
22
|
|
|
|
23
|
|
|
private static $allow_cancelling = true; |
24
|
|
|
|
25
|
|
|
protected $order; |
26
|
|
|
|
27
|
1 |
|
public function __construct($controller, $name, Order $order) |
28
|
|
|
{ |
29
|
1 |
|
$this->order = $order; |
30
|
1 |
|
$fields = FieldList::create( |
31
|
1 |
|
HiddenField::create('OrderID', '', $order->ID) |
32
|
1 |
|
); |
33
|
1 |
|
$actions = FieldList::create(); |
34
|
|
|
//payment |
35
|
1 |
|
if (self::config()->allow_paying && $order->canPay()) { |
36
|
1 |
|
$gateways = GatewayInfo::getSupportedGateways(); |
37
|
|
|
//remove manual gateways |
38
|
1 |
|
foreach ($gateways as $gateway => $gatewayname) { |
39
|
1 |
|
if (GatewayInfo::isManual($gateway)) { |
40
|
1 |
|
unset($gateways[$gateway]); |
41
|
1 |
|
} |
42
|
1 |
|
} |
43
|
1 |
|
if (!empty($gateways)) { |
44
|
1 |
|
$fields->push( |
45
|
1 |
|
HeaderField::create( |
46
|
1 |
|
"MakePaymentHeader", |
47
|
1 |
|
_t("OrderActionsForm.MakePayment", "Make Payment") |
48
|
1 |
|
) |
49
|
1 |
|
); |
50
|
1 |
|
$outstandingfield = Currency::create(); |
51
|
1 |
|
$outstandingfield->setValue($order->TotalOutstanding(true)); |
52
|
1 |
|
$fields->push( |
53
|
1 |
|
LiteralField::create( |
54
|
1 |
|
"Outstanding", |
55
|
1 |
|
_t( |
56
|
1 |
|
'Order.OutstandingWithAmount', |
57
|
1 |
|
'Outstanding: {Amount}', |
58
|
1 |
|
'', |
59
|
1 |
|
array('Amount' => $outstandingfield->Nice()) |
|
|
|
|
60
|
1 |
|
) |
61
|
1 |
|
) |
62
|
1 |
|
); |
63
|
1 |
|
$fields->push( |
64
|
1 |
|
OptionsetField::create( |
65
|
1 |
|
'PaymentMethod', |
66
|
1 |
|
_t("OrderActionsForm.PaymentMethod", "Payment Method"), |
67
|
1 |
|
$gateways, |
68
|
1 |
|
key($gateways) |
69
|
1 |
|
) |
70
|
1 |
|
); |
71
|
|
|
|
72
|
1 |
|
$actions->push( |
73
|
1 |
|
FormAction::create( |
74
|
1 |
|
'dopayment', |
75
|
1 |
|
_t('OrderActionsForm.PayOrder', 'Pay outstanding balance') |
76
|
1 |
|
) |
77
|
1 |
|
); |
78
|
1 |
|
} |
79
|
1 |
|
} |
80
|
|
|
//cancelling |
81
|
1 |
|
if (self::config()->allow_cancelling && $order->canCancel()) { |
82
|
1 |
|
$actions->push( |
83
|
1 |
|
FormAction::create( |
84
|
1 |
|
'docancel', |
85
|
1 |
|
_t('OrderActionsForm.CancelOrder', 'Cancel this order') |
86
|
1 |
|
) |
87
|
1 |
|
); |
88
|
1 |
|
} |
89
|
1 |
|
parent::__construct($controller, $name, $fields, $actions); |
90
|
1 |
|
$this->extend("updateForm", $order); |
91
|
1 |
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Make payment for a place order, where payment had previously failed. |
95
|
|
|
* |
96
|
|
|
* @param array $data |
97
|
|
|
* @param Form $form |
98
|
|
|
* |
99
|
|
|
* @return boolean |
100
|
|
|
*/ |
101
|
1 |
|
public function dopayment($data, $form) |
|
|
|
|
102
|
|
|
{ |
103
|
1 |
|
if (self::config()->allow_paying |
104
|
1 |
|
&& $this->order |
105
|
1 |
|
&& $this->order->canPay() |
106
|
1 |
|
) { |
107
|
|
|
// Save payment data from form and process payment |
108
|
1 |
|
$data = $form->getData(); |
109
|
1 |
|
$gateway = (!empty($data['PaymentMethod'])) ? $data['PaymentMethod'] : null; |
110
|
|
|
|
111
|
1 |
|
if (!GatewayInfo::isManual($gateway)) { |
112
|
|
|
/** @var OrderProcessor $processor */ |
113
|
1 |
|
$processor = OrderProcessor::create($this->order); |
114
|
1 |
|
$response = $processor->makePayment($gateway, $data, $processor->getReturnUrl()); |
115
|
1 |
|
if($response && !$response->isError()){ |
116
|
|
|
return $response->redirectOrRespond(); |
|
|
|
|
117
|
|
|
} else { |
118
|
1 |
|
$form->sessionMessage($processor->getError(), 'bad'); |
119
|
|
|
} |
120
|
1 |
|
} else { |
121
|
|
|
$form->sessionMessage(_t('OrderActionsForm.ManualNotAllowed', "Manual payment not allowed"), 'bad'); |
122
|
|
|
} |
123
|
|
|
|
124
|
1 |
|
return $this->controller->redirectBack(); |
125
|
|
|
} |
126
|
|
|
$form->sessionMessage( |
127
|
|
|
_t('OrderForm.CouldNotProcessPayment', 'Payment could not be processed.'), |
128
|
|
|
'bad' |
129
|
|
|
); |
130
|
|
|
$this->controller->redirectBack(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Form action handler for CancelOrderForm. |
135
|
|
|
* |
136
|
|
|
* Take the order that this was to be change on, |
137
|
|
|
* and set the status that was requested from |
138
|
|
|
* the form request data. |
139
|
|
|
* |
140
|
|
|
* @param array $data The form request data submitted |
141
|
|
|
* @param Form $form The {@link Form} this was submitted on |
142
|
|
|
*/ |
143
|
|
|
public function docancel($data, $form) |
|
|
|
|
144
|
|
|
{ |
145
|
|
|
if (self::config()->allow_cancelling |
146
|
|
|
&& $this->order->canCancel() |
147
|
|
|
) { |
148
|
|
|
$this->order->Status = 'MemberCancelled'; |
149
|
|
|
$this->order->write(); |
150
|
|
|
|
151
|
|
|
if (self::config()->email_notification) { |
152
|
|
|
OrderEmailNotifier::create($this->order)->sendCancelNotification(); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
$this->controller->sessionMessage( |
156
|
|
|
_t("OrderForm.OrderCancelled", "Order sucessfully cancelled"), |
157
|
|
|
'warning' |
158
|
|
|
); |
159
|
|
|
if (Member::currentUser() && $link = $this->order->Link()) { |
160
|
|
|
$this->controller->redirect($link); |
161
|
|
|
} else { |
162
|
|
|
$this->controller->redirectBack(); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|