Skip to content

Instantly share code, notes, and snippets.

@miniplay
Last active December 19, 2015 05:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miniplay/5907809 to your computer and use it in GitHub Desktop.
Save miniplay/5907809 to your computer and use it in GitHub Desktop.
Sample on how to validate signature and handle our Server 2 Server notifications for item purchases, game likes, dislikes...
<?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);
/*
Always present keys:
"event" : "MINIPLAY_EVENT_TYPE",
"game_id" : "MINIPLAY_GAME_ID",
"game_is_devel" : true|false,
"game_is_production" : true|false,
"time" : "TIMESTAMP_IN_MILLIS",
"user_id" : "MINIPLAY_USER_ID",
"user_token" : "MINIPPLAY_USER_TOKEN"
*/
if (isset($json['event'])) {
if ($json['event'] === "buy-item")) {
/* It's an item purchase event!, 'event_payload' contains the purchased items. What to do now?: */
/* a) If you have your own economy or inventory management, you can
ignore the payload and just read the user inventory and consume
every items you found to remove them from our system.
Please DO NOT EVER add them to your inventory without consuming
them from our inventory. */
/* b) If you don't have your own economy you can probably
just log this message */
} elseif($json['event'] === "validate-item")) {
/* .... 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 */
} elseif($json['event'] === "like-yes")) {
/* .... user liked your game! */
} elseif($json['event'] === "like-no")) {
/* .... user disliked your game! */
} elseif($json['event'] === "fav-add")) {
/* .... user favorited your game! */
} elseif($json['event'] === "fav-remove")) {
/* .... user unfavorited your game! */
} elseif($json['event'] === "share")) {
/* .... user shared your game! */
} elseif($json['event'] === "invite")) {
/* .... user made an invitation to your game! */
} elseif($json['event'] === "challenge")) {
/* .... user challenged other user to your game! */
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment