|
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 capture an authorized payment |
|
18
|
|
|
* |
|
19
|
|
|
* @package SilverStripe\Omnipay\Admin\GridField |
|
20
|
|
|
*/ |
|
21
|
|
|
class GridFieldCaptureAction 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('capturepayment'); |
|
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->canCapture()) { |
|
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::captureMode($record->Gateway)) { |
|
57
|
|
|
case GatewayInfo::MULTIPLE: |
|
58
|
|
|
$infoText = 'MultiCaptureInfo'; |
|
59
|
|
|
break; |
|
60
|
|
|
case GatewayInfo::PARTIAL: |
|
61
|
|
|
$infoText = 'SingleCaptureInfo'; |
|
62
|
|
|
break; |
|
63
|
|
|
case GatewayInfo::FULL: |
|
64
|
|
|
$infoText = 'FullCaptureInfo'; |
|
65
|
|
|
break; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** @var DBMoney $money */ |
|
69
|
|
|
$money = $record->dbObject('Money'); |
|
70
|
|
|
$money->setAmount($record->getMaxCaptureAmount()); |
|
71
|
|
|
|
|
72
|
|
|
/** @var GridField_FormAction $field */ |
|
73
|
|
|
$field = GridField_FormAction::create( |
|
74
|
|
|
$gridField, |
|
75
|
|
|
'CapturePayment' . $record->ID, |
|
76
|
|
|
false, |
|
77
|
|
|
'capturepayment', |
|
78
|
|
|
array('RecordID' => $record->ID) |
|
79
|
|
|
) |
|
80
|
|
|
->addExtraClass('gridfield-button-capture payment-dialog-button') |
|
81
|
|
|
->setAttribute('title', _t('GridFieldCaptureAction.Title', 'Capture Payment')) |
|
82
|
|
|
->setAttribute('data-icon', 'button-capture') |
|
83
|
|
|
->setAttribute('data-dialog', json_encode(array( |
|
84
|
|
|
'maxAmount' => $money->Nice(), |
|
85
|
|
|
'maxAmountNum' => $money->getAmount(), |
|
86
|
|
|
'hasAmountField' => $record->canCapture(null, true), |
|
87
|
|
|
'infoTextKey' => $infoText, |
|
88
|
|
|
'buttonTextKey' => 'CaptureAmount' |
|
89
|
|
|
))) |
|
90
|
|
|
->setDescription(_t('GridFieldCaptureAction.Description', 'Capture authorized Payment')); |
|
91
|
|
|
|
|
92
|
|
|
return $field->Field(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Handle the actions and apply any changes to the GridField |
|
97
|
|
|
* |
|
98
|
|
|
* @param GridField $gridField |
|
99
|
|
|
* @param string $actionName |
|
100
|
|
|
* @param mixed $arguments |
|
101
|
|
|
* @param array $data - form data |
|
102
|
|
|
* @return void |
|
103
|
|
|
* @throws \SilverStripe\Omnipay\Exception\InvalidConfigurationException |
|
104
|
|
|
* @throws ValidationException |
|
105
|
|
|
*/ |
|
106
|
|
|
public function handleAction(GridField $gridField, $actionName, $arguments, $data) |
|
107
|
|
|
{ |
|
108
|
|
|
if ($actionName == 'capturepayment') { |
|
109
|
|
|
$item = $gridField->getList()->byID($arguments['RecordID']); |
|
110
|
|
|
if (!($item instanceof Payment)) { |
|
111
|
|
|
return; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$serviceData = array_intersect_key($data, array('amount' => null)); |
|
115
|
|
|
|
|
116
|
|
|
/** @var ServiceFactory $factory */ |
|
117
|
|
|
$factory = ServiceFactory::create(); |
|
118
|
|
|
$captureService = $factory->getService($item, ServiceFactory::INTENT_CAPTURE); |
|
119
|
|
|
|
|
120
|
|
|
try { |
|
121
|
|
|
$serviceResponse = $captureService->initiate($serviceData); |
|
122
|
|
|
} catch (Exception $ex) { |
|
123
|
|
|
throw new ValidationException($ex->getMessage(), 0); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
if ($serviceResponse->isError()) { |
|
127
|
|
|
throw new ValidationException( |
|
128
|
|
|
_t('GridFieldCaptureAction.CaptureError', 'Unable to capture payment. An error occurred.'), |
|
129
|
|
|
0 |
|
130
|
|
|
); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|