Completed
Push — master ( 9097d5...0ce82e )
by Florian
06:13
created

Capture::getInvoiceList()   C

Complexity

Conditions 8
Paths 4

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 14
nc 4
nop 1
dl 0
loc 22
rs 6.6037
c 0
b 0
f 0
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\Payment\Model\InfoInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Payment\Model\InfoInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
30
use Payone\Core\Model\Methods\PayoneMethod;
31
use Magento\Sales\Model\Order;
0 ignored issues
show
Bug introduced by
The type Magento\Sales\Model\Order was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
32
33
/**
34
 * Class for the PAYONE Server API request "capture"
35
 */
36
class Capture 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
     * Constructor
52
     *
53
     * @param \Payone\Core\Helper\Shop                $shopHelper
54
     * @param \Payone\Core\Helper\Environment         $environmentHelper
55
     * @param \Payone\Core\Helper\Api                 $apiHelper
56
     * @param \Payone\Core\Model\ResourceModel\ApiLog $apiLog
57
     * @param \Payone\Core\Model\Api\Invoice          $invoiceGenerator
58
     * @param \Payone\Core\Helper\Database            $databaseHelper
59
     */
60
    public function __construct(
61
        \Payone\Core\Helper\Shop $shopHelper,
62
        \Payone\Core\Helper\Environment $environmentHelper,
63
        \Payone\Core\Helper\Api $apiHelper,
64
        \Payone\Core\Model\ResourceModel\ApiLog $apiLog,
65
        \Payone\Core\Model\Api\Invoice $invoiceGenerator,
66
        \Payone\Core\Helper\Database $databaseHelper
67
    ) {
68
        parent::__construct($shopHelper, $environmentHelper, $apiHelper, $apiLog);
69
        $this->invoiceGenerator = $invoiceGenerator;
70
        $this->databaseHelper = $databaseHelper;
71
    }
72
73
    /**
74
     * Generate position list for invoice data transmission
75
     *
76
     * @param Order $oOrder
77
     * @return array|false
78
     */
79
    protected function getInvoiceList(Order $oOrder)
80
    {
81
        $aInvoice = $this->shopHelper->getRequestParameter('invoice');
82
83
        $aPositions = [];
84
        $blFull = true;
85
        if ($aInvoice && array_key_exists('items', $aInvoice) !== false) {
86
            foreach ($oOrder->getAllItems() as $oItem) {
87
                if (isset($aInvoice['items'][$oItem->getItemId()]) && $aInvoice['items'][$oItem->getItemId()] > 0) {
88
                    $aPositions[$oItem->getProductId().$oItem->getSku()] = $aInvoice['items'][$oItem->getItemId()];
89
                    if ($aInvoice['items'][$oItem->getItemId()] != $oItem->getQtyOrdered()) {
90
                        $blFull = false;
91
                    }
92
                } else {
93
                    $blFull = false;
94
                }
95
            }
96
        }
97
        if ($blFull === true) {
98
            $aPositions = false;
99
        }
100
        return $aPositions;
101
    }
102
103
    /**
104
     * Send request "capture" to PAYONE server API
105
     *
106
     * @param  PayoneMethod  $oPayment
107
     * @param  InfoInterface $oPaymentInfo
108
     * @param  float         $dAmount
109
     * @return array
110
     */
111
    public function sendRequest(PayoneMethod $oPayment, InfoInterface $oPaymentInfo, $dAmount)
112
    {
113
        $oOrder = $oPaymentInfo->getOrder();
114
115
        $aPositions = $this->getInvoiceList($oOrder);
116
117
        $iTxid = $oPaymentInfo->getParentTransactionId();
118
119
        $this->setOrderId($oOrder->getRealOrderId());
120
121
        $this->addParameter('request', 'capture'); // Request method
122
        $this->addParameter('mode', $oPayment->getOperationMode()); // PayOne Portal Operation Mode (live or test)
123
        $this->addParameter('language', $this->shopHelper->getLocale());
124
125
        // Total order sum in smallest currency unit
126
        $this->addParameter('amount', number_format($dAmount, 2, '.', '') * 100);
127
        $this->addParameter('currency', $oOrder->getOrderCurrencyCode()); // Currency
128
129
        $this->addParameter('txid', $iTxid); // PayOne Transaction ID
130
        $this->addParameter('sequencenumber', $this->databaseHelper->getSequenceNumber($iTxid));
131
132
        $this->addParameter('settleaccount', 'auto');
133
134
        if ($this->apiHelper->isInvoiceDataNeeded($oPayment)) {
135
            $this->invoiceGenerator->addProductInfo($this, $oOrder, $aPositions); // add invoice parameters
0 ignored issues
show
Bug introduced by
$aPositions of type false is incompatible with the type array expected by parameter $aPositions of Payone\Core\Model\Api\Invoice::addProductInfo(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

135
            $this->invoiceGenerator->addProductInfo($this, $oOrder, /** @scrutinizer ignore-type */ $aPositions); // add invoice parameters
Loading history...
136
        }
137
138
        $aResponse = $this->send($oPayment);
139
140
        return $aResponse;
141
    }
142
}
143