Retrieving information about past purchases

You can retrieve information about past purchases by invoking PaymentEngine.getExistingPurchases(boolean allowRefresh) for a synchronous call, or PurchaseHistory.get(PurchaseHistoryListingListener listener) for an asynchronous call. A record of past purchases is necessary if you want to customize your application to reflect past purchases, or if you want to implement a subscription model that initiates recurring purchases.

Invoking getExistingPurchases() returns an ExistingPurchasesResult object. You can retrieve an array of Purchase objects that represent existing purchases by invoking ExistingPurchasesResult.getPurchases(). Each result in the array contains details about a purchase, such as the SKU, digital good ID, metadata and the date of the purchase.

The allowRefresh parameter is a Boolean value that indicates whether the application can retrieve the records of purchases from the Payment Service server, or that the application can only retrieve records that are stored on the BlackBerry device. To retrieve records from the Payment Service, log in using a BlackBerry ID. To retrieve records from the BlackBerry device, you do not need to log in. You may choose to query the Payment Service server after a device switch because records of purchases might not be stored on the BlackBerry device. An indeterminate progress dialog appears while the user's purchase history is retrieved from the Payment Service server.

Code sample: Using a synchronous call to retrieve information about past purchases

try
{
    ExistingPurchasesResult result = engine.getExistingPurchases(true);
    Purchase[] resultPurchases = result.getPurchases();
}
catch (PaymentException e)
{
    //code that is executed if an exception is thrown
}

Code sample: Using an asynchronous call to retrieve information about past purchases

private PurchaseHistoryListingListener listener;
public void getPurchaseHistory() {
    listener = new PurchaseHistoryListingListener() {
            
        public void error( String message, int errorCode ) {
            // Handle error
        }
        
        public void success( Purchase[] purchases ) {
            // Handle the purchases
        }
    };
    
    PurchaseHistory.get(listener);
}

Was this information helpful? Send us your comments.