/
Abstract.php
494 lines (430 loc) · 13.8 KB
/
Abstract.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
<?php
/**
* Shopgate GmbH
*
* URHEBERRECHTSHINWEIS
*
* Dieses Plugin ist urheberrechtlich geschützt. Es darf ausschließlich von Kunden der Shopgate GmbH
* zum Zwecke der eigenen Kommunikation zwischen dem IT-System des Kunden mit dem IT-System der
* Shopgate GmbH über www.shopgate.com verwendet werden. Eine darüber hinausgehende Vervielfältigung, Verbreitung,
* öffentliche Zugänglichmachung, Bearbeitung oder Weitergabe an Dritte ist nur mit unserer vorherigen
* schriftlichen Zustimmung zulässig. Die Regelungen der §§ 69 d Abs. 2, 3 und 69 e UrhG bleiben hiervon unberührt.
*
* COPYRIGHT NOTICE
*
* This plugin is the subject of copyright protection. It is only for the use of Shopgate GmbH customers,
* for the purpose of facilitating communication between the IT system of the customer and the IT system
* of Shopgate GmbH via www.shopgate.com. Any reproduction, dissemination, public propagation, processing or
* transfer to third parties is only permitted where we previously consented thereto in writing. The provisions
* of paragraph 69 d, sub-paragraphs 2, 3 and paragraph 69, sub-paragraph e of the German Copyright Act shall remain unaffected.
*
* @author Shopgate GmbH <interfaces@shopgate.com>
*/
/**
* Abstract payment model
*
* @package Shopgate_Framework_Model_Payment_Abstract
* @author Konstantin Kiritsenko <konstantin@kiritsenko.com>
*/
class Shopgate_Framework_Model_Payment_Abstract extends Mage_Core_Model_Abstract
{
/**
* Has to match the payment_method coming from API call
*/
const PAYMENT_IDENTIFIER = '';
/**
* Model code of the class that inherits mage's Payment_Method_Abstract
* Defaults to Shopgate's Mobile payment block
*/
const PAYMENT_MODEL = 'shopgate/payment_mobilePayment';
/**
* The config path to module enabled
*/
const XML_CONFIG_ENABLED = '';
/**
* The config path to module's paid status
*/
const XML_CONFIG_STATUS_PAID = '';
/**
* The config path to module's not paid status
*/
const XML_CONFIG_STATUS_NOT_PAID = '';
/**
* The name of the module, as defined in etc/modules/*.xml
*/
const MODULE_CONFIG = '';
/**
* @var null|Mage_Sales_Model_Order
*/
protected $_order = null;
/**
* Comes from payment_type of API
*/
protected $_payment_method;
/**
* Shopgate order inserted upon instantiation
*
* @var ShopgateOrder
*/
protected $_shopgate_order;
/**
* $this->_data contains constructor param
* Pass it into the Mage:getModel('',$param)
*
* @return ShopgateOrder
* @throws Exception
*/
public function _construct()
{
if ($this->_shopgate_order) {
return $this->_shopgate_order;
}
$shopgateOrder = $this->_data;
if (!$shopgateOrder instanceof ShopgateOrder) {
$error = $this->_getHelper()->__('Incorrect class provided to: %s::_constructor()', get_class($this));
ShopgateLogger::getInstance()->log($error, ShopgateLogger::LOGTYPE_ERROR);
throw new Exception($error);
}
return $this->setShopgateOrder($shopgateOrder);
}
/**
* @param $shopgateOrder ShopgateOrder
*
* @return $this
*/
public function setShopgateOrder(ShopgateOrder $shopgateOrder)
{
$this->_shopgate_order = $shopgateOrder;
return $this;
}
/**
* @return ShopgateOrder
*/
public function getShopgateOrder()
{
return $this->_shopgate_order;
}
/**
* @param $paymentMethod string
*
* @return $this
*/
public function setPaymentMethod($paymentMethod)
{
$this->_payment_method = $paymentMethod;
return $this;
}
/**
* @return string
*/
public function getPaymentMethod()
{
if (!$this->_payment_method) {
$this->_payment_method = $this->getShopgateOrder()->getPaymentMethod();
}
return $this->_payment_method;
}
/**
* Magento order getter
*
* @return Mage_Sales_Model_Order
*/
public function getOrder()
{
return $this->_order;
}
/**
* Helps initialize magento order
*
* @param Mage_Sales_Model_Order $order
*
* @return Mage_Sales_Model_Order
*/
public function setOrder(Mage_Sales_Model_Order $order)
{
return $this->_order = $order;
}
/**
* Get version of plugin
*
* @return mixed
*/
protected function _getVersion()
{
$constant = $this->getConstant('MODULE_CONFIG');
return Mage::getConfig()->getModuleConfig($constant)->version;
}
/**
* ===========================================
* ============= Active Checkers =============
* ===========================================
*/
/**
* All around check for whether module is the one to use
*
* @return bool
*/
public function isValid()
{
return $this->isPayment() && $this->isEnabled() && $this->isModuleActive() && $this->checkGenericValid();
}
/**
* Checks that api->payment_method is equals to class constant
*
* @return bool
*/
public function isPayment()
{
$payment = $this->getConstant('PAYMENT_IDENTIFIER');
$flag = $this->getPaymentMethod() === $payment;
if (!$flag) {
$debug = $this->_getHelper()->__(
'Payment method "%s" does not equal to identifier "%s" in class "%s"',
$this->getPaymentMethod(),
$payment,
get_class($this)
);
ShopgateLogger::getInstance()->log($debug, ShopgateLogger::LOGTYPE_DEBUG);
}
return $flag;
}
/**
* Checks store config to be active
*
* @return bool
*/
public function isEnabled()
{
$config = $this->getConstant('XML_CONFIG_ENABLED');
$val = Mage::getStoreConfig($config);
$enabled = !empty($val);
if (!$enabled) {
$debug = $this->_getHelper()->__(
'Enabled check by path "%s" was evaluated as empty: "%s" in class "%s"',
$config,
$val,
get_class($this)
);
ShopgateLogger::getInstance()->log($debug, ShopgateLogger::LOGTYPE_DEBUG);
}
return $enabled;
}
/**
* Checks module node to be active
*
* @return mixed
*/
public function isModuleActive()
{
$config = $this->getConstant('MODULE_CONFIG');
$active = Mage::getConfig()->getModuleConfig($config)->is('active', 'true');
if (!$active) {
$debug = $this->_getHelper()->__(
'Module by config "%s" was not active in class "%s"',
$config,
get_class($this)
);
ShopgateLogger::getInstance()->log($debug, ShopgateLogger::LOGTYPE_DEBUG);
}
return $active;
}
/**
* Implement any custom validation
*
* @return bool
*/
public function checkGenericValid()
{
return true;
}
/**
* ===========================================
* ======== Payment necessary methods ========
* ===========================================
*/
/**
* Default order creation if no payment matches
*
* @param Mage_Sales_Model_Quote $quote
*
* @return Mage_Sales_Model_Order
* @throws Exception
*/
public function createNewOrder($quote)
{
$service = Mage::getModel('sales/service_quote', $quote);
if (!Mage::helper("shopgate/config")->getIsMagentoVersionLower15()) {
$service->submitAll();
return $this->setOrder($service->getOrder());
} else {
return $this->setOrder($service->submit());
}
}
/**
* Generic order manipulation, taken originally from Plugin::_setOrderPayment()
*
* @param Mage_Sales_Model_Order $magentoOrder
*
* @return Mage_Sales_Model_Order
*/
public function manipulateOrderWithPaymentData($magentoOrder)
{
$shopgateOrder = $this->getShopgateOrder();
if ($shopgateOrder->getIsPaid()) {
if ($magentoOrder->getBaseTotalDue()) {
$magentoOrder->getPayment()->setShouldCloseParentTransaction(true);
$magentoOrder->getPayment()->registerCaptureNotification($shopgateOrder->getAmountComplete());
$magentoOrder->addStatusHistoryComment($this->_getHelper()->__("[SHOPGATE] Payment received."), false)
->setIsCustomerNotified(false);
}
}
$magentoOrder->getPayment()->setLastTransId($shopgateOrder->getPaymentTransactionNumber());
return $magentoOrder;
}
/**
* Default quote prepare handler
*
* @param Mage_Sales_Model_Quote $quote
* @param $info
*
* @return Mage_Sales_Model_Quote
*/
public function prepareQuote($quote, $info)
{
return $quote;
}
/**
* Setting for default magento status
*
* @param $magentoOrder Mage_Sales_Model_Order
*
* @return mixed
*/
public function setOrderStatus($magentoOrder)
{
$paid = $this->getConstant('XML_CONFIG_STATUS_PAID');
$notPaid = $this->getConstant('XML_CONFIG_STATUS_NOT_PAID');
if ($this->getShopgateOrder()->getIsPaid()) {
$status = Mage::getStoreConfig($paid, $magentoOrder->getStoreId());
} else {
if ($notPaid) {
$status = Mage::getStoreConfig($notPaid, $magentoOrder->getStoreId());
} else {
$status = Mage::getStoreConfig($paid, $magentoOrder->getStoreId());
}
}
if ($status) {
$state = $this->_getHelper()->getStateForStatus($status);
$message = $this->_getHelper()->__('[SHOPGATE] Using native plugin status');
$magentoOrder->setState($state, $status, $message);
$magentoOrder->setShopgateStatusSet(true);
}
return $magentoOrder;
}
/**
* Returns the payment model of a class,
* else falls back to mobilePayment
*
* @return mixed
*/
public function getPaymentModel()
{
$payment = $this->getConstant('PAYMENT_MODEL');
$model = Mage::getModel($payment);
if (!$model) {
$debug = $this->_getHelper()->__(
'Could not find PAYMENT_MODEL %s in class %s',
$payment,
get_class($this)
);
ShopgateLogger::getInstance()->log($debug, ShopgateLogger::LOGTYPE_DEBUG);
$model = Mage::getModel(self::PAYMENT_MODEL);
}
return $model;
}
/**
* Manipulation of new magento order, BEFORE payment is processed
*
* @param Mage_Sales_Model_Order $magentoOrder
*
* @return Mage_Sales_Model_Order
*/
protected function _beforeOrderManipulate($magentoOrder)
{
$shopgateOrder = $this->getShopgateOrder();
if ($this->_getHelper()->oneCentBugDetected($shopgateOrder, $magentoOrder)) {
$fixedOrderAmount = $shopgateOrder->getAmountComplete();
$magentoOrder->setBaseTotalDue($fixedOrderAmount);
$magentoOrder->setTotalDue($fixedOrderAmount);
$magentoOrder->setBaseGrandTotal($fixedOrderAmount);
$magentoOrder->setGrandTotal($fixedOrderAmount);
}
return $magentoOrder;
}
/**
* Manipulation of new magento order, AFTER payment is processed
*
* @param Mage_Sales_Model_Order $magentoOrder
*
* @return Mage_Sales_Model_Order
*/
protected function _afterOrderManipulate($magentoOrder)
{
$shopgateOrder = $this->getShopgateOrder();
if ($this->_getHelper()->oneCentBugDetected($shopgateOrder, $magentoOrder)) {
$fixedOrderAmount = $shopgateOrder->getAmountComplete();
$magentoOrder->getPayment()->setAmountOrdered($fixedOrderAmount);
$magentoOrder->getPayment()->setBaseAmountOrdered($fixedOrderAmount);
if ($magentoOrder->getInvoiceCollection()->count() == 1) {
foreach ($magentoOrder->getInvoiceCollection() as $invoice) {
$invoice->setGrandTotal($magentoOrder->getGrandTotal());
$invoice->setBaseGrandTotal($magentoOrder->getBaseGrandTotal());
$magentoOrder->setBaseTotalInvoiced($invoice->getBaseGrandTotal());
$magentoOrder->setTotalInvoiced($invoice->getGrandTotal());
$magentoOrder->setTotalPaid($invoice->getGrandTotal());
$magentoOrder->setBaseTotalPaid($magentoOrder->getBaseTotalInvoiced());
}
}
}
return $magentoOrder;
}
/**
* =======================================
* ============ Helpers ==================
* =======================================
*/
/**
* @return Shopgate_Framework_Helper_Data
*/
protected function _getHelper()
{
return Mage::helper('shopgate');
}
/**
* @return Shopgate_Framework_Helper_Payment_Abstract
*/
protected function _getPaymentHelper()
{
return Mage::helper('shopgate/payment_abstract');
}
/**
* @return Shopgate_Framework_Helper_Config
*/
protected function _getConfigHelper()
{
return Mage::helper('shopgate/config');
}
/**
* Added support for PHP version 5.2
* constant retrieval
*
* @param string $input
*
* @return mixed
*/
protected final function getConstant($input)
{
$configClass = new ReflectionClass($this);
return $configClass->getConstant($input);
}
}