Skip to content

Commit

Permalink
Merge pull request #453 from FatchipRobert/MAG2-250-Ratepay-allowed-m…
Browse files Browse the repository at this point in the history
…onths

MAG2-250 - Fixed problem with installment runtimes in guest mode
  • Loading branch information
janteuber authored May 23, 2022
2 parents 422907d + 0b4d4ff commit 77e9a87
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 40 deletions.
14 changes: 14 additions & 0 deletions Api/Data/InstallmentPlanResponseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,18 @@ public function getErrormessage();
* @return string
*/
public function getInstallmentPlanHtml();

/**
* Return installment plan json array string
*
* @return string
*/
public function getInstallmentPlan();

/**
* Return allowed runtime months json array string
*
* @return string
*/
public function getAllowedMonths();
}
9 changes: 9 additions & 0 deletions Api/InstallmentPlanInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,13 @@ public function getInstallmentPlan($cartId, $birthday, $email = false);
* @return \Payone\Core\Service\V1\Data\InstallmentPlanResponse
*/
public function getInstallmentPlanRatepay($cartId, $calcType, $calcValue);

/**
* Collects allowed runtimes afterwards
* Needed for guest checkout since the billing country is not known when checkout is loaded
*
* @param string $cartId
* @return \Payone\Core\Service\V1\Data\InstallmentPlanResponse
*/
public function getAllowedMonths($cartId);
}
3 changes: 3 additions & 0 deletions Model/Methods/Ratepay/RatepayBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@ public function getPaymentSpecificParameters(Order $oOrder)
protected function getShopIdByQuote(\Magento\Quote\Api\Data\CartInterface $quote)
{
$sCountryCode = $quote->getShippingAddress()->getCountryId();
if (empty($sCountryCode)) {
$sCountryCode = $quote->getBillingAddress()->getCountryId();
}
$sCurrency = $this->apiHelper->getCurrencyFromQuote($quote);
$dGrandTotal = $this->apiHelper->getQuoteAmount($quote);

Expand Down
10 changes: 10 additions & 0 deletions Service/V1/Data/InstallmentPlanResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,14 @@ public function getInstallmentPlan()
{
return $this->_get('installmentPlan');
}

/**
* Return allowed runtime months json array string
*
* @return string
*/
public function getAllowedMonths()
{
return $this->_get('allowedMonths');
}
}
22 changes: 22 additions & 0 deletions Service/V1/InstallmentPlan.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,28 @@ public function getInstallmentPlanRatepay($cartId, $calcType, $calcValue)
return $oResponse;
}

/**
* Collects allowed runtimes afterwards
* Needed for guest checkout since the billing country is not known when checkout is loaded
*
* @param string $cartId
* @return \Payone\Core\Service\V1\Data\InstallmentPlanResponse
*/
public function getAllowedMonths($cartId)
{
$oResponse = $this->responseFactory->create();
$oResponse->setData('success', false); // set success to false as default, set to true later if true

$oQuote = $this->checkoutSession->getQuote();

$aAllowedMonths = $this->ratepayInstallment->getAllowedMonths($oQuote);

$oResponse->setData('allowedMonths', json_encode($aAllowedMonths));
$oResponse->setData('success', true);

return $oResponse;
}

/**
* @param array $aResponse
* @return array
Expand Down
17 changes: 17 additions & 0 deletions etc/webapi.xml
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,21 @@
<parameter name="cartId" force="true">%cart_id%</parameter>
</data>
</route>

<route url="/V1/guest-carts/:cartId/payone-ratepayGetAllowedMonths" method="POST">
<service class="Payone\Core\Api\InstallmentPlanInterface" method="getAllowedMonths"/>
<resources>
<resource ref="anonymous" />
</resources>
</route>

<route url="/V1/carts/mine/payone-ratepayGetAllowedMonths" method="POST">
<service class="Payone\Core\Api\InstallmentPlanInterface" method="getAllowedMonths"/>
<resources>
<resource ref="self" />
</resources>
<data>
<parameter name="cartId" force="true">%cart_id%</parameter>
</data>
</route>
</routes>
72 changes: 36 additions & 36 deletions view/frontend/templates/redirect_return.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -25,42 +25,42 @@
?>

<script type="text/javascript">
<?php if ($block->isRedirectCancellation()) { ?>
require([
'Magento_Customer/js/customer-data',
'Magento_Checkout/js/checkout-data'
], function (customerData, checkoutData) {
customerData.invalidate(['cart', 'checkout-data']);
<?php if ($block->isRedirectCancellation()): ?>
require([
'Magento_Customer/js/customer-data',
'Magento_Checkout/js/checkout-data'
], function (customerData, checkoutData) {
customerData.invalidate(['cart', 'checkout-data']);

<?php $oShippingAddress = $block->getShippingAddress(); ?>
<?php if ($block->isGuest() && $oShippingAddress && $oShippingAddress->getFirstname() && $oShippingAddress->getLastname()) { ?>
var shippingAddressData = {
'company': '<?php echo $oShippingAddress->getCompany(); ?>',
'telephone' : '<?php echo $oShippingAddress->getTelephone(); ?>',
'firstname' : '<?php echo $oShippingAddress->getFirstname(); ?>',
'lastname' : '<?php echo $oShippingAddress->getLastname(); ?>',
'street' : {
0 : '<?php echo $oShippingAddress->getStreet()[0]; ?>',
1 : '<?php echo isset($oShippingAddress->getStreet()[1]) ? $oShippingAddress->getStreet()[1] : ''; ?>'
},
'city' : '<?php echo $oShippingAddress->getCity(); ?>',
'country_id' : '<?php echo $oShippingAddress->getCountryId(); ?>',
'postcode' : '<?php echo $oShippingAddress->getPostcode(); ?>'
};
<?php if ($oShippingAddress->getRegionCode() && $oShippingAddress->getRegionId()) { ?>
shippingAddressData.region = '<?php echo $oShippingAddress->getRegionCode(); ?>';
shippingAddressData.region_id = '<?php echo $oShippingAddress->getRegionId(); ?>';
<?php } ?>
checkoutData.setShippingAddressFromData(shippingAddressData);
checkoutData.setSelectedShippingRate('<?php echo $oShippingAddress->getShippingMethod(); ?>');
checkoutData.setInputFieldEmailValue('<?php echo $oShippingAddress->getEmail(); ?>');
checkoutData.setValidatedEmailValue('<?php echo $oShippingAddress->getEmail(); ?>');
<?php $oShippingAddress = $block->getShippingAddress(); ?>
<?php if ($block->isGuest() && $oShippingAddress && $oShippingAddress->getFirstname() && $oShippingAddress->getLastname()): ?>
var shippingAddressData = {
'company': '<?php echo $block->escapeJs($oShippingAddress->getCompany()); ?>',
'telephone' : '<?php echo $block->escapeJs($oShippingAddress->getTelephone()); ?>',
'firstname' : '<?php echo $block->escapeJs($oShippingAddress->getFirstname()); ?>',
'lastname' : '<?php echo $block->escapeJs($oShippingAddress->getLastname()); ?>',
'street' : {
0 : '<?php echo $block->escapeJs($oShippingAddress->getStreet()[0]); ?>',
1 : '<?php echo isset($oShippingAddress->getStreet()[1]) ? $block->escapeJs($oShippingAddress->getStreet()[1]) : ''; ?>'
},
'city' : '<?php echo $block->escapeJs($oShippingAddress->getCity()); ?>',
'country_id' : '<?php echo $block->escapeJs($oShippingAddress->getCountryId()); ?>',
'postcode' : '<?php echo $block->escapeJs($oShippingAddress->getPostcode()); ?>'
};
<?php if ($oShippingAddress->getRegionCode() && $oShippingAddress->getRegionId()): ?>
shippingAddressData.region = '<?php echo $block->escapeJs($oShippingAddress->getRegionCode()); ?>';
shippingAddressData.region_id = '<?php echo $block->escapeJs($oShippingAddress->getRegionId()); ?>';
<?php endif; ?>
checkoutData.setShippingAddressFromData(shippingAddressData);
checkoutData.setSelectedShippingRate('<?php echo $oShippingAddress->getShippingMethod(); ?>');
checkoutData.setInputFieldEmailValue('<?php echo $oShippingAddress->getEmail(); ?>');
checkoutData.setValidatedEmailValue('<?php echo $oShippingAddress->getEmail(); ?>');

<?php $oQuotePayment = $this->getQuotePayment(); ?>
<?php if ($oQuotePayment) { ?>
checkoutData.setSelectedPaymentMethod('<?php echo $oQuotePayment->getMethod(); ?>');
<?php } ?>
<?php } ?>
});
<?php } ?>
<?php $oQuotePayment = $this->getQuotePayment(); ?>
<?php if ($oQuotePayment): ?>
checkoutData.setSelectedPaymentMethod('<?php echo $oQuotePayment->getMethod(); ?>');
<?php endif; ?>
<?php endif; ?>
});
<?php endif; ?>
</script>
73 changes: 73 additions & 0 deletions view/frontend/web/js/action/updateallowedmonths.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* PAYONE Magento 2 Connector is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PAYONE Magento 2 Connector is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with PAYONE Magento 2 Connector. If not, see <http://www.gnu.org/licenses/>.
*
* PHP version 5
*
* @category Payone
* @package Payone_Magento2_Plugin
* @author FATCHIP GmbH <[email protected]>
* @copyright 2003 - 2017 Payone GmbH
* @license <http://www.gnu.org/licenses/> GNU Lesser General Public License
* @link http://www.payone.de
*/
/*jshint browser:true jquery:true*/
/*global alert*/
define([
'jquery',
'Magento_Checkout/js/model/url-builder',
'mage/storage',
'Magento_Checkout/js/model/full-screen-loader',
'Magento_Checkout/js/model/quote',
'Magento_Customer/js/model/customer'
], function ($, urlBuilder, storage, fullScreenLoader, quote, customer) {
'use strict';

/** Override default place order action and add agreement_ids to request */
return function (baseView) {
var serviceUrl;

var request = {
cartId: quote.getQuoteId()
};
if (!customer.isLoggedIn()) {
serviceUrl = urlBuilder.createUrl('/guest-carts/:quoteId/payone-ratepayGetAllowedMonths', {
quoteId: quote.getQuoteId()
});
} else {
serviceUrl = urlBuilder.createUrl('/carts/mine/payone-ratepayGetAllowedMonths', {});
}

fullScreenLoader.startLoader();

return storage.post(
serviceUrl,
JSON.stringify(request)
).done(
function (response) {
if (response.success === true && response.allowed_months !== undefined) {
baseView.allowedMonths(JSON.parse(response.allowed_months));
} else {
alert(response.errormessage);
}
fullScreenLoader.stopLoader();
}
).fail(
function (response) {
//errorProcessor.process(response, messageContainer);
alert('An error occured.');
fullScreenLoader.stopLoader();
}
);
};
});
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ define(
'Payone_Core/js/view/payment/method-renderer/ratepay_base',
'Magento_Checkout/js/model/quote',
'Payone_Core/js/action/installmentplanratepay',
'Payone_Core/js/action/updateallowedmonths',
'Magento_Catalog/js/price-utils',
'jquery',
'mage/translate'
],
function (Component, quote, installmentplan, utils, $, $t) {
function (Component, quote, installmentplan, updateAllowedMonths, utils, $, $t) {
'use strict';
return Component.extend({
defaults: {
Expand All @@ -48,7 +49,8 @@ define(
installmentTotalAmount: null,
interestRate: null,
useDirectDebit: true,

allowedMonths: window.checkoutConfig.payment.payone.ratepayAllowedMonths,
allowedMonthsReloaded: false
},
initObservable: function () {
this._super()
Expand All @@ -65,7 +67,9 @@ define(
'installmentLastAmount',
'installmentTotalAmount',
'interestRate',
'useDirectDebit'
'useDirectDebit',
'allowedMonths',
'allowedMonthsReloaded'
]);
return this;
},
Expand Down Expand Up @@ -150,7 +154,11 @@ define(
this.useDirectDebit(!this.useDirectDebit());
},
getAllowedMonths: function () {
return window.checkoutConfig.payment.payone.ratepayAllowedMonths;
if ((window.checkoutConfig.payment.payone.ratepayAllowedMonths === undefined || window.checkoutConfig.payment.payone.ratepayAllowedMonths.length == 0) && this.allowedMonthsReloaded() === false) {
updateAllowedMonths(this);
this.allowedMonthsReloaded(true);
}
return this.allowedMonths();
},
useMonthDropdown: function () {
if (this.getAllowedMonths().length > 9) {
Expand Down

0 comments on commit 77e9a87

Please sign in to comment.