/
Info.php
232 lines (216 loc) · 7 KB
/
Info.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Sales\Model\Order\Payment;
use Magento\Sales\Model\AbstractModel;
use Magento\Payment\Model\Method\Substitution;
use Magento\Payment\Model\InfoInterface;
/**
*
* Payment information model
*/
class Info extends AbstractModel implements InfoInterface
{
/**
* Additional information container
*
* @var array
*/
protected $additionalInformation = [];
/**
* Payment data
*
* @var \Magento\Payment\Helper\Data
*/
protected $paymentData;
/**
* @var \Magento\Framework\Encryption\EncryptorInterface
*/
protected $encryptor;
/**
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
* @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory
* @param \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory,
* @param \Magento\Payment\Helper\Data $paymentData
* @param \Magento\Framework\Encryption\EncryptorInterface $encryptor
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
* @param array $data
*/
public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
\Magento\Framework\Api\AttributeValueFactory $customAttributeFactory,
\Magento\Payment\Helper\Data $paymentData,
\Magento\Framework\Encryption\EncryptorInterface $encryptor,
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = []
) {
$this->paymentData = $paymentData;
$this->encryptor = $encryptor;
parent::__construct(
$context,
$registry,
$extensionFactory,
$customAttributeFactory,
$resource,
$resourceCollection,
$data
);
}
/**
* Retrieve data
*
* @param string $key
* @param mixed $index
* @return mixed
*/
public function getData($key = '', $index = null)
{
if ('cc_number' === $key) {
$ccNumber = parent::getData('cc_number');
$ccNumberEnc = parent::getData('cc_number_enc');
if (empty($ccNumber) && !empty($ccNumberEnc)) {
$this->setData('cc_number', $this->decrypt($ccNumberEnc));
}
}
if ('cc_cid' === $key) {
$ccCid = parent::getData('cc_cid');
$ccCidEnc = parent::getData('cc_cid_enc');
if (empty($ccCid) && !empty($ccCidEnc)) {
$this->setData('cc_cid', $this->decrypt($ccCidEnc));
}
}
return parent::getData($key, $index);
}
/**
* Retrieve payment method model object
*
* @return \Magento\Payment\Model\MethodInterface
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function getMethodInstance()
{
if (!$this->hasMethodInstance()) {
if (!$this->getMethod()) {
throw new \Magento\Framework\Exception\LocalizedException(
__('The payment method you requested is not available.')
);
}
try {
$instance = $this->paymentData->getMethodInstance($this->getMethod());
} catch (\UnexpectedValueException $e) {
$instance = $this->paymentData->getMethodInstance(Substitution::CODE);
}
$instance->setInfoInstance($this);
$this->setMethodInstance($instance);
}
return $this->getData('method_instance');
}
/**
* Encrypt data
*
* @param string $data
* @return string
*/
public function encrypt($data)
{
return $this->encryptor->encrypt($data);
}
/**
* Decrypt data
*
* @param string $data
* @return string
*/
public function decrypt($data)
{
return $this->encryptor->decrypt($data);
}
/**
* Additional information setter
* Updates data inside the 'additional_information' array
* or all 'additional_information' if key is data array
*
* @param string|array $key
* @param mixed $value
* @return $this
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function setAdditionalInformation($key, $value = null)
{
if (is_object($value)) {
throw new \Magento\Framework\Exception\LocalizedException(__('The payment disallows storing objects.'));
}
$this->initAdditionalInformation();
if (is_array($key) && $value === null) {
$this->additionalInformation = $key;
} else {
$this->additionalInformation[$key] = $value;
}
return $this->setData('additional_information', $this->additionalInformation);
}
/**
* Getter for entire additional_information value or one of its element by key
*
* @param string $key
* @return array|null|mixed
*/
public function getAdditionalInformation($key = null)
{
$this->initAdditionalInformation();
if (null === $key) {
return $this->additionalInformation;
}
return isset($this->additionalInformation[$key]) ? $this->additionalInformation[$key] : null;
}
/**
* Unsetter for entire additional_information value or one of its element by key
*
* @param string $key
* @return $this
*/
public function unsAdditionalInformation($key = null)
{
if ($key && isset($this->additionalInformation[$key])) {
unset($this->additionalInformation[$key]);
return $this->setData('additional_information', $this->additionalInformation);
} elseif (null === $key) {
$this->additionalInformation = [];
return $this->unsetData('additional_information');
}
return $this;
}
/**
* Check whether there is additional information by specified key
*
* @param mixed|null $key
* @return bool
*/
public function hasAdditionalInformation($key = null)
{
$this->initAdditionalInformation();
return null === $key ? !empty($this->additionalInformation) : array_key_exists(
$key,
$this->additionalInformation
);
}
/**
* Initialize additional information container with data from model
* if property empty
*
* @return void
*/
protected function initAdditionalInformation()
{
$additionalInfo = $this->getData('additional_information');
if (empty($this->additionalInformation) && $additionalInfo) {
$this->additionalInformation = $additionalInfo;
}
}
}