Completed
Push — master ( 6c4da9...442164 )
by Florian
14s queued 11s
created

SafeInvoice::handleResponse()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 8.2222
c 0
b 0
f 0
cc 7
eloc 11
nc 4
nop 2
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\Methods;
28
29
use Payone\Core\Model\PayoneConfig;
30
use Magento\Sales\Model\Order;
31
use Magento\Framework\DataObject;
32
33
/**
34
 * Model for safe invoice payment method
35
 */
36
class SafeInvoice extends PayoneMethod
37
{
38
    /**
39
     * Payment method code
40
     *
41
     * @var string
42
     */
43
    protected $_code = PayoneConfig::METHOD_SAFE_INVOICE;
44
45
    /**
46
     * Clearingtype for PAYONE authorization request
47
     *
48
     * @var string
49
     */
50
    protected $sClearingtype = 'rec';
51
52
    /**
53
     * Determines if the invoice information has to be added
54
     * to the authorization-request
55
     *
56
     * @var bool
57
     */
58
    protected $blNeedsProductInfo = true;
59
60
    /**
61
     * Payment ban entity
62
     *
63
     * @var \Payone\Core\Model\ResourceModel\PaymentBan
64
     */
65
    protected $paymentBan;
66
67
    /**
68
     * Payment ban duration in hours
69
     *
70
     * @var int
71
     */
72
    protected $iBanDuration = 24;
73
74
    /**
75
     * Constructor
76
     *
77
     * @param \Magento\Framework\Model\Context                        $context
78
     * @param \Magento\Framework\Registry                             $registry
79
     * @param \Magento\Framework\Api\ExtensionAttributesFactory       $extensionFactory
80
     * @param \Magento\Framework\Api\AttributeValueFactory            $customAttrFactory
81
     * @param \Magento\Payment\Helper\Data                            $paymentData
82
     * @param \Magento\Framework\App\Config\ScopeConfigInterface      $scopeConfig
83
     * @param \Magento\Payment\Model\Method\Logger                    $logger
84
     * @param \Payone\Core\Helper\Toolkit                             $toolkitHelper
85
     * @param \Payone\Core\Helper\Shop                                $shopHelper
86
     * @param \Magento\Framework\Url                                  $url
87
     * @param \Magento\Checkout\Model\Session                         $checkoutSession
88
     * @param \Payone\Core\Model\Api\Request\Debit                    $debitRequest
89
     * @param \Payone\Core\Model\Api\Request\Capture                  $captureRequest
90
     * @param \Payone\Core\Model\Api\Request\Authorization            $authorizationRequest
91
     * @param \Payone\Core\Model\ResourceModel\PaymentBan             $paymentBan
92
     * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
93
     * @param \Magento\Framework\Data\Collection\AbstractDb           $resourceCollection
94
     * @param array                                                   $data
95
     */
96 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
        \Magento\Framework\Model\Context $context,
98
        \Magento\Framework\Registry $registry,
99
        \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
100
        \Magento\Framework\Api\AttributeValueFactory $customAttrFactory,
101
        \Magento\Payment\Helper\Data $paymentData,
102
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
103
        \Magento\Payment\Model\Method\Logger $logger,
104
        \Payone\Core\Helper\Toolkit $toolkitHelper,
105
        \Payone\Core\Helper\Shop $shopHelper,
106
        \Magento\Framework\Url $url,
107
        \Magento\Checkout\Model\Session $checkoutSession,
108
        \Payone\Core\Model\Api\Request\Debit $debitRequest,
109
        \Payone\Core\Model\Api\Request\Capture $captureRequest,
110
        \Payone\Core\Model\Api\Request\Authorization $authorizationRequest,
111
        \Payone\Core\Model\ResourceModel\PaymentBan $paymentBan,
112
        \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
113
        \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
114
        array $data = []
115
    ) {
116
        parent::__construct($context, $registry, $extensionFactory, $customAttrFactory, $paymentData, $scopeConfig, $logger, $toolkitHelper, $shopHelper, $url, $checkoutSession, $debitRequest, $captureRequest, $authorizationRequest, $resource, $resourceCollection, $data);
117
        $this->paymentBan = $paymentBan;
118
    }
