Flash and PRO Merchant PayPal APIs

Recently a job popped up to modify a current Flash ActionScript 2.0 site which had a custom cart that sent the user to a PayPal page. The changes where to make the custom cart use the PayPal Merchant Pro API so that it ran the whole purchasing process within Flash. As simple as this should be there, is a lack of support or documentation kicking about on doing this within Flash. So after searching and working out how easy it really is I thought it would be a good idea to post about it, as the amount of questions and forum posts I came across asking if it’s possible and how to go about it was rather extreme.

This guide will cover the rough basics of creating the link beteen Flash and the PayPal API using a simple sendandload. It should be easy enough to adapt and tidy up to produce a proper transaction form.

The first, and quite possibly the longest step is to head over to http://sandbox.paypal.com and setup a sandbox account. You will need to acquire both a personal and business account within your sandbox. Once you have set up a businesses account you will also need to go through the process of upgrading it to a Merchant Pro account. Eventually you will be given the following details:

Test Account: ******

API Username: ******

API Password: ******

Signature: ******

And that’s us, good to go.

The site I was implementing this into was ActionScript 2.0 however it will be the same principals for ActionScript 3.0.

on(release){

var ppVars:LoadVars = new LoadVars ();

ppVars.METHOD = "DoDirectPayment";

ppVars["VERSION"] = 2.3;

ppVars.USER = "******";

ppVars.PWD = "******";

ppVars.SIGNATURE = "******";

ppVars.PAYMENTACTION = "Sale";

ppVars.sendAndLoad("https://api-3t.sandbox.paypal.com/nvp", ppReturn, "POST");

}

Throw this code onto a button and replace the ****** with your own details as required.

Next go to the actions for the 1st frame and place in the following script:

var ppReturn:LoadVars = new LoadVars();

ppReturn.onLoad = function(success) {

trace(ppReturn);

}

Testing your movie and clicking your button should give you a response. Currently the traced output will contain a whole load of URL encoding and be pretty unreadable to the eye, but as long as you get some sort of response you have successfully connected the NVP API needed for your PayPal form. We’ll first of all filter the response and clear it up so it becomes more readable.

Change the ppReturn function on your 1st frames actions to reflect the following:

ppReturn.onLoad = function(success) {

var x = String(ppReturn);

x = (replace(x, "%20", " "));

x = (replace(x, "%28", "("));

x = (replace(x, "%29", ")"));

x = (replace(x, "%2E", "."));

x = (replace(x, "%5F", "_"));

x = (replace(x, "%2D", "-"));

x = (replace(x, "%3A", ":"));

x = (replace(x, "%5B", "["));

x = (replace(x, "%5D", "]"));

trace(x);

}

And then add the following function either before or after:

function replace(thisString, changeThis, withThis) {

newString = thisString.split(changeThis);

return newString.join(withThis);

}

Again run your script and now your response from PayPal should be more interpretable. If you are familiar to coding Flash alongside PHP you might notice that our returned message is made from a bunch of variables all starting with the “&” symbol meaning accessing them is no problem.

The returned field which is important to us is “L_LONGMESSAGE0” which we access simply by writing ppReturn.L_LONGMESSAGE0.

We will alter the actions on our frame for the last time now to focus on this variable.

Under trace(x) place:

if(ppReturn.L_LONGMESSAGE0){

trace ("\nError: " +ppReturn.L_LONGMESSAGE0);

}else{

trace( "Success");

}

And that’s us, you can remove the X string replacing and tracing, however it is useful for viewing everything that the PayPal API is returning. Further syntax needed for your button now to be able to successfully complete a full transaction is the following, again replacing ****** with the details of your sandbox personal account details:

ppVars.CREDITCARDTYPE = "VISA";

ppVars.ACCT = "******";

ppVars.EXPDATE = "******";

ppVars.CVV2 = "******";

ppVars. AMT = 123;

That is basically all that’s required for a simple transaction from your personal account to your business one. PayPal provides a lot further control and input validation which should be looked at here:

https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_api_nvp_r_DoDirectPayment

Any other variables or input you want to use and pass to the PayPal NVP API is as simple as placing ppVars. Field = “******”; into the button.

Anyway hope this helps a few people out and saves some time.

10 Responses to “Flash and PRO Merchant PayPal APIs”

  1. Anonymous says:

    Great help, had mayjor problems with seting up the paypal sandbox though :s

  2. Yeh took me quite a few attempts and more time than it probably should have…

  3. Craig Wright says:

    Nice post, is what I was going to say but you have a min. length on comments? :-S

  4. Hmmm yep the spam filter plug-in I was using had a 15 character limit. It also reported blocking 18 doses of spam, but I’m interested in seeing what exactly my blogs getting spammed with so switched it off :o

  5. Turned it back on already, either a lot of people who lost 30 pounds are overly interested and keen in my blog posts or that would be the answer to what I get spammed with…

  6. eduardo says:

    I was testing this code and does not work when I upload to the server. That may be what this happening?

  7. Hi eduardo,

    are you still having troubles? Can you be more specific on what is and what’s not working and I’ll see if I can help.

    Iain

  8. Anonymous says:

    Thanks! I needed that. Otherwise I would be figuring it out myself. Seems like it should be harder. Of course, The site needs to be SSL as well. Many many thanks. If I have a problem I will write back Ian.

  9. Anonymous says:

    Hi, I have success in Flash, but in the browser I get policy errors and a loadpolicyfile failed.

    Anyone?

  10. Hi there,

    The policy error seems to be quite an issue on things like these. Have a look at the following and give it a bash:

    http://www.actionscript.org/forums/showthread.php3?t=170572

    http://www.actionscript.org/forums/showthread.php3?t=221396

    Maybe good starting places :)

Leave a Reply