Quick Guide: Creating Paypal Buttons with ActionScript 3.0

In this tutorial we’ll quickly create two different Paypal buttons: "Buy Now" and "Donate". All we’ll need is a Paypal Business Account to generate some code for us.

Final Result Preview

Let’s take a look at the final result we will be working towards:

Step 1 – Document Setup

Launch Flash and create a new ActionScript 3.0 document (File > New…). Set the stage size to 600 px width and 400px height with 30 fps. I’ve chosen black (#000000) for the background.

””

Step 2 – Create the Flash Buttons

Create two layers. Name the first layer "Buttons" and the second "Actions". On the "Buttons" layer, create two buttons; one with the text "Donate a Cup of Coffee" and the other with the text "Buy Donuts". Select the Donate button, then go to properties and assign an instance name of "btDonate". Lastly, assign an instance name of "btBuynow " for the Buy Now button.

Step 3 – Login to Paypal.com

Go to www.paypal.com and login as shown in the image below.

””

Step 4 – Customizing the Donate Button

Go to "Merchant Services" and click on the option "Donations" located in the "Key Features" panel.

Firstly, we’ll customize our button "Donate". Enter a name for the "Service" and then a "Donation ID". Now choose the currency and select "Donors contribute a fixed amount" (just type an amount). The final task for this step is to select "Plain text e-mail address".

Now we move to step 2 and check "Save button at Paypal"; this is a new feature and it’s very useful.

For step 3 we’ll customize the advanced features. Select "No" for the first two options and if you wish, you can enter the url for "cancellation" and "successful" pages.

Click on "Create button" and the next screen will show you the HTML code.

””/

Step 5 – Customizing the Buy Now Button   

To customize the other button, again go to "Merchant Services". This time, click on the "Buy Now" option located in the "Key Features" panel.

Within the first step choose "Products" from the combo box, select "Accept payments for" and "No, create a Buy Now button". Then enter a "Name" and "ID" for your service or product. Set the "price", "currency " and specify the "shipping" amount.

Then, go to step 2 and check "Save button at Paypal".

Lastly, within step 3 select "No" for the first two options and again, if you wish, you can type the url for "cancellation" and "successful" pages.

Click on "Create button".

””/

Step 6 – Copy the HTML Code 

Now the two buttons have been created, go to your "Profile" and click on "My Saved Buttons" which you’ll find in the "Selling Preferences" panel. Click on the "Action" drop down menu and select "View code". Copy the code, go back to Flash and paste the clipboard contents into your "Actions" layer. Repeat these steps for the other button and comment it all as HTML code.

””/

Step 7 – Define "EventListener"

Copy the following lines of code into your "Actions" layer:

btDonate.addEventListener(MouseEvent.CLICK, donate);
btBuynow.addEventListener(MouseEvent.CLICK, buynow);

Step 8 – Define the "buynow" and "donate" Functions

Copy the following lines of code into your "Actions" layer:

function donate (event:MouseEvent):void
  {
var url:String = "https://www.paypal.com/cgi-bin/webscr"; //<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
var request:URLRequest = new URLRequest(url);
var paypal_donate:URLVariables = new URLVariables();
paypal_donate.cmd="_s-xclick"; //<input type="hidden" name="cmd" value="_s-xclick">
paypal_donate.hosted_button_id="4893693"; //<input type="hidden" name="hosted_button_id" value="4893693">
request.data = paypal_donate;
navigateToURL(request);
trace(paypal_donate)
}

function buynow (event:MouseEvent):void
  {
  var url:String = "https://www.paypal.com/cgi-bin/webscr"; //<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
  var request:URLRequest = new URLRequest(url);
  var paypal_buynow:URLVariables = new URLVariables();
  paypal_buynow.cmd="_s-xclick"; //<input type="hidden" name="cmd" value="_s-xclick">
  paypal_buynow.hosted_button_id="4901978"; //<input type="hidden" name="hosted_button_id" value="4901978">
  request.data = paypal_buynow;
  navigateToURL(request);
  trace(paypal_buynow)
  }

Conclusion

Test your movie to check the link between flash and paypal.com. If you wish, you can modify the paypal buttons (taxes, tracking, inventory, donation amount, etc), have fun playing with it! I hope you enjoyed this quick guide.

Add Comment

Discussion 58 Comments

  1. zeroone says:

    Greate tutorials

  2. nightwork says:

    Awesome tutorial! Was looking for these!
    Thanks!

  3. Franky says:

    Nice tut. I guess this would also work for opening any external page!

    Nice.

  4. Good tutorial. Simple and straight forward.

  5. joell lapitan says:

    nice tut.. this will be very helpful..

  6. James says:

    Awesome! very well explained

  7. shampi says:

    Awesome ! Really effective

  8. lawrence77 says:

    nice tut thanks! ;)

  9. tirtha says:

    it’s so fantastic please sent me this like any tuts at pakhrin299@hotmail.com

  10. Luis María says:

    Me da mucho orgullo saber que un compatriota mexicano escribe en este tipo de sitios con gran calidad. Te felicito. Vi tu sitio y me gusto mucho la gráfica además de su sencillez.

    yo soy del estado de México y llevo diseñando en flash como tres años, mi fuerte más bien es el diseño y animación, me cuesta la programación.

    Yo también hago tutos pero con tópicos más comunes.

    Te dejo Saludos!!!

    • Author

      Gracias María, checa mi nuevo blog de labs tengo 2 tuts por publicar, quedate pendiente. Saludos desde Cancun. Sigueme en twitter @_dariux. Saludos.

      Thanks Maria, you can check my new blog dg labs, I have 2 tuts for soon publish, I will wait your visit. Best regards!

      • Tommy Guetta says:

        very helpfull – thanx! everything works 100% fine it’s just the buttons directing to the chocolate donats… where do i change that?

  11. qana6r-des.com says:

    wooow
    gooog

  12. swarup podar says:

    very nice tut…

  13. atanism says:

    no add to shopping cart button?

  14. teneb says:

    Is it possible to let the user input any other amount instead of the predefined values?

  15. brendan says:

    thanks

  16. brendan says:

    how would a view cart work though?

  17. Lisa Mercedes says:

    Nice tutorial. It would be grand if you taught us how to also create “View Cart” and “Checkout” buttons to complete the tutorial.

    x lisa x

  18. sergio says:

    Awsome tutorial, been looking for something like this!!

  19. angie says:

    this is great! I am stuck trying to incorporate the flash combobox component so I can have choices for sizes. Any pointers?

    Thanks!

  20. Eric says:

    @atanism: shopping cart? like if you wanted to buy a dozen donuts? haha

    -Eric

  21. how would i create a flash action script 3.0 button with multiple options, that communicates with paypal, to purchase products, I’ve created individual buttons in flash (for Now) using AC 2.0 see (i.e) on my site…
    gogreekstore.com

  22. Dulisha says:

    This is Good Man,In moment need me Flash Button to use for Pay Pal.

  23. Lisa Mercedes says:

    View cart code for ActionScript 3.0 is soooooooo hard to find…why?

  24. alex says:

    Is there a way to have it so that instead of donating a cup of coffee, it goes to a Text input box (numbers only)

    Where the user enters like 5.00 and that is what they are donating?

    Or 7.00 and thats their donation?

    Is that possible?

    Thanks

  25. david says:

    hey guys this is the tuts ive been after for ages does any one know how to make a view cart button :) i keep getting ioerror

  26. david says:

    great tut could anyone help me create a view cart the same way i keep gettng a error

  27. Jen says:

    Great tutorial Dario! Is it possible to create an “Add to Cart” button using the method shown in this tutorial?

  28. Michael Webb says:

    Can this be modified to work with actionscripting 2.0 ??

  29. lean ray says:

    How can I make a button like this using flash actionscript 2 & 1?

  30. Faraz says:

    Nice Script very simple and easy. Thanks Faraz

  31. Osirus says:

    This is an excellent start for me … thank you. I am working to build a shopping cart using Flash CS3 and would like to know if it is possible to add combo boxes containing color/size options and passed onto paypal for purchase using Actionscript 3?

  32. Kamil says:

    Could you please post it in ActionScript 2.0?
    I just need the script in AS2.0 for “buy now” button.
    I’d appreciate any help,

  33. Rob says:

    Not totally related, but if you want to create over a hundred buttons, it is obviously quite painful to do it through button manager, is there any other way to create paypal buttons. I have a mysql database in the background with all the info. I need, but I don’t have the hosted_button_id for each button unless I create the buttons online?
    Thanks for any help.

  34. Curi says:

    Thanks, was playing around with the coding for a bit myself ^^;

  35. Felicos says:

    Hey Dario,

    nice tutorial

    do you know the codes sending paypal your shopping & handling cost?
    I am thinking of making shopping fee based on the country selected by users.

  36. This is very awesome tutorial. However, I am a regular (type:personal) PayPal user and don’t have Business account. Can I still be to create a donation?

    In code, where you get your ID, I get encrypted text:
    <input type="hidden" name="encrypted" value="—–BEGIN PKCS7—–o;vn;eovna;onv;oaivnoeivnoiWHATEVERkjnadkjn —–END PKCS7—–;

    is it still doable?

    • Chris says:

      Yes, very much so… but, I have found Internet Explorer (as per usual) doesn’t like it. And I’m now wondering if there is a way of fixing it for IE, seeing a majority of people still use it??

      But yea, it works for every other browser!

      • Yiego says:

        like Tomas I also son’t have a business acct. I tried replacing “hidden_button_id” and its “value” with “encrypted” and the very long encrypted “value”. The Paypal acct shows up but it has an error message saying that there is “a problem with the seller’s website”. Am I not doing this correctly?

    • Elia says:

      Hi,
      you can use email code and it works with IE too.

      function buynow ():void
      {
      var url:String = “https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business=eliabster%40gmail%2ecom&lc=IT&item_name=cavallo&item_number=123&amount=11%2e00&currency_code=EUR&button_subtype=services&shipping=10%2e00&bn=PP%2dBuyNowBF%3abtn_buynowCC_LG%2egif%3aNonHosted”; //
      var request:URLRequest = new URLRequest(url);
      request.method = URLRequestMethod.GET;
      navigateToURL(request);

      }

      Bye
      .

  37. Steroids says:

    very nice can i use the same code for action Script 3.

    Please guide me

    Thanks
    Steroids

  38. Jonny says:

    Very nice, but how can we get it to pass confirmation back to flash?

  39. Luciano says:

    Hi, I want to sell a T-shirt on my website and I can’t figure out how to add a “Size” option and a “Color” option to that code. I already did everything on PayPal but I’m trying to add more things to the code and I get an error on the PayPal website. Can anyone guide me? Thanks

  40. chris says:

    HI i am trying to make this work but its telling me the mouse event cant be loaded… or my url request. Thought maybe somebody here could help me. this is my paypal code

    AND THIS IS MY ACTION SCRIPT CODE

    function buynow (event:MouseEvent):void
    {
    var url:String = “https://www.paypal.com/cgi-bin/webscr”; //
    var request:URLRequest = new URLRequest(“http://20sidedrecords.com/wp-content/uploads/2010/10/diceaddtocart.jpg”);
    var paypal_buynow:URLVariables = new URLVariables(“http://20sidedrecords.com/wp-content/uploads/2010/10/diceaddtocart.jpg”);
    paypal_buy.cmd=”_s-xclick”; //
    paypal_buynow.hosted_button_id=”GXEH2JSYGAEWC”; //

    thank you!
    request.data = paypal_buynow;
    navigateToUrl(request);
    trace(paypal_buynow)
    }

  41. chris says:

    sorrry. about that.

    this is the paypal code

    form action=”https://www.paypal.com/cgi-bin/webscr” method=”post”
    input type=”hidden” name=”cmd” value=”_s-xclick”
    input type=”hidden” name=”hosted_button_id” value=”GXEH2JSYGAEWC”
    input type=”image” src=”http://20sidedrecords.com/wp-content/uploads/2010/10/diceaddtocart.jpg” border=”0″ name=”submit” alt=”PayPal – The safer, easier way to pay online!”
    img alt=”" border=”0″ src=”https://www.paypal.com/en_US/i/scr/pixel.gif” width=”1″ height=”1″
    /form

    and this is the action script

    function buynow (event:MouseEvent):void
    {
    var url:String = “https://www.paypal.com/cgi-bin/webscr”; //
    var request:URLRequest = new URLRequest(“http://20sidedrecords.com/wp-content/uploads/2010/10/diceaddtocart.jpg”);
    var paypal_buynow:URLVariables = new URLVariables(“http://20sidedrecords.com/wp-content/uploads/2010/10/diceaddtocart.jpg”);
    paypal_buy.cmd=”_s-xclick”; //
    paypal_buynow.hosted_button_id=”GXEH2JSYGAEWC”; //
    request.data = paypal_buynow;
    navigateToUrl(request);
    trace(paypal_buynow)
    }

  42. Sonicpossum says:

    Dude ur a lifesaver, been searching everywhere for this, thanx a million

  43. Elie says:

    Is there a way to do this same but in AS 2.0?
    Thanks

  44. Zlatan Nikolic says:

    I got
    “PayPal copy, Layer ‘button’, Frame 1, Line 1 1086: Syntax error: expecting semicolon before leftbrace.”
    What Kind of error is that??
    Thank you for your respond

  45. XaiLo says:

    @Zlatan Nikolic, if you copied and pasted the code delete and retype all the quotation marks.

Add a Comment

To add a code snippet to your comment, please wrap your code like so: <pre name="code" class="html">YOUR CODE</pre>. You can replace the class name with "js," "css," "sql," or "php." If there are any "<" or ">" within your code, please search and replace them with: &lt; and &gt; respectively.