forked from quafzi/payone-magento
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Abstract.php
542 lines (462 loc) · 16.5 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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
<?php
/**
*
* NOTICE OF LICENSE
*
* This source file is subject to the GNU General Public License (GPL 3)
* that is bundled with this package in the file LICENSE.txt
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Payone_Core to newer
* versions in the future. If you wish to customize Payone_Core for your
* needs please refer to http://www.payone.de for more information.
*
* @category Payone
* @package Payone_Core_Model
* @subpackage Mapper
* @copyright Copyright (c) 2012 <info@noovias.com> - www.noovias.com
* @author Matthias Walter <info@noovias.com>
* @license <http://www.gnu.org/licenses/> GNU General Public License (GPL 3)
* @link http://www.noovias.com
*/
/**
*
* @category Payone
* @package Payone_Core_Model
* @subpackage Mapper
* @copyright Copyright (c) 2012 <info@noovias.com> - www.noovias.com
* @license <http://www.gnu.org/licenses/> GNU General Public License (GPL 3)
* @link http://www.noovias.com
*/
abstract class Payone_Core_Model_Mapper_ApiRequest_Payment_Abstract
extends Payone_Core_Model_Mapper_ApiRequest_Abstract
implements Payone_Core_Model_Mapper_ApiRequest_Payment_Interface
{
const DEFAULT_SHIPPING_SKU = 'Shipping';
const DEFAULT_ADJUSTMENT_POSITIVE_SKU = 'Adjustment Refund';
const DEFAULT_ADJUSTMENT_NEGATIVE_SKU = 'Adjustment Fee';
const DEFAULT_DISCOUNT_SKU = 'Discount';
const DEFAULT_TAX_SKU = 'Tax';
const EVENT_PREFIX = 'payone_core_mapper_apirequest_payment';
/** @var float */
protected $amount = 0.00;
/** @var Mage_Sales_Model_Order */
protected $order = null;
/** @var Payone_Core_Model_Payment_Method_Abstract */
protected $paymentMethod = null;
/** @var Payone_Core_Model_Config_Payment_Method_Interface */
protected $configPayment = null;
/** @var Payone_Core_Model_Config_Misc */
protected $configMisc = null;
/**
* @return string
*/
abstract public function getEventType();
/**
* @param Mage_Sales_Model_Order_Payment $payment
*/
protected function init(Mage_Sales_Model_Order_Payment $payment)
{
$this->setOrder($payment->getOrder());
$this->setPaymentMethod($payment->getMethodInstance());
}
/**
* @param Payone_Api_Request_Interface $request
*/
protected function mapDefaultParameters(Payone_Api_Request_Interface $request)
{
$helper = $this->helper();
$solutionName = 'noovias';
$solutionVersion = $helper->getPayoneVersion();
$integratorName = 'magento';
$integratorVersion = $helper->getMagentoVersion();
$request->setEncoding('UTF-8');
$request->setMid($this->getConfigPayment()->getMid());
$request->setPortalid($this->getConfigPayment()->getPortalid());
$request->setMode($this->getConfigPayment()->getMode());
$request->setKey($this->getConfigPayment()->getKey());
$request->setIntegratorName($integratorName);
$request->setIntegratorVersion($integratorVersion);
$request->setSolutionName($solutionName);
$request->setSolutionVersion($solutionVersion);
}
/**
* @return Payone_Api_Request_Parameter_Invoicing_Item
*/
protected function mapShippingFeeAsItem()
{
$order = $this->getOrder();
$configMiscShipping = $this->getConfigMisc()->getShippingCosts();
$sku = $configMiscShipping->getSku();
if (empty($sku)) {
$sku = $this->helper()->__(self::DEFAULT_SHIPPING_SKU);
}
$params['id'] = $sku;
$params['de'] = $order->getShippingDescription();
$params['no'] = 1;
$params['pr'] = $order->getShippingInclTax();
$params['va'] = $this->getShippingTaxRate();
if ($this->getPaymentMethod()->mustTransmitInvoicingItemTypes()) {
$params['it'] = Payone_Api_Enum_InvoicingItemType::SHIPMENT;
}
$item = new Payone_Api_Request_Parameter_Invoicing_Item();
$item->init($params);
return $item;
}
/**
* @param float $discountAmount
* @return Payone_Api_Request_Parameter_Invoicing_Item
*/
protected function mapDiscountAsItem($discountAmount)
{
$configMiscDiscount = $this->getConfigMisc()->getDiscount();
$sku = $configMiscDiscount->getSku();
$description = $configMiscDiscount->getDescription();
if (empty($sku)) {
$sku = $this->helper()->__(self::DEFAULT_DISCOUNT_SKU);
}
if (empty($description)) {
$description = $this->helper()->__(self::DEFAULT_DISCOUNT_SKU);
}
$params['id'] = $sku;
$params['de'] = $description;
$params['no'] = 1;
$params['pr'] = $discountAmount;
if ($this->getPaymentMethod()->mustTransmitInvoicingItemTypes()) {
$params['it'] = Payone_Api_Enum_InvoicingItemType::VOUCHER;
}
$item = new Payone_Api_Request_Parameter_Invoicing_Item();
$item->init($params);
return $item;
}
/**
* @param Mage_Sales_Model_Order_Creditmemo $creditmemo
* @return Payone_Api_Request_Parameter_Invoicing_Item
*
* used by Payone_Core_Model_Mapper_ApiRequest_Payment_Debit
*/
protected function mapRefundShippingAsItemByCreditmemo(Mage_Sales_Model_Order_Creditmemo $creditmemo)
{
$order = $this->getOrder();
$configMiscShipping = $this->getConfigMisc()->getShippingCosts();
$sku = $configMiscShipping->getSku();
if (empty($sku)) {
$sku = $this->helper()->__(self::DEFAULT_SHIPPING_SKU);
}
$params['id'] = $sku;
$params['de'] = $order->getShippingDescription();
$params['no'] = 1;
$params['pr'] = $creditmemo->getShippingInclTax();
if ($this->getPaymentMethod()->mustTransmitInvoicingItemTypes()) {
$params['it'] = Payone_Api_Enum_InvoicingItemType::SHIPMENT;
}
$item = new Payone_Api_Request_Parameter_Invoicing_Item();
$item->init($params);
return $item;
}
/**
* @param Mage_Sales_Model_Order_Creditmemo $creditmemo
* @return Payone_Api_Request_Parameter_Invoicing_Item
*
* used by Payone_Core_Model_Mapper_ApiRequest_Payment_Debit
*/
protected function mapAdjustmentPositiveAsItemByCreditmemo(Mage_Sales_Model_Order_Creditmemo $creditmemo)
{
$configMiscCreditmemo = $this->getConfigMisc()->getCreditmemo();
$sku = $configMiscCreditmemo->getAdjustmentRefundSku();
$name = $configMiscCreditmemo->getAdjustmentRefundName();
if (empty($sku)) {
$sku = $this->helper()->__(self::DEFAULT_ADJUSTMENT_POSITIVE_SKU);
}
if (empty($name)) {
$name = $this->helper()->__(self::DEFAULT_ADJUSTMENT_POSITIVE_SKU);
}
$params['id'] = $sku;
$params['de'] = $name;
$params['no'] = 1;
$params['pr'] = $creditmemo->getAdjustmentPositive();
if ($this->getPaymentMethod()->mustTransmitInvoicingItemTypes()) {
$params['it'] = Payone_Api_Enum_InvoicingItemType::VOUCHER;
}
$item = new Payone_Api_Request_Parameter_Invoicing_Item();
$item->init($params);
return $item;
}
/**
* @param Mage_Sales_Model_Order_Creditmemo $creditmemo
* @return Payone_Api_Request_Parameter_Invoicing_Item
*
* used by Payone_Core_Model_Mapper_ApiRequest_Payment_Debit
*/
protected function mapAdjustmentNegativeAsItemByCreditmemo(Mage_Sales_Model_Order_Creditmemo $creditmemo)
{
$configMiscCreditmemo = $this->getConfigMisc()->getCreditmemo();
$sku = $configMiscCreditmemo->getAdjustmentFeeSku();
$name = $configMiscCreditmemo->getAdjustmentFeeName();
if (empty($sku)) {
$sku = $this->helper()->__(self::DEFAULT_ADJUSTMENT_NEGATIVE_SKU);
}
if (empty($name)) {
$name = $this->helper()->__(self::DEFAULT_ADJUSTMENT_NEGATIVE_SKU);
}
$params['id'] = $sku;
$params['de'] = $name;
$params['no'] = 1;
$params['pr'] = $creditmemo->getAdjustmentNegative() * (-1);
if ($this->getPaymentMethod()->mustTransmitInvoicingItemTypes()) {
$params['it'] = Payone_Api_Enum_InvoicingItemType::GOODS;
}
$item = new Payone_Api_Request_Parameter_Invoicing_Item();
$item->init($params);
return $item;
}
protected function getShippingTaxRate()
{
$order = $this->getOrder();
$storeId = $this->getStoreId();
$factory = $this->getFactory();
$store = $factory->getModelCoreStore()->load($storeId);
/** @var $taxCalculationModel Mage_Tax_Model_Calculation */
$taxCalculationModel = $factory->getSingletonTaxCalculation();
$shippingAddress = $order->getShippingAddress();
$billingAddress = $order->getBillingAddress();
$quoteId = $order->getQuoteId();
$quote = $factory->getModelSalesQuote();
$quote->load($quoteId);
$customerTaxClassId = $quote->getCustomerTaxClassId();
$request = $taxCalculationModel->getRateRequest($shippingAddress, $billingAddress, $customerTaxClassId, $store);
$shippingTaxClass = $this->helperConfig()->getShippingTaxClassId($storeId);
if ($shippingTaxClass) {
$request->setProductClassId($shippingTaxClass);
return $taxCalculationModel->getRate($request);
}
else {
return 0.0;
}
}
/**
* Returns the invoice appendix and substitutes the placeholders, as far as possible
*
* @param Mage_Sales_Model_Order_Invoice|null $invoice
* @return mixed|string
*/
public function getInvoiceAppendix(Mage_Sales_Model_Order_Invoice $invoice = null)
{
$order = $this->getOrder();
/** @var $customer Mage_Customer_Model_Customer */
$customer = $this->getFactory()->getModelCustomer();
$customer->load($order->getCustomerId());
$invoiceIncrementId = '';
if (!is_null($invoice)) {
$invoiceIncrementId = $invoice->getIncrementId();
}
$substitutionArray = array(
'{{order_increment_id}}' => $order->getIncrementId(),
'{{order_id}}' => $order->getId(),
'{{invoice_increment_id}}' => $invoiceIncrementId,
'{{customer_increment_id}}' => $customer->getIncrementId(),
'{{customer_id}}' => $order->getCustomerId(),
);
$appendix = $this->getConfigParameterInvoice()->getInvoiceAppendix();
$appendix = str_replace(array_keys($substitutionArray), array_values($substitutionArray), $appendix);
return $appendix;
}
/**
* Returns the refund appendix and substitutes the placeholders, as far as possible
*
* @param Mage_Sales_Model_Order_Creditmemo $creditmemo
* @return string
*/
public function getInvoiceAppendixRefund(Mage_Sales_Model_Order_Creditmemo $creditmemo)
{
$order = $this->getOrder();
/** @var $customer Mage_Customer_Model_Customer */
$customer = $this->getFactory()->getModelCustomer();
$customer->load($order->getCustomerId());
/** @var $invoice Mage_Sales_Model_Order_Invoice */
$invoice = $creditmemo->getInvoice();
$substitutionArray = array(
'{{order_increment_id}}' => $order->getIncrementId(),
'{{order_id}}' => $order->getId(),
'{{creditmemo_increment_id}}' => $creditmemo->getIncrementId(),
'{{invoice_increment_id}}' => $invoice->getIncrementId(),
'{{invoice_id}}' => $invoice->getId(),
'{{customer_increment_id}}' => $customer->getIncrementId(),
'{{customer_id}}' => $order->getCustomerId(),
);
$appendix = $this->getConfigParameterInvoice()->getInvoiceAppendixRefund();
$appendix = str_replace(array_keys($substitutionArray), array_values($substitutionArray), $appendix);
return $appendix;
}
/**
* @return bool
*/
protected function mustTransmitInvoiceData()
{
if ($this->getConfigPayment()->isInvoiceTransmitEnabled()) {
return true;
}
$paymentMethod = $this->getPaymentMethod();
if ($paymentMethod->mustTransmitInvoicingData()) { // Certain payment methods require invoicing data to be transmitted ALWAYS.
return true;
}
return false;
}
/**
* @param Mage_Sales_Model_Abstract $object
* @return string
*/
protected function fetchNewIncrementId(Mage_Sales_Model_Abstract $object)
{
$entityTypeModel = $this->getFactory()->getModelEavEntityType();
$code = '';
if ($object instanceof Mage_Sales_Model_Order_Invoice) {
$code = 'invoice';
}
elseif ($object instanceof Mage_Sales_Model_Order_Creditmemo) {
$code = 'creditmemo';
}
$entityType = $entityTypeModel->loadByCode($code);
$newIncrementId = $entityType->fetchNewIncrementId($this->getStoreId());
if ($newIncrementId !== false) {
$object->setIncrementId($newIncrementId);
}
return $newIncrementId;
}
/**
* @param Payone_Core_Model_Config_Payment_Method_Interface $configPayment
*/
public function setConfigPayment(Payone_Core_Model_Config_Payment_Method_Interface $configPayment)
{
$this->configPayment = $configPayment;
}
/**
* @return Payone_Core_Model_Config_Payment_Method_Interface
*/
public function getConfigPayment()
{
return $this->configPayment;
}
/**
* @return Payone_Core_Model_Config_Misc
*/
protected function getConfigMisc()
{
if ($this->configMisc === null) {
$this->configMisc = $this->helperConfig()->getConfigMisc($this->getStoreId());
}
return $this->configMisc;
}
/**
* @param Payone_Core_Model_Config_Misc $configMisc
*/
public function setConfigMisc(Payone_Core_Model_Config_Misc $configMisc)
{
$this->configMisc = $configMisc;
}
/**
* @return Payone_Core_Model_Config_General_ParameterInvoice
*/
protected function getConfigParameterInvoice()
{
return $this->helperConfig()->getConfigGeneral($this->getStoreId())->getParameterInvoice();
}
/**
* @return int
*/
protected function getStoreId()
{
return $this->getPaymentMethod()->getStore();
}
/**
* @param $storeId
* @return Payone_Core_Model_Config_General
*/
protected function getConfigGeneral($storeId = null)
{
if (is_null($storeId)) {
$storeId = $this->getStoreId();
}
return $this->helperConfig()->getConfigGeneral($storeId);
}
/**
* @param Mage_Sales_Model_Order $order
*/
public function setOrder(Mage_Sales_Model_Order $order)
{
$this->order = $order;
}
/**
* @return Mage_Sales_Model_Order
*/
public function getOrder()
{
return $this->order;
}
/**
* @param float $amount
*/
public function setAmount($amount)
{
$this->amount = $amount;
}
/**
* @return float
*/
public function getAmount()
{
return $this->amount;
}
/**
* @param Payone_Core_Model_Payment_Method_Abstract $paymentMethod
*/
public function setPaymentMethod(Payone_Core_Model_Payment_Method_Abstract $paymentMethod)
{
$this->paymentMethod = $paymentMethod;
}
/**
* @return Payone_Core_Model_Payment_Method_Abstract
*/
public function getPaymentMethod()
{
return $this->paymentMethod;
}
/**
* @return Payone_Core_Helper_Config
*/
protected function helperConfig()
{
return $this->getFactory()->helperConfig();
}
/**
* @return Payone_Core_Helper_Registry
*/
protected function helperRegistry()
{
return $this->getFactory()->helperRegistry();
}
protected function getEventPrefix()
{
return self::EVENT_PREFIX;
}
/**
* @return string
*/
protected function getEventName()
{
return $this->getEventPrefix() . '_' . $this->getEventType();
}
/**
* Wrapper for Mage::dispatchEvent()
*
* @param $name
* @param array $data
*
* @return Mage_Core_Model_App
*/
protected function dispatchEvent($name, array $data = array())
{
return Mage::dispatchEvent($name, $data);
}
}