Skip to content

Instantly share code, notes, and snippets.

@miniplay
Last active August 29, 2015 14:00
Show Gist options
  • Save miniplay/11146102 to your computer and use it in GitHub Desktop.
Save miniplay/11146102 to your computer and use it in GitHub Desktop.
Sample on how to pre-validate item purchases with our Server 2 Server notifications.
<?php
/* ---- PLEASE MAKE SURE YOUR ENCODING IS: UTF-8 (without BOM) ---- */
$jsonString = file_get_contents("php://input"); /* Get the RAW POST DATA */
/* Validate signature */
if ( ! ( isset($_SERVER['HTTP_MINI_SIGNATURE'])
&& $_SERVER['HTTP_MINI_SIGNATURE'] == md5("[YOUR_API_KEY]".$jsonString) )) {
throw new Exception("Invalid signature");
}
/* Decode the JSON string received and convert it to an associative array */
$json = json_decode($jsonString, true);
if (isset($json['event'])) {
if ($json['event'] === "validate-item" && isset($json['event_payload'])) {
/* .... user wants to buy an item, 'event_payload' contains the item detail. What to do now? */
/* 1) check your system to decide if the user can purchase the item
2) echo "OK" if the user can buy it or another string if the user cannot buy it, you can also set a HTTP ERROR HEADER */
echo "OK";
/*
Example data received
{
"event" : "validate-item",
"event_payload" : {
"current_stock" : 0, // Current stock of the user (Amount of units already in the inventory)
"item" : {
"catalog_id" : "4", // Catalog id
"id" : "12", // Item id
"is_buyable" : "1",
"is_deleted" : "0",
"is_devel" : "1",
"is_enabled" : "1",
"is_usable" : "0",
"max_stock" : "1",
"must_validate" : "1",
"name" : "Double cannon",
"price_minicoins" : "20",
"uid" : "cannon_x2",
"is_dynamic": 0, // Only for dynamic items (for selected developers)
"icon": "", // Only for dynamic items (for selected developers)
"dynamic_payload": "" // Only for dynamic items (for selected developers)
},
"qty" : 1, // Amount of units that the user wants to purchase of this item
"time" : "1398090845255"
},
"game_id" : "xxxxx",
"game_is_devel" : true,
"game_is_production" : false,
"time" : "1398090845255",
"user_id" : "xxxxxx", // MINIPLAY user id
"user_token" : "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" // MINIPLAY user token
}
*/
}
/* ---- If your callback also receives other types of events they can be implemented here ---- */
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment