Skip to content

Commit

Permalink
Merge pull request #552 from FatchipRobert/MAG2-311-CSP-inline-javasc…
Browse files Browse the repository at this point in the history
…ript

MAG2-311 - Added fix for inline javascript problem in Mage 2.4.7
  • Loading branch information
janteuber authored Aug 22, 2024
2 parents 7d3fea8 + 3890a69 commit 596c20a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 44 deletions.
24 changes: 22 additions & 2 deletions Block/RedirectReturn.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Magento\Framework\View\Element\Template;
use Magento\Quote\Model\Quote;
use Magento\Checkout\Model\Session;
use Payone\Core\Helper\Script;

/**
* Block class for re-setting the guest checkout information
Expand All @@ -42,17 +43,28 @@ class RedirectReturn extends Template
*/
protected $checkoutSession;

/**
* @var Script
*/
protected $scriptHelper;

/**
* Constructor
*
* @param Template\Context $context
* @param Session $checkoutSession
* @param Script $scriptHelper
* @param array $data
*/
public function __construct(Template\Context $context, Session $checkoutSession, array $data = [])
{
public function __construct(
Template\Context $context,
Session $checkoutSession,
Script $scriptHelper,
array $data = []
) {
parent::__construct($context, $data);
$this->checkoutSession = $checkoutSession;
$this->scriptHelper = $scriptHelper;
}

/**
Expand Down Expand Up @@ -115,4 +127,12 @@ public function isGuest()
{
return $this->getQuote()->getCustomerIsGuest();
}

/**
* @return Script
*/
public function getScriptHelper()
{
return $this->scriptHelper;
}
}
90 changes: 48 additions & 42 deletions view/frontend/templates/redirect_return.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -22,55 +22,61 @@
* @license <http://www.gnu.org/licenses/> GNU Lesser General Public License
* @link http://www.payone.de
*/
?>

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

<?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(); ?>');
if ($block->isRedirectCancellation()) {
$script = "
require([
'Magento_Customer/js/customer-data',
'Magento_Checkout/js/checkout-data'
], function (customerData, checkoutData) {
function redirectCancellation(customerData, checkoutData) {
customerData.invalidate(['cart', 'checkout-data']); ";

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

$oQuotePayment = $block->getQuotePayment();
if ($oQuotePayment) {
$script .= " checkoutData.setSelectedPaymentMethod('".$oQuotePayment->getMethod()."'); ";
}
}

if (typeof customerData.getInitCustomerData === "function") { // finish customerData initialization first if possible
$script .= "
}
if (typeof customerData.getInitCustomerData === 'function') { // finish customerData initialization first if possible
customerData.getInitCustomerData().done(function () {
redirectCancellation(customerData, checkoutData);
});
} else { // old functionality as fallback
redirectCancellation(customerData, checkoutData);
}
});
<?php endif; ?>
</script>
});";
echo $block->getScriptHelper()->insertScript($script);
}

0 comments on commit 596c20a

Please sign in to comment.