<?php
    http_response_code(200);
    
date_default_timezone_set('America/Denver');
    require_once 'testdb_stripe_database2024.php';
    require_once "KLogger.php";
    
    $dbObject = new MySQLDB;
    $myLogger = new KLogger(STRIPE_LOG_FILE_SUBSCRIPTION, KLogger::DEBUG);
    require_once STRIPE_INIT_LOCATION;
        
\Stripe\Stripe::setApiKey(STRIPE_SECRET_KEY);

$handled=false;
// Retrieve the request's body and parse it as JSON
$body = @file_get_contents('php://input');
$event_json = json_decode($body);

$myLogger->logDebug(" event : ".$event_json->type);    
//$myLogger->logDebug(" event : ".print_r($event_json,true));    


if ($event_json->type=='customer.subscription.created') {
    $handled=true;
    $StripeCustomerID = $event_json->data->object->customer;
    $StripeSubscriptionName = $event_json->data->object->plan->name;
    $TrialPeriodDays = $event_json->data->object->plan->trial_period_days;
    /*$cu = \Stripe\Customer::retrieve($StripeCustomerID);
    $MetadataTenantID = $cu->metadata->TenantID;*/
    /*$tenantID = $event_json->data->object->metadata->TenantID;*/
    /*$Tenant_info = $dbObject->getUserInfoByStripeID($StripeCustomerID);*/
    
    $TenantID =$dbObject->getTenantIDFromStripeID($StripeCustomerID);
    // update KharmaMonthlyFee to be the subscription charge
    // save the subscription to the StripeSubscriptions table
    
    if (isset($TenantID)) {
        $dbObject->logUserEvent("Subscribed tenant to plan ".$StripeSubscriptionName.", trial period is ".$TrialPeriodDays." days.", $TenantID);
        $dbObject->saveStripeSubscription($TenantID, $event_json->data->object);
    } else {
        $myLogger->logDebug(" unable to find tenant for event: ".$event_json->type.", Stripe CustomerID: ".$StripeCustomerID);    
    }
}

if ($event_json->type=='customer.subscription.deleted') {
    // the only one we really care about right now

    $handled=true;
    // this tends to happen on subscriptions.
    
    $StripeCustomerID = $event_json->data->object->customer;
    $SubscriptionID = $event_json->data->object->id;
    $StripeSubscriptionName = $event_json->data->object->plan->name;
    $TrialPeriodDays = $event_json->data->object->plan->trial_period_days;
    $TenantID = $dbObject->getTenantIDFromStripeID($StripeCustomerID);
    
    // update KharmaMonthlyFee to 0.00
    // remove the subscription from  the StripeSubscriptions table
    
    if (isset($TenantID)) {
        $dbObject->logUserEvent("Deleted subscription ".$StripeSubscriptionName." from tenant.", $TenantID);
        //if ($dbObject->stripeSubscriptionExists($SubscriptionID)) {
        //    $dbObject->updateStripeSubscription($TenantID, $event_json->data->object);
        //} else {
        //    $dbObject->saveStripeSubscription($TenantID, $event_json->data->object);
        //}
        $dbObject->removeStripeSubscription($TenantID);
    } else {
        $myLogger->logDebug(" unable to find tenant for event: ".$event_json->type.", Stripe CustomerID: ".$StripeCustomerID);    
    }
} 

if ($event_json->type=='customer.subscription.updated') {
    $handled=true;
    $SubscriptionID = $event_json->data->object->id;
    $StripeCustomerID = $event_json->data->object->customer;
    $StripeSubscriptionName = $event_json->data->object->plan->name;
    $TrialPeriodDays = $event_json->data->object->plan->trial_period_days;
    
    $TenantID =$dbObject->getTenantIDFromStripeID($StripeCustomerID);
    
    if (isset($TenantID)) {
        $dbObject->logUserEvent("Subscribed tenant to plan ".$StripeSubscriptionName.", trial period is ".$TrialPeriodDays." days.", $TenantID);
        //if ($dbObject->stripeSubscriptionExists($SubscriptionID)) {
        //    $dbObject->updateStripeSubscription($TenantID, $event_json->data->object);
        //} else {
            $dbObject->saveStripeSubscription($TenantID, $event_json->data->object);
        //}
    } else {    
         $myLogger->logDebug(" unable to find tenant for event: ".$event_json->type.", Stripe CustomerID: ".$StripeCustomerID);    
    }
}

if ($event_json->type=='customer.subscription.paused') {
    // dump these to compare data layouts
    $myLogger->logDebug(" customer.subscription.paused event: ".print_r($event_json,true));
    $handled=true;  
}

if ($event_json->type=='customer.subscription.resumed') {
    // dump these to compare data layouts
    $myLogger->logDebug(" customer.subscription.resumed event: ".print_r($event_json,true));
    $handled=true;  
}

if ($handled == false) {
    $myLogger->logDebug(" unhandled EVENT CONTENTS: ".print_r($event_json, true));
}



?>