119
120
    /**
121
     * Return parameters specific to this payment type
122
     *
123
     * @param  Order $oOrder
124
     * @return array
125
     */
126
    public function getPaymentSpecificParameters(Order $oOrder)
127
    {
128
        $aParams = ['clearingsubtype' => 'POV'];
129
130
        $sDob = $this->getInfoInstance()->getAdditionalInformation('dob');
131
        if ($sDob) {
132
            $aParams['birthday'] = $sDob;
133
        }
134
135
        $sBusinessrelation = 'b2c';
136
        if (!empty($oOrder->getBillingAddress()->getCompany())) {
137
            $sBusinessrelation = 'b2b';
138
        }
139
        $aParams['businessrelation'] = $sBusinessrelation;
140
141
        return $aParams;
142
    }
143
144
    /**
145
     * Returns formatted birthday if possible
146
     *
147
     * @param  DataObject $data
148
     * @return string|false
149
     */
150 View Code Duplication
    protected function getFormattedBirthday(DataObject $data)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
151
    {
152
        $sFormattedDob = false;
153
154
        $sBirthday = $this->toolkitHelper->getAdditionalDataEntry($data, 'birthday');
155
        $sBirthmonth = $this->toolkitHelper->getAdditionalDataEntry($data, 'birthmonth');
156
        $sBirthyear = $this->toolkitHelper->getAdditionalDataEntry($data, 'birthyear');
157
        if ($sBirthday && $sBirthmonth && $sBirthyear) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $sBirthday of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
Bug Best Practice introduced by
The expression $sBirthmonth of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
Bug Best Practice introduced by
The expression $sBirthyear of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
158
            $sDob = $sBirthyear.'-'.$sBirthmonth.'-'.$sBirthday;
159
            $iDobTime = strtotime($sDob);
160
            if ($iDobTime !== false) {
161
                $sFormattedDob = date('Ymd', $iDobTime);
162
            }
163
        }
164
        return $sFormattedDob;
165
    }
166
167
    /**
168
     * Add the checkout-form-data to the checkout session
169
     *
170
     * @param  DataObject $data
171
     * @return $this
172
     */
173
    public function assignData(DataObject $data)
174
    {
175
        parent::assignData($data);
176
177
        $sFormattedDob = $this->getFormattedBirthday($data);
178
        if ($sFormattedDob !== false) {
179
            $oInfoInstance = $this->getInfoInstance();
180
            $oInfoInstance->setAdditionalInformation('dob', $sFormattedDob);
181
        }
182
183
        return $this;
184
    }
185
186
    /**
187
     * Perform certain actions with the response
188
     *
189
     * @param  array $aResponse
190
     * @param  Order $oOrder
191
     * @return void
192
     */
193
    protected function handleResponse($aResponse, Order $oOrder)
194
    {
195
        if (isset($aResponse['status']) && $aResponse['status'] == 'ERROR'
196
            && isset($aResponse['errorcode']) && $aResponse['errorcode'] == '351'
197
        ) {
198
            if (!empty($oOrder->getCustomerId())) {
199
                $this->paymentBan->addPaymentBan($this->getCode(), $oOrder->getCustomerId(), $this->iBanDuration);
200
            } else { // guest checkout
201
                $aBans = $this->checkoutSession->getPayonePaymentBans();
202
                if (!$aBans) {
203
                    $aBans = [];
204
                }
205
                $aBans[$this->getCode()] = $this->paymentBan->getBanEndDate($this->iBanDuration);
206
                $this->checkoutSession->setPayonePaymentBans($aBans);
207
            }
208
        }
209
    }
210
}
211