How to implement server to server callback for rapidoreach SDKs?

Viewed 68

I have successfully added rapidoreach ios app monetization SDK into my app. i am not very much aware about server to server callbacks..Any idea how it can be done with nodejs and express?

2 Answers

You can use request method which comes by default with node.js or by using axios library for server to server callback, but a quick check at rapidoreach site (https://www.rapidoreach.com/docs#/callbacks) leads to this doc for callback setup and looks like you want to receive callbacks, in that case you have to setup a api at your end to receive their server call, this is one example done in Nodejs

Example:

        function rapidoreachPostback(req: Request, res: Response) {

      var IP = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
      var Input = req.query;
      
      if (Input.status == 'C' || Input.status == "P" || Input.status == "F") {

        if(Input.status === "C"){
          //record your completion here
        }
    
        //if rapidoreach allows disaqualification points
        if(Input.status === "P"){
          //record your disqualification transaction over here
        }
        if(Input.status === "F"){
          //record your survey offer failure transaction over here
        }
        
        }
          } catch (error) {
            // send "1" in response to server call from rapidoreach.
            res.send("1");
            return;
          }
          res.send("1");
    
          return;
        })
        return;
      }
      res.send("1");
      return;
    }
    
    

Rapidoreach will send server to server callback using get request to the callback URL maintained in App setings.

Assuming Callback URL in app settings is set as

https://local.callback.com/callback.php

Below is an example of server to server callback. Details of parameters available in callback can be found in their official docs https://www.rapidoreach.com/docs#/callbacks

https://local.callback.com/callback.php?cmd=P&userId=SYKUser-CvkBPlfibeV-aba04fe2fc72219fdbb9a9353a68713d&amt=0.01&offerInvitationId=trans0002&status=P&offerHash=4fdcc6461ec225ba8af3bc66ccf4017c&currencyAmt=0.80&transactionId=trans0002&endUserId=SYKUser&txnHash=d5fecc45d5be250ff757f8694a6d65a1&useragent=Rapidoreach&currencyName=Local Coins&offerType=1&deviceType=Desktop&intergrationMethod=IFRAME

Here is an example how you can handle callback using PHP.

File callback.php

<?php

/**
 * ApplicationKey can be found in credentials tab of app created in Rapidoreach
 */
$ApplicationKey = "<Replace this>"; //<- replace this with real application key

/**
 * $endUserId is unique endUserId in publisher's system.
 */
$EndUserId = $_REQUEST['endUserId'];

/**
 * Unique offer Id 
 */
$OfferInvitationId = $_REQUEST['offerInvitationId'];

/**
 * TransactionId
 */
$TransactionId = $_REQUEST['transactionId'];

/**
 * Status
 *    C: Completed - User has successfully completed an offer and should be rewarded with currencyAmt
 *    P: Attempted - User has attempted an offer and attempt is valid will be rewarded with currencyAmt
 *                  currencyAmt will not be full in this case and will be equal to screenout reward maintained in App Setting of Rapidoreach Publisher portal
 *    F: Failed - User has failed to complete an offer and hence terminated. No rewards should be awarded
 */
$Status = $_REQUEST['status'];

/**
 * Validate offerHash
 * You should verify oidHash upon receipt of the callback by recomputing it with the callback offerInvitationId and your ApplicationKey. 
 * This will secure against users faking their own id and passing it in if by some chance they come across the script.
 */
$offerHash = $_REQUEST['offerHash'];
$CalculatedOfferHash = md5($OfferInvitationId . $ApplicationKey);
if ($offerHash != $CalculatedOfferHash) {
  /**
   * TODO: User is trying to manupulate offerId to get credits. Flag this user within this condition if required (optional).
   * Do not credit this user and send success response to Rapidoreach
   * Do not continue further
   */
  echo "1";
  die;
}

/**
 * Validate txnHash
 */

$txnHash = $_REQUEST['txnHash'];
$CalculatedTxnHash = md5($TransactionId . $ApplicationKey);
if ($txnHash != $CalculatedTxnHash) {
  /**
   * TODO: User is trying to manupulate transaction id to get credits. Flag this user within this condition if required (optional).
   * Do not credit this user and send success response to Rapidoreach
   * Do not continue further
   */
  echo "1";
  die;
}

/**
 * Credit the user based on offer status
 */

switch ($Status) {
  case 'C':
    # TODO: Credit the $EndUserId with $currencyAmt use $TransactionId to avoid duplicates
    echo "1";
    die;
    break;
  case 'P':
    # TODO: Credit the $EndUserId with $currencyAmt use $TransactionId to avoid duplicates
    echo "1";
    die;
    break;
  case 'F':
    # TODO: User has failed to complete an offer
    echo "1";
    die;
    break;

  default:
    # code...
    break;
}
Related