JSON format for shopping cart data

📘

simpleCart(JS)

If you are using simpleCart(JS) as explained at the corresponding page you needn't be familiar with all the tiny details below. Everything works without considering any JSON formats with simpleCart(JS).

If you don't want to use simpleCart(JS) but your own shopping cart framework we advise you to store the data in a custom donation field in the following JSON format. By doing so the shopping cart data could be shown as a human readable table in the FundraisingBox backend.

{
  "headers":[
    "name",
    "quantity",
    "price"
  ],
  "items":{
    "1":{"name":"Inoculation","quantity":"1","price":"20"},
    "2":{"name":"Pig","quantity":"3","price":"5"}
  },
  "total":35
}
<?php
$donationFormUrl = ""; // TODO url of your donation form
$formHash = ""; // TODO hash of the donation form
$customFieldId = 0; // TODO id of the custom field that stores the cart content
$shoppingCartContentAsJson = "{}"; // TODO shopping cart content as JSON (see above)
$shoppingCartSum = 0; // TODO total amount of the shopping cart


$curl = curl_init("https://secure.fundraisingbox.com/app/createSession");
 
curl_setopt($curl, CURLOPT_CAINFO, dirname(__FILE__).'/cacert.pem');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
 
// set values
$data = array(
  "hash" => $formHash,
  "payment" => array(
    "amount_fix" => $shoppingCartSum,
	"donation_custom_field_".$customFieldId => $shoppingCartContentAsJson
  )
);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
 
// receive session
$result = json_decode(curl_exec($curl), true);
$status = $result["status"];
$session = $result["session"];
 
// TODO error handling
$curl_error = curl_error($curl);
$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$curl_error_no = curl_errno($curl);
if($curl_error)
{
   // do something
}
curl_close($curl);

// redirect to the donation form with session hash
header("Location: ".$donationFormUrl."?fbSessionHash=".$result["session"]["hash"]);
?>