All Collections
General questions
Section heading
Using a post-hooked cloud script to override product purchase rewards
Using a post-hooked cloud script to override product purchase rewards

product, verifyPurchase, PurchaseRewardHook

Jason Liang avatar
Written by Jason Liang
Updated over a week ago

In the case that you want to double the rewards of a promotional item, instead of offering the same items for less $, you may want to use PurchaseRewardHook feature to override the configured reward amounts for product purchases.

The following example will show you the steps to complete this process.

  • First, create a hook script as below. it's well-described as the in-line comments. It is going to override the rewards for a purchase given promotion status.

    "use strict";
    function main() {
    var response = {};
    // The goal of this routine is to adjust the purchase rewards if a promotion
    // is in progress - according to the custom JSON in the promotion record.
    // First check to see if promotions are active
    if ( data.promotions.length > 0 ) {
    // Then check to see if this item will actually be purchased...
    // (Not much point in doing the calculations othersiwe)
    if ( data.alreadyPurchased === false ) {
    // Okay - lets see what item is being purchased
    var itemId = data.itemId;
    var newRewardData = {};
    var newPromotionId = data.promotionId; // no idea, may change this based on promotionalOverridesApplied below?
    var currentPromotionPriority = -1;
    // Iterate through the promotions to see if there is one with replacement currency values
    // Note that we've included a "priority" field in the customJson portion of the promotion,
    // so that this script can tell which priority rewards to apply (in cases where multiple promotions are active)
    var promotionalOverridesApplied = false;
    for (var i = 0; i < data.promotions.length; i++ ) {
    // We don't need to apply the pricing changes - brainCloud will have already done that.
    // We just focus on the custom rewards and custom JSON
    // First check to see if it's higher priority than the current promotion (if any)
    if ( data.promotions[i].customJson.priority > currentPromotionPriority ) {
    // Now see if there is actually an item that matches in the promotion
    if (data.promotions[i].customJson.itemIds.hasOwnProperty(itemId) === true ) {
    // Booya - let's grab the updated currency and customJson values
    currentPromotionPriority = data.promotions[i].customJson.priority;
    newRewardData.currency = data.promotions[i].customJson.itemIds[itemId].currency;
    newRewardData.extra = data.promotions[i].customJson.itemIds[itemId].extra;
    //newPromotionId = -1; // ONLY IF want data.promotionId removed from transaction
    promotionalOverridesApplied = true;
    newPromotionId = data.promotions[i].promotionId;
    }
    }
    }
    if ( promotionalOverridesApplied === true ) {
    response.rewards = newRewardData;
    response.promotionId = newPromotionId;
    return response;
    } else {
    return null;
    }
    } else {
    return null;
    }
    } else {
    return null;
    }
    }
    main();
  • Then, create a promotion for your app from the portal promotions section. Make sure to set the custom JSON fields accordingly to the script above.

    Note: replace the rewards rule as you need.

    {
    "priority": 100,
    "itemIds": {
    "barBundle1Imp": {
    "currency": {
    "bars": 12
    },
    "extra": {
    "specialItemReward": "bonusItemA"
    }
    },
    "gemcollection": {
    "currency": {
    "gems": 7
    },
    "extra": {
    "specialItemReward": "bonusItemA"
    }
    }
    }
    }

  • After then, go to Design | Cloud Code | API Hooks page, click Create, hook the script you just created as post-hook to PurchaseRewardHook operation as below:

  • Done!

Pro-tips:

  • You can verify the rewards changes by calling GetEligiblePromotions() or RefreshPromotions() from API Explorer for a user who is eligible for the promotion you set. (Note that the rewards for bars is overridden from original 10 to 12, and the gems is changed from 5 to 7)

    The original rewards for bars:

    The original rewards for gems:

    After the above script is hooked

  • Once the purchase is finished, you can also verify the rewards from the Transactions page as well by viewing the Rewards Data of that transaction entry.

Did this answer your question?