1 | <?php |
||
18 | class Twocheckout extends Module |
||
19 | { |
||
20 | |||
21 | /** |
||
22 | * The current order |
||
23 | * @var array |
||
24 | */ |
||
25 | protected $data_order; |
||
26 | |||
27 | /** |
||
28 | * Omnipay response instance |
||
29 | * @var object |
||
30 | */ |
||
31 | protected $response; |
||
32 | |||
33 | /** |
||
34 | * Frontend controller instance |
||
35 | * @var \gplcart\core\controllers\frontend\Controller $controller |
||
36 | */ |
||
37 | protected $controller; |
||
38 | |||
39 | /** |
||
40 | * Order model instance |
||
41 | * @var \gplcart\core\models\Order $order |
||
42 | */ |
||
43 | protected $order; |
||
44 | |||
45 | /** |
||
46 | * Module class instance |
||
47 | * @var \gplcart\core\Module $module |
||
48 | */ |
||
49 | protected $module; |
||
50 | |||
51 | /** |
||
52 | * @param Module $module |
||
53 | */ |
||
54 | public function __construct(Module $module) |
||
55 | { |
||
56 | $this->module = $module; |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Implements hook "route.list" |
||
61 | * @param array $routes |
||
62 | */ |
||
63 | public function hookRouteList(array &$routes) |
||
64 | { |
||
65 | $routes['admin/module/settings/twocheckout'] = array( |
||
66 | 'access' => 'module_edit', |
||
67 | 'handlers' => array( |
||
68 | 'controller' => array('gplcart\\modules\\twocheckout\\controllers\\Settings', 'editSettings') |
||
69 | ) |
||
70 | ); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Implements hook "module.enable.before" |
||
75 | * @param mixed $result |
||
76 | */ |
||
77 | public function hookModuleEnableBefore(&$result) |
||
78 | { |
||
79 | $this->checkGateway($result); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Implements hook "module.install.before" |
||
84 | * @param mixed $result |
||
85 | */ |
||
86 | public function hookModuleInstallBefore(&$result) |
||
87 | { |
||
88 | $this->checkGateway($result); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Check 2checkout gateway class is loaded |
||
93 | * @param mixed $result |
||
94 | */ |
||
95 | protected function checkGateway(&$result) |
||
96 | { |
||
97 | try { |
||
98 | $this->getGateway(); |
||
99 | } catch (\InvalidArgumentException $ex) { |
||
100 | $result = $ex->getMessage(); |
||
101 | } |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Implements hook "payment.methods" |
||
106 | * @param array $methods |
||
107 | */ |
||
108 | public function hookPaymentMethods(array &$methods) |
||
109 | { |
||
110 | $methods['twocheckout'] = array( |
||
111 | 'module' => 'twocheckout', |
||
112 | 'image' => 'image/icon.png', |
||
113 | 'title' => '2 Checkout', |
||
114 | 'status' => $this->getStatus(), |
||
115 | 'template' => array('complete' => 'pay') |
||
116 | ); |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Implements hook "order.add.before" |
||
121 | * @param array $order |
||
122 | * @param \gplcart\core\models\Order $object |
||
123 | */ |
||
124 | public function hookOrderAddBefore(array &$order, $object) |
||
125 | { |
||
126 | // Adjust order status before creation |
||
127 | // We want to get payment in advance, so assign "awaiting payment" status |
||
128 | if ($order['payment'] === 'twocheckout') { |
||
129 | $order['status'] = $object->getStatusAwaitingPayment(); |
||
130 | } |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Implements hook "order.checkout.complete" |
||
135 | * @param string $message |
||
136 | * @param array $order |
||
137 | */ |
||
138 | public function hookOrderCompleteMessage(&$message, $order) |
||
139 | { |
||
140 | if ($order['payment'] === 'twocheckout') { |
||
141 | $message = ''; // Hide default message |
||
142 | } |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Implements hook "order.complete.page" |
||
147 | * @param array $order |
||
148 | * @param \gplcart\core\models\Order $model |
||
149 | * @param \gplcart\core\controllers\frontend\Controller $controller |
||
150 | */ |
||
151 | public function hookOrderCompletePage(array $order, $model, $controller) |
||
152 | { |
||
153 | $this->setOrderCompletePage($order, $model, $controller); |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * Set order complete page |
||
158 | * @param array $order |
||
159 | * @param \gplcart\core\models\Order $model |
||
160 | * @param \gplcart\core\controllers\frontend\Controller $controller |
||
161 | */ |
||
162 | protected function setOrderCompletePage(array $order, $model, $controller) |
||
163 | { |
||
164 | $this->order = $model; |
||
165 | $this->data_order = $order; |
||
166 | $this->controller = $controller; |
||
167 | |||
168 | if ($order['payment'] === 'twocheckout') { |
||
169 | $this->submitPayment(); |
||
170 | $this->completePayment(); |
||
171 | } |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * Get gateway instance |
||
176 | * @return \Omnipay\TwoCheckoutPlus\Gateway |
||
177 | * @throws \InvalidArgumentException |
||
178 | */ |
||
179 | public function getGateway() |
||
180 | { |
||
181 | /* @var $module \gplcart\modules\omnipay_library\OmnipayLibrary */ |
||
182 | $module = $this->module->getInstance('omnipay_library'); |
||
183 | $gateway = $module->getGatewayInstance('TwoCheckoutPlus'); |
||
184 | |||
185 | if (!$gateway instanceof \Omnipay\TwoCheckoutPlus\Gateway) { |
||
|
|||
186 | throw new \InvalidArgumentException('Object is not instance of Omnipay\TwoCheckoutPlus\Gateway'); |
||
187 | } |
||
188 | |||
189 | return $gateway; |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * Returns a module setting |
||
194 | * @param string $name |
||
195 | * @param mixed $default |
||
196 | * @return mixed |
||
197 | */ |
||
198 | protected function getModuleSetting($name, $default = null) |
||
199 | { |
||
200 | return $this->module->getSettings('twocheckout', $name, $default); |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * Returns the current status of the payment method |
||
205 | */ |
||
206 | protected function getStatus() |
||
210 | |||
211 | /** |
||
212 | * Performs actions when purchase is completed |
||
213 | */ |
||
214 | protected function completePayment() |
||
215 | { |
||
216 | if ($this->controller->isQuery('paid')) { |
||
217 | $gateway = $this->getGateway(); |
||
218 | $this->response = $gateway->completePurchase($this->getPurchaseParams())->send(); |
||
219 | $this->processResponse(); |
||
220 | } |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * Handles submitted payment |
||
225 | */ |
||
226 | protected function submitPayment() |
||
254 | |||
255 | /** |
||
256 | * Returns an array of purchase parameters |
||
257 | * @return array |
||
258 | */ |
||
259 | protected function getPurchaseParams() |
||
268 | |||
269 | /** |
||
270 | * Processes gateway response |
||
271 | */ |
||
272 | protected function processResponse() |
||
284 | |||
285 | /** |
||
286 | * Redirect on error transaction |
||
287 | */ |
||
288 | protected function redirectError() |
||
292 | |||
293 | /** |
||
294 | * Redirect on successful transaction |
||
295 | */ |
||
296 | protected function redirectSuccess() |
||
306 | |||
307 | /** |
||
308 | * Update order status after successful transaction |
||
309 | */ |
||
310 | protected function updateOrderStatus() |
||
318 | |||
319 | /** |
||
320 | * Adds a transaction |
||
321 | * @return integer |
||
322 | */ |
||
323 | protected function addTransaction() |
||
338 | |||
339 | } |
||
340 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.