Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Highlighted
Contributor
Posts: 41
Registered: ‎07-19-2011
My Device: 9500
My Carrier: airtel

Payment sdk integration Problem

Hi all,

 

I am working on an application which contains In app purchase and i am using (Payment sdk 1.8). I had written some test code & uploaded in appworld & created a sandbox account and downloaded to simulator. Everything goes fine upto here but when i had clicked on display purchase History or BUY button, the screens got freezed & No keys are working. I am attaching the code also.

 

/**
 * 
 */
package com.id.ui;

import net.rim.device.api.ui.UiApplication;

/**
 * @author K.Sandeep Sagar
 *
 */
public class EntryPoint extends UiApplication {
/**
 * 
 */
public EntryPoint() {
UiApplication.getUiApplication().pushScreen(new PaymentHomeScreen());
}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new EntryPoint().enterEventDispatcher();

	}

}

 

/**
 * 
 */
package com.id.ui;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.BasicEditField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.RichTextField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rimlib.blackberry.api.paymentsdk.AppWorldUpdateRequired;
import net.rimlib.blackberry.api.paymentsdk.PaymentEngine;
import net.rimlib.blackberry.api.paymentsdk.PaymentException;
import net.rimlib.blackberry.api.paymentsdk.Purchase;
import net.rimlib.blackberry.api.paymentsdk.PurchaseArgumentsBuilder;
import net.rimlib.blackberry.api.paymentsdk.PurchaseResult;
import net.rimlib.blackberry.api.paymentsdk.purchaseHistory.PurchaseHistory;
import net.rimlib.blackberry.api.paymentsdk.purchaseHistory.PurchaseHistoryListingListener;

/**
 * @author K.Sandeep Sagar
 * 
 */
public class PaymentHomeScreen extends MainScreen implements
		FieldChangeListener {
	private ButtonField buyButton;
	private ButtonField displayButton;
	private BasicEditField digitalGoodName;
	private BasicEditField digitalGoodSku;
	private PaymentEngine engine = PaymentEngine.getInstance();
	private RichTextField errorMessage;
	private PurchaseHistoryListingListener purchaseHistoryListener;

	/**
 * 
 */
	public PaymentHomeScreen() {
		try {
			PaymentEngine.isAppWorldInstalledAndAtCorrectVersion();
		} catch (AppWorldUpdateRequired awur) {
			// launches browser for user to upgrade their BlackBerry App World
			// client
			PaymentEngine.upDateAppWorld();
		}
		engine = PaymentEngine.getInstance();
		if (engine != null) {
			digitalGoodName = new BasicEditField("Digital Good Name:  ",
					"Sample Good");
			add(digitalGoodName);
			digitalGoodSku = new BasicEditField("Digital Good SKU:   ",
					"abc123");
			add(digitalGoodSku);

			HorizontalFieldManager hfm = new HorizontalFieldManager();
			add(hfm);

			buyButton = new ButtonField("Buy");
			buyButton.setChangeListener(this);
			hfm.add(buyButton);
			displayButton = new ButtonField("Display Purchases");
			displayButton.setChangeListener(this);
			hfm.add(displayButton);
		} else {
			errorMessage = new RichTextField(
					"Sorry, in-app purchases are unavailable");
			add(errorMessage);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * net.rim.device.api.ui.FieldChangeListener#fieldChanged(net.rim.device
	 * .api.ui.Field, int)
	 */
	public void fieldChanged(Field field, int context) {
		if (field == buyButton) {
			UiApplication.getUiApplication().invokeLater(new Runnable() {

				public void run() {

					String name = digitalGoodName.getText();
					String sku = digitalGoodSku.getText();
					PurchaseArgumentsBuilder arguments = new PurchaseArgumentsBuilder()
							.withDigitalGoodSku(sku).withDigitalGoodName(name)
							.withMetadata(name).withPurchasingAppName(
									"Payment Service SDK Demo");

					try {
						PurchaseResult purchase = engine.purchase(arguments
								.build());
						Dialog.inform("Purchase of " + purchase.getMetadata()
								+ " is successful.");
					} catch (IllegalArgumentException e) {
						Dialog.inform(e.getMessage());
					} catch (PaymentException e) {
						Dialog.inform(e.getMessage());
					}

				}
			});
		} else if (field == displayButton) {
			purchaseHistoryListener = new PurchaseHistoryListingListener() {
				public void error(String message, int errorCode) {
					Dialog.inform(message);
				}

				public void success(final Purchase[] purchases) {
					UiApplication.getUiApplication().invokeLater(
							new Runnable() {

								public void run() {

									if (purchases.length != 0) {
										if (getFieldCount() > 3) {
											deleteRange(3,
													(getFieldCount() - 3));
										}
										for (int i = 0; i < purchases.length; i++) {
											RichTextField purchaseRecord = new RichTextField(
													"Name: "
															+ purchases[i]
																	.getMetadata()
															+ " SKU: "
															+ purchases[i]
																	.getDigitalGoodSku());
											add(purchaseRecord);
										}
									} else {
										Dialog.inform("No existing purchases");
									}

								}
							});
				}
			};
			PurchaseHistory.get(purchaseHistoryListener);
		}

	}
}

 

Please help to solve this.