PHPClass

Met de Spari PHPClass (download hier) kan je eenvoudig de belangrijkste features inladen in jouw product. Hiermee zal het mogelijk zijn om de aan jouw bedrijf gerelateerde stempelkaarten uit te lezen en zelfs te bestempelen. Zo zal deze Class ervoor zorgen dat bepaalde data op de juiste wijze op de achtergrond verwerkt, zodat jij als software leverancier Spari naadloos in de applicatie kan implementeren. Ook krijgt de klant hiermee direct te zien wat zijn huidige kaarten bij jou zijn en, wanneer je in een afrekenproces stempels toe wilt kennen, inzicht in wat deze zal ontvangen.

ConnectSpari.php

Alle mogelijkheden van de Spari Class uitgelegd in onderstaande code, voor de uitgebreidere documentatie neem de Getting Started docs door.

// Include our API PHPClass 
// > file downloadable at http://spari.nl/files/ConnectSpari-api-v1.zip
include 'ConnectSpari.php';

// Always start with defining our class
$spari = new ConnectSpari("use_api_public_key_here", "use_api_secret_key_here");

// Use the connection session to receive the right data
$spari->sessionCustomer = "the_customer_hash_goes_here";

// ** 
//  The GET DATA codeblocks
// ** 

/*/ 
 * Check if the customer session is valid
 * Parameter $session can be used to validate if session exists
 * Without parameter the function will use the $spari->sessionCustomer variable
 * Return (string): 
 * - VALID (when user session exists)
 * - INVALID (when user session was not found)
/*/ 
$spari->checkCustomer($session = '');


/*/ 
 * Receive all client's cards and stamps
 * Will use the $spari->sessionCustomer to identify customer
 * Return (array) params: 
 * -  title (the card deal)
 * -  amount (current amount of stamps)
 * -  required (total stamps required)
 * -  completed (status of completed)
 * -  image (url to uploaded image)
/*/ 
$results = $spari->myCards();


// This is the format the API will have to receive to be able to read the rewards 
// The array will contain demo data, you have to adjust these to your own software
$rewards = array(
  // this is the total value of the order, which is used for
  'value' => '100.00', 

  // the items element will be containing all the product and category id's
  'items' => array( 
     // this array will contain all the product ids and attached data
     'products' => array(
	'1' => array( // this array will contain all the necessary product data
	   'category_id' => '8', // tell the API what the category_id of the product is
	   'amount' => '2', // provide the amount of products in the cart that have this product_id
	   'name' => 'Test product 1' // tell the API what the name of this product is
	)
     )
  )
);

// Request to read the possible rewards
// Output the API gives will be based on howmuch the $order_value is and the previously mentioned $rewards format
$results = $spari->readRewards($order_value, $rewards);

// EOF 
// ** 
//  The SET DATA codeblocks
// ** 

// Register a new user with just one click
// Results will be a Json string containing the sessionCustomer
// Notice: this feature fully depends on your Spari App Premissions
$spari->registerCustomer($id, $email, $firstname, $lastname, $gender, $birthday, $street = '', $suburb = '', $zipcode = '', $city = '', $country = '');

/*/ 
 * This is the format the API will require to receive and to be able to set the rewards 
 * The array will contain demo data, you have to adjust these to your own software
 * Notice: this will be the demo data we're referring to when using the variable in other codeblocks
/*/
$rewards = array(
  // this is the total value of the order, which is used for
  'value' => '100.00', 

  // the items element will be containing all the product and category id's
  'items' => array( 
     // this array will contain all the product ids and attached data
     'products' => array(
	'1' => array( // this array will contain all the necessary product data
	   'category_id' => '8', // tell the API what the category_id of the product is
	   'amount' => '2', // provide the amount of products in the cart that have this product_id
	   'name' => 'Test product 1' // tell the API what the name of this product is
	)
     )
  )
);

// Make an reservation for the new stamps the following way
$results = $spari->requestStamp($rewards);

// The output on $results will be containing the reservation key
// NOTICE: Make sure to store this key somewhere attached to the order, so you will be able to make the reservation official on completing 
$results = array('status' => 'CREATED', 'succes' => true, 'reserved_key' => 'this_is_the_key_you_have_to_store');


/*/ 
 * This function requires to send the cards that have to be claimed, we will use variable $cards in this example which presents the format 
 * Notice: this will be the demo data we're referring to when using the variable in other codeblocks
/*/ 
$cards = array(
  'the_first_card_id_goes_here' => 'amount_of_first_cards_that_will_be_exchanged_goes_here', 
  'the_second_card_id_goes_here' => 'amount_of_second_cards_that_will_be_exchanged_goes_here' 
  // if more cards are being exchanged, follow the template above.
  // this array may also contain just one card
);

// Mark the stamps as being reserved
$results = $spari->requestClaim($cards);

// The output on $results should be as following
$results = array(
  // as usual, it will return the default variables
  'reserved_key' => 'the_reserved_key_can_be_used_for_your_validation', 
  'status' => 'CREATED', 
  'succes' => true, 

  // and finally it will return the rewards
  'rewards' => array( 
     // this array will contain all the data attached to the card which the user exchanged
     '23' => array(
	'type' => 'cash', // the reward type will either be cash or product

	// When the reward type is product, the API will return the free product id the following way
	'products' => array( // this array will contain all the product id's that will be free on the order
	   '0' => '123' // the 123 will be the free product id that has been set through our platform
	)

	// When the reward type is cash, the API will return the following discount array
	'discount' => array( // this array will contain all the necessary product data
	   'value' => '1.97', // the value of the discount will be mentioned here, where the type has to be applied on
	   'type' => 'fixed' // this will be the discount type applied with the value, which is either fixed or percent
	)

	// It's either the products array or the discount array, at this moment we won't return both at the same time
     )
  )
);


/*/ 
 * First we'll be editing the stamps a user will receive 
 * For this example we'll be using the previous $rewards array example.
/*/ 
$results = $spari->updateStamp("the_reserved_key_goes_here", $rewards);

// The output on $results should be as following
$results = array('status' => 'DONE', 'succes' => true, 'reserved_key' => 'the_reserved_key_can_be_used_for_your_validation');


/*/ 
 * And last we'll be editing the claimed stamps that are reserved 
 * For this example we'll be using the previous $cards array example.
/*/ 
$results = $spari->updateClaim("the_reserved_key_goes_here", $cards);

// The output on $results should be as following
$results = array('status' => 'DONE', 'succes' => true, 'reserved_key' => 'the_reserved_key_can_be_used_for_your_validation');


// Cancel the reservation for claiming or setting new stamps 
$cancel = $spari->cancelRequest("the_reserved_key_goes_here");

// The function will return the following array
$cancel = array('succes' => true, 'status' => 'CANCELED', 'reserved_key' => 'the_reserved_key_can_be_used_for_your_validation'); 


// Make the reservation official and available 
$confirm = $spari->confirmRequest("the_reserved_key_goes_here");

// The function will return the following array
$confirm = array('succes' => true, 'status' => 'DONE', 'reserved_key' => 'the_reserved_key_can_be_used_for_your_validation'); 

// EOF