Completed
Push — 2.0 ( 7f87f2...afdd14 )
by Roman
16:48
created

OrderActionsForm::dopayment()   C

Complexity

Conditions 8
Paths 7

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 10.3696

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 31
ccs 14
cts 21
cp 0.6667
rs 5.3846
cc 8
eloc 20
nc 7
nop 2
crap 10.3696
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(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
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())
0 ignored issues
show
Documentation introduced by
array('Amount' => $outstandingfield->Nice()) is of type array<string,?,{"Amount":"?"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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)
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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();
0 ignored issues
show
Bug Compatibility introduced by
The expression $response->redirectOrRespond(); of type SS_HTTPResponse|null adds the type SS_HTTPResponse to the return on line 116 which is incompatible with the return type documented by OrderActionsForm::dopayment of type boolean.
Loading history...
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)
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $form is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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