Completed
Push — master ( 06a496...be22d6 )
by Will
16s
created

CheckoutPageController::getViewer()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 2
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverShop\Page;
4
5
use PageController;
0 ignored issues
show
Bug introduced by Sam Minnée
The type PageController was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use SilverShop\Cart\ShoppingCart;
7
use SilverShop\Checkout\CheckoutComponentConfig;
8
use SilverShop\Checkout\Component\OnsitePayment;
9
use SilverShop\Checkout\SinglePageCheckoutComponentConfig;
10
use SilverShop\Checkout\Step\Address;
11
use SilverShop\Checkout\Step\AddressBook;
12
use SilverShop\Checkout\Step\ContactDetails;
13
use SilverShop\Checkout\Step\Membership;
14
use SilverShop\Checkout\Step\PaymentMethod;
15
use SilverShop\Checkout\Step\Summary;
16
use SilverShop\Extension\SteppedCheckoutExtension;
17
use SilverShop\Extension\ViewableCartExtension;
18
use SilverShop\Forms\PaymentForm;
19
use SilverStripe\Forms\FieldList;
20
use SilverStripe\Forms\FormAction;
21
use SilverStripe\Omnipay\Model\Message\GatewayErrorMessage;
22
23
/**
24
 * @package shop
25
 * @mixin CheckoutPage
26
 * @mixin SteppedCheckoutExtension
27
 * @mixin Address
28
 * @mixin AddressBook
29
 * @mixin ContactDetails
30
 * @mixin Membership
31
 * @mixin PaymentMethod
32
 * @mixin Summary
33
 * @mixin ViewableCartExtension
34
 */
35
class CheckoutPageController extends PageController
36
{
37
    private static $url_segment     = 'checkout';
0 ignored issues
show
introduced by Roman Schmid
The private property $url_segment is not used, and could be removed.
Loading history...
38
39
    private static $allowed_actions = array(
0 ignored issues
show
introduced by Roman Schmid
The private property $allowed_actions is not used, and could be removed.
Loading history...
40
        'OrderForm',
41
        'payment',
42
        'PaymentForm',
43
    );
44
45
    public function Title()
46
    {
47
        if ($this->failover && $this->failover->Title) {
48
            return $this->failover->Title;
49
        }
50
51
        return _t('SilverShop\Page\CheckoutPage.DefaultTitle', "Checkout");
52
    }
53
54
    public function OrderForm()
55
    {
56
        if (!(bool)$this->Cart()) {
57
            return false;
58
        }
59
60
        /**
61
         * @var CheckoutComponentConfig $config
62
         */
63
        $config = SinglePageCheckoutComponentConfig::create(ShoppingCart::curr());
64
        $form = PaymentForm::create($this, 'OrderForm', $config);
65
66
        // Normally, the payment is on a second page, either offsite or through /checkout/payment
67
        // If the site has customised the checkout component config to include an onsite payment
68
        // component, we should honor that and change the button label. PaymentForm::checkoutSubmit
69
        // will also check this and process payment if needed.
70
        if ($config->hasComponentWithPaymentData()) {
71
            $form->setActions(
72
                FieldList::create(
73
                    FormAction::create('checkoutSubmit', _t('SilverShop\Page\CheckoutPage.SubmitPayment', 'Submit Payment'))
74
                )
75
            );
76
        }
77
78
        $form->Cart = $this->Cart();
79
        $this->extend('updateOrderForm', $form);
80
81
        return $form;
82
    }
83
84
    /**
85
     * Action for making on-site payments
86
     */
87
    public function payment()
88
    {
89
        if (!$this->Cart()) {
90
            return $this->redirect($this->Link());
91
        }
92
93
        return array(
94
            'Title'     => 'Make Payment',
95
            'OrderForm' => $this->PaymentForm(),
96
        );
97
    }
98
99
    public function PaymentForm()
100
    {
101
        if (!(bool)$this->Cart()) {
102
            return false;
103
        }
104
105
        $config = CheckoutComponentConfig::create(ShoppingCart::curr(), false);
106
        $config->addComponent(OnsitePayment::create());
107
108
        $form = PaymentForm::create($this, "PaymentForm", $config);
109
110
        $form->setActions(
111
            FieldList::create(
112
                FormAction::create("submitpayment", _t('SilverShop\Page\CheckoutPage.SubmitPayment', "Submit Payment"))
113
            )
114
        );
115
116
        $form->setFailureLink($this->Link());
117
        $this->extend('updatePaymentForm', $form);
118
119
        return $form;
120
    }
121
122
    /**
123
     * Retrieves error messages for the latest payment (if existing).
124
     * This can originate e.g. from an earlier offsite gateway API response.
125
     *
126
     * @return string
127
     */
128
    public function PaymentErrorMessage()
129
    {
130
        $order = $this->Cart();
131
        if (!$order) {
132
            return false;
0 ignored issues
show
Bug Best Practice introduced by Roman Schmid
The expression return false returns the type false which is incompatible with the documented return type string.
Loading history...
133
        }
134
135
        $lastPayment = $order->Payments()->sort('Created', 'DESC')->first();
136
        if (!$lastPayment) {
137
            return false;
0 ignored issues
show
Bug Best Practice introduced by Roman Schmid
The expression return false returns the type false which is incompatible with the documented return type string.
Loading history...
138
        }
139
140
        $errorMessages = $lastPayment->Messages()->exclude('Message', '')->sort('Created', 'DESC');
141
        $lastErrorMessage = null;
142
        foreach ($errorMessages as $errorMessage) {
143
            if ($errorMessage instanceof GatewayErrorMessage) {
144
                $lastErrorMessage = $errorMessage;
145
                break;
146
            }
147
        }
148
        if (!$lastErrorMessage) {
149
            return false;
0 ignored issues
show
Bug Best Practice introduced by Roman Schmid
The expression return false returns the type false which is incompatible with the documented return type string.
Loading history...
150
        }
151
152
        return $lastErrorMessage->Message;
153
    }
154
    
155
    /**
156
     * Override viewer to get correct template for first step
157
     * 
158
     * {@inheritDoc}
159
     * @see \SilverStripe\CMS\Controllers\ContentController::getViewer()
160
     */
161
    public function getViewer($action)
162
    {
163
        if (CheckoutPage::config()->first_step && $action == 'index') {
164
            $action = CheckoutPage::config()->first_step;
165
        }
166
        return parent::getViewer($action);
167
    }
168
}
169