Creating a PayPal "Buy Now" button

Extending Flashblocks ML is very easy. In this tutorial we will turn a Movie Clip into a PayPal "Buy Now" button / shopping cart. You can see a working example of the following in the flashblocks_demoSite.fla that comes with Flashblocks ML.

  • Create your "Buy Now" Movie Clip and give it any instance name. For example: "buyNow1"
  • Create a Button inside of the "Buy Now" Movie Clip and give it the instance name of "btn"
  • Insert the following code into the "Buy Now" Movie Clip:
function editblockFunction (obj) {
	//	set editor tags from here 
	//	rather then from editblock component inspector.
	//	obj.target.editblockHeader = "Buy Now"
	obj.target.editors = {paypal:[true]}
   
	//	retrieve stored data and remove unneeded characters
	var paypal = obj.data.paypal[0]
	
	if (paypal.business) {
		btn.onRelease = function  () {
			var v = new LoadVars();
			v.business = paypal.business
			v.undefined_quantity = paypal.quantity
			v.item_name = paypal.itemname
			v.item_number = paypal.itemnumber
			v.amount = paypal.amount
			v.shipping = paypal.shipping
			v.shipping2 = paypal.shipping
			v.no_note = "1"
			
			v.currency_code = paypal.currencycode
			//v.lc = "US"
			
			//	add to cart button
			v["add"] = "1"
			v.cmd = "_cart";
			v.send("https://www.paypal.com/cgi-bin/webscr","paypal", "POST");
			
			//	buy now button - single item
			// v.cmd = "_xclick";
			// v.send("https://www.paypal.com/cgi-bin/webscr", "_blank", "POST");
		}
	}
	else delete btn.onRelease
}

So what exactly is happening here? When you drop an Editblock instance onto a Movie Clip, Flashblocks registers that Movie Clip instance. Then when the Movie Clip appears on stage, editblockFunction() is called within that registered Movie Clip. editblockFunction() is passed an Object containing a reference to the Editblock's data. So if there is any PayPal data in the Flashblocks data file then we create the onRelease function for the button, "btn".

Note that in order to use this module you will need to first sign up with PayPal for a free business account.