Code sample: Creating a basic application

The sample application displays a list of goods and contains the following buttons.

Button Description
Buy Initiate the purchase of a digital good.
Refresh Retrieve an array of past successful purchases.

PaymentServiceDemo.as

package com.test.paymentapi 
{
	import flash.display.StageScaleMode;
	import flash.display.StageAlign;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	import qnx.fuse.ui.text.Label;
	import qnx.fuse.ui.buttons.LabelButton;
	
	import qnx.ui.data.DataProvider;
	import qnx.fuse.ui.listClasses.List;
	import qnx.fuse.ui.listClasses.ListSelectionMode;

	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	
	import net.rim.blackberry.events.PaymentErrorEvent;
	import net.rim.blackberry.events.PaymentSuccessEvent;
	import net.rim.blackberry.payment.PaymentSystem;
	import net.rim.blackberry.payment.Purchase;
	
	public class PaymentAPITest extends Sprite {
		
		private var paymentSystem:PaymentSystem;	
		private var buyList:List;
		private var buyButton:LabelButton;		
		private var refreshButton:LabelButton;
		private var myLabel:Label = new Label();
		
		private var digitalGoods:ArrayCollection;
		private var httpService:HTTPService;
		
		public function PaymentAPITest() {
			
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			// configures the application for testing
			paymentSystem = new PaymentSystem();
			paymentSystem.setConnectionMode(PaymentSystem.CONNECTION_MODE_LOCAL);
			
			// listeners for successful and unsuccessful payment events
			paymentSystem.addEventListener(PaymentSuccessEvent.PURCHASE_SUCCESS, purchaseSuccessHandler);
			paymentSystem.addEventListener(PaymentErrorEvent.PURCHASE_ERROR, purchaseErrorHandler);
			paymentSystem.addEventListener(PaymentSuccessEvent.GET_EXISTING_PURCHASES_SUCCESS, getPurchasesSuccessHandler);
			paymentSystem.addEventListener(PaymentErrorEvent.GET_EXISTING_PURCHASES_ERROR, getPurchasesErrorHandler);
						
			initializeUI();
			
		}
		private function initializeUI():void
		{	

			myLabel.setPosition(10, 10);
			myLabel.height = 100;
			myLabel.width = 500;
			myLabel.text = "Basic Payment Application";
			addChild(myLabel);
					
			// creates the Buy button
			buyButton = new LabelButton();
			buyButton.setPosition(50, 100);
			buyButton.width = 200;
			buyButton.height = 80;
			buyButton.label = "Buy";
			buyButton.addEventListener(MouseEvent.CLICK, purchaseHandler);
			this.addChild(buyButton);

			
			// creates the Refresh button
			refreshButton = new LabelButton();
			refreshButton.setPosition(300, 100);
			refreshButton.width = 200;
			refreshButton.height = 80;
			refreshButton.label = "Refresh";
			refreshButton.addEventListener(MouseEvent.CLICK, refreshHandler);
			this.addChild(refreshButton);
			
			// creates the list that displays purchase options
			buyList = new List();  
			buyList.setPosition(0,300);
			buyList.width = stage.stageWidth;            
			buyList.height = stage.stageHeight - 300;
			//buyList.columnWidth = 400;
			buyList.scrollable = true;
			buyList.allowDeselect = false;
			buyList.selectionMode = ListSelectionMode.SINGLE;	
			this.addChild(buyList);
			
			// sends an HTTPService request to retrieve a list of digital goods
			httpService = new HTTPService();
			httpService.url = "http://docs.blackberry.com/digitalgoods.xml";
			httpService.addEventListener(ResultEvent.RESULT, resultHandler);
			httpService.addEventListener(FaultEvent.FAULT, faultHandler);
			httpService.send();	
		}
		
		private function resultHandler(event:ResultEvent):void
		{
			var remoteData:ArrayCollection;			
			var data:DataProvider;
			var dgObject:DigitalGoodsInfo;

			
			// creates an array of raw xml data
			remoteData = event.result.digitalgoods.digitalgood;
			
			// sets a data provider for the list
			data = new DataProvider();
			buyList.dataProvider = data;
			
			digitalGoods = new ArrayCollection();
			
			// loops through each object in the array of xml data, creates 
			// DigitalGoodsInfo objects, and displays the names of the 
			// digital goods in the list

			for each(var obj:Object in remoteData)
			{
				dgObject = new DigitalGoodsInfo(obj.name, obj.sku, obj.metadata);
				digitalGoods.addItem(dgObject);				
				buyList.dataProvider.addItem({label: obj.name});
			}
			buyList.selectedIndex = 0;
		}
		
		private function faultHandler(event:FaultEvent):void
		{
			trace("Unable to retrieve digital goods from the server.");
		}
		
		private function purchaseHandler(event:MouseEvent):void
		{
			var index:int;
			trace("in purxhaseHandler.");
			index = buyList.selectedIndex;
			
			// initiates a purchase using the properties of the digital good
			// that is at the equivalent position in the array as the position
			// in the list that is currently selected
			trace("initiates purchase.");
			paymentSystem.purchase(null, 
				digitalGoods.getItemAt(index).sku,
				digitalGoods.getItemAt(index).label,
				digitalGoods.getItemAt(index).metadata,
				digitalGoods.getItemAt(index).appName,
				digitalGoods.getItemAt(index).icon);
		}
		
		private function refreshHandler(event:MouseEvent):void
		{
			// initiates a local request for a list of existing purchases
			paymentSystem.getExistingPurchases(false);
			trace("refresh handler.");
		}
		
		private function purchaseSuccessHandler(event:PaymentSuccessEvent):void
		{
			var purchase:Purchase = event.purchase;
			
			trace("Purchase Success - " + purchase.date + " : " + 
				purchase.digitalGoodID + " : " + purchase.digitalGoodSKU + 
				" : " + purchase.licenseKey + " : " + purchase.metaData + 
				" : " + purchase.purchaseID);
		}
		
		private function purchaseErrorHandler(event:PaymentErrorEvent):void
		{		
			trace("Purchase Error - " + event.errorID + " : " + event.text);		
		}
		
		private function getPurchasesSuccessHandler(event:PaymentSuccessEvent):void
		{
			var purchase:Purchase;
			var pastPurchases:Array = event.existingPurchases;
			
			if (pastPurchases.length == 0)
			{
				trace("No existing purchases");
			}
			else
			{
				// iterates through the list of existing purchases and prints the data
				for (var i:int = 0; i < pastPurchases.length; i++)
				{
					purchase = pastPurchases[i];
					
					trace("Get Existing Purchases Success - " + purchase.date + 
						" : " + purchase.digitalGoodID + " : " + 
						purchase.digitalGoodSKU + " : " + purchase.licenseKey + 
						" : " + purchase.metaData + " : " + purchase.purchaseID);
				}
			}						
		}
		
		private function getPurchasesErrorHandler(event:PaymentErrorEvent):void
		{
			trace("Get Existing Purchases Error - " + event.errorID + " : " + event.text);
		}
		
	}
	
}

DigitalGoodsInfo.as

package com.test.paymentapi {
	public class DigitalGoodsInfo
	{
		//public var name:String;
		public var label: String
		public var sku:String;
		public var metadata:String;
		public var icon:String;
		public var appName:String;
		
		public function DigitalGoodsInfo( name:String, sku:String, metadata:String )
		{
			trace("in DigitalGoodsInfo.");
			this.label = name;
			this.sku = sku;
			this.metadata = metadata;
			
			icon = "http://www.rim.com/products/appworld_3col.jpg";
			appName = "PaymentServiceDemo";			
		}
	}
}

digitalgoods.xml

<?xml version="1.0" encoding="utf-8"?>
<digitalgoods>
    <digitalgood>
        <name>Developing for BlackBerry</name>
        <sku>123456abc123</sku>
        <metadata>1234-56-7890</metadata>
    </digitalgood>
    <digitalgood>
        <name>Intro to the BlackBerry Tablet OS</name>
        <sku>123456abc123</sku>
        <metadata>09-4567-1243</metadata>
    </digitalgood>
    <digitalgood>
        <name>Tutorial: Selling Digital Goods</name>
        <sku>123456abc123</sku>
        <metadata>654-34-29304</metadata>
    </digitalgood>
    <digitalgood>
        <name>Feature and Technical Overview</name>
        <sku>123456abc123</sku>
        <metadata>7399-29-0940</metadata>
    </digitalgood>
    <digitalgood>
        <name>Development Guide</name>
        <sku>123456abc123</sku>
        <metadata>34-90390-124</metadata>
    </digitalgood>
</digitalgoods>