Managing subscriptions

You can use the following functions in PaymentManager to manage subscriptions in your app:

Request the terms of a digital good subscription

You can use the SubscriptionTermsReply class to interpret the results of a call to requestSubscriptionTerms(). The requestSubscriptionTerms() function is similar to all other PaymentManager requests. You can check that the requestSubscriptionTerms call finished using the SubscriptionTermsReply::finished() signal or the PaymentManager::subscriptionTermsFinished() signal.

When you call the requestSubscriptionTerms() function, only the ID or SKU of the digital good to be purchased is required; you don't need to provide both. If the ID and SKU are both provided, then the ID is used. To use the SKU, you must pass an empty string for the digitalGoodId.

void MyPaymentApp::getSubscriptionTerms(
    const QString &id, const QString &sku)
{
    if (id.isEmpty())
        return;

    const SubscriptionTermsReply *reply = 
        m_paymentManager->requestSubscriptionTerms(id, sku);

    bool success = QObject::connect(reply, 
         SIGNAL(finished()),
         this, 
         SLOT(subscriptionTermsReply()));

    if (success) {
       // Signal was successfully connected.

    } else {
       // Failed to connect to signal.
    } 
}

Here's a code sample that shows how you can handle the SubscriptionTermsReply. You can display the subscription terms in your UI when the subscriptionTermsResponseSuccess() signal is emitted or you can display an error with the error code and text in your UI when the infoResponseError() signal is emitted.

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

    // Emit an error signal if there were errors.

    if (reply->isError()) {

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

    // Emit a success signal if the request succeeded.

    } else {

       emit subscriptionTermsResponseSuccess(reply->price(), 
                                             reply->initialPeriod(), 
                                             reply->renewalPrice(), 
                                             reply->renewalPeriod());
    }

    reply->deleteLater();
}

Check the status of a subscription

You can use the SubscriptionStatusReply class to interpret the results of your call to requestSubscriptionStatus().

When you call the requestSubscriptionStatus() function, only the ID or SKU of the digital good to be purchased is required; you don't need to provide both. If you do provide both, the ID takes precedence.

You can use the constant PaymentManager::Subscription_App as the digitalGoodId in a call to the requestSubscriptionStatus() function to check the status of an app that is registered as a subscription in BlackBerry World.

void MyPaymentApp::checkSubscriptionStatus(const QString &id, 
                                           const QString &sku)
{
    if (id.isEmpty())
        return;

    const SubscriptionStatusReply *reply =
        m_paymentManager->requestSubscriptionStatus(id, sku);

    bool success = QObject::connect(reply,
         SIGNAL(finished()),
         this,
         SLOT(subscriptionStatusResponse()));

    if (success) {
       // Signal was successfully connected.

    } else {
       // Failed to connect to signal.
    }
}

Here's a code sample that shows how you can handle the SubscriptionStatusReply:

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

    // Emit an error signal if there were errors.

    if (reply->isError()) {

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

    // Emit a success signal if the request succeeded.

    } else {

       emit checkStatusResponseSuccess(reply->isActive());
    }

    reply->deleteLater();
}

Cancel a subscription

To cancel a subscription to a digital good, use the ID of the subscription from the receipt of the purchase or from the list of previous successful purchases. You can retrieve previous purchases using PaymentManager::RequestExistingPurchases(). You can use the CancelSubscriptionReply class to interpret the results of your call to RequestCancelSubscription().

void MyPaymentApp::cancelSubscription(const QString &purchaseId)
{
    if (purchaseId.isEmpty())
        return;

    const CancelSubscriptionReply *reply = 
        m_paymentManager->requestCancelSubscription(purchaseId);

    bool success = QObject::connect(reply, 
         SIGNAL(finished()),
         this, 
         SLOT(cancelSubscriptionResponse()));

    if (success) {
       // Signal was successfully connected.

    } else {
       // Failed to connect to signal.
    }
}

Here's a code sample that shows how you can handle the CancelSubscriptionReply:

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

    // Emit an error signal if there were errors.

    if (reply->isError()) {

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

    // Emit a success signal if the request succeeded.
    } else {

       emit cancelSubscriptionResponseSuccess(reply->isCanceled());
    }

    reply->deleteLater();
}

Last modified: 2013-12-21

comments powered by Disqus