Monday, July 21, 2008

Flex and Paypal - Adding a payPal “buy now” button in Flex

Adding a payPal “buy now” button in Flex

I’m in the early stages of developing a paypal shopping cart system in Flex. A cursory glance on the web told me that many of you have questions about this topic. I’ve seen many questions from people using thee HTTPService class to try to do this. It seems (although I’ve only been looking at this for a very short time) that HTTPService will not redirect after a “POST”. So for those wanting to crack this open a bit, here’s a method of sending data to paypal with a redirect from a paypal button using flash.net.URL request. Although this example is very basic, and a full shopping cart app integrated with paypal is obviously alot more work, one can see that doing a simple “buy now” is pretty easy. Most of the (not working) examples I saw were making it much harder than it is. After all the HTML is just:

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″ />
<title>Untitled Document</title>
</head>

<body>
<form action=”https://www.paypal.com/cgi-bin/webscr” method=”post”>
<input type=”hidden” name=”cmd” value=”_xclick”>
<input type=”hidden” name=”business” value=”you@youremail.com”>
<input type=”hidden” name=”item_name” value=”Item Name”>
<input type=”hidden” name=”currency_code” value=”USD”>
<input type=”hidden” name=”amount” value=”0.00″>
<input type=”image” src=”http://www.paypal.com/en_US/i/btn/x-click-but01.gif” name=”submit” alt=”Make payments with PayPal - it’s fast, free and secure!”>
</form>
</body>
</html>

Here’s the Flex:

<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute” >

<mx:Image source=”http://www.paypal.com/en_US/i/btn/x-click-but01.gif” horizontalCenter=”0″ verticalCenter=”0″ click=”makePayment();”/>
<mx:Script>
<![CDATA[
import flash.net.URLRequest
public function makePayment():void{
var url:String = “https://www.paypal.com/cgi-bin/webscr”;
var request:URLRequest = new URLRequest(url);
var variables:URLVariables = new URLVariables();
variables.cmd=”_xclick”;
variables.currency_code=”USD”;
variables.business=”clayton@tetraktysdesign.com”;
variables.item_number = “001″;
variables.item_name = “Donation to Flex Community For This Awesome Code Sample”;
variables.amount= “2.00″;
variables.quantity = 1;
variables.tax = ”;
request.data = variables;
request.method = URLRequestMethod.POST;
navigateToURL(request,”_parent”);
}
]]>
</mx:Script>
</mx:Application>

The first things that come to mind for me are:

1) get your item data from an external xml file and an HTTPService call

2) use the repeater component to create your buttons (and product shots)

and of course, the whole shopping cart situation … I’ll be up for a while.

Hope this helps some of you.

Flex - creating an xml photo gallery

This is a method of creating a photo gallery in Flex with a main photo, and scrolling thumbnails, populated by an external XML file. This is a continuation of the concept in this article: http://blog.flexcommunity.net/?p=24


This shows how easy it is to create things of this nature using the repeater component, and one simple function. I’ll leave out the detailed explanation, as it’s almost identical to the above cited article.

example here

here’s the application code:


<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” xmlns:custom=”custom.*” layout=”absolute” backgroundColor=”#030303″ creationComplete=”imageData.send();”>
<mx:Script>
<![CDATA[

var mySource:String=”http://path/to/the/first/image/to/display/1.jpg”
function setPath(myPath:String):void{
mainImage.source=myPath;}
]]>
</mx:Script>

<mx:HTTPService id=”imageData” url=”images.xml” resultFormat=”e4x”/>
<mx:Image id=”mainImage” source=”{mySource}” width=”500″ height=”400″ verticalCenter=”0″ horizontalCenter=”0″ >

</mx:Image>
<mx:VBox height=”400″ horizontalScrollPolicy=”off” verticalCenter=”0″ horizontalCenter=”325″>
<mx:Repeater id=”imageRepeater” dataProvider=”{imageData.lastResult.img}”>
<mx:Canvas width=”120″ height=”80″ backgroundColor=”#000000″ horizontalScrollPolicy=”off” verticalScrollPolicy=”off” borderStyle=”none”>
<custom:CustomImage path=”{imageRepeater.currentItem.src}” width=”100″ height=”100%” useHandCursor=”true” source=”{imageRepeater.currentItem.thumbnail}” verticalCenter=”0″ left=”0″ click=”setPath(String(event.currentTarget.path));”/>
</mx:Canvas>
</mx:Repeater>

</mx:VBox>
</mx:Application>

here’s the XML structure:

<?xml version=”1.0″ encoding=”utf-8″?>
<gallery>
<img>
<src>http://path/to/your/large/image/1.jpg
</src>

<thumbnail>http://path/to/your/thumbnail/t1.jpg

</thumbnail>
</img></gallery>


here’s the custom image code (file is called CustomImage.as and is kept in a folder called custom)

package custom
{
import mx.controls.Image;

public class CustomImage extends Image

{
public function CustomImage()
{
super();
}
public var path:String = “”;

[Inspectable(category=”General”, defaultValue=”")]

}
}