Advertisement
emansammutfetchit

business_registration_webhook_v1

Jan 23rd, 2019
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.00 KB | None | 0 0
  1. <?php
  2.     ini_set('display_errors', 1);
  3.     include_once "../../misc/common_functions.php";
  4.     $file = "./logs/business_registration.log";
  5.     $conn = connectMySql();
  6.     $body = json_decode(file_get_contents("php://input"));
  7.  
  8.     $localities_areas = array(
  9.         "Blata l-Bajda" => "east",
  10.         "Floriana" => "east",
  11.         "Gwardamangia" => "east",
  12.         "Gzira" => "east",
  13.         "Hamrun" => "east",
  14.         "Ibragg" => "east",
  15.         "Kappara" => "east",
  16.         "Msida" => "east",
  17.         "Paceville" => "east",
  18.         "Pembroke" => "east",
  19.         "Pieta" => "east",
  20.         "San Gwann" => "east",
  21.         "Santa Venera" => "east",
  22.         "Sliema" => "east",
  23.         "St Andrews" => "east",
  24.         "St Julians" => "east",
  25.         "Ta Giorni" => "east",
  26.         "Ta Xbiex" => "east",
  27.         "Valletta" => "east",
  28.         "Attard" => "central",
  29.         "Bahar ic-Caghaq" => "central",
  30.         "Balzan" => "central",
  31.         "Birkirkara" => "central",
  32.         "Gharghur" => "central",
  33.         "Iklin" => "central",
  34.         "Lija" => "central",
  35.         "Madliena" => "central",
  36.         "Marsa" => "central",
  37.         "Mosta" => "central",
  38.         "Mriehel" => "central",
  39.         "Msierah" => "central",
  40.         "Naxxar" => "central",
  41.         "Qormi" => "central",
  42.         "San Pawl tat-Targa" => "central",
  43.         "Swatar" => "central",
  44.         "Swieqi" => "central",
  45.         "Bugibba" => "north",
  46.         "Burmarrad" => "north",
  47.         "Maghtab" => "north",
  48.         "Manikata" => "north",
  49.         "Mellieha" => "north",
  50.         "Mgarr" => "north",
  51.         "Qawra" => "north",
  52.         "Salina" => "north",
  53.         "St Pauls Bay" => "north",
  54.         "Xemxija" => "north",
  55.         "Birgu" => "south",
  56.         "Birzebbuga" => "south",
  57.         "Bormla" => "south",
  58.         "Fgura" => "south",
  59.         "Ghaxaq" => "south",
  60.         "Gudja" => "south",
  61.         "Hal-far" => "south",
  62.         "Isla" => "south",
  63.         "Kalkara" => "south",
  64.         "Kirkop" => "south",
  65.         "Luqa" => "south",
  66.         "Marsaskala" => "south",
  67.         "Marsaxlokk" => "south",
  68.         "Mqabba" => "south",
  69.         "Paola" => "south",
  70.         "Qrendi" => "south",
  71.         "Safi" => "south",
  72.         "Santa Lucija" => "south",
  73.         "Tarxien" => "south",
  74.         "Xghajra" => "south",
  75.         "Zabbar" => "south",
  76.         "Zejtun" => "south",
  77.         "Zurrieq" => "south",
  78.         "Bahrija" => "west",
  79.         "Dingli" => "west",
  80.         "Mdina" => "west",
  81.         "Mtarfa" => "west",
  82.         "Rabat" => "west",
  83.         "Siggiewi" => "west",
  84.         "Ta Qali" => "west",
  85.         "Tal-Virtu" => "west",
  86.         "Zebbug" => "west"
  87.     );
  88.  
  89.     addToLog($file, "new business", $body);
  90.     $action = $body->action;
  91.     if ($action){
  92.         switch ($action){
  93.             case "_new_registration":  
  94.                 storeRegistrationDetails($conn, $body);
  95.                 break;
  96.             case "_activate":
  97.                 createUser($conn, $body->data->signup_id);
  98.                 break;
  99.         }
  100.     }
  101.  
  102.     //on post request store new registration on database (unactivated)
  103.     function storeRegistrationDetails($conn, $body){
  104.         global $file;
  105.         $new_entry = (object) $body->data;
  106.         $query = $conn->prepare("INSERT INTO tb_business_signups
  107.                                        (business_name, business_email, business_phone, business_vat, business_address, business_locality,
  108.                                        business_type, business_no_locations, business_weekly_deliveries)
  109.                                VALUES (?,?,?,?,?,?,?,?,?)");
  110.         $query->bind_param( 'sssssssis', $new_entry->business_name, $new_entry->business_email, $new_entry->business_phone, $new_entry->business_vat,
  111.                                         $new_entry->business_address, $new_entry->business_locality, $new_entry->business_type,
  112.                                         $new_entry->business_no_locations, $new_entry->business_weekly_del_est);
  113.         $query->execute();
  114.         if (isset($conn->error) && $conn->error != "") { addToLog($file, "<add signup details db> :: ", $conn->error); }
  115.     }
  116.    
  117.     //create user on tookan and get vendor id
  118.     function createUser($conn, $signup_id){
  119.         global $file;
  120.         $getBusinessQuery = "SELECT * FROM tb_business_signups WHERE signup_id = '$signup_id';";
  121.         $getBusinessQueryExec = sqlQuery($conn, $getBusinessQuery);
  122.  
  123.         if ($getBusinessQueryExec->num_rows > 0) {
  124.             $business  = (object) $getBusinessQueryExec->fetch_assoc();
  125.             $postData = array(
  126.                 "api_key"=> "695e96ffe7ff1525fd7c51cd48fb8b3cab2e3becef025951dc1c5a4a9f602e4c",
  127.                 "user_type"=> 1,
  128.                 "name"=> "$business->business_name", //might want to change to another custom field (upon activation)
  129.                 "phone"=> "$business->business_phone",
  130.                 "email"=> "$business->business_email",
  131.                 "password" => "$business->business_email",
  132.                 "domain" => "fetchit.tookan.in",
  133.                 "form_ids" => [293],
  134.                 "company" => "$business->business_name",
  135.                 "address" => "$business->business_address",
  136.                 "description" => "Corporate Account",
  137.                 "latitude" => 35.8817,
  138.                 "longitude" => 14.4498
  139.             );
  140.  
  141.             $result = (object) postRequest("https://api.tookanapp.com/v2/customer/add", $postData);
  142.             if ($result->data["customer_id"]) { findUser($result->data["customer_id"], $business); } else {
  143.                 addToLog($file, "<create user on tookan> :: ", $result);
  144.             }
  145.         }
  146.     }
  147.  
  148.     //get user vendor id
  149.     function findUser($customerId, $business){
  150.         global $file;
  151.         $postData = array(
  152.             "api_key" => "695e96ffe7ff1525fd7c51cd48fb8b3cab2e3becef025951dc1c5a4a9f602e4c",
  153.             "customer_id" => $customerId
  154.         );
  155.         $customer = (object) postRequest("https://api.tookanapp.com/v2/view_customer_profile", $postData);
  156.         $vendorId = $customer->data["cust_details"][0]["vendor_id"];
  157.         if ($vendorId) {
  158.             createMerchant($customer, $customerId, $vendorId, $business);
  159.         } else {
  160.             addToLog($file, "<create user on tookan> :: ", $customer);
  161.         }
  162.     }
  163.  
  164.     //update user details vendor_id / customer_id
  165.     function createMerchant($customer, $customerId, $vendorId, $business){
  166.         global $conn, $file, $localities_areas;
  167.         //change to activated on signups page and add current date time to activated_date_timer
  168.         $updateSignupQuery = $conn->prepare("UPDATE tb_business_signups SET activated = ?, activated_date_time = ? WHERE signup_id = ?");
  169.         $updateSignupQuery->bind_param('isi', $activated = 1, $$current_date_time = date("Y-m-d H:i:s"), $business->signup_id);
  170.         $updateSignupQuery->execute();
  171.         if (isset($conn->error) && $conn->error != "") { addToLog($file, "<update signup row db> :: ", $conn->error); }
  172.         //create a new row in merchants with the details for a new account and all other values as default (password etc) + vendor_id
  173.         $newMerchantQuery = $conn->prepare("INSERT INTO merchants (merchant_name, username, password, area, merchant_locality, merchant_type,
  174.                                                        email_address, contact_no, merchant_id, business_address, vendor_id, activation_date)
  175.                                                    VALUES
  176.                                                        (?,?,?,?,?,?,?,?,?,?,?,?)");
  177.         $password = password_hash($business->business_email, PASSWORD_DEFAULT);
  178.         $newMerchantQuery->bind_param("ssssssssisis",   $business->business_name, $business->business_email, $password,
  179.                                                         $localities_areas[$business->business_locality], $business->business_locality,
  180.                                                         $merchantType = "corporate", $business->business_email, $business->business_phone,
  181.                                                         $customerId, $business->business_address, $vendorId, $activationDate = date("Y-m-d H:i:s"));  
  182.         $newMerchantQuery->execute();                                          
  183.         if (isset($conn->error) && $conn->error != "") { addToLog($file, "<create new merchant row db> :: ", $conn->error); }
  184.     }
  185.    
  186.     //send instructions to client (email)
  187.     function sendNotification($customerEmail){
  188.  
  189.     }
  190.  
  191.  
  192. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement