Completed
Push — master ( 055e45...5df371 )
by Florian
12s
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
c 1
b 0
f 0
dl 0
loc 13
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
     * Map for custom parameters to be added $sParamName => $sConfigName
68
     *
69
     * @var array
70
     */
71
    protected $aCustomParamMap = [
72
        'mid' => 'mid',
73
        'portalid' => 'portalid',
74
        'aid' => 'aid',
75
        'key' => 'key',
76
        'request' => 'request',
77
    ];
78
79
    /**
80
     * Constructor
81
     *
82
     * @param \Payone\Core\Helper\Shop                $shopHelper
83
     * @param \Payone\Core\Helper\Environment         $environmentHelper
84
     * @param \Payone\Core\Helper\Api                 $apiHelper
85
     * @param \Payone\Core\Model\ResourceModel\ApiLog $apiLog
86
     * @param \Payone\Core\Helper\Customer            $customerHelper
87
     * @param \Payone\Core\Model\Api\Invoice          $invoiceGenerator
88
     * @param \Magento\Checkout\Model\Session         $checkoutSession
89
     * @param \Payone\Core\Helper\Toolkit             $toolkitHelper
90
     */
91
    public function __construct(
92
        \Payone\Core\Helper\Shop $shopHelper,
93
        \Payone\Core\Helper\Environment $environmentHelper,
94
        \Payone\Core\Helper\Api $apiHelper,
95
        \Payone\Core\Model\ResourceModel\ApiLog $apiLog,
96
        \Payone\Core\Helper\Customer $customerHelper,
97
        \Payone\Core\Model\Api\Invoice $invoiceGenerator,
98
        \Magento\Checkout\Model\Session $checkoutSession,
99
        \Payone\Core\Helper\Toolkit $toolkitHelper
100
    ) {
101
        parent::__construct($shopHelper, $environmentHelper, $apiHelper, $apiLog, $customerHelper);
102
        $this->invoiceGenerator = $invoiceGenerator;
103
        $this->checkoutSession = $checkoutSession;
104
        $this->toolkitHelper = $toolkitHelper;
105
    }
106
107
    /**
108
     * Send request to PAYONE Server-API with
109
     * request-type "authorization" or "preauthorization"
110
     *
111
     * @param  PayoneMethod $oPayment payment object
112
     * @param  Order        $oOrder   order object
113
     * @param  float        $dAmount  order sum amount
114
     * @return array
115
     */
116
    public function sendRequest(PayoneMethod $oPayment, Order $oOrder, $dAmount)
117
    {
118
        $this->setOrderId($oOrder->getRealOrderId()); // save order id to object for later use
119
120
        $this->addParameter('request', $oPayment->getAuthorizationMode()); // add request type
121
        $this->addParameter('mode', $oPayment->getOperationMode()); // add mode ( live or test )
122
        $this->addParameter('customerid', $oOrder->getCustomerId()); // add customer id
123
        $this->addParameter('aid', $this->shopHelper->getConfigParam('aid')); // add sub account id
124
        $this->setAuthorizationParameters($oPayment, $oOrder, $dAmount); // set authorization params
125
        if ($oPayment->hasCustomConfig()) {// if payment type doesnt use the global settings
126
            $this->addCustomParameters($oPayment); // add custom connection settings
127
        }
128
129
        $aResponse = $this->send(); // send request to PAYONE Server API
130
131
        $this->apiHelper->addPayoneOrderData($oOrder, $this->getParameters(), $aResponse); // add payone data to order
132
133
        return $aResponse;
134
    }
135
136
    /**
137
     * Add user parameters
138
     *
139
     * @param  PayoneMethod $oPayment
140
     * @param  Order        $oOrder
141
     * @return void
142
     */
143
    protected function setUserParameters(PayoneMethod $oPayment, Order $oOrder)
