thenbrent / paypal-digital-goods

The missing PayPal Digital Goods PHP library. Discontinued - PayPal no longer offer the Digital Goods product.
Other
121 stars 38 forks source link

Execute PayPal JavaScript on a "Pay With PayPal" button loaded with AJAX. #10

Closed rmuktader closed 11 years ago

rmuktader commented 11 years ago

I have a question. But I did not know how to discuss it without posting it as an "issue".

I am loading the "Pay With PayPal" through AJAX because the when the user enters a coupon code I have to reload the button with the new price.

Unfortunately, the PayPal window does not open in a light-box after doing this.

I have tried

$('#paypal-submit').live("click", function(){
    var dg = new PAYPAL.apps.DGFlow({
        trigger: "paypal-submit" 
    });      
});

And :

$('#paypal-submit').live("click", function(){
    var dg = new PAYPAL.apps.DGFlow();
    dg.startFlow('https://www.sandbox.paypal.com/incontext?token=EC-5B05061913748630E');

});

Is there a solution this problem?

thenbrent commented 11 years ago

I do exactly that with this snipped:

    var dg = new PAYPAL.apps.DGFlow({trigger:'place_order'});
    dg.startFlow(result.redirect);

Where result.redirect is https://www.sandbox.paypal.com/incontext?token=EC-5B05061913748630E. So not sure why your code isn't work, but perhaps you need to combine the two (set a tigger on PAYPAL.apps.DGFlow construction and still call dg.startFlow).

rmuktader commented 11 years ago

Brent, Thank you for your prompt reply. It made me realize that there IS a solution and I'll find it if I keep chipping away at the problem.

As it turns out var dg = new PAYPAL.apps.DGFlow({trigger:'paypal-submit'}); was executing before the button finished loading. So I placed it in the load's call back function.

For others who are reading this: the print-button.php uses echo $purchase->get_buy_button(); to print the button.

$(document).ready(function(){

    // load paypal button
    $('#paypal-container').load('print-button.php', function(){
        var dg = new PAYPAL.apps.DGFlow({trigger:'paypal-submit'});
    });

    // on click
    $('#paypal-submit').on('click', function(){
        dg.startFlow($(this).attr('href'));
    });
});
thenbrent commented 11 years ago

As it turns out

Thanks for sharing the solution @rmuktader