Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paypal Adaptive payment for mobile web

Tags:

Im integrating Paypal Adaptive Payment API for a mobile website. But when Im submitting payment to

https://www.paypal.com/webscr?cmd=_ap-payment&paykey=value

( For Sandbox : https://www.sandbox.paypal.com/cgi-bin/webscr )

Its always redirecting to Paypal Main Web. Not to Paypal Mobile website. How to redirect client to paypal mobile web?

like image 917
Sency Avatar asked May 05 '12 15:05

Sency


People also ask

What is Adaptive payment?

Adaptive payments are payments made between one sender and multiple receivers–for example, let's say you need a cell phone carrier, cable company, and internet service provider to fulfill your communication needs.


2 Answers

Try redirecting your site to

https://www.sandbox.paypal.com/webapps/adaptivepayment/flow/pay?paykey=AP-XYZ&expType=mini

Fill your pay key in place of AP-XYZ

Let me know if it works.

like image 161
Rushik Avatar answered Sep 21 '22 22:09

Rushik


The best approach I found was mini browser experience. But I had a variety of different issues on mobile devices implementing it (which is what it was meant for in the first place). You will see many similar questions about adaptive payments and all sorts of issues with using lightbox and mini browser experience.

But FINALLY... I have figured it out after hours upon hours, days upon days! This should solve everyone's problems of all different varieties when it comes to issues with PayPal Adaptive Payments and the issues with:

  1. The default redirected paypal page is NOT mobile responsive and looks horrible on mobile devices.
  2. The lightbox gets "hung up" and does not close on some mobile devices.
  3. The mini browser doesn't close after completing payment or cancelling.
  4. The mini browser doesn't redirect to the callBackFunction from paypal apdg.js script.
  5. Not redirecting to returnUrl and cancelUrl after payment completion (or when cancelling)
  6. Chrome for ios (iphones) doesn't initiate the callbackfunction and therefore after payment completion or cancellation, it just keeps you at the page you launched the paypal payment page from which prevents you from validating success or failure of payment.

Drum roll please.... here it is!! This replaces any need for PayPal javascript files etc. All you need is what is below along with your own method of obtaining the PayKey to add to the redirect url. My live website, with adaptive payments working correctly using below code, is https://www.trackabill.com.

<div>
    <?php $payUrl = 'https://www.paypal.com/webapps/adaptivepayment/flow/pay?expType=mini&paykey=' . $payKey ?>

    <button onclick="loadPayPalPage('<?php echo $payUrl; ?>')" title="Pay online with PayPal">PayPal</button>
</div>
<script>
    function loadPayPalPage(paypalURL)
    {
        var ua = navigator.userAgent;
        var pollingInterval = 0;
        var win;
        // mobile device
        if (ua.match(/iPhone|iPod|Android|Blackberry.*WebKit/i)) {
            //VERY IMPORTANT - You must use '_blank' and NOT name the window if you want it to work with chrome ios on iphone
                //See this bug report from google explaining the issue: https://code.google.com/p/chromium/issues/detail?id=136610
            win = window.open(paypalURL,'_blank');

            pollingInterval = setInterval(function() {
                if (win && win.closed) {
                    clearInterval(pollingInterval);
                    returnFromPayPal();
                }
            } , 1000);
        }
        else
        {
            //Desktop device
            var width = 400,
                height = 550,
                left,
                top;

            if (window.outerWidth) {
                left = Math.round((window.outerWidth - width) / 2) + window.screenX;
                top = Math.round((window.outerHeight - height) / 2) + window.screenY;
            } else if (window.screen.width) {
                left = Math.round((window.screen.width - width) / 2);
                top = Math.round((window.screen.height - height) / 2);
            }

            //VERY IMPORTANT - You must use '_blank' and NOT name the window if you want it to work with chrome ios on iphone
                //See this bug report from google explaining the issue: https://code.google.com/p/chromium/issues/detail?id=136610
            win = window.open(paypalURL,'_blank','top=' + top + ', left=' + left + ', width=' + width + ', height=' + height + ', location=0, status=0, toolbar=0, menubar=0, resizable=0, scrollbars=1');

            pollingInterval = setInterval(function() {
                if (win && win.closed) {
                    clearInterval(pollingInterval);
                    returnFromPayPal();
                }
            } , 1000);
        }
    }

    var returnFromPayPal = function()
    {
       location.replace("www.yourdomain.com/paypalStatusCheck.php");
        // Here you would need to pass on the payKey to your server side handle (use session variable) to call the PaymentDetails API to make sure Payment has been successful
        // based on the payment status- redirect to your success or cancel/failed page
    }
</script>
like image 27
Murf Avatar answered Sep 22 '22 22:09

Murf