<?php
    http_response_code(200);
    
date_default_timezone_set('America/Denver');
    require_once 'test_utility_stripe_database2026.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);    

 
$StripeCustomerID = $event_json->data->object->customer;
$StripeSubscriptionName = $event_json->data->object->plan->name;
$TrialPeriodDays = $event_json->data->object->plan->trial_period_days;
    
$MemberID =$dbObject->getMemberIDFromStripeID($StripeCustomerID);


if ($event_json->type=='customer.subscription.created') {
    $handled=true;
    /*$cu = \Stripe\Customer::retrieve($StripeCustomerID);
    $MetadataTenantID = $cu->metadata->TenantID;*/
    /*$MemberID = $event_json->data->object->metadata->TenantID;*/
    /*$Tenant_info = $dbObject->getUserInfoByStripeID($StripeCustomerID);*/
    
    
    // update KharmaMonthlyFee to be the subscription charge
    // save the subscription to the StripeSubscriptions table
    
    if (isset($MemberID)) {
        $dbObject->logUserEvent("Subscribed member to plan ".$StripeSubscriptionName.", trial period is ".$TrialPeriodDays." days.", $MemberID);
        $dbObject->saveStripeSubscription($MemberID, $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.
    
    // update KharmaMonthlyFee to 0.00
    // remove the subscription from  the StripeSubscriptions table
    
    if (isset($MemberID)) {
        $dbObject->logUserEvent("Deleted subscription ".$StripeSubscriptionName." from tenant.", $MemberID);
        //if ($dbObject->stripeSubscriptionExists($SubscriptionID)) {
        //    $dbObject->updateStripeSubscription($MemberID, $event_json->data->object);
        //} else {
        //    $dbObject->saveStripeSubscription($MemberID, $event_json->data->object);
        //}
        $dbObject->removeStripeSubscription($MemberID);
    } else {
        $myLogger->logDebug(" unable to find tenant for event: ".$event_json->type.", Stripe CustomerID: ".$StripeCustomerID);    
    }
} 

if ($event_json->type=='customer.subscription.updated') {
    $handled=true;
    
    if (isset($MemberID)) {
        $dbObject->logUserEvent("Subscribed tenant to plan ".$StripeSubscriptionName.", trial period is ".$TrialPeriodDays." days.", $MemberID);
        //if ($dbObject->stripeSubscriptionExists($SubscriptionID)) {
        //    $dbObject->updateStripeSubscription($MemberID, $event_json->data->object);
        //} else {
            $dbObject->saveStripeSubscription($MemberID, $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));
}



?>