144
    {
145
        $oQuote = $this->checkoutSession->getQuote(); // get quote from session
146
        $oCustomer = $oQuote->getCustomer(); // get customer object from quote
147
        $this->addUserDataParameters($oOrder->getBillingAddress(), $oPayment, $oCustomer->getGender(), $oOrder->getCustomerEmail(), $oCustomer->getDob());
148
149
        $oShipping = $oOrder->getShippingAddress(); // get shipping address from order
150
        if ($oShipping) {// shipping address existing?
151
            $this->addAddress($oShipping, true); // add regular shipping address
152
        } elseif ($oPayment->getCode() == PayoneConfig::METHOD_PAYDIREKT || ($oPayment->getCode() == PayoneConfig::METHOD_PAYPAL && $this->shopHelper->getConfigParam('bill_as_del_address', PayoneConfig::METHOD_PAYPAL, 'payone_payment'))) {
153
            $this->addAddress($oOrder->getBillingAddress(), true); // add billing address as shipping address
154
        }
155
    }
156
157
    /**
158
     * Set the parameters needed for the authorization requests
159
     *
160
     * @param  PayoneMethod $oPayment
161
     * @param  Order        $oOrder
162
     * @param  float        $dAmount
163
     * @return void
164
     */
165
    protected function setAuthorizationParameters(PayoneMethod $oPayment, Order $oOrder, $dAmount)
166
    {
167
        $sRefNr = $this->shopHelper->getConfigParam('ref_prefix').$oOrder->getIncrementId(); // ref_prefix to prevent duplicate refnumbers in testing environments
168
        $sRefNr = $oPayment->formatReferenceNumber($sRefNr); // some payment methods have refnr regulations
169
        $this->addParameter('reference', $sRefNr); // add ref-nr to request
170
        $this->addParameter('amount', number_format($dAmount, 2, '.', '') * 100); // add price to request
171
        $this->addParameter('currency', $oOrder->getOrderCurrencyCode()); // add currency to request
172
        if ($this->shopHelper->getConfigParam('transmit_ip') == '1') {// is IP transmission needed?
173
            $sIp = $this->environmentHelper->getRemoteIp(); // get remote IP
174
            if ($sIp != '') {// is IP not empty
175
                $this->addParameter('ip', $sIp); // add IP address to the request
176
            }
177
        }
178
        $this->setUserParameters($oPayment, $oOrder); // add user data - addresses etc.
179
        $this->setPaymentParameters($oPayment, $oOrder); // add payment specific parameters
180
181
        if ($this->apiHelper->isInvoiceDataNeeded($oPayment)) {//
182
            $this->invoiceGenerator->addProductInfo($this, $oOrder); // add invoice parameters
183
        }
184
    }
185
186
    /**
187
     * Set payment-specific parameters
188
     *
189
     * @param  PayoneMethod $oPayment
190
     * @param  Order        $oOrder
191
     * @return void
192
     */
193
    protected function setPaymentParameters(PayoneMethod $oPayment, Order $oOrder)
194
    {
195
        $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...
196
        $sNarrativeText = $this->toolkitHelper->getNarrativeText($oOrder, $oPayment);
197
        if (!empty($sNarrativeText)) {// narrative text existing?
198
            $this->addParameter('narrative_text', $sNarrativeText); // add narrative text parameter
199
        }
200
        $aPaymentParams = $oPayment->getPaymentSpecificParameters($oOrder); // get payment params specific to the payment type
201
        $this->aParameters = array_merge($this->aParameters, $aPaymentParams); // merge payment params with other params
202
        if ($oPayment->needsRedirectUrls() === true) {// does the used payment type need redirect urls?
203
            $this->addRedirectUrls($oPayment); // add needed redirect urls
204
        }
205
    }
206
207
    /**
208
     * Add non-global parameters specifically configured in the payment type
209
     *
210
     * @param  PayoneMethod $oPayment
211
     * @return void
212
     */
213
    protected function addCustomParameters(PayoneMethod $oPayment)
214
    {
215
        foreach ($this->aCustomParamMap as $sParamName => $sConfigName) {// add all custom parameters
216
            $sCustomConfig = $oPayment->getCustomConfigParam($sConfigName); // get custom config param
217
            if (!empty($sCustomConfig)) { // only add if the param is configured
218
                if ($sConfigName == 'key') {
219
                    $this->addParameter($sParamName, md5($sCustomConfig)); // key isn't hashed in db
220
                } else {
221
                    $this->addParameter($sParamName, $sCustomConfig); // add custom param to request
222
                }
223
            }
224
        }
225
    }
226
}
227