Magento Paypalplus - Deactivate Payment-Methods via PHP

246 Views Asked by At

we are using Magento with some Extensions, one of it is Paypalplus and it is essential.

Now we want to sell gift-coupons, which is explicitely forbidden by Paypal. So we need to deactivate Paypayplus, or at least some of its Payment Methods.

We tried first to deactivate it completely which results in a broken Javascript and the order is refused by Magento. It seems that if PPP is activated, the other built-in methods doesnt load their Js and instead try to use PPP-Js, which isnt loaded, when a gift-coupon exists in the cart.

Then i tried to deactivate the forbidden payment methods using its own Js, where it renders all the methods by itself.

window.ppp = PAYPAL.apps.PPP({ ... })

This should set up the PPP Js. But i dont know wether it is possible nor which argument to be passed in the Object, to partially or fully deactivate its own payment-methods. Can anyone tell me that. I search for some time now, but im not able to find a documentation about this API.

Here is the complete Code that should render PPP, contained in the Magento PPP-Extension:

<div id="ppplus"></div>
<script type="text/javascript">
    if(typeof payment == 'undefined') {
        var payment = {};
    }
    function selectMethod(code) {
        try {
            if(typeof awOSCPayment != "undefined") {
                awOSCPayment.currentMethod = code;
            }
        } catch (e) {

        }
        document.getElementById('p_method_' + code).click();
    }
    window.externMethodEnabled = false;
    window.startPPP = function () {
        if(typeof window.thirdPartyObject == 'undefined') {
            window.thirdPartyObject = <?php echo $this->getThirdPartyJsonObject(); ?>;
            window.thirdPartyMethodObject = <?php echo $this->getThirdPartyMethodJsonObject(); ?>;
        }
        document.cookie = 'paypalplus_session=; Path=/checkout/onepage; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
        document.cookie = 'paypalplus_session=; Path=/checkout; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
        document.cookie = 'paypalplus_session=; Path=/firecheckout; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
        document.cookie = 'paypalplus_session=; Path=/onestepcheckout; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
        window.pppId = '<?php echo $this->getPayPalPaymentId(); ?>';
        selectMethod('<?php echo Iways_PayPalPlus_Model_Payment::METHOD_CODE; ?>');
        window.ppp = PAYPAL.apps.PPP(
            {
                approvalUrl: "<?php echo $paymentExperience; ?>",
                placeholder: "ppplus",
                mode: "<?php echo Mage::getStoreConfig('iways_paypalplus/api/mode'); ?>",
                useraction:"commit",
                buttonLocation:"outside",
                <?php if($this->isPuiSandboxMode()) : ?>
                showPuiOnSandbox: true,
                <?php endif; ?>
                <?php if($this->showLoadingIndicator()) : ?>
                showLoadingIndicator: true,
                <?php endif; ?>
                country:"<?php echo $this->getCountryId(); ?>",
                language:"<?php echo $this->getLanguage(); ?>",
                onContinue: function () {
                    payment.save()
                },
                onThirdPartyPaymentMethodSelected:function (data) {
                    this.lastCall = 'onThirdPartyPaymentMethodSelected';
                    selectMethod(window.thirdPartyObject[data.thirdPartyPaymentMethod]);
                },
                enableContinue: function (data) {
                    if(this.lastCall != 'onThirdPartyPaymentMethodSelected') {
                        selectMethod('<?php echo Iways_PayPalPlus_Model_Payment::METHOD_CODE; ?>');
                    }
                    this.lastCall = 'enableContinue';
                    try {
                        if($$('#payment-buttons-container > button').length > 0) {
                            $$('#payment-buttons-container > button')[0].enable();
                        }
                        if($('onestepcheckout-place-order') != null) {
                            $('onestepcheckout-place-order').enable();
                        }
                        if($$('#review-buttons-container > button').length > 0) {
                            $$('#review-buttons-container > button')[0].enable();
                        }
                        if($('onestepcheckout-button-place-order') != null) {
                            $('onestepcheckout-button-place-order').enable();
                        }
                        if($$('#checkout-review-submit > button.opc-btn-checkout').length > 0) {
                            $$('#checkout-review-submit > button.opc-btn-checkout')[0].enable();
                        }
                        if($$('#aw-onestepcheckout-place-order-button').length > 0) {
                            $$('#aw-onestepcheckout-place-order-button')[0].enable();
                        }
                    }catch (e) {
                        console.log(e);
                    }
                },
                disableContinue: function (data) {
                    if(!window.externMethodEnabled) {
                        try {
                            if($$('#payment-buttons-container > button').length > 0) {
                                $$('#payment-buttons-container > button')[0].disable();
                            }
                            if($('onestepcheckout-place-order') != null) {
                                $('onestepcheckout-place-order').disable();
                            }
                            if($$('#review-buttons-container > button').length > 0) {
                                $$('#review-buttons-container > button')[0].disable();
                            }
                            if($('onestepcheckout-button-place-order') != null) {
                                $('onestepcheckout-button-place-order').disable();
                            }
                            if($$('#checkout-review-submit > button.opc-btn-checkout').length > 0) {
                                $$('#checkout-review-submit > button.opc-btn-checkout')[0].disable();
                            }
                            if($$('#aw-onestepcheckout-place-order-button').length > 0) {
                                $$('#aw-onestepcheckout-place-order-button')[0].disable();
                            }
                        }catch (e) {
                            console.log(e);
                        }
                    }
                },
                <?php echo $this->getThirdPartyMethods(); ?>
            });
    }
    window.startPPP();
    function checkStep() {
        try {
            if(typeof window.checkout != 'undefined' && typeof window.lastStep == 'undefined') {
                window.lastStep = window.checkout.accordion.currentSection;
            }
            if (typeof window.lastStep != 'undefined' && window.lastStep != window.checkout.accordion.currentSection) {
                window.lastStep = checkout.accordion.currentSection;
                if (checkout.accordion.currentSection == "opc-payment") {
                    window.startPPP();
                }
            }
        } catch(e) {
        }
    }
    window.setInterval(checkStep, 1000);

</script>

Thank you for your attention.

1

There are 1 best solutions below

1
On

Okay, we found out, that there is no argument to pass to partially disable payment-methods. I found another way to diasble the the hard way. The Argument onLoad takes a function which is called when the iframe is ready. We manually remove the payment method by removing them from the iframes DOM, whenever a gift-coupon is in the cart. This is not an optimal solution, but it works.

Hope to see, that Paypal extends their API, to give devs more control.