Responding to a purchase

Your app can use the PurchaseReply class to let your user know what happened during a PaymentManager::requestPurchase(). The PurchaseReply contains a PurchaseReceipt that you can use to display a purchase receipt.

The PurchaseReply class is a subclass of the DigitalGoodReply class, which is a subclass of the PaymentReply class. The functions and signals that are inherited from the PaymentReply class are used for checking if a request to PaymentManager finished and succeeded. The following information can be found in the PurchaseReply:

  • The isFinished() function is used to check if the requestPurchase() finished.

  • The isError() function is used to check if the requestPurchase() succeeded. If the purchase was successful, the PurchaseReply object contains the data you requested, and data that is related to the result of the request that you can display in your UI.

  • The errorCode() and errorText() functions can be used to present a dialog box or toast to your user if the purchase was not successful.

The functions that are inherited from the DigitalGoodReply class are used to find the ID and SKU of the digital good.

  • You can use the digitalGoodId() function to find the ID of a digital good that you supplied to requestPurchase(). The ID is empty if you did not supply an ID to requestPurchase().

  • You can use the digitalGoodSku() to find the SKU of a digital good that you supplied to requestPurchase(). The SKU is empty if you did not supply a SKU to requestPurchase().

The functions in the PurchaseReply class are used to find the receipt for a successful purchase and the metadata associated with the successful purchase.

  • If isError() returns false, you can use the receipt() function or the purchaseMetadata() function to present information about the successful purchase to the user.

The following C++ code sample requests a purchase, passing in an ID and a SKU to requestPurchase(). The response is returned as a PurchaseReply. Then it connects the finished() signal to a handlePurchase() function that is declared in your MyPaymentApp.hpp file and implemented in your MyPayment.cpp file.

void MyPaymentApp::handlePurchase()
{
    bb::platform::PurchaseReply *reply = 
        qobject_cast<bb::platform::PurchaseReply*>(sender());
    Q_ASSERT(reply);

    // Check to see if the request finished 
    if (reply->isFinished()) {
    	
    	// Emit an error signal if there were errors.
        if (reply->isError()) {

            emit infoResponseError(reply->errorCode(), 
                reply->errorText());

        //Emit a success signal if there were no errors. 
        } else {
            const QString displayString = 
                receiptToString(reply->receipt());

            emit purchaseResponseSuccess(displayString);
        }
    } else {
        qDebug() << "Purchase request did not finish.";
    }

    reply->deleteLater();
}

You can extract the information from the PurchaseReceipt with a receiptToString() function:

static QString receiptToString(bb::platform::PurchaseReceipt r)
{
  const QString initialPeriod = QString::number(r.initialPeriod());
  const QDateTime startDate = r.startDate();
  const QString startDateStr = 
        startDate.isNull() ? "N/A" : startDate.toString();
  const QDateTime endDate = r.endDate();
  const QString endDateStr = 
        endDate.isNull() ? "N/A" : endDate.toString();
  const QString isSubscr = r.isSubscription() ? "true" : "false";
  const QString itemStateStr = 
        QString::number(static_cast<int>(r.state()));

  const QString displayString = "Date: " + r.date().toString() +
      "\nID/SKU: " + r.digitalGoodId() + "/" + r.digitalGoodSku() +
      "\nPurchaseID/licenseKey: " + r.purchaseId() + 
          "/" + r.licenseKey() +
      "\nMetadata: " + r.purchaseMetadata() +
      "\nItemState/isSubscription?: " + itemStateStr + 
          "/" + isSubscr +
      "\nStart/End: " + startDateStr + "/" + endDateStr +
      "\nInitialPeriod: " + initialPeriod + "\n";

    return displayString;
}

Last modified: 2013-12-21



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

comments powered by Disqus