File: /home/awaldron/_accomods/api.accomods.com/v1/thrive.php
<?php
/*
* Subscription documentation:
* https://developers.thrivecart.com/documentation/event_subscription/order_payment_product/
*
*/
//**********************************************************************************************************//
// Method: POST
// Endpoint: thrive/lastuser
// Params:
function post_v1_thrive_lastuser($args, $body, $uid, $is_admin, $db)
{
$sql = "SELECT * FROM users WHERE email='".$body['email']."' AND pass IS NULL ORDER BY id DESC";
$user = qRow( $db, $sql );
if ($user) {
response(200, ['success' => true, 'payload'=>$user]);
} else {
response(200, ['success' => false, 'err' => $body['email']]);
}
}
//**********************************************************************************************************//
// Method: POST
// Endpoint: thrive/setpw
// Params:
function post_v1_thrive_setpw($args, $body, $uid, $is_admin, $db)
{
$rules = [
'newPw' => 'req',
'userId' => 'req'
];
validate_body( $body, $rules );
$upd = "UPDATE users SET pass='".md5($body['newPw'])."' WHERE pass IS NULL AND id=".$body['userId'];
$res = $db->query($upd);
response(200, ['success' => !!$res]);
}
//**********************************************************************************************************//
// Method: POST
// Endpoint: thrive/log
// Params:
function post_v1_thrive_log($args, $body, $uid, $is_admin, $db)
{
$json = json_encode($body);
$arr = json_decode($json, true);
$event = $arr['event'];
$mode = $arr['mode'];
$product_id = $arr['base_product'];
$orderId = $arr['order']['id'];
$customerId = $arr['customer_identifier']; // thrive_id
$cid = $arr['customer_id']; // thrive_id2
$email = $arr['customer']['email'];
$expires = substr($arr['order']['future_charges'][0]['due'], 0, 10);
// LOG TRANSACTION
$ins = "INSERT INTO transactions (txn_source, txn_thriveid, txn_thriveid2, txn_email, txn_event, txn_orderid, txn_data, txn_mode)".
" VALUES ('thrive', '".$customerId."', '".$cid."', '".$email."', '".$event."', '".$orderId."', '".str_replace("'","\'",$json)."', '".$mode."')" .
" ON DUPLICATE KEY UPDATE txn_data='".str_replace("'","\'",$json)."', ctr=ctr+1";
$res1 = $db->query($ins);
if ($product_id == 2) {
// ADD / UPDATE USER IN DATABASE
if ($event == 'order.success') {
// CHECK IF thrive_id2 EXISTS
$find = "SELECT thrive_id2 FROM users WHERE thrive_id2='".$cid."'";
$exists = qVal( $db, $find );
if (!$exists) {
$reg = "INSERT INTO users (`thrive_id`, `thrive_id2`, `email`, `fname`, `lname`, `phone`, `school_name`, `expires`) VALUES (".
"'" . $customerId . "',".
"'" . $cid . "',".
"'" . $arr['customer']['email'] . "',".
"'" . addslashes($arr['customer']['first_name']) . "',".
"'" . addslashes($arr['customer']['last_name']) . "',".
"'" . $arr['customer']['custom_fields']['phone'] . "',".
"'" . addslashes($arr['customer']['custom_fields']['sname']) . "',".
"'" . $expires . "'".
")";
$res2 = $db->query($reg);
}
else {
$upd = "UPDATE users SET fname='".addslashes($arr['customer']['first_name'])."',".
" lname='".addslashes($arr['customer']['last_name'])."',".
" phone='".$arr['customer']['custom_fields']['phone']."',".
" school_name='".addslashes($arr['customer']['custom_fields']['sname'])."',".
" expires='".$expires."', billfail=0, cancelled=0".
" WHERE thrive_id2='".$cid."'";
$res2 = $db->query($upd);
}
// CHECK IF THIS IS THE FIRST INSERT:
$check = "SELECT ctr FROM transactions WHERE txn_event='order.success' AND txn_orderid='".$orderId."'";
$ctr = qVal( $db, $check );
if ($arr['base_product']==2 && !$exists) {
// SEND EMAIL TO USER WITH PASSWORD SET URL
send_pw_email( $arr['customer']['email'] );
}
}
else if ($event === "order.rebill") {
$customer_id = $arr['customer']['id'];
$expires = substr($arr['subscription']['next_payment_date_iso8601'], 0, 10);
$upd = "UPDATE users SET expires='".$expires."' WHERE thrive_id2='".$customer_id."'";
$db->query($upd);
$res2 = true;
}
else if ($event === "order.subscription_payment") {
$next_renew = $arr["subscription"]["next_payment_date"];
$exp = date('Y-m-d', $next_renew);
$upd = "UPDATE users SET billfail=0, cancelled=0, expires='".$exp."' WHERE thrive_id2='".$cid."'";
$db->query($upd);
$res2 = true;
}
else if ($event === "order.rebill_failed") {
$upd = "UPDATE users SET billfail=1, cancelled=0 WHERE thrive_id2='".$cid."'";
$db->query($upd);
$res2 = true;
}
else if ($event === "order.subscription_cancelled" || $event === "order.rebill_cancelled") {
$upd = "UPDATE users SET billfail=0, cancelled=1 WHERE thrive_id2='".$cid."'";
$db->query($upd);
$res2 = true;
}
else {
$res2 = true;
}
}
// SEND ADMIN TRACKING EMAIL
$email_body = '<pre>' . print_r($arr, true) . print_r($_SERVER, true) . '</pre>';
$email_body .= "\n\n"."Registration Query:"."\n".$reg;
$subject = 'PROD/'.strtoupper($mode).' - Accomods API Tracking';
send_tracking_email($email_body, $subject);
if ($res1 && $res2) {
response(200, ['success' => true, 'event' => $event, 'sql'=>$upd]);
} else {
response(200, ['success' => false, 'event' => $event, 'sql'=>$upd]);
}
}
//**********************************************************************************************************//
// Method: POST
// Endpoint: thrive/log
// Params:
function head_v1_thrive_log($args, $body, $uid, $is_admin, $db)
{
response(200, ['success' => true]);
}
//**********************************************************************************************************//
// Method: GET
// Endpoint: thrive/subscribe
// Params:
/*
function get_v1_thrive_subscribe($args, $body, $uid, $is_admin, $db)
{
//$tc = new \ThriveCart\Api('7F2RNUYH-WD3GDCRE-BHG5MRYJ-B3KS8FZZ'); /// ACCOMODS
$tc = new \ThriveCart\Api('AIO2N8Y8-GVFA2PSQ-LWIKKTOG-KEVMVCBF'); /// INTENTIONAL IEP
$event = "*";
//$target_url = "https://api.accomods.com/v1/thrive/log";
$target_url = "https://app.theintentionaliep.com/iep-api/thriveWebhook";
$trigger_fields = [ "mode_int" => 2 ]; // 1=test, 2=live
try {
$response = $tc->createEventSubscription($event, $target_url, $trigger_fields);
response(200, ['success' => true, 'response' => $response]);
} catch (\ThriveCart\Exception $e) {
response(200, ['success' => false, 'error' => $e]);
}
}
*/
//**********************************************************************************************************//
// Method: GET
// Endpoint: thrive/unsubscribe
// Params:
/*
function get_v1_thrive_unsubscribe($args, $body, $uid, $is_admin, $db)
{
//$tc = new \ThriveCart\Api('7F2RNUYH-WD3GDCRE-BHG5MRYJ-B3KS8FZZ'); /// ACCOMODS
$tc = new \ThriveCart\Api('AIO2N8Y8-GVFA2PSQ-LWIKKTOG-KEVMVCBF'); /// INTENTIONAL IEP
$event = "*";
$target_url = "https://app.theintentionaliep.com//iep-api/thriveWebhook";
$trigger_fields = [ "mode_int" => 2 ]; // 1=test, 2=live
try {
$response = $tc->cancelEventSubscription($target_url);
response(200, ['success' => true, 'response' => $response]);
} catch (\ThriveCart\Exception $e) {
response(200, ['success' => false, 'error' => $e]);
}
}
*/
//**********************************************************************************************************//
// Method: GET
// Endpoint: thrive/tester
// Params:
/*
function get_v1_thrive_tester($args, $body, $uid, $is_admin, $db)
{
$tc = new \ThriveCart\Api('7F2RNUYH-WD3GDCRE-BHG5MRYJ-B3KS8FZZ');
// // Get a list of all products in the account
// try {
// $products = $tc->getProducts(array('status' => 'test')); // live, test
// response(200, ['success'=>true, 'products'=>$products]);
// } catch(\ThriveCart\Exception $e) {
// response(200, ['success'=>false, 'products'=>null ]);
// }
// Switch to operating in test mode
\ThriveCart\Api::setMode('test');
try {
$customer = $tc->customer(array('email' => 'awaldron@buildinmotion.com'));
response(200, ['success' => true, 'customer' => $customer]);
} catch (\ThriveCart\Exception $e) {
response(200, ['success' => false, 'customer' => null]);
}
}
*/
//**********************************************************************************************************//
// Method: POST
// Endpoint: thrive/setpw
// Params:
/*
function get_v1_thrive_updateorders($args, $body, $uid, $is_admin, $db)
{
$res = qSet( $db, "SELECT * FROM transactions" );
for ($i=0; $i < count($res); $i++) {
$r = $res[$i];
$json = json_decode( $r['txn_data'], true );
$id = $r['txn_id'];
$cus_id = $json['customer_identifier'];
$cid = $json['customer_id'];
$email = $json['customer']['email'];
$mode = $json['mode'];
$res1 = $db->query( "UPDATE transactions
SET txn_email='".$email."',
txn_thriveid='".$cus_id."',
txn_thriveid2='".$cid."',
txn_mode='".$mode."'
WHERE txn_id='".$id."'" );
}
response(200, ['success' => true]);
}
*/