Skip to content

Commit

Permalink
Merge pull request #15 in DEV-MO/shopware5-module from fix/SHPWR-328_…
Browse files Browse the repository at this point in the history
…extend-plugin-update-function-for-backend-orders to release/5.2.3

* commit '332c1d4d29b346aa4f61a9d41b9a67a3d37f4636':
  fix/SHPWR-328_extend-plugin-update-function-for-backend-orders
  fix/SHPWR-328_extend-plugin-update-function-for-backend-orders update function and plugin save function still work (sw 5.4.5)
  • Loading branch information
rpWhittington committed Aug 24, 2018
2 parents ff81b0c + 332c1d4 commit c6c6941
Show file tree
Hide file tree
Showing 5 changed files with 231 additions and 335 deletions.
175 changes: 10 additions & 165 deletions Bootstrapping/Events/PluginConfigurationSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*/
namespace RpayRatePay\Bootstrapping\Events;

use RpayRatePay\Component\Service\RatepayConfigWriter;

class PluginConfigurationSubscriber implements \Enlight\Event\SubscriberInterface
{
protected $_countries = array('de', 'at', 'ch', 'nl', 'be');
Expand Down Expand Up @@ -50,9 +52,6 @@ public function beforeSavePluginConfig(\Enlight_Hook_HookArgs $arguments)

$shopCredentials = array();

// Remove old configs
$this->_truncateConfigTables();

foreach ($parameter['elements'] as $element) {
foreach ($this->_countries AS $country) {
if ($element['name'] === 'RatePayProfileID' . strtoupper($country)) {
Expand All @@ -78,189 +77,35 @@ public function beforeSavePluginConfig(\Enlight_Hook_HookArgs $arguments)
}
}

$rpayConfigWriter = new RatepayConfigWriter(Shopware()->Db());

$rpayConfigWriter->truncateConfigTables();

foreach($shopCredentials as $shopId => $credentials) {
foreach ($this->_countries AS $country) {
if (null !== $credentials[$country]['profileID'] &&
null !== $credentials[$country]['securityCode']) {
if ($this->getRatepayConfig($credentials[$country]['profileID'], $credentials[$country]['securityCode'], $shopId, $country)) {
if ($rpayConfigWriter->writeRatepayConfig($credentials[$country]['profileID'], $credentials[$country]['securityCode'], $shopId, $country)) {
Shopware()->PluginLogger()->addNotice('Ruleset for ' . strtoupper($country) . ' successfully updated.');
}
if ($country == 'de') {
if ($this->getRatepayConfig($credentials[$country]['profileID'] . '_0RT', $credentials[$country]['securityCode'], $shopId, $country)) {
if ($rpayConfigWriter->writeRatepayConfig($credentials[$country]['profileID'] . '_0RT', $credentials[$country]['securityCode'], $shopId, $country)) {
Shopware()->PluginLogger()->addNotice('Ruleset 0RT for ' . strtoupper($country) . ' successfully updated.');
}
}
}
if (null !== $credentials[$country]['profileIDBackend'] &&
null !== $credentials[$country]['securityCodeBackend']) {
if ($this->getRatepayConfig($credentials[$country]['profileIDBackend'], $credentials[$country]['securityCodeBackend'], $shopId, $country, true)) {
if ($rpayConfigWriter->writeRatepayConfig($credentials[$country]['profileIDBackend'], $credentials[$country]['securityCodeBackend'], $shopId, $country, true)) {
Shopware()->PluginLogger()->addNotice('Ruleset BACKEND for ' . strtoupper($country) . ' successfully updated.');
}
if ($country == 'de') {
if ($this->getRatepayConfig($credentials[$country]['profileIDBackend'] . '_0RT', $credentials[$country]['securityCodeBackend'], $shopId, $country, true)) {
if ($rpayConfigWriter->writeRatepayConfig($credentials[$country]['profileIDBackend'] . '_0RT', $credentials[$country]['securityCodeBackend'], $shopId, $country, true)) {
Shopware()->PluginLogger()->addNotice('Ruleset BACKEND 0RT for ' . strtoupper($country) . ' successfully updated.');
}
}
}
}
}
}

/**
* Truncate config tables
*
* @return bool
*/
private function _truncateConfigTables()
{
$configSql = 'TRUNCATE TABLE `rpay_ratepay_config`;';
$configPaymentSql = 'TRUNCATE TABLE `rpay_ratepay_config_payment`;';
$configInstallmentSql = 'TRUNCATE TABLE `rpay_ratepay_config_installment`;';
try {
Shopware()->Db()->query($configSql);
Shopware()->Db()->query($configPaymentSql);
Shopware()->Db()->query($configInstallmentSql);
} catch (\Exception $exception) {
Shopware()->Pluginlogger()->info($exception->getMessage());
return false;
}
return true;
}

/**
* Sends a Profile_request and saves the data into the Database
*
* @param string $profileId
* @param string $securityCode
* @param int $shopId
* @param string $country
* @param bool $backend
*
* @return mixed
* @throws exception
*/
private function getRatepayConfig($profileId, $securityCode, $shopId, $country, $backend = false)
{
$factory = new \Shopware_Plugins_Frontend_RpayRatePay_Component_Mapper_ModelFactory(null, $backend);
$data = array(
'profileId' => $profileId,
'securityCode' => $securityCode
);

$response = $factory->callRequest('ProfileRequest', $data);

$payments = array('invoice', 'elv', 'installment');

if (is_array($response) && $response !== false) {

foreach ($payments AS $payment) {
if (strstr($profileId, '_0RT') !== false) {
if ($payment !== 'installment') {
continue;
}
}

$dataPayment = array(
$response['result']['merchantConfig']['activation-status-' . $payment],
$response['result']['merchantConfig']['b2b-' . $payment] == 'yes' ? 1 : 0,
$response['result']['merchantConfig']['tx-limit-' . $payment . '-min'],
$response['result']['merchantConfig']['tx-limit-' . $payment . '-max'],
$response['result']['merchantConfig']['tx-limit-' . $payment . '-max-b2b'],
$response['result']['merchantConfig']['delivery-address-' . $payment] == 'yes' ? 1 : 0,
);

$paymentSql = 'INSERT INTO `rpay_ratepay_config_payment`'
. '(`status`, `b2b`,`limit_min`,`limit_max`,'
. '`limit_max_b2b`, `address`)'
. 'VALUES(' . substr(str_repeat('?,', 6), 0, -1) . ');';
try {
Shopware()->Db()->query($paymentSql, $dataPayment);
$id = Shopware()->Db()->fetchOne('SELECT `rpay_id` FROM `rpay_ratepay_config_payment` ORDER BY `rpay_id` DESC');
$type[$payment] = $id;
} catch (\Exception $exception) {
Shopware()->Pluginlogger()->error($exception->getMessage());
return false;
}
}

if ($response['result']['merchantConfig']['activation-status-installment'] == 2) {
$installmentConfig = array(
$type['installment'],
$response['result']['installmentConfig']['month-allowed'],
$response['result']['installmentConfig']['valid-payment-firstdays'],
$response['result']['installmentConfig']['rate-min-normal'],
$response['result']['installmentConfig']['interestrate-default'],
);
$paymentSql = 'INSERT INTO `rpay_ratepay_config_installment`'
. '(`rpay_id`, `month-allowed`,`payment-firstday`,`interestrate-default`,'
. '`rate-min-normal`)'
. 'VALUES(' . substr(str_repeat('?,', 5), 0, -1) . ');';
try {
Shopware()->Db()->query($paymentSql, $installmentConfig);
} catch (\Exception $exception) {
Shopware()->Pluginlogger()->error($exception->getMessage());
return false;
}

}

if (strstr($profileId, '_0RT') !== false) {
$qry = "UPDATE rpay_ratepay_config SET installment0 = '" . $type['installment'] . "' WHERE profileId = '" . substr($profileId, 0, -4) . "'";
Shopware()->Db()->query($qry);
} else {
$data = array(
$response['result']['merchantConfig']['profile-id'],
$type['invoice'],
$type['installment'],
$type['elv'],
0,
0,
$response['result']['merchantConfig']['eligibility-device-fingerprint'] ? : 'no',
$response['result']['merchantConfig']['device-fingerprint-snippet-id'],
strtoupper($response['result']['merchantConfig']['country-code-billing']),
strtoupper($response['result']['merchantConfig']['country-code-delivery']),
strtoupper($response['result']['merchantConfig']['currency']),
strtoupper($country),
$response['sandbox'],
$backend,
//shopId always needs be the last line
$shopId
);

$activePayments[] = '"rpayratepayinvoice"';
$activePayments[] = '"rpayratepaydebit"';
$activePayments[] = '"rpayratepayrate"';
$activePayments[] = '"rpayratepayrate0"';

if (count($activePayments) > 0) {
$updateSqlActivePaymentMethods = 'UPDATE `s_core_paymentmeans` SET `active` = 1 WHERE `name` in(' . implode(",", $activePayments) . ') AND `active` <> 0';
}
$configSql = 'INSERT INTO `rpay_ratepay_config`'
. '(`profileId`, `invoice`, `installment`, `debit`, `installment0`, `installmentDebit`,'
. '`device-fingerprint-status`, `device-fingerprint-snippet-id`,'
. '`country-code-billing`, `country-code-delivery`,'
. '`currency`,`country`, `sandbox`,'
. '`backend`, `shopId`)'
. 'VALUES(' . substr(str_repeat('?,', 15), 0, -1) . ');'; // In case of altering cols change 14 by amount of affected cols
try {
Shopware()->Db()->query($configSql, $data);
if (count($activePayments) > 0) {
Shopware()->Db()->query($updateSqlActivePaymentMethods);
}

return true;
} catch (\Exception $exception) {
Shopware()->Pluginlogger()->error($exception->getMessage());

return false;
}
}
} else {
Shopware()->Pluginlogger()->error('RatePAY: Profile_Request failed!');

if (strstr($profileId, '_0RT') == false) {
throw new Exception('RatePAY: Profile_Request failed!');
}
}
}
}
Loading

0 comments on commit c6c6941

Please sign in to comment.