2

I'm still trying to skin the same cat: Controlling order of javascript execution PayPal Objects

In short, I have a database of digital songs for purchase. I would like to have an ever changing list of, say 50 songs for purchase.

The docs Paypal has are pretty crummy IMO. And the examples -everyone- seems to use require hard coding the ID of the element (button) you want to sell because you need a script in the footer of your page to actually 'trigger' the call to PayPal

<script type="text/javascript">
  var embeddedPPFlow1 = new PAYPAL.apps.DGFlow( {trigger : 'buysong_1'});
</script>

...so if you have 50 'Buy' buttons, you would need to declare 50 PAYPAL.apps.DGFlow objects in the footer at load time and they have to, of course, have the correct IDs. This makes pulling different items from a database a bit tricky (not to mention making for very heavy pages.)

PayPal docs indicate one should be able to get around this by using a single object but with a URL and the startFlow method instead of the trigger, but there is no example on their site and I can find no working examples on the interweb.

embeddedPPFlow = new PAYPAL.apps.DGFlow();
// url = (paypal url?)
this.embeddedPPFlow.startFlow(url);

But I don't get what the URL should be... Is it the Token one gets from PayPal? If so, how does one get the token -before- calling PayPal NVP?

In short: does -someone- have a working example of this, or know how to use startFlow with a dynamically assigned buy button? I've tried posting on the X developer forum and got no responses.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
jchwebdev
  • 5,034
  • 5
  • 21
  • 30

1 Answers1

2

I stumbled across your question while searching the same thing, and found the answer that worked for me, I hope this helps....

The url is the location that you want to call inside of the lightbox. It is different depending on what API (and SDK) you are using. In my case, I'm using the Merchant API SDK. https://github.com/paypal/merchant-sdk-php

Inside the SDK is a file DGsetExpressCheckout.php, at the bottom of the file is the url:

https://www.sandbox.paypal.com/incontext?token=$token

I'm not really sure what the "incontext" part means, but when I used that link (along with the token at the end) PayPal figured things out and directed to:

https://www.sandbox.paypal.com/webapps/checkout/webflow/sparta/expresscheckoutvalidatedataflow?execution=e1s3

Which allows for login and the rest of the payment flow.

My JavaScript looks like this (with jQuery):

//setup the PayPal digital goods flow
var dg = new PAYPAL.apps.DGFlow({
  //don't trigger the flow with a button (we're going to submit manually)
  trigger: null
}),
//post object for payment
post = {
  amount: '1.00',
  currencyId: 'USD'
  //.... whatever else you need to pass to $_POST ....
};
//process checkout
$.post("/url/to/your/sdk/method", post, function(token) {
  //if successful...
  if ( token ) {
    //direct the dialog box for our digital goods pay flow to the correct place
    var flowUrl = 'https://www.sandbox.paypal.com/incontext?token='+token;
    dg.startFlow( flowUrl );
  }
});

You may need to adapt the SDK to your situation. You have to make sure to do the things inside of DGdoExpressCheckout.php also, which should be set to the returnUrl that gets run after the payflow, at that point you run:

dg.closeFlow(); 

to close the dialog box and overlay.

Allen
  • 2,717
  • 2
  • 30
  • 34