Completed
Push — master ( 341936...178071 )
by Florian
03:31
created

Debit::sendRequest()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 38
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 38
c 1
b 0
f 0
rs 8.5806
cc 4
eloc 21
nc 8
nop 3
1
<?php
2
3
/**
4
 * PAYONE Magento 2 Connector is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU Lesser General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * PAYONE Magento 2 Connector is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public License
15
 * along with PAYONE Magento 2 Connector. If not, see <http://www.gnu.org/licenses/>.
16
 *
17
 * PHP version 5
18
 *
19
 * @category  Payone
20
 * @package   Payone_Magento2_Plugin
21
 * @author    FATCHIP GmbH <[email protected]>
22
 * @copyright 2003 - 2016 Payone GmbH
23
 * @license   <http://www.gnu.org/licenses/> GNU Lesser General Public License
24
 * @link      http://www.payone.de
25
 */
26
27
namespace Payone\Core\Model\Api\Request;
28
29
use Magento\Sales\Model\Order;
30
use Payone\Core\Model\Methods\PayoneMethod;
31
use Magento\Payment\Model\InfoInterface;
32
33
/**
34
 * Class for the PAYONE Server API request "debit"
35
 */
36
class Debit extends Base
37
{
38
    /**
39
     * @var \Payone\Core\Model\Api\Invoice $invoiceGenerator
40
     */
41
    protected $invoiceGenerator;
42
43
    /**
44
     * PAYONE database helper
45
     *
46
     * @var \Payone\Core\Helper\Database
47
     */
48
    protected $databaseHelper;
49
50
    /**
51
     * PAYONE toolkit helper
52
     *
53
     * @var \Payone\Core\Helper\Toolkit
54
     */
55
    protected $toolkitHelper;
56
57
    /**
58
     * Constructor
59
     *
60
     * @param \Payone\Core\Helper\Shop                $shopHelper
61
     * @param \Payone\Core\Helper\Environment         $environmentHelper
62
     * @param \Payone\Core\Helper\Api                 $apiHelper
63
     * @param \Payone\Core\Model\ResourceModel\ApiLog $apiLog
64
     * @param \Payone\Core\Model\Api\Invoice          $invoiceGenerator
65
     * @param \Payone\Core\Helper\Database            $databaseHelper
66
     * @param \Payone\Core\Helper\Toolkit             $toolkitHelper
67
     */
68
    public function __construct(
69
        \Payone\Core\Helper\Shop $shopHelper,
70
        \Payone\Core\Helper\Environment $environmentHelper,
71
        \Payone\Core\Helper\Api $apiHelper,
72
        \Payone\Core\Model\ResourceModel\ApiLog $apiLog,
73
        \Payone\Core\Model\Api\Invoice $invoiceGenerator,
74
        \Payone\Core\Helper\Database $databaseHelper,
75
        \Payone\Core\Helper\Toolkit $toolkitHelper
76
    ) {
77
        parent::__construct($shopHelper, $environmentHelper, $apiHelper, $apiLog);
78
        $this->invoiceGenerator = $invoiceGenerator;
79
        $this->databaseHelper = $databaseHelper;
80
        $this->toolkitHelper = $toolkitHelper;
81
    }
82
83
    /**
84
     * Generate position list for invoice data transmission
85
     *
86
     * @param Order $oOrder
87
     * @return array|false
88
     */
89
    protected function getInvoiceList(Order $oOrder)
90
    {
91
        $aCreditmemo = $this->shopHelper->getRequestParameter('creditmemo');
92
93
        $aPositions = [];
94
        $blFull = true;
95
        if ($aCreditmemo && array_key_exists('items', $aCreditmemo) !== false) {
96
            foreach ($oOrder->getAllItems() as $oItem) {
97
                if (isset($aCreditmemo['items'][$oItem->getItemId()]) && $aCreditmemo['items'][$oItem->getItemId()]['qty'] > 0) {
98
                    $aPositions[$oItem->getProductId()] = $aCreditmemo['items'][$oItem->getItemId()]['qty'];
99
                    if ($aCreditmemo['items'][$oItem->getItemId()]['qty'] != $oItem->getQtyOrdered()) {
100
                        $blFull = false;
101
                    }
102
                } else {
103
                    $blFull = false;
104
                }
105
            }
106
        }
107
        if (isset($aCreditmemo['shipping_amount']) && $aCreditmemo['shipping_amount'] != 0) {
108
            $aPositions['delcost'] = $aCreditmemo['shipping_amount'];
109
        }
110
        if ($blFull === true && (!isset($aCreditmemo['shipping_amount']) || $aCreditmemo['shipping_amount'] == $oOrder->getBaseShippingInclTax())) {
111
            $aPositions = false; // false = full debit
112
        }
113
        return $aPositions;
114
    }
115
116
    /**
117
     * Send request "debit" to PAYONE server API
118
     *
119
     * @param  PayoneMethod  $oPayment
120
     * @param  InfoInterface $oPaymentInfo
121
     * @param  float         $dAmount
122
     * @return array
123
     */
124
    public function sendRequest(PayoneMethod $oPayment, InfoInterface $oPaymentInfo, $dAmount)
125
    {
126
        $oOrder = $oPaymentInfo->getOrder();
127
128
        $aPositions = $this->getInvoiceList($oOrder);
129
130
        $iTxid = $oPaymentInfo->getParentTransactionId();
131
        if (strpos($iTxid, '-') !== false) {
132
            $iTxid = substr($iTxid, 0, strpos($iTxid, '-')); // clean the txid from the magento-suffixes
133
        }
134
135
        $this->setOrderId($oOrder->getRealOrderId());
136
137
        $this->addParameter('request', 'debit'); // Request method
138
        $this->addParameter('mode', $oPayment->getOperationMode()); // PayOne Portal Operation Mode (live or test)
139
        $this->addParameter('txid', $iTxid); // PayOne Transaction ID
140
        $this->addParameter('sequencenumber', $this->databaseHelper->getSequenceNumber($iTxid));
141
142
        // Total order sum in smallest currency unit
143
        $this->addParameter('amount', number_format((-1 * $dAmount), 2, '.', '') * 100);
144
        $this->addParameter('currency', $oOrder->getOrderCurrencyCode()); // Currency
145
        $this->addParameter('transactiontype', 'GT');
146
147
        $sRefundAppendix = $this->getRefundAppendix($oOrder, $oPayment);
148
        if (!empty($sRefundAppendix)) {
149
            $this->addParameter('invoiceappendix', $sRefundAppendix);
150
        }
151
152
        if ($this->apiHelper->isInvoiceDataNeeded($oPayment)) {
153
            $this->invoiceGenerator->addProductInfo($this, $oOrder, $aPositions, true); // add invoice parameters
154
        }
155
156
        // Add debit bank data given - see oxid integration
157
158
        $aResponse = $this->send();
159
160
        return $aResponse;
161
    }
162
163
    /**
164
     * Get substituted refund appendix text
165
     *
166
     * @param  Order        $oOrder
167
     * @param  PayoneMethod $oPayment
168
     * @return string
169
     */
170
    protected function getRefundAppendix(Order $oOrder, PayoneMethod $oPayment)
171
    {
172
        $sText = $this->shopHelper->getConfigParam('invoice_appendix_refund', 'invoicing');
173
        $sCreditMemoIncrId = '';
174
        $sInvoiceIncrementId = '';
175
        $sInvoiceId = '';
176
177
        $oCreditmemo = $oPayment->getCreditmemo();
178
        if ($oCreditmemo) {
179
            $sCreditMemoIncrId = $oCreditmemo->getIncrementId();
180
            $oInvoice = $oCreditmemo->getInvoice();
181
            if ($oInvoice) {
182
                $sInvoiceIncrementId = $oInvoice->getIncrementId();
183
                $sInvoiceId = $oInvoice->getId();
184
            }
185
        }
186
187
        $aSubstitutionArray = [
188
            '{{order_increment_id}}' => $oOrder->getIncrementId(),
189
            '{{order_id}}' => $oOrder->getId(),
190
            '{{customer_id}}' => $oOrder->getCustomerId(),
191
            '{{creditmemo_increment_id}}' => $sCreditMemoIncrId,
192
            '{{invoice_increment_id}}' => $sInvoiceIncrementId,
193
            '{{invoice_id}}' => $sInvoiceId,
194
        ];
195
        $sRefundAppendix = $this->toolkitHelper->handleSubstituteReplacement($sText, $aSubstitutionArray, 255);
0 ignored issues
show
Documentation introduced by
$aSubstitutionArray is of type array<string,?,{"{{order...,"{{invoice_id}}":"?"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
196
        return $sRefundAppendix;
197
    }
198
}
199