Purchases a reward from an App Domain's reward store.
Allows a user that is logged in to purchase a reward from the reward store
https://redcritterconnecter.com/services/gamificationv1/PurchaseUserReward?sessionID={sessionID}&appID={appID}&appDomainID={appDomainID}&rewardID={rewardID}&purchasePrice={purchasePrice})
SessionID | string | yes | The SessionID is established when you are currently logged into the system using your RedCritter username or email and password |
AppID | int64 | yes | The appID is the ID number of the App. You can determine this number using the GetRewards API command. |
AppDomainID | int64 | yes | The appDomainID is the number of the App Domain ID. You can determine this number using the GetRewards API command. |
RewardID | string | yes | This is the ID of the reward you would like to purchase. You can determine this ID using the GetRewards API command. |
PurchasePrice | int32 | yes | This is the purchase price of the reward in the RedCritter reward store. You can determine this price using the GetRewards API command. |
Code Samples Javascript, C#
PurchaseUserReward with JavaScript
This is a minimal example of calling the PurchaseUserReward API via HTML and Javascript. You need to have a session ID (obtained by signing into the system using the SignIn API documentation).
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.js"></script>
<style type='text/css'>
body {
font-family: "Arial";
}
.rctable{
padding-top:20px;
padding-bottom:10px;
border-bottom:solid 1px #ccc;
width:600px;
}
.rclabel {
font-size:14px;
font-weight:bold;
}
.rcval{
font-size:14px;
padding-left:10px;
}
</style>
<script type="text/javascript">
var connecterURL = "https://www.redcritterconnecter.com/";
function purchaseUserReward(sessionID, appID, appDomainID, rewardID, purchasePrice, cbSuccess, cbFail) {
$.getJSON(connecterURL + "services/gamificationv1/purchaseUserReward?jsoncallback=?", { SessionID: sessionID, appID: appID, appDomainID: appDomainID, rewardID: rewardID, purchasePrice: purchasePrice },
function (data) {
if (data.Result) {
cbSuccess(data);
} else {
cbFail(data);
}
});
}
//My Success Callback
function onMyPurchaseUserRewardSuccessHandler(data) {
//data is JSON response
renderRewards(data)
}
//My Failure Callback
function onMyPurchaseUserRewardFailHandler(data) {
//Something went wrong
alert("Something went wrong");
}
//Sample function to generate the rewardslist output in html
function renderRewards(data) {
var h = "<table class='rctable'>";
h += "<tr><td class='rclabel'>Original Balance : </td><td class='rcval'>" + HTMLEncode(data.OriginalBalance) + "</td></tr>"
h += "<tr><td class='rclabel'>New Balance : </td><td class='rcval'>" + HTMLEncode(data.NewBalance) + "</td></tr>"
h += "<tr><td class='rclabel'>Receipt ID : </td><td class='rcval'>" + HTMLEncode(data.ReceiptID) + "</td></tr>"
h += "</table>";
//display the results
$("#results").html(h);
}
function HTMLEncode(str) {
var div = document.createElement('div');
var text = document.createTextNode(str);
div.appendChild(text);
return div.innerHTML;
}
</script>
</head>
<body>
<table>
<tr>
<td>Session ID</td>
<td>
<input type="text" id="tSessionID" style="width: 500px" />
</td>
</tr>
<tr>
<td>App ID</td>
<td>
<input type="text" id="tAppID" style="width: 500px" />
</td>
</tr>
<tr>
<td>App Domain ID</td>
<td>
<input type="text" id="tAppDomainID" style="width: 500px" />
</td>
</tr>
<tr>
<td>Reward ID</td>
<td>
<input type="text" id="tRewardID" style="width: 500px" />
</td>
</tr>
<tr>
<td>Purchase Price</td>
<td>
<input type="text" id="tPurchasePrice" style="width: 500px" />
</td>
</tr>
<tr>
<td></td>
<td>
<input type="button" onclick="purchaseUserReward($('#tSessionID').val(), $('#tAppID').val(), $('#tAppDomainID').val(), $('#tRewardID').val(), $('#tPurchasePrice').val(), onMyPurchaseUserRewardSuccessHandler, onMyPurchaseUserRewardFailHandler)" value="Purchase Reward" /></td>
</tr>
</table>
<br />
<div id="results"></div>
</body>
</html>
PurchaseUserReward with C#
This is a minmal example of calling the PurchaseUserReward API and parsing the JSON result into a populated C# object. This example uses asynchronous techniques to raise a callback when the response is received. You will be allowed to sign in to RedCritter first to get your SessionID. You can get a list of the AppID, AppDomainID, RewardID and Cost from the GetRewards API call.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using System.Web; //Add reference to System.Web
using System.Runtime.Serialization.Json; //Add reference to System.Runtime.Serialization
namespace RedCritterConnecter.Samples
{
//Create a class to contain the response
public class PurchaseUserRewardResponse
{
public Boolean Result { get; set; }
public String ReceiptID { get; set; }
public Int32 OriginalBalance { get; set; }
public Int32 NewBalance { get; set; }
}
public class PurchaseUserReward
{
public delegate void OnPurchaseUserRewardResponse(PurchaseUserRewardResponse PurchaseUserRewardResponse);
public event OnPurchaseUserRewardResponse onPurchaseUserRewardResponse;
public delegate void OnPurchaseUserRewardResponseError(PurchaseUserRewardResponse PurchaseUserRewardResponse);
public event OnPurchaseUserRewardResponseError onPurchaseUserRewardResponseError;
const string CONST_ConnecterBaseURL = "https://www.redcritterconnecter.com/";
public void Execute(String SessionID, String AppID, String AppDomainID, String RewardID, String PurchasePrice)
{
try
{
//Create url encoded parameters in query string
String queryString = "sessionid=" + System.Web.HttpUtility.UrlEncode(SessionID) + "&appid=" + System.Web.HttpUtility.UrlEncode(AppID.ToString()) + "&appdomainid=" + System.Web.HttpUtility.UrlEncode(AppDomainID.ToString()) + "&rewardid=" + System.Web.HttpUtility.UrlEncode(RewardID.ToString()) + "&purchaseprice=" + System.Web.HttpUtility.UrlEncode(PurchasePrice.ToString());
//Create a new instance of a WebClient
WebClient wc = new System.Net.WebClient();
//Prevent this request from caching in order to ensure that it is sent to server
wc.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
//Attach an event handler to receive the response
wc.DownloadStringCompleted += onPurchaseUserRewardResponseReceived;
//Make the call
wc.DownloadStringAsync(new Uri(CONST_ConnecterBaseURL + "services/gamificationv1/PurchaseUserReward?" + queryString, UriKind.Absolute));
}
catch
{
//Something went wrong communicating with the server
if (onPurchaseUserRewardResponseError != null)
{
onPurchaseUserRewardResponseError(null);
}
}
}
private void onPurchaseUserRewardResponseReceived(object sender, DownloadStringCompletedEventArgs e)
{
try
{
//Create a JSON serializer
System.Runtime.Serialization.Json.DataContractJsonSerializer s = new DataContractJsonSerializer(typeof(PurchaseUserRewardResponse));
//Copy the string into a memory stream
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(e.Result));
//Read the stream into an object matching the response's type
PurchaseUserRewardResponse PurchaseUserRewardResponse = (PurchaseUserRewardResponse)s.ReadObject(ms);
//Work with the populated response object
if (PurchaseUserRewardResponse.Result == true)
{
//Request was accepted, raise the success event
onPurchaseUserRewardResponse(PurchaseUserRewardResponse);
}
else
{
//Something went wrong
if (onPurchaseUserRewardResponseError != null)
{
//Request failed, raise the error event
onPurchaseUserRewardResponseError(PurchaseUserRewardResponse);
}
}
}
catch
{
//Something went wrong
onPurchaseUserRewardResponseError(null);
}
}
}
}
Responses JSON, XML
XML Response
<Response xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="Connecter">
<APIVersion>1.0.0</APIVersion>
<AuthErrorCode>0</AuthErrorCode>
<ErrorMessage />
<AppDomainID>0</AppDomainID>
<AppID>0</AppID>
<ErrorCode>0</ErrorCode>
<NewBalance>98</NewBalance>
<OriginalBalance>99</OriginalBalance>
<ProfileEntity>JoeUser@somedomain.com</ProfileEntity>
<ReceiptID>635138398953551105</ReceiptID>
<Result>true</Result>
<Reward>
<AppDomainID>59</AppDomainID>
<AvailQty>1111</AvailQty>
<Cost>1</Cost>
<Description>An easy reward to afford.</Description>
<EndDate>2000-01-01T00:00:00</EndDate>
<ImageURL>
http://connecterstoredev.blob.core.windows.net/rewards/rewardicon_0.png
</ImageURL>
<LowLevel>0</LowLevel>
<RewardID>2013-08-22 19:37:09.7743603</RewardID>
<RewardName>CheapTreat</RewardName>
<StartDate>2000-01-01T00:00:00</StartDate>
</Reward>
</Response>
JSON Response
{
"AppID": 0,
"AppDomainID": 0,
"Reward": {
"AppDomainID": 59,
"RewardID": "2013-08-22 19:37:09.7743603",
"RewardName": "CheapTreat",
"Description": "An easy reward to afford.",
"AvailQty": 1110,
"Cost": 1,
"LowLevel": 0,
"StartDate": "2000-01-01T00:00:00",
"EndDate": "2000-01-01T00:00:00",
"ImageURL": "http://connecterstoredev.blob.core.windows.net/rewards/rewardicon_0.png"
},
"ProfileEntity": "JoeUser@somedomain.com",
"OriginalBalance": 98,
"NewBalance": 97,
"ReceiptID": "635138401202860270",
"Result": true,
"ErrorMessage": "",
"ErrorCode": 0,
"APIVersion": "1.0.0",
"AuthErrorCode": 0
}
|
Use |
Runtime |
Method |
HTTP GET |
Invites User |
No |
Billable |
No |
Response |
JSON,XML |
API Version |
1 |
|