String method = request.getMethodName();
Object[] args = request.getArgs();
String msg = "";
int code = JSExtensionReturnValue.SUCCESS;
JSONObject data = new JSONObject();
JSONArray dataArray = new JSONArray();
JSONObject returnValue = null;
PaymentEngine engine = PaymentSystem.getInstance();
if( engine == null ) {
throw new IllegalArgumentException(
"Sorry, in-app purchases are unavailable. Make sure BlackBerry App World v2.1 or higher is installed on your device." );
}
if( !SUPPORTED_METHODS.contains( method ) ) {
throw new WidgetException( "Undefined method: " + method );
}
try {
if( method.equals( FUNCTION_PURCHASE ) ) {
String digitalGoodID = (String) request.getArgumentByName( KEY_DG_ID );
String digitalGoodSKU = (String) request.getArgumentByName( KEY_DG_SKU );
String digitalGoodName = (String) request.getArgumentByName( KEY_DG_NAME );
String metaData = (String) request.getArgumentByName( KEY_METADATA );
String purchaseAppName = (String) request.getArgumentByName( KEY_APP_NAME );
String purchaseAppIcon = (String) request.getArgumentByName( KEY_APP_ICON );
PurchaseArgumentsBuilder arguments = new PurchaseArgumentsBuilder().withDigitalGoodId( digitalGoodID )
.withDigitalGoodName( digitalGoodName ).withDigitalGoodSku( digitalGoodSKU ).withMetadata( metaData )
.withPurchasingAppName( purchaseAppName )
.withPurchasingAppIcon( ( purchaseAppIcon != null ? Bitmap.getBitmapResource( purchaseAppIcon ) : null ) );
// Blocking call: engine.purchase() invokes AppWorld screen.
Purchase successfulPurchase = engine.purchase( arguments.build() );
String purchaseJSON = purchaseToJSONString( successfulPurchase );
data = new JSONObject( purchaseJSON );
code = JSExtensionReturnValue.SUCCESS;
msg = "Purchase Successful";
} else if( method.equals( FUNCTION_GETEXISTINGPURCHASES ) ) {
String refresh = (String) request.getArgumentByName( KEY_REFRESH );
// Blocking call: engine.getExistingPurchases() invokes AppWorld screen.
Purchase[] purchases = engine.getExistingPurchases( parseBoolean( refresh ) );
if( purchases.length != 0 ) {
for( int i = 0; i < purchases.length; i++ ) {
String purchaseJSON = "";
purchaseJSON += purchaseToJSONString( purchases[ i ] );
JSONObject temp = new JSONObject( purchaseJSON );
dataArray.add( temp );
}
}
} else if( method.equals( new String( FUNCTION_GETMODE ) ) ) {
data.put( "developmentMode", PaymentSystem.getMode() );
code = JSExtensionReturnValue.SUCCESS;