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

Authorization::addCustomParameters()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 13
c 1
b 0
f 0
rs 9.2
cc 4
eloc 8
nc 4
nop 1
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
        if ($oPayment->hasCustomConfig()) {// if payment type doesnt use the global settings
113
            $this->addCustomParameters($oPayment); // add custom connection settings
114
        }
115
116
        $aResponse = $this->send(); // send request to PAYONE Server API
117
118
        $this->apiHelper->addPayoneOrderData($oOrder, $this->getParameters(), $aResponse); // add payone data to order
119
120
        return $aResponse;
121
    }
122
123
    /**
124
     * Add user parameters
125
     *
126
     * @param  PayoneMethod $oPayment
127
     * @param  Order        $oOrder
128
     * @return void
129
     */
130
    protected function setUserParameters(PayoneMethod $oPayment, Order $oOrder)
131
    {
132
        $oQuote = $this->checkoutSession->getQuote(); // get quote from session
133
        $oCustomer = $oQuote->getCustomer(); // get customer object from quote
134
        $this->addUserDataParameters($oOrder->getBillingAddress(), $oPayment, $oCustomer->getGender(), $oOrder->getCustomerEmail(), $oCustomer->getDob());
135
136
        $oShipping = $oOrder->getShippingAddress(); // get shipping address from order
137
        if ($oShipping) {// shipping address existing?
138
            $this->addAddress($oShipping, true); // add regular shipping address
139
        } elseif ($oPayment->getCode() == PayoneConfig::METHOD_PAYDIREKT || ($oPayment->getCode() == PayoneConfig::METHOD_PAYPAL && $this->shopHelper->getConfigParam('bill_as_del_address', PayoneConfig::METHOD_PAYPAL, 'payone_payment'))) {
140
            $this->addAddress($oOrder->getBillingAddress(), true); // add billing address as shipping address
141
        }
142
    }
143
144
    /**
145
     * Set the parameters needed for the authorization requests
146
     *
147
     * @param  PayoneMethod $oPayment
148
     * @param  Order        $oOrder
149
     * @param  float        $dAmount
150
     * @return void
151
     */
152
    protected function setAuthorizationParameters(PayoneMethod $oPayment, Order $oOrder, $dAmount)
153
    {
154
        $sRefNr = $this->shopHelper->getConfigParam('ref_prefix').$oOrder->getIncrementId(); // ref_prefix to prevent duplicate refnumbers in testing environments
155
        $sRefNr = $oPayment->formatReferenceNumber($sRefNr); // some payment methods have refnr regulations
156
        $this->addParameter('reference', $sRefNr); // add ref-nr to request
157
        $this->addParameter('amount', number_format($dAmount, 2, '.', '') * 100); // add price to request
158
        $this->addParameter('currency', $oOrder->getOrderCurrencyCode()); // add currency to request
159
        if ($this->shopHelper->getConfigParam('transmit_ip') == '1') {// is IP transmission needed?
160
            $sIp = $this->environmentHelper->getRemoteIp(); // get remote IP
161
            if ($sIp != '') {// is IP not empty
162
                $this->addParameter('ip', $sIp); // add IP address to the request
163
            }
164
        }
165
        $this->setUserParameters($oPayment, $oOrder); // add user data - addresses etc.
166
        $this->setPaymentParameters($oPayment, $oOrder); // add payment specific parameters
167
168
        if ($this->apiHelper->isInvoiceDataNeeded($oPayment)) {
169
            $this->invoiceGenerator->addProductInfo($this, $oOrder); // add invoice parameters
170
        }
171
    }
172
173
    /**
174
     * Set payment-specific parameters
175
     *
176
     * @param  PayoneMethod $oPayment
177
     * @param  Order        $oOrder
178
     * @return void
179
     */
180
    protected function setPaymentParameters(PayoneMethod $oPayment, Order $oOrder)
181
    {
182
        $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...
183
        $sNarrativeText = $this->toolkitHelper->getNarrativeText($oOrder, $oPayment);
184
        if (!empty($sNarrativeText)) {// narrative text existing?
185
            $this->addParameter('narrative_text', $sNarrativeText); // add narrative text parameter
186
        }
187
        $aPaymentParams = $oPayment->getPaymentSpecificParameters($oOrder); // get payment params specific to the payment type
188
        $this->aParameters = array_merge($this->aParameters, $aPaymentParams); // merge payment params with other params
189
        if ($oPayment->needsRedirectUrls() === true) {// does the used payment type need redirect urls?
190
            $this->addRedirectUrls($oPayment); // add needed redirect urls
191
        }
192
    }
193
}
194