Authorization   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 1
cbo 7
dl 0
loc 148
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
A sendRequest() 0 16 1
B setUserParameters() 0 13 5
A setAuthorizationParameters() 0 20 4
A setPaymentParameters() 0 13 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 Payone\Core\Model\PayoneConfig;
30
use Payone\Core\Model\Methods\PayoneMethod;
31
use Magento\Sales\Model\Order;
32
33
/**
34
 * Class for the PAYONE Server API request "(pre)authorization"
35
 *
36
 * @category  Payone
37
 * @package   Payone_Magento2_Plugin
38
 * @author    FATCHIP GmbH <[email protected]>
39
 * @copyright 2003 - 2016 Payone GmbH
40
 * @license   <http://www.gnu.org/licenses/> GNU Lesser General Public License
41
 * @link      http://www.payone.de
42
 */
43
class Authorization extends AddressRequest
44
{
45
    /**
46
     * Invoice generator
47
     *
48
     * @var \Payone\Core\Model\Api\Invoice
49
     */
50
    protected $invoiceGenerator;
51
52
    /**
53
     * Checkout session object
54
     *
55
     * @var \Magento\Checkout\Model\Session
56
     */
57
    protected $checkoutSession;
58
59
    /**
60
     * PAYONE toolkit helper
61
     *
62
     * @var \Payone\Core\Helper\Toolkit
63
     */
64
    protected $toolkitHelper;
65
66
    /**
67
     * Constructor
68
     *
69
     * @param \Payone\Core\Helper\Shop                $shopHelper
70
     * @param \Payone\Core\Helper\Environment         $environmentHelper
71
     * @param \Payone\Core\Helper\Api                 $apiHelper
72
     * @param \Payone\Core\Model\ResourceModel\ApiLog $apiLog
73
     * @param \Payone\Core\Helper\Customer            $customerHelper
74
     * @param \Payone\Core\Model\Api\Invoice          $invoiceGenerator
75
     * @param \Magento\Checkout\Model\Session         $checkoutSession
76
     * @param \Payone\Core\Helper\Toolkit             $toolkitHelper
77
     */
78
    public function __construct(
79
        \Payone\Core\Helper\Shop $shopHelper,
80
        \Payone\Core\Helper\Environment $environmentHelper,
81
        \Payone\Core\Helper\Api $apiHelper,
82
        \Payone\Core\Model\ResourceModel\ApiLog $apiLog,
83
        \Payone\Core\Helper\Customer $customerHelper,
84
        \Payone\Core\Model\Api\Invoice $invoiceGenerator,
85
        \Magento\Checkout\Model\Session $checkoutSession,
86
        \Payone\Core\Helper\Toolkit $toolkitHelper
87
    ) {
88
        parent::__construct($shopHelper, $environmentHelper, $apiHelper, $apiLog, $customerHelper);
89
        $this->invoiceGenerator = $invoiceGenerator;
90
        $this->checkoutSession = $checkoutSession;
91
        $this->toolkitHelper = $toolkitHelper;
92
    }
93
94
    /**
95
     * Send request to PAYONE Server-API with
96
     * request-type "authorization" or "preauthorization"
97
     *
98
     * @param  PayoneMethod $oPayment payment object
99
     * @param  Order        $oOrder   order object
100
     * @param  float        $dAmount  order sum amount
101
     * @return array
102
     */
103
    public function sendRequest(PayoneMethod $oPayment, Order $oOrder, $dAmount)
104
    {
105
        $this->setOrderId($oOrder->getRealOrderId()); // save order id to object for later use
106
107
        $this->addParameter('request', $oPayment->getAuthorizationMode()); // add request type
108
        $this->addParameter('mode', $oPayment->getOperationMode()); // add mode ( live or test )
109
        $this->addParameter('customerid', $oOrder->getCustomerId()); // add customer id
110
        $this->addParameter('aid', $this->shopHelper->getConfigParam('aid')); // add sub account id
111
        $this->setAuthorizationParameters($oPayment, $oOrder, $dAmount); // set authorization params
112
113
        $aResponse = $this->send($oPayment); // send request to PAYONE Server API
114
115
        $this->apiHelper->addPayoneOrderData($oOrder, $this->getParameters(), $aResponse); // add payone data to order
116
117
        return $aResponse;
118
    }
119
120
    /**
121
     * Add user parameters
122
     *
123
     * @param  PayoneMethod $oPayment
124
     * @param  Order        $oOrder
125
     * @return void
126
     */
127
    protected function setUserParameters(PayoneMethod $oPayment, Order $oOrder)
128
    {
129
        $oQuote = $this->checkoutSession->getQuote(); // get quote from session
130
        $oCustomer = $oQuote->getCustomer(); // get customer object from quote
131
        $this->addUserDataParameters($oOrder->getBillingAddress(), $oPayment, $oCustomer->getGender(), $oOrder->getCustomerEmail(), $oCustomer->getDob());
132
133
        $oShipping = $oOrder->getShippingAddress(); // get shipping address from order
134
        if ($oShipping) {// shipping address existing?
135
            $this->addAddress($oShipping, true); // add regular shipping address
136
        } elseif ($oPayment->getCode() == PayoneConfig::METHOD_PAYDIREKT || ($oPayment->getCode() == PayoneConfig::METHOD_PAYPAL && $this->shopHelper->getConfigParam('bill_as_del_address', PayoneConfig::METHOD_PAYPAL, 'payone_payment'))) {
137
            $this->addAddress($oOrder->getBillingAddress(), true); // add billing address as shipping address
138
        }
139
    }
140
141
    /**
142
     * Set the parameters needed for the authorization requests
143
     *
144
     * @param  PayoneMethod $oPayment
145
     * @param  Order        $oOrder
146
     * @param  float        $dAmount
147
     * @return void
148
     */
149
    protected function setAuthorizationParameters(PayoneMethod $oPayment, Order $oOrder, $dAmount)
150
    {
151
        $sRefNr = $this->shopHelper->getConfigParam('ref_prefix').$oOrder->getIncrementId(); // ref_prefix to prevent duplicate refnumbers in testing environments
152
        $sRefNr = $oPayment->formatReferenceNumber($sRefNr); // some payment methods have refnr regulations
153
        $this->addParameter('reference', $sRefNr); // add ref-nr to request
154
        $this->addParameter('amount', number_format($dAmount, 2, '.', '') * 100); // add price to request
155
        $this->addParameter('currency', $oOrder->getOrderCurrencyCode()); // add currency to request
156
        if ($this->shopHelper->getConfigParam('transmit_ip') == '1') {// is IP transmission needed?
157
            $sIp = $this->environmentHelper->getRemoteIp(); // get remote IP
158
            if ($sIp != '') {// is IP not empty
159
                $this->addParameter('ip', $sIp); // add IP address to the request
160
            }
161
        }
162
        $this->setUserParameters($oPayment, $oOrder); // add user data - addresses etc.
163
        $this->setPaymentParameters($oPayment, $oOrder); // add payment specific parameters
164
165
        if ($this->apiHelper->isInvoiceDataNeeded($oPayment)) {
166
            $this->invoiceGenerator->addProductInfo($this, $oOrder); // add invoice parameters
167
        }
168
    }
169
170
    /**
171
     * Set payment-specific parameters
172
     *
173
     * @param  PayoneMethod $oPayment
174
     * @param  Order        $oOrder
175
     * @return void
176
     */
177
    protected function setPaymentParameters(PayoneMethod $oPayment, Order $oOrder)
178
    {
179
        $this->addParameter('clearingtype', $oPayment->getClearingtype()); // add payment type to request
0 ignored issues
show
Bug introduced by
It seems like $oPayment->getClearingtype() targeting Payone\Core\Model\Method...thod::getClearingtype() can also be of type boolean; however, Payone\Core\Model\Api\Request\Base::addParameter() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
180
        $sNarrativeText = $this->toolkitHelper->getNarrativeText($oOrder, $oPayment);
181
        if (!empty($sNarrativeText)) {// narrative text existing?
182
            $this->addParameter('narrative_text', $sNarrativeText); // add narrative text parameter
183
        }
184
        $aPaymentParams = $oPayment->getPaymentSpecificParameters($oOrder); // get payment params specific to the payment type
185
        $this->aParameters = array_merge($this->aParameters, $aPaymentParams); // merge payment params with other params
186
        if ($oPayment->needsRedirectUrls() === true) {// does the used payment type need redirect urls?
187
            $this->addRedirectUrls($oPayment); // add needed redirect urls
188
        }
189
    }
190
}
191