Capture   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 47
dl 0
loc 126
rs 10
c 1
b 0
f 0
wmc 15

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
B getInvoiceList() 0 29 11
A sendRequest() 0 41 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\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
use Magento\Sales\Model\Order\Invoice;
0 ignored issues
show
Bug introduced by
The type Magento\Sales\Model\Order\Invoice 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...
33
34
/**
35
 * Class for the PAYONE Server API request "capture"
36
 */
37
class Capture extends Base
38
{
39
    /**
40
     * @var \Payone\Core\Model\Api\Invoice $invoiceGenerator
41
     */
42
    protected $invoiceGenerator;
43
44
    /**
45
     * PAYONE database helper
46
     *
47
     * @var \Payone\Core\Helper\Database
48
     */
49
    protected $databaseHelper;
50
51
    /**
52
     * Constructor
53
     *
54
     * @param \Payone\Core\Helper\Shop                $shopHelper
55
     * @param \Payone\Core\Helper\Environment         $environmentHelper
56
     * @param \Payone\Core\Helper\Api                 $apiHelper
57
     * @param \Payone\Core\Helper\Toolkit             $toolkitHelper
58
     * @param \Payone\Core\Model\ResourceModel\ApiLog $apiLog
59
     * @param \Payone\Core\Model\Api\Invoice          $invoiceGenerator
60
     * @param \Payone\Core\Helper\Database            $databaseHelper
61
     */
62
    public function __construct(
63
        \Payone\Core\Helper\Shop $shopHelper,
64
        \Payone\Core\Helper\Environment $environmentHelper,
65
        \Payone\Core\Helper\Api $apiHelper,
66
        \Payone\Core\Helper\Toolkit $toolkitHelper,
67
        \Payone\Core\Model\ResourceModel\ApiLog $apiLog,
68
        \Payone\Core\Model\Api\Invoice $invoiceGenerator,
69
        \Payone\Core\Helper\Database $databaseHelper
70
    ) {
71
        parent::__construct($shopHelper, $environmentHelper, $apiHelper, $toolkitHelper, $apiLog);
72
        $this->invoiceGenerator = $invoiceGenerator;
73
        $this->databaseHelper = $databaseHelper;
74
    }
75
76
    /**
77
     * Generate position list for invoice data transmission
78
     *
79
     * @param  Order   $oOrder
80
     * @param  Invoice $oInvoice
81
     * @return array|false
82
     */
83
    protected function getInvoiceList(Order $oOrder, Invoice $oInvoice)
84
    {
85
        $aPositions = [];
86
        $blFull = true;
87
        foreach ($oOrder->getAllItems() as $oItem) {
88
            $blFound = false;
89
            foreach ($oInvoice->getAllItems() as $oInvoiceItem) {
90
                if ($oInvoiceItem->getOrderItemId() == $oItem->getItemId() && $oInvoiceItem->getQty() > 0) {
91
                    $blFound = true;
92
                    $aPositions[$oItem->getProductId().$oItem->getSku()] = $oInvoiceItem->getQty();
93
                    if ($oInvoiceItem->getQty() != $oItem->getQtyOrdered()) {
94
                        $blFull = false;
95
                    }
96
                }
97
            }
98
            if ($blFound === false) {
99
                $blFull = false;
100
            }
101
        }
102
        if ($blFull !== true && $oInvoice->getBaseDiscountAmount() != 0) {
103
            $aPositions['discount'] = $oInvoice->getBaseDiscountAmount();
104
            if ($this->shopHelper->getConfigParam('currency', 'global', 'payone_general', $this->storeCode) == 'display') {
105
                $aPositions['discount'] = $oInvoice->getDiscountAmount();
106
            }
107
        }
108
        if ($blFull === true) {
109
            $aPositions = false;
110
        }
111
        return $aPositions;
112
    }
113
114
    /**
115
     * Send request "capture" to PAYONE server API
116
     *
117
     * @param  PayoneMethod  $oPayment
118
     * @param  InfoInterface $oPaymentInfo
119
     * @param  float         $dAmount
120
     * @return array
121
     */
122
    public function sendRequest(PayoneMethod $oPayment, InfoInterface $oPaymentInfo, $dAmount)
123
    {
124
        $oOrder = $oPaymentInfo->getOrder();
125
126
        $this->setStoreCode($oOrder->getStore()->getCode());
127
128
        $oInvoice = $oPaymentInfo->getOrder()->getInvoiceCollection()->getLastItem();
129
        $aPositions = $this->getInvoiceList($oOrder, $oInvoice);
130
131
        if ($this->shopHelper->getConfigParam('currency', 'global', 'payone_general', $this->storeCode) == 'display') {
132
            $dAmount = $oInvoice->getGrandTotal(); // send display amount instead of base amount
133
        }
134
135
        $iTxid = $oPaymentInfo->getParentTransactionId();
136
137
        $this->setOrderId($oOrder->getRealOrderId());
138
139
        $this->addParameter('request', 'capture'); // Request method
140
        $this->addParameter('mode', $oPayment->getOperationMode()); // PayOne Portal Operation Mode (live or test)
141
        $this->addParameter('language', $this->shopHelper->getLocale());
142
143
        // Total order sum in smallest currency unit
144
        $this->addParameter('amount', number_format($dAmount, 2, '.', '') * 100); // add price to request
145
        $this->addParameter('currency', $this->apiHelper->getCurrencyFromOrder($oOrder)); // add currency to request
146
147
        $this->addParameter('txid', $iTxid); // PayOne Transaction ID
148
        $this->addParameter('sequencenumber', $this->databaseHelper->getSequenceNumber($iTxid));
149
150
        $this->addParameter('settleaccount', 'auto');
151
152
        $this->aParameters = array_merge($this->aParameters, $oPayment->getPaymentSpecificCaptureParameters($oOrder));
153
154
        if ($this->apiHelper->isInvoiceDataNeeded($oPayment)) {
155
            $this->invoiceGenerator->addProductInfo($this, $oOrder, $aPositions); // add invoice parameters
0 ignored issues
show
Bug introduced by
It seems like $aPositions can also be of type false; however, parameter $aPositions of Payone\Core\Model\Api\Invoice::addProductInfo() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

155
            $this->invoiceGenerator->addProductInfo($this, $oOrder, /** @scrutinizer ignore-type */ $aPositions); // add invoice parameters
Loading history...
156
        }
157
158
        $aResponse = $this->send($oPayment);
159
160
        $this->apiHelper->addPayoneOrderData($oOrder, false, $aResponse); // add payone data to order
161
162
        return $aResponse;
163
    }
164
}
165