1

我正在使用 Payment SDK 1.8 版了解黑莓中的应用内购买任务。目前我正在使用以下代码:

package com.purchasedemo;

///import net.rimlib.blackberry.api.payment.*;
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;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;

public class PurchaseDemo extends UiApplication
{
    public static void main(String[] args) 
    {
        PurchaseDemo theApp = new PurchaseDemo();
        theApp.enterEventDispatcher();  
    }
    public PurchaseDemo() 
    {   
        pushScreen(new PurchaseDemoScreen());
    }


      private static class PurchaseDemoScreen extends MainScreen implements FieldChangeListener {
            private BasicEditField parentSku;
            private ButtonField buyButton;
            private ButtonField displayButton;
            private BasicEditField digitalGoodName;
            private BasicEditField digitalGoodSku;
            private PurchaseHistoryListingListener purchaseHistoryListener;
            private PaymentEngine **engine** = PaymentEngine.getInstance();

            public PurchaseDemoScreen() {
                setTitle( "Payment Service SDK Demo" );

                if( **engine** != null ) {
                    parentSku = new BasicEditField( "Parent SKU: ", "abc123" );
                    add( parentSku );
                    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 {
                    RichTextField errorMessage = new RichTextField( "Sorry, in-app purchases are unavailable" );
                    add( errorMessage );
                }
            }

            public void fieldChanged( Field field, int context ) {
                if( field == buyButton ) {
                    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( Purchase[] purchases ) {
                            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);
                }
            }
        }
}

通过使用这段代码,我只是想了解这个过程实际上是如何工作的。但问题是,我在PaymentEngine的引擎对象中得到了 null 。请让我知道我该如何解决这个问题。以及这项服务的实际要求是什么。目前我正在使用 5.0 操作系统和设备 6.0 操作系统

4

1 回答 1

2

在打电话给你之前,你要做的第一件事PaymentEngine.getInstance()应该是先打电话PaymentEngine.isAppWorldInstalledAndAtCorrectVersion()。此调用检查设备上安装了哪个版本的 App World 以及是否需要升级。支付 SDK 需要随 SDK 版本而变化的 App World 的最低版本,因此上述调用将有助于确保您的安全。如果这引发异常,您可以调用PaymentEngine.upDateAppWorld()以启动更新。

如果 App World 的版本是正确的,请提供有关设备上 App World 版本的一些信息,以及用于重现问题的确切设备型号和软件版本。

于 2012-07-30T13:59:44.870 回答