Skip to content

Commit 4362365

Browse files
committed
MAG2-135 - Fixed problem with score of creditrating getting lost on 2nd order try in check after payment selection mode
1 parent f3d1a98 commit 4362365

File tree

8 files changed

+180
-22
lines changed

8 files changed

+180
-22
lines changed

Helper/Database.php

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,32 @@ class Database extends \Payone\Core\Helper\Base
4343
*/
4444
protected $databaseResource;
4545

46+
/**
47+
* Object of CheckedAddresses resource
48+
*
49+
* @var \Payone\Core\Model\ResourceModel\CheckedAddresses
50+
*/
51+
protected $addressesChecked;
52+
4653
/**
4754
* Constructor
4855
*
49-
* @param \Magento\Framework\App\Helper\Context $context
50-
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
51-
* @param \Payone\Core\Helper\Shop $shopHelper
52-
* @param \Magento\Framework\App\ResourceConnection $resource
56+
* @param \Magento\Framework\App\Helper\Context $context
57+
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
58+
* @param \Payone\Core\Helper\Shop $shopHelper
59+
* @param \Magento\Framework\App\ResourceConnection $resource
60+
* @param \Payone\Core\Model\ResourceModel\CheckedAddresses $addressesChecked
5361
*/
5462
public function __construct(
5563
\Magento\Framework\App\Helper\Context $context,
5664
\Magento\Store\Model\StoreManagerInterface $storeManager,
5765
\Payone\Core\Helper\Shop $shopHelper,
58-
\Magento\Framework\App\ResourceConnection $resource
66+
\Magento\Framework\App\ResourceConnection $resource,
67+
\Payone\Core\Model\ResourceModel\CheckedAddresses $addressesChecked
5968
) {
6069
parent::__construct($context, $storeManager, $shopHelper);
6170
$this->databaseResource = $resource;
71+
$this->addressesChecked = $addressesChecked;
6272
}
6373

6474
/**
@@ -200,7 +210,7 @@ public function getConfigParamWithoutCache($sKey, $sGroup = 'global', $sSection
200210
* @param bool $blIsCreditrating
201211
* @return string
202212
*/
203-
public function getOldAddressStatus(AddressInterface $oAddress, $blIsCreditrating = true)
213+
protected function getStatusFromPreviousQuoteAddress(AddressInterface $oAddress, $blIsCreditrating = true)
204214
{
205215
$sSelectField = 'payone_protect_score';
206216
if ($blIsCreditrating === false) {
@@ -246,6 +256,34 @@ public function getOldAddressStatus(AddressInterface $oAddress, $blIsCreditratin
246256
return $this->getDb()->fetchOne($oSelect, $aParams);
247257
}
248258

259+
/**
260+
* Returns last check score for given address
261+
*
262+
* @param AddressInterface $oAddress
263+
* @return string
264+
* @throws \Magento\Framework\Exception\LocalizedException
265+
*/
266+
protected function getScoreFromCheckedAddresses(AddressInterface $oAddress)
267+
{
268+
return $this->addressesChecked->getLatestScoreForAddress($oAddress, true);
269+
}
270+
271+
/**
272+
* Get the address status from a previous order address
273+
*
274+
* @param AddressInterface $oAddress
275+
* @param bool $blIsCreditrating
276+
* @return string
277+
*/
278+
public function getOldAddressStatus(AddressInterface $oAddress, $blIsCreditrating = true)
279+
{
280+
$sStatus = $this->getStatusFromPreviousQuoteAddress($oAddress, $blIsCreditrating);
281+
if(empty($sStatus) && $blIsCreditrating === true && $this->getConfigParam('integration_event', 'creditrating', 'payone_protect') == 'after_payment') {
282+
$sStatus = $this->getScoreFromCheckedAddresses($oAddress);
283+
}
284+
return $sStatus;
285+
}
286+
249287
/**
250288
* Relabel sales payment transaction to substitute order
251289
*

Model/Config/Export.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,12 +227,26 @@ protected function addConsumerscore($sStoreCode)
227227
$this->writeToXml('<consumerscore>', 3);
228228
$this->writeNode("active", $this->configExportHelper->getConfigParam('enabled', 'creditrating', 'payone_protect', $sStoreCode), 4);
229229
$this->writeNode("mode", $this->configExportHelper->getConfigParam('mode', 'creditrating', 'payone_protect', $sStoreCode), 4);
230+
$this->writeNode("integration_event", $this->configExportHelper->getConfigParam('integration_event', 'creditrating', 'payone_protect', $sStoreCode), 4);
231+
$this->writeNode("enabled_for_payment_methods", $this->configExportHelper->getConfigParam('enabled_for_payment_methods', 'creditrating', 'payone_protect', $sStoreCode), 4);
232+
$this->writeNode("payment_hint_enabled", $this->configExportHelper->getConfigParam('payment_hint_enabled', 'creditrating', 'payone_protect', $sStoreCode), 4);
233+
$this->writeNode("payment_hint_text", $this->configExportHelper->getConfigParam('payment_hint_text', 'creditrating', 'payone_protect', $sStoreCode), 4);
234+
$this->writeNode("agreement_enabled", $this->configExportHelper->getConfigParam('agreement_enabled', 'creditrating', 'payone_protect', $sStoreCode), 4);
235+
$this->writeNode("agreement_message", $this->configExportHelper->getConfigParam('agreement_message', 'creditrating', 'payone_protect', $sStoreCode), 4);
230236
$this->writeNode("min_order_total", $this->configExportHelper->getConfigParam('min_order_total', 'creditrating', 'payone_protect', $sStoreCode), 4);
231237
$this->writeNode("max_order_total", $this->configExportHelper->getConfigParam('max_order_total', 'creditrating', 'payone_protect', $sStoreCode), 4);
232238
$this->writeNode("consumerscoretype", $this->configExportHelper->getConfigParam('type', 'creditrating', 'payone_protect', $sStoreCode), 4);
239+
$this->writeNode("addresschecktype", $this->configExportHelper->getConfigParam('addresscheck', 'creditrating', 'payone_protect', $sStoreCode), 4);
240+
$this->writeNode("score_when_unknown", $this->configExportHelper->getConfigParam('unknown_value', 'creditrating', 'payone_protect', $sStoreCode), 4);
233241
$this->writeNode("red", $this->configExportHelper->getConfigParam('allow_payment_methods_red', 'creditrating', 'payone_protect', $sStoreCode), 4);
234242
$this->writeNode("yellow", $this->configExportHelper->getConfigParam('allow_payment_methods_yellow', 'creditrating', 'payone_protect', $sStoreCode), 4);
235243
$this->writeNode("duetime", $this->configExportHelper->getConfigParam('result_lifetime', 'creditrating', 'payone_protect', $sStoreCode), 4);
244+
$this->writeNode("sample_mode_enabled", $this->configExportHelper->getConfigParam('sample_mode_enabled', 'creditrating', 'payone_protect', $sStoreCode), 4);
245+
$this->writeNode("sample_mode_frequency", $this->configExportHelper->getConfigParam('sample_mode_frequency', 'creditrating', 'payone_protect', $sStoreCode), 4);
246+
$this->writeNode("handle_response_error", $this->configExportHelper->getConfigParam('handle_response_error', 'creditrating', 'payone_protect', $sStoreCode), 4);
247+
$this->writeNode("stop_checkout_message", $this->configExportHelper->getConfigParam('stop_checkout_message', 'creditrating', 'payone_protect', $sStoreCode), 4);
248+
$this->writeNode("insufficient_score_message", $this->configExportHelper->getConfigParam('insufficient_score_message', 'creditrating', 'payone_protect', $sStoreCode), 4);
249+
$this->writeNode("result_lifetime", $this->configExportHelper->getConfigParam('result_lifetime', 'creditrating', 'payone_protect', $sStoreCode), 4);
236250
$this->writeToXml('</consumerscore>', 3);
237251
}
238252

Model/PayoneConfig.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
abstract class PayoneConfig
3333
{
3434
/* Module version */
35-
const MODULE_VERSION = '2.8.0';
35+
const MODULE_VERSION = '2.8.1';
3636

3737
/* Authorization request types */
3838
const REQUEST_TYPE_PREAUTHORIZATION = 'preauthorization';

Model/ResourceModel/CheckedAddresses.php

Lines changed: 57 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -147,33 +147,43 @@ public function addCheckedAddress(AddressInterface $oAddress, $aResponse, $sChec
147147
'address_hash' => $sHash,
148148
'is_bonicheck' => $blIsBonicheck,
149149
'checktype' => $sChecktype,
150+
'score' => isset($aResponse['score']) ? $aResponse['score'] : ''
150151
]
151152
);
152153
return $this;
153154
}
154155

155156
/**
156-
* Check and return if this exact address has been checked before
157+
* Get lifetime config
157158
*
158-
* @param AddressInterface $oAddress
159-
* @param string $sChecktype
160-
* @param bool $blIsBonicheck
161-
* @return bool
159+
* @param string $sConfigField
160+
* @param bool $blIsBonicheck
161+
* @return string
162162
*/
163-
public function wasAddressCheckedBefore(AddressInterface $oAddress, $sChecktype, $blIsBonicheck = false)
163+
protected function getConfigValue($sConfigField, $blIsBonicheck)
164164
{
165165
$sGroup = 'address_check';
166166
if ($blIsBonicheck === true) {
167167
$sGroup = 'creditrating';
168168
}
169+
return $this->shopHelper->getConfigParam($sConfigField, $sGroup, 'payone_protect');
170+
}
169171

170-
$sLifetime = $this->shopHelper->getConfigParam('result_lifetime', $sGroup, 'payone_protect');
171-
if (empty($sLifetime) || !is_numeric($sLifetime)) {
172-
return false; // no lifetime = check every time
173-
}
174-
172+
/**
173+
* Executes a select on the checked addresses table
174+
*
175+
* @param AddressInterface $oAddress
176+
* @param string $sChecktype
177+
* @param bool $blIsBonicheck
178+
* @param string $sLifetime
179+
* @param array $aSelectFields
180+
* @return string
181+
* @throws \Magento\Framework\Exception\LocalizedException
182+
*/
183+
protected function selectAddressesChecked(AddressInterface $oAddress, $sChecktype, $blIsBonicheck, $sLifetime, $aSelectFields)
184+
{
175185
$oSelect = $this->getConnection()->select()
176-
->from($this->getMainTable(), ['checkdate'])
186+
->from($this->getMainTable(), $aSelectFields)
177187
->where("address_hash = :hash")
178188
->where("is_bonicheck = :isBoni")
179189
->where("checktype = :checkType")
@@ -186,7 +196,41 @@ public function wasAddressCheckedBefore(AddressInterface $oAddress, $sChecktype,
186196
'lifetime' => $sLifetime
187197
];
188198

189-
$sDate = $this->getConnection()->fetchOne($oSelect, $aParams);
199+
return $this->getConnection()->fetchOne($oSelect, $aParams);
200+
}
201+
202+
/**
203+
* Returns score for the given address
204+
*
205+
* @param AddressInterface $oAddress
206+
* @param bool $blIsBonicheck
207+
* @return string
208+
* @throws \Magento\Framework\Exception\LocalizedException
209+
*/
210+
public function getLatestScoreForAddress(AddressInterface $oAddress, $blIsBonicheck)
211+
{
212+
$sLifetime = $this->getConfigValue('result_lifetime', $blIsBonicheck);
213+
$sChecktype = $this->getConfigValue('type', $blIsBonicheck);
214+
return $this->selectAddressesChecked($oAddress, $sChecktype, $blIsBonicheck, $sLifetime, ['score']);
215+
}
216+
217+
/**
218+
* Check and return if this exact address has been checked before
219+
*
220+
* @param AddressInterface $oAddress
221+
* @param string $sChecktype
222+
* @param bool $blIsBonicheck
223+
* @return bool
224+
* @throws \Magento\Framework\Exception\LocalizedException
225+
*/
226+
public function wasAddressCheckedBefore(AddressInterface $oAddress, $sChecktype, $blIsBonicheck = false)
227+
{
228+
$sLifetime = $this->getConfigValue('result_lifetime', $blIsBonicheck);
229+
if (empty($sLifetime) || !is_numeric($sLifetime)) {
230+
return false; // no lifetime = check every time
231+
}
232+
233+
$sDate = $this->selectAddressesChecked($oAddress, $sChecktype, $blIsBonicheck, $sLifetime, ['checkdate']);
190234
if ($sDate != false) {
191235
return true;
192236
}

Setup/UpgradeSchema.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,20 @@ protected function addNewColumns(SchemaSetupInterface $setup, ModuleContextInter
116116
]
117117
);
118118
}
119+
120+
if (!$setup->getConnection()->tableColumnExists($setup->getTable(CheckedAddresses::TABLE_CHECKED_ADDRESSES), 'score')) {
121+
$setup->getConnection()->addColumn(
122+
$setup->getTable(CheckedAddresses::TABLE_CHECKED_ADDRESSES),
123+
'score',
124+
[
125+
'type' => Table::TYPE_TEXT,
126+
'length' => 16,
127+
'nullable' => false,
128+
'default' => '',
129+
'comment' => 'Score'
130+
]
131+
);
132+
}
119133
}
120134

121135
/**

Test/Unit/Helper/DatabaseTest.php

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@
3535
use Magento\Framework\App\ResourceConnection;
3636
use Magento\Framework\DB\Select;
3737
use Magento\Quote\Model\Quote\Address;
38+
use Payone\Core\Model\ResourceModel\CheckedAddresses;
3839
use Payone\Core\Test\Unit\BaseTestCase;
3940
use Payone\Core\Test\Unit\PayoneObjectManager;
41+
use Magento\Store\Model\ScopeInterface;
4042

4143
class DatabaseTest extends BaseTestCase
4244
{
@@ -92,10 +94,14 @@ protected function setUp()
9294
$this->databaseResource = $this->getMockBuilder(ResourceConnection::class)->disableOriginalConstructor()->getMock();
9395
$this->databaseResource->method('getConnection')->willReturn($this->connection);
9496

97+
$addressesChecked = $this->getMockBuilder(CheckedAddresses::class)->disableOriginalConstructor()->getMock();
98+
$addressesChecked->method('getLatestScoreForAddress')->willReturn('R');
99+
95100
$this->database = $this->objectManager->getObject(Database::class, [
96101
'context' => $context,
97102
'storeManager' => $storeManager,
98-
'databaseResource' => $this->databaseResource
103+
'databaseResource' => $this->databaseResource,
104+
'addressesChecked' => $addressesChecked
99105
]);
100106
}
101107

@@ -219,6 +225,37 @@ public function testGetOldAddressStatus()
219225
$this->assertEquals($expected, $result);
220226
}
221227

228+
public function testGetOldAddressStatusEmpty()
229+
{
230+
$address = $this->getMockBuilder(Address::class)
231+
->disableOriginalConstructor()
232+
->setMethods(['getFirstname', 'getLastname', 'getStreet', 'getCity', 'getRegion', 'getPostcode', 'getCountryId', 'getId', 'getCustomerId', 'getAddressType'])
233+
->getMock();
234+
$address->method('getFirstname')->willReturn('Paul');
235+
$address->method('getLastname')->willReturn('Payer');
236+
$address->method('getStreet')->willReturn(['Teststr. 3']);
237+
$address->method('getCity')->willReturn('Paycity');
238+
$address->method('getRegion')->willReturn('Bremen');
239+
$address->method('getPostcode')->willReturn('12345');
240+
$address->method('getCountryId')->willReturn('DE');
241+
$address->method('getId')->willReturn('5');
242+
$address->method('getCustomerId')->willReturn('18');
243+
$address->method('getAddressType')->willReturn('billing');
244+
245+
$this->databaseResource->method('getTableName')->willReturn('quote_address');
246+
$this->connection->method('fetchOne')->willReturn(false);
247+
248+
$this->scopeConfig->method('getValue')
249+
->willReturnMap(
250+
[
251+
['payone_protect/creditrating/integration_event', ScopeInterface::SCOPE_STORES, null, 'after_payment'],
252+
]
253+
);
254+
255+
$result = $this->database->getOldAddressStatus($address);
256+
$this->assertEquals('R', $result);
257+
}
258+
222259
public function testRelabelTransaction()
223260
{
224261
$expected = '1';

Test/Unit/Model/ResourceModel/CheckedAddressesTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,17 @@ public function testWasAddressCheckedBeforeNoResult()
134134
$this->assertFalse($result);
135135
}
136136

137+
public function testGetLatestScoreForAddress()
138+
{
139+
$expected = "R";
140+
141+
$this->shopHelper->method('getConfigParam')->willReturn(5);
142+
$this->connection->method('fetchOne')->willReturn($expected);
143+
144+
$result = $this->classToTest->getLatestScoreForAddress($this->address, true);
145+
$this->assertEquals($expected, $result);
146+
}
147+
137148
public function testWasAddressCheckedBefore()
138149
{
139150
$this->shopHelper->method('getConfigParam')->willReturn(5);

etc/module.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
*/
2626
-->
2727
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
28-
<module name="Payone_Core" setup_version="2.8.0">
28+
<module name="Payone_Core" setup_version="2.8.1">
2929
<sequence>
3030
<module name="Magento_Quote" />
3131
<module name="Magento_Sales" />

0 commit comments

Comments
 (0)