API Service Helper v PHP


Priložený kód slúži ako ukážka na jednoduchú integráciu blueticket.middleware API do Vášho eShopu v PHP:


class syncServiceHelper {

    public $action_code = ""; // nazov akcie, ktora sa ma spustat napr. syncOrder
    public $action_type = "Global"; // typ akcie (case sensitive): Global - univerzalna akcia systemu blueticket.middleware / External - akcia vytvorena pre konkretneho zakaznika
    public $api_key = ""; // API kluc poskytne zakaznik
    public $server_login_address = "https://admin.blueticket.eu/?presenter=api&do=apiLogin"; // url adresa pre prihlasenie k blueticket cloud
    public $server_address = "https://admin.blueticket.eu/?presenter=api&do=executeAction{actionType}&action_code={actionCode}&hash="; // url adresa pre zaslanie objednavky do blueticket cloud
    public $server_name = "admin.blueticket.eu"; // server name pre blueticket.middleware
    private $hash = "";
    public $posid = 1; // ID pokladne ku ktorej sa viaze zaslana objednavka (default = 1)
    public $warehouseid = 1; // ID strediska ku ktoremu sa viaze zaslana objednavka (default = 1)
    public $result; //tu je obsiahnutý údaj o výsledku spracovania v JSON formáte

    public function __construct($action_code, $action_type, $api_key, $server_login_address = "", $server_address = "", $server_name = "", $posid = 1, $warehouseid = 1) {
        if (strlen($server_login_address) > 0) {
            $this->server_login_address = $server_login_address;
        }

        if (strlen($server_address) > 0) {
            $this->server_address = $server_address;
        }

        if (strlen($server_name) > 0) {
            $this->server_name = $server_name;
        }

        $this->action_code = $action_code;
        $this->action_type = $action_type;

        $this->server_address = str_replace("{actionType}", $action_type, $this->server_address);
        $this->server_address = str_replace("{actionCode}", $action_code, $this->server_address);

        $this->api_key = $api_key;
        $this->posid = $posid;
        $this->warehouseid = $warehouseid;
    }

    /*     * *
     * 
     * helper na prihlasenie sluzby ku cloud blueticket.
     * 
     * Po uspesnom prihlaseni sa v objekte odpovede nachadza hash, pre podpisovanie dalsich poziadaviek. Jeho platnost je 24 hodin.
     * 
     */

    public function loginByApiKey() {
        $post_data = "key=$this->api_key";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->server_login_address);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);

        $result = curl_exec($ch);

        $result = json_decode($result);

        $this->result = $result;

        if ($result->result == "OK") { // ak prebehlo prihlasenie v poriadku
            $this->hash = $result->hash; // tento hash je potrebny na nasledne podpisovanie dalsich poziadaviek
            return TRUE;
        } else {
            return FALSE;
        }
    }

    /*     * *
     * Odosle JSON objekt s objednavkou v nasledovnom formate:
     * 
     * {
     *      reference_number: "123456", // cislo objednavky v eShope
     *      table: "F3", // cislo stola
     *      order_date: "YYYY-MM-DD", // datum objednavky v tvare RRRR-MM-DD
     *      order_time: "H:i:s", // cas objednavky v tvare hh:mm:ss
     *      payment_type: 1, // Typ platby : 1 - Hotovost, 2 - Karta, 3 - Stravenka
     *      paid: 0, // V pripade, ak zakaznik objednavku zaplati kartou online paid=1, v opacnom pripade paid=0
     *      waiter: "milan@restaurant.eu", // email obsluhy, ktorej je pridelena objednavka
     *      total: 25.50, // Sucet hodnoty objednavky
     *      note: "Poprosim vyniest na druhe poschodie", // Poznamka k objednavke
     *      name: "Jan Mrkvicka", // Meno zakaznika
     *      name_on_bell: "Jozef Paduch", // Meno na zvonceku
     *      address: "Ruzova ulica 12, 222 11 Namestovo", // Adresa dorucenia
     *      phone: "0905123456", // Telefon zakaznika
     *      email: "jan@mrkvicka.sk", // Email zakaznika
     *      items: [ // polozky objednavky
     *          {
     *              code: "100001", // registracne cislo tovaru v systeme blueticket
     *              name: "Pizza - 4 druhy syra",   // nazov tovaru v Eshope
     *              price: 6.90, // predajna cena s DPH v Eshope
     *              quantity: 2, // objednane mnozstvo
     *              note: "Bez kukurice", // poznamka k objednavke
     *              reference_id: "101", // id polozky v eshope pre spatnu referenciu
     *              extras: [ // extras ku polozke
     *                  {
     *                      code: "100001", // registracne cislo tovaru v systeme bt
     *                      name: "Americke zemiaky",   // nazov tovaru
     *                      price: 6.90, // predajna cena s DPH
     *                      quantity: 2, // mnozstvo
     *                      note: "" // poznamka k objednavke
     *                      reference_id: "10", // id polozky v eshope pre spatnu referenciu
     *                  }
     *              ],
     *              ingredients: [ // uprava zlozenia polozky riadene
     *                  {
     *                      code: "100001", // registracne cislo tovaru v systeme bt
     *                      name: "Chilli",   // nazov tovaru
     *                      price: 6.90, // predajna cena s DPH
     *                      quantity: -1, // mnozstvo +/-
     *                      note: "" // poznamka k objednavke
     *                      reference_id: "21", // id polozky v eshope pre spatnu referenciu
     *                  }
     *              ]
     *          },
     *          {
     *              code: "100001",
     *              name: "Pizza tycinky",
     *              price: 3.90,
     *              quantity: 3,
     *              reference_id: "102" // id polozky v eshope pre spatnu referenciu
     *          }
     *      ]
     * }
     */

    public function sendData($data) {

        if ($this->loginByApiKey()) {
            $values = array(
                'arguments' => json_encode($order),
                'posid' => $this->posid,
                'warehouseid' => $this->warehouseid
            );

            $post_data = http_build_query($values);

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $this->server_address . $this->hash);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);

            $result = curl_exec($ch);

            $result = json_decode($result);

            $this->result = $result;

            if ($result->result == "OK") {
                return TRUE;
            }
        } else {
            return FALSE;
        }
    }

}​


Následne už len stačí zavolať vyššie uvedený helper, odoslať údaje a spracovať odpoveď:

$myservice = new syncServiceHelper("syncOrder", "Global", ""); // vytvorenie instancie objektu

$object = array();

// zakladne udaje objednavky
$object["reference_number"] = "123456";
$object["order_date"] = "2019-10-11";
$object["order_time"] = "12:32:12";
$object["payment_type"] = 1;
$object["paid"] = 0;
$object["total"] = 25.50;
$object["note"] = "Poprosim vyniest na druhe poschodie";
$object["name"] = "Jan Mrkvicka";
$object["name_on_bell"] = "Jozef Paduch";
$object["address"] = "Ruzova ulica 12; 222 11 Namestovo";
$object["phone"] = "0905123456";
$object["email"] = "jan@mrkvicka.sk";

$item = array();
$item["code"] = "10000019";
$item["name"] = "Pizza - 4 druhy syra";
$item["price"] = 6.90;
$item["quantity"] = 2;

$object["items"][] = (object) $item; // pridanie 1.polozky typu stdClass do objednavky

$item = array();
$item["code"] = "10000020";
$item["name"] = "Pizza tycinky";
$item["price"] = 3.90;
$item["quantity"] = 3;
$item["note"] = "Bez feferonov";

$object["items"][] = (object) $item; // pridanie 2.polozky typu stdClass do objednavky

$order = (object) $object;

$myservice->sendData($order); // odoslanie objektu do sluzby blueticket.middleware

var_dump($myservice->result); // zobrazenie vysledku spracovania


Výsledok spracovania je buď OK, alebo error s popisom chyby a prípadnými ďaľšími doplnkovými údajmi:

{
   "received_order":"{\"reference_number\":\"123456\",\"order_date\":\"2019-10-11\",\"order_time\":\"12:32:12\",\"payment_type\":1,\"paid\":0,\"total\":25.5,\"note\":\"Poprosim vyniest na druhe poschodie\",\"name\":\"Jan Mrkvicka\",\"name_on_bell\":\"Jozef Paduch\",\"address\":\"Ruzova ulica 12; 222 11 Namestovo\",\"phone\":\"0905123456\",\"email\":\"jan@mrkvicka.sk\",\"items\":[{\"code\":\"10000019\",\"name\":\"Pizza - 4 druhy syra\",\"price\":6.9,\"quantity\":2},{\"code\":\"10000020\",\"name\":\"Pizza tycinky\",\"price\":3.9,\"quantity\":3,\"note\":\"Bez feferonov\"}]}",
   "result":"OK",
   "status":
   {
      "code":"OK",
      "error":""
   }
}


Chyba môže byť buď na úrovni business logiky:

{
   "received_order":"{\"reference_number\":\"123456\",\"order_date\":\"2019-10-11\",\"order_time\":\"12:32:12\",\"payment_type\":1,\"paid\":0,\"total\":25.5,\"note\":\"Poprosim vyniest na druhe poschodie\",\"name\":\"Jan Mrkvicka\",\"name_on_bell\":\"Jozef Paduch\",\"address\":\"Ruzova ulica 12; 222 11 Namestovo\",\"phone\":\"0905123456\",\"email\":\"jan@mrkvicka.sk\",\"items\":[{\"code\":\"10000019\",\"name\":\"Pizza - 4 druhy syra\",\"price\":6.9,\"quantity\":2},{\"code\":\"1000020\",\"name\":\"Pizza tycinky\",\"price\":3.9,\"quantity\":3,\"note\":\"Bez feferonov\"}]}",
   "result":"error",
   "status":
   {
      "code":"error",
      "error":"Produkt 1000020 - Pizza tycinky nebol nájdený objednávka nemohla byť spracovaná."
   }
}


Alebo systémová, ako napríklad neošetrený vstup atď:

{
   "received_order":"{\"reference_number\":\"123456\",\"order_date\":\"Mrkvicka\",\"order_time\":\"12:32:12\",\"payment_type\":1,\"paid\":0,\"total\":25.5,\"note\":\"Poprosim vyniest na druhe poschodie\",\"name\":\"Jan Mrkvicka\",\"name_on_bell\":\"Jozef Paduch\",\"address\":\"Ruzova ulica 12; 222 11 Namestovo\",\"phone\":\"0905123456\",\"email\":\"jan@mrkvicka.sk\",\"items\":[{\"code\":\"10000019\",\"name\":\"Pizza - 4 druhy syra\",\"price\":6.9,\"quantity\":2},{\"code\":\"1000020\",\"name\":\"Pizza tycinky\",\"price\":3.9,\"quantity\":3,\"note\":\"Bez feferonov\"}]}",
   "result":"error",
   "status":
   {
      "code":"error",
      "error":"SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect date value: 'Mrkvicka' for column 'order_date' at row 1"
   }
}


Dátové spracovanie prebieha transkančne a teda v prípade výskytu akejkoľvek chyby je zrušená celá požiadavka na spracovanie.


Pomohol Vám tento článok?