Checking for past purchases

You can avoid offering a digital good to a user who has already purchased it by checking the user's purchase history with the blackberry.payment.getExistingPurchases function.

You can also use the blackberry.payment.getExistingPurchases function when a user initiates a request to cancel a subscription, in order to obtain a purchase ID for the cancellation process.

The blackberry.payment.getExistingPurchases function includes a boolean parameter, refresh, that allows you to specify how the purchase history is retrieved. Set refresh to True to retrieve a purchase history from the Payment Service server; set it to False to attempt to use the local purchase history, and only retrieve purchase history from the Payment Service server if no match is made locally.

If the transaction is successful, the onSuccess callback function returns a purchase object for each purchase of the app, made by the user.

<script type="text/javascript">
  function getExistingPurchases() {
      try {
          blackberry.payment.getExistingPurchases(true, onSuccess, onFailure);
      } catch (e) {
          alert ("Error" + e);
      }
  }

 function onSuccess(purchases) {
     for (var i = 0; i < purchases.length; i++) {
         var transId = purchases[i].transactionID;
         var sku = purchases[i].digitalGoodSKU;
         var dgId = purchases[i].digitalGoodID;
         alert("Purchased Item " + i + ": " + transId + "," + sku + "," + dgId);
     }
 }

 function onFailure(err) {
     alert("Error occurred: " + err.errorText + ", " + err.errorID);
 }
</script>

Last modified: 2014-10-09



Got questions about leaving a comment? Get answers from our Disqus FAQ.

comments powered by Disqus