Skip to content

Instantly share code, notes, and snippets.

@DarkAllMan
Last active October 19, 2020 16:50
Show Gist options
  • Save DarkAllMan/10bbefa1db6823243507ad8869ab0e2c to your computer and use it in GitHub Desktop.
Save DarkAllMan/10bbefa1db6823243507ad8869ab0e2c to your computer and use it in GitHub Desktop.
Kiyoh, Klantenvertellen create Invite on API
<?php
// HELPER FUNCTION
public function endsWith( $haystack, $needle ) {
return $needle === "" || (substr($haystack, -strlen($needle)) === $needle);
}
public function create_kiyoh_invite($order_id, $this_status_transition_from, $this_status_transition_to, $instance){
$result=false;
$current_domain = 'yourdomain.com';
// GENERAL DATA
$kiyoh_api_key = 'a12b3c4d-5e6f-7g8h-9i0j-1a2b3c4d5e6f'; // YOUR KIYOH/KLANTENVERTELLEN KEY
$kiyoh_location_id = 1234567; //
$url = 'https://www.kiyoh.com/v1/invite/external';
// URL PARAMETERS
$data['hash'] = $kiyoh_api_key;
$data['locationId'] = $kiyoh_location_id;
$data['tenantId'] = 98; // 99 FOR KLANTENVERTELLEN
$data['location_id']= $kiyoh_location_id;
$data['delay'] = 1;
$data['language'] = 'nl';
$data['ref_code'] = $order_id;
// IF STATUS IS SET TO SENT, ANYOTHERSTATUS
if(in_array($this_status_transition_to,array('sent','any-other-status'))){
// GET ORDER DATA
$order = wc_get_order($order_id);
$kk_review_invite_created = get_post_meta($order_id, 'kk_review_invite_created', true);
if (!empty($kk_review_invite_created)){
$order->add_order_note( 'Geen nieuwe Kiyoh invite. Deze was al aangemaakt voor deze order!' );
return false;
}
// URL PARAMETERS
$data['invite_email'] = $order->get_billing_email();
$data['first_name'] = $order->get_billing_first_name();
$data['last_name'] = $order->get_billing_last_name();
// IF ORDER IS FROM LIEVELABELS DO NOT CREATE KIYOH REQUEST
if($this->endsWith( $data['invite_email'], $current_domain )){
$order->add_order_note( 'Geen Kiyoh invite voor ' . $current_domain );
return false;
}
// CREATE FULL URL
$url = sprintf("%s?%s", $url, http_build_query($data));
// MAKE GET API CALL TO SET INVITE IN X DAYS
$curl = curl_init();
// SET CURL OPTIONS
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($curl, CURLOPT_TIMEOUT, 400);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// EXECUTE CURL AND GET RESULT
$result = curl_exec($curl);
$resultCheck = json_decode($result);
if ($result==false || $resultCheck->code!='OK' ) {
update_post_meta($order->get_id(), 'kk_review_invite_created', current_time('mysql'));
// ADD ERROR TO ORDER NOTES
$detailedError = $resultCheck->detailedError[0]->message;
$order->add_order_note( 'Kiyoh uitnodiging niet aangemaakt: ' . $detailedError );
curl_close($curl);
return false;
}
// ADD NOTE TO ORDER
$order->add_order_note( 'Kiyoh uitnodiging aangemaakt' );
curl_close($curl);
// UPDATE META ON SUCCES
update_post_meta($order->get_id(), 'kk_review_invite_created', current_time('mysql'));
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment