- Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathProfileConfig.php
More file actions
245 lines (221 loc) · 9.1 KB
/
ProfileConfig.php
File metadata and controls
245 lines (221 loc) · 9.1 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
<?php
/**
* Copyright (c) Ratepay GmbH
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace RatePAY\Payment\Helper;
use RatePAY\Payment\Model\Entities\ProfileConfiguration;
use RatePAY\Payment\Model\Method\AbstractMethod;
class ProfileConfig extends \Magento\Framework\App\Helper\AbstractHelper
{
protected $sConfigPath = 'payment/ratepay_config/ratepay/profile_config';
protected $sConfigPathBackend = 'payment/ratepay_config/ratepay_backend/profile_config_backend';
/**
* @var \RatePAY\Payment\Controller\LibraryController
*/
protected $libraryController;
/**
* @var \RatePAY\Payment\Model\LibraryModel
*/
protected $libraryModel;
/**
* @var \RatePAY\Payment\Model\ResourceModel\ProfileConfiguration
*/
protected $profileConfigResource;
/**
* @var Data
*/
protected $ratepayHelper;
/**
* @var \RatePAY\Payment\Model\Entities\ProfileConfigurationFactory
*/
protected $profileConfigFactory;
/**
* Payment constructor.
* @param \Magento\Framework\App\Helper\Context $context
* @param \RatePAY\Payment\Controller\LibraryController $libraryController
* @param \RatePAY\Payment\Model\LibraryModel $libraryModel
* @param \RatePAY\Payment\Model\ResourceModel\ProfileConfiguration $profileConfigResource
* @param \RatePAY\Payment\Helper\Data $ratepayHelper
* @param \RatePAY\Payment\Model\Entities\ProfileConfigurationFactory $profileConfigFactory
*/
public function __construct(
\Magento\Framework\App\Helper\Context $context,
\RatePAY\Payment\Controller\LibraryController $libraryController,
\RatePAY\Payment\Model\LibraryModel $libraryModel,
\RatePAY\Payment\Model\ResourceModel\ProfileConfiguration $profileConfigResource,
\RatePAY\Payment\Helper\Data $ratepayHelper,
\RatePAY\Payment\Model\Entities\ProfileConfigurationFactory $profileConfigFactory
) {
parent::__construct($context);
$this->libraryController = $libraryController;
$this->libraryModel = $libraryModel;
$this->profileConfigResource = $profileConfigResource;
$this->ratepayHelper = $ratepayHelper;
$this->profileConfigFactory = $profileConfigFactory;
}
/**
* @param string $sProfileId
* @param string $sSecurityCode
* @param bool $blSandbox
* @return bool
*/
public function importProfileConfiguration($sProfileId, $sSecurityCode, $blSandbox)
{
$oHead = $this->libraryModel->getRequestHead(null, null, null, null, $sProfileId, $sSecurityCode);
$oProfileRequest = $this->libraryController->callProfileRequest($oHead, (bool)$blSandbox);
if ($oProfileRequest->isSuccessful()) {
$oResult = $oProfileRequest->getResult();
$this->profileConfigResource->insertProfileConfiguration($oResult);
return true;
}
return false;
}
/**
* Returns profiles from config
*
* @param string|null $sPaymentMethod
* @param string|null $sStoreCode
* @return array
*/
public function getConfiguredProfiles($sPaymentMethod = null, $sStoreCode = null)
{
$aReturnProfiles = [];
$sConfigPath = $this->sConfigPath;
if ($sPaymentMethod && stripos($sPaymentMethod, AbstractMethod::BACKEND_SUFFIX) !== false) {
$sConfigPath = $this->sConfigPathBackend;
}
$sShopConfig = $this->ratepayHelper->getRpConfigDataByPath($sConfigPath, $sStoreCode);
if (!empty($sShopConfig)) {
$aProfiles = json_decode($sShopConfig, true);
if (is_array($aProfiles)) {
foreach ($aProfiles as $aProfile) {
if (!empty($aProfile['profileId'])) {
$aReturnProfiles[] = $aProfile;
}
}
}
}
return $aReturnProfiles;
}
/**
* Returns configured security code for given profile id
*
* @param string $sProfileId
* @return string|null
*/
public function getSecurityCodeForProfileId($sProfileId, $sPaymentMethod)
{
$aProfileData = $this->getConfiguredProfiles($sPaymentMethod);
foreach ($aProfileData as $aProfile) {
if ($aProfile['profileId'] == $sProfileId) {
return $aProfile['securityCode'];
}
}
return null;
}
/**
* Returns configured sandbox mode for given profile id
*
* @param string $sProfileId
* @param string|null $sPaymentMethod
* @return bool|null
*/
public function getSandboxModeForProfileId($sProfileId, $sPaymentMethod = null)
{
$aProfileData = $this->getConfiguredProfiles($sPaymentMethod);
foreach ($aProfileData as $aProfile) {
if ($aProfile['profileId'] == $sProfileId) {
return (bool)$aProfile['sandbox'];
}
}
return null;
}
/**
* Requests profile configuration for all configured profiles from Ratepay API and inserts or updates it in the database
*
* @return void
*/
public function refreshProfileConfigurations($sMethodCode)
{
$aProfiles = $this->getConfiguredProfiles($sMethodCode);
foreach ($aProfiles as $aProfile) {
$this->importProfileConfiguration($aProfile['profileId'], $aProfile['securityCode'], (bool)$aProfile['sandbox']);
}
}
/**
* Returns array with all configured profile models
*
* @return ProfileConfiguration[]
*/
public function getProfileData($sMethodCode)
{
$aProfileData = $this->getConfiguredProfiles($sMethodCode);
$aProfiles = [];
foreach ($aProfileData as $aProfile) {
$oProfileConfig = $this->profileConfigFactory->create();
$oProfileConfig->load($aProfile['profileId']);
if (!empty($oProfileConfig->getData('profile_id'))) {
$aProfiles[] = $oProfileConfig;
}
}
return $aProfiles;
}
/**
* Returns Profile applicable for the current order process
*
* @param \Magento\Quote\Api\Data\CartInterface $oQuote
* @param string $sMethodCode
* @param string $sStoreCode
* @param double $dGrandTotal
* @param string $sBillingCountryId
* @param string $sShippingCountryId
* @param string $sCurrency
* @return ProfileConfiguration|false
*/
public function getMatchingProfile(\Magento\Quote\Api\Data\CartInterface $oQuote = null, $sMethodCode = null, $sStoreCode = null, $dGrandTotal = null, $sBillingCountryId = null, $sShippingCountryId = null, $sCurrency = null)
{
$aProfileData = $this->getConfiguredProfiles($sMethodCode, $sStoreCode);
foreach ($aProfileData as $aProfile) {
/** @var ProfileConfiguration $oProfileConfig */
$oProfileConfig = $this->profileConfigFactory->create();
$oProfileConfig->load($aProfile['profileId']);
$oProfileConfig->setSandboxMode((bool)$aProfile['sandbox']);
$oProfileConfig->setSecurityCode($aProfile['securityCode']);
if ($oProfileConfig->isApplicableForQuote($oQuote, $sMethodCode, $dGrandTotal, $sBillingCountryId, $sShippingCountryId, $sCurrency) === true) {
return $oProfileConfig;
}
}
return false;
}
/**
* Returns all matching profiles for the current order process
*
* @param \Magento\Quote\Api\Data\CartInterface $oQuote
* @param string $sMethodCode
* @param string $sStoreCode
* @param double $dGrandTotal
* @param string $sBillingCountryId
* @param string $sShippingCountryId
* @param string $sCurrency
* @return ProfileConfiguration[]
*/
public function getAllMatchingProfiles(\Magento\Quote\Api\Data\CartInterface $oQuote, $sMethodCode, $sStoreCode = null, $dGrandTotal = null, $sBillingCountryId = null, $sShippingCountryId = null, $sCurrency = null)
{
$aProfiles = [];
$aProfileData = $this->getConfiguredProfiles($sMethodCode, $sStoreCode);
foreach ($aProfileData as $aProfile) {
/** @var ProfileConfiguration $oProfileConfig */
$oProfileConfig = $this->profileConfigFactory->create();
$oProfileConfig->load($aProfile['profileId']);
$oProfileConfig->setSandboxMode((bool)$aProfile['sandbox']);
$oProfileConfig->setSecurityCode($aProfile['securityCode']);
if ($oProfileConfig->isApplicableForQuote($oQuote, $sMethodCode, $dGrandTotal, $sBillingCountryId, $sShippingCountryId, $sCurrency) === true) {
$aProfiles[] = $oProfileConfig;
}
}
return $aProfiles;
}
}