Completed
Push — master ( bb4762...ee0943 )
by Roman
15:21
created

GridFieldRefundAction   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 11

Importance

Changes 0
Metric Value
wmc 12
lcom 0
cbo 11
dl 0
loc 113
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getActions() 0 4 1
B getColumnContent() 0 52 6
A handleAction() 0 28 5
1
<?php
2
namespace Bummzack\SsOmnipayUI\GridField;
3
4
use SilverStripe\Forms\GridField\GridField;
5
use SilverStripe\Forms\GridField\GridField_ActionProvider;
6
use SilverStripe\Forms\GridField\GridField_FormAction;
7
use SilverStripe\Omnipay\GatewayInfo;
8
use SilverStripe\Omnipay\Model\Payment;
9
use SilverStripe\Omnipay\Service\ServiceFactory;
10
use SilverStripe\Omnipay\Exception\Exception;
11
use SilverStripe\ORM\DataObject;
12
use SilverStripe\ORM\FieldType\DBMoney;
13
use SilverStripe\ORM\ValidationException;
14
use SilverStripe\View\Requirements;
15
16
/**
17
 * A GridField button that can be used to refund a payment
18
 *
19
 * @package SilverStripe\Omnipay\Admin\GridField
20
 */
21
class GridFieldRefundAction extends GridFieldPaymentAction implements GridField_ActionProvider
22
{
23
    /**
24
     * Which GridField actions are this component handling
25
     *
26
     * @param GridField $gridField
27
     * @return array
28
     */
29
    public function getActions($gridField)
30
    {
31
        return array('refundpayment');
32
    }
33
34
    /**
35
     *
36
     * @param GridField $gridField
37
     * @param DataObject $record
38
     * @param string $columnName
39
     * @return string|null - the HTML for the column
40
     */
41
    public function getColumnContent($gridField, $record, $columnName)
42
    {
43
        if (!($record instanceof Payment)) {
44
            return null;
45
        }
46
47
        if (!$record->canRefund()) {
48
            return null;
49
        }
50
51
        Requirements::css('bummzack/silverstripe-omnipay-ui: client/dist/css/omnipay-ui-cms.css');
52
        Requirements::javascript('bummzack/silverstripe-omnipay-ui: client/dist/javascript/omnipay-ui-cms.js');
53
        Requirements::add_i18n_javascript('bummzack/silverstripe-omnipay-ui: client/dist/javascript/lang');
54
55
        $infoText = '';
56
        switch (GatewayInfo::refundMode($record->Gateway)) {
57
            case GatewayInfo::MULTIPLE:
58
                $infoText = 'MultiRefundInfo';
59
                break;
60
            case GatewayInfo::PARTIAL:
61
                $infoText = 'SingleRefundInfo';
62
                break;
63
            case GatewayInfo::FULL:
64
                $infoText = 'FullRefundInfo';
65
                break;
66
        }
67
68
        /** @var DBMoney $money */
69
        $money = $record->dbObject('Money');
70
71
        /** @var GridField_FormAction $field */
72
        $field = GridField_FormAction::create(
73
            $gridField,
74
            'RefundPayment' . $record->ID,
75
            false,
76
            'refundpayment',
77
            array('RecordID' => $record->ID)
78
        )
79
            ->addExtraClass('gridfield-button-refund payment-dialog-button')
80
            ->setAttribute('title', _t('GridFieldRefundAction.Title', 'Refund Payment'))
81
            ->setAttribute('data-icon', 'button-refund')
82
            ->setAttribute('data-dialog', json_encode(array(
83
                'maxAmount' => $money->Nice(),
84
                'maxAmountNum' => $money->getAmount(),
85
                'hasAmountField' => $record->canRefund(null, true),
86
                'infoTextKey' => $infoText,
87
                'buttonTextKey' => 'RefundAmount'
88
            )))
89
            ->setDescription(_t('GridFieldRefundAction.Description', 'Refund a captured payment'));
90
91
        return $field->Field();
92
    }
93
94
    /**
95
     * Handle the actions and apply any changes to the GridField
96
     *
97
     * @param GridField $gridField
98
     * @param string $actionName
99
     * @param mixed $arguments
100
     * @param array $data - form data
101
     * @return void
102
     * @throws ValidationException when there was an error
103
     * @throws \SilverStripe\Omnipay\Exception\InvalidConfigurationException
104
     */
105
    public function handleAction(GridField $gridField, $actionName, $arguments, $data)
106
    {
107
        if ($actionName == 'refundpayment') {
108
            $item = $gridField->getList()->byID($arguments['RecordID']);
109
            if (!($item instanceof Payment)) {
110
                return;
111
            }
112
113
            $serviceData = array_intersect_key($data, array('amount' => null));
114
115
            /** @var ServiceFactory $factory */
116
            $factory = ServiceFactory::create();
117
            $refundService = $factory->getService($item, ServiceFactory::INTENT_REFUND);
118
119
            try {
120
                $serviceResponse = $refundService->initiate($serviceData);
121
            } catch (Exception $ex) {
122
                throw new ValidationException($ex->getMessage(), 0);
123
            }
124
125
            if ($serviceResponse->isError()) {
126
                throw new ValidationException(
127
                    _t('GridFieldRefundAction.RefundError', 'Unable to refund payment. An error occurred.'),
128
                    0
129
                );
130
            }
131
        }
132
    }
133
}
134