<?php
    http_response_code(200);
    
date_default_timezone_set('America/Denver');
    require_once 'stripe_database2024.php';
    require_once "KLogger.php";
    
    $dbObject = new MySQLDB;
    $myLogger = new KLogger(STRIPE_LOG_FILE, KLogger::DEBUG);
    require_once '/var/www/rk-include/stripe/stripe-init.php';
        
\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("STRIPE_event_C event : ".$event_json->type);    
$myLogger->logDebug("STRIPE_event_C event : ".print_r($event_json,true));    

if ($event_json->type=='customer.created') {
    $handled=true;
    $StripeCustomerID = $event_json->data->object->id;
    
    $TenantID = $dbObject->getTenantIDFromStripeID($StripeCustomerID);
    if (isset($TenantID)) {
        $dbObject->logUserEvent("Created Stripe Customer account, ".$StripeCustomerID, $TenantID);
    }
    $myLogger->logDebug("STRIPE_event_C event : ".$event_json->type.", customerID: ".$StripeCustomerID);    
}
if ($event_json->type=='customer.deleted') {
    $handled=true;
    $StripeCustomerID = $event_json->data->object->id;
    
    $TenantID = $dbObject->getTenantIDFromStripeID($StripeCustomerID);
    if (isset($TenantID)) {
        $myLogger->logDebug("found tenant ID (".$TenantID.") for ".$event_json->type.", customerID: ".$StripeCustomerID);    
        $dbObject->logUserEvent("deleted Stripe Customer account, ".$StripeCustomerID, $TenantID);
    }
    $myLogger->logDebug("STRIPE_event_C event : ".$event_json->type.", customerID: ".$StripeCustomerID);    
    // want to log this to a customer Event
}

if ($event_json->type=='customer.updated') {
    $handled=true;
    $StripeCustomerID = $event_json->data->object->id;

    $TenantID = $dbObject->getTenantIDFromStripeID($StripeCustomerID);
    if (isset($TenantID)) {
        $dbObject->logUserEvent("Updated Stripe Customer account, ".$StripeCustomerID, $TenantID);
    }
    $myLogger->logDebug("STRIPE_event_C event : ".$event_json->type.", customerID: ".$StripeCustomerID);    
    
    // want to log this to a customer Event
}

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("STRIPE_event_C 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("STRIPE_event_C 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("STRIPE_event_C unable to find tenant for event: ".$event_json->type.", Stripe CustomerID: ".$StripeCustomerID);    
    }
}

if ($event_json->type=='transfer.paid') {
    $handled=true;  
}


if ($event_json->type=='invoice.upcoming') {
    $handled=true;  
    $EventID = $event_json->id;
    $EventDate = $event_json->created;
    $StripeCustomerID = $event_json->data->object->customer;
    $UpcomingInvoiceObject = $event_json->data->object;
    // get the tenant by the customer ID
    $TenantID = $dbObject->getTenantIDFromStripeID($StripeCustomerID);
    //$dbObject->saveUpcomingStripeInvoice($TenantID, $UpcomingInvoiceObject, $EventID, $EventDate);
    $dbObject->saveStripeInvoice($TenantID, $UpcomingInvoiceObject, $EventID, $EventDate);
    // save the StripeInvoice
}

if ($event_json->type=='invoice.created') {
    $handled=true;  
    $EventID = $event_json->id;
    $EventDate = $event_json->created;
    $InvoiceID = $event_json->data->object->id;
    $StripeCustomerID = $event_json->data->object->customer;
    $InvoiceObject = $event_json->data->object;
    $TenantID = $dbObject->getTenantIDFromStripeID($StripeCustomerID);
    //$dbObject->saveStripeInvoice($TenantID, $InvoiceObject);
    $dbObject->saveStripeInvoice($TenantID, $UpcomingInvoiceObject, $EventID, $EventDate);
    // save the StripeInvoice
}

if ($event_json->type=='invoice.updated') {
    $handled=true;  
    
    $InvoiceID = $event_json->data->object->id;
    $EventID = $event_json->id;
    $EventDate = $event_json->created;
    
    $StripeCustomerID = $event_json->data->object->customer;
    $InvoiceObject = $event_json->data->object;
    // get the tenant by the customer ID
    $TenantID = $dbObject->getTenantIDFromStripeID($StripeCustomerID);
    //if ($dbObject->stripeInvoiceExists($InvoiceObject)) {
    //    $dbObject->updateStripeInvoice($TenantID, $InvoiceObject);
    //} else {
        //$dbObject->saveStripeInvoice($TenantID, $InvoiceObject);
        $dbObject->saveStripeInvoice($TenantID, $UpcomingInvoiceObject, $EventID, $EventDate);
    //}
}

if ($event_json->type=='customer.card.created' || $event_json->type=='customer.card.updated' || $event_json->type=='customer.card.deleted') {
    $handled=true;
    $myLogger->logDebug("STRIPE_event_C event : ".$event_json->type);    
    //if ($event_json->type=='customer.card.created') {
    //    $Action = substr($event_json->type, 14);
    
    $Action = substr($event_json->type, 14);
    
    $StripeCustomerID = $event_json->data->object->customer;
    $LastFour = $event_json->data->object->last4;
    $CardType = $event_json->data->object->brand;
    /*$TenantID = $dbObject->getUserInfoByStripeID($StripeCustomerID);*/
    $TenantID = $dbObject->getTenantIDFromStripeID($StripeCustomerID);
    $dbObject->updateStripeCardInfo($event_json->data->object);
    if (isset($TenantID)) {
        $dbObject->logUserEvent("Stripe ".$Action." card (".$CardType.") ending in ".$LastFour." to customer.", $TenantID);
    } else {
        $myLogger->logDebug("STRIPE_event_C unable to find tenant for event: ".$event_json->type.", Stripe CustomerID: ".$StripeCustomerID);    
    }
}


if ($event_json->type=='invoice.payment_succeeded') {
    $handled=true;
    
    $invoiceID = $event_json->data->object->id;
    $invoiceAmountTemp = $event_json->data->object->total;
    $invoiceAmount = floatval(($invoiceAmountTemp));
    
    $StripeCustomerID = $event_json->data->object->customer;
    // get the tenant by the customer ID
    /*
    $this_tenant = $dbObject->getUserInfoByStripeID($StripeCustomerID);
    $TenantID = $this_tenant["userID"];
    */
    
    $TenantID = $dbObject->getTenantIDFromStripeID($StripeCustomerID);
    
    if ($event_json->data->object->lines->data[0]->type == "subscription") {
        $StripePlanID = $event_json->data->object->lines->data[0]->plan->id;
        $myLogger->logDebug("STRIPE_event_C subscription event : ".$event_json->type.", customerID: ".$StripeCustomerID.", planID:".$StripePlanID);    
        $dbObject->logUserEvent("Stripe Invoice for $".$invoiceAmount." created and paid for Plan ".$StripePlanID, $TenantID);
    } else {
        $myLogger->logDebug("STRIPE_event_C event : ".$event_json->type.", customerID: ".$StripeCustomerID);
        $dbObject->logUserEvent("Stripe Invoice for $".$invoiceAmount." created and paid.", $TenantID);
    }
    $dbObject->resumeTenantReporting($TenantID);    
}

if ($event_json->type=='invoice.payment_failed') {
    $handled=true;
    // this is for a monthly subscription payment that fails.
    $EventID = $event_json->id;
    $EventDate = $event_json->created;
    
    $InvoiceID = $event_json->data->object->id;
    $invoiceAmountTemp = $event_json->data->object->total;
    $invoiceAmount = floatval(($invoiceAmountTemp));
    
    $StripeCustomerID = $event_json->data->object->customer;
    $InvoiceObject = $event_json->data->object;
    // get the tenant by the customer ID
    /*
    $this_tenant = $dbObject->getUserInfoByStripeID($StripeCustomerID);
    $TenantID = $this_tenant["userID"];*/
    $TenantID = $dbObject->getTenantIDFromStripeID($StripeCustomerID);
    
    //if ($dbObject->stripeInvoiceExists($InvoiceObject)) {
    //    $dbObject->updateStripeInvoice($TenantID, $InvoiceObject);
    //} else {
        //$dbObject->saveStripeInvoice($TenantID, $InvoiceObject);
        $dbObject->saveStripeInvoice($TenantID, $UpcomingInvoiceObject, $EventID, $EventDate);
    //}

    $dbObject->logUserEvent("Stripe Invoice for ".$invoiceAmount." FAILED.", $TenantID);
    $myLogger->logDebug("STRIPE_event_C event : ".$event_json->type.", customerID: ".$StripeCustomerID);    
    $dbObject->suspendTenantReporting($TenantID);    
}

if ($event_json->type=='charge.succeeded') {
    $handled=true;
    
    $randomStuffString = serialize($event_json->data->object->metadata);
    
    $eventResult = 'Paid';
    $chargeID = $event_json->data->object->id;
    $chargeDate = $event_json->data->object->created;
    $tempDate = date("Y-m-d H:i:s", substr($chargeDate, 0, 10));
    $chargeAmountTemp = $event_json->data->object->amount;
    $chargeAmount = floatval(($chargeAmountTemp)/100);
    $cardType = $event_json->data->object->card->type;
    $lastFour = $event_json->data->object->card->last4;    
    //$myLogger->logDebug("debugging charge.succeeded. 20");
    $StripeCustomerID = $event_json->data->object->customer;
    
    $RK_EmailAddress = $event_json->data->object->receipt_email;

    if (property_exists($event_json->data->object->metadata, 'TenantID')) {
        $RK_TenantID = $event_json->data->object->metadata->TenantID;
    } else {
        $RK_TenantID = "";        
        $RK_TenantID = $dbObject->getTenantIDFromStripeID($StripeCustomerID);
    }

    if ($RK_TenantID  == "PENDING") {
        // then this is the first charge.  We need the signup page to handle it.
        $myLogger->logDebug("Stripe Event C Ignored stripe charge event because it's for a pending tenant.  Stripe ID: ".$chargeID.", Pending ID:".$event_json->data->object->metadata->SubmissionID);
    } else {
        if ($event_json->data->object->invoice == null) {
            // then this is a one-time charge
            // this means that the registration page is handling this, so we'll ignore it.
            $eventType = "Registration";
            //$RK_ChargeID = $event_json->data->object->metadata->RK_ChargeID;
            if (property_exists($event_json->data->object->metadata, 'RK_ChargeID')) {
                $RK_ChargeID = $event_json->data->object->metadata->RK_ChargeID;
            } else {
                $RK_ChargeID = "";
            }
            
            $msg = "STRIPE_event_C ".$eventType." chargeID : ".$chargeID.", RK ChargeID:".$RK_ChargeID.", RK UserID:".$RK_TenantID;
            $msg .= " chargeDate: ".$chargeDate.", source charge date:".$tempDate.", amount:".$chargeAmount;
            $msg .= ", source amount:".$chargeAmountTemp.", type:".$cardType.", last4: ".$lastFour;

            $myLogger->logDebug($msg);
            $sql = "select count(*) from StripePayments where StripePaymentID = ?";
        
            $stmt = $dbObject->connection->prepare($sql);
            $stmt->bindParam(1, $RK_ChargeID);
            $stmt->execute();
            $paymentCount = $stmt->fetch(PDO::FETCH_NUM);
            if ($paymentCount[0] == 0) {
                $myLogger->logDebug("no previous payment found, inserting a new one with the Stripe CustomerID if we can.");
                //$dbObject->saveStripeResultByEmail($RK_EmailAddress, $eventType, $chargeID, $chargeDate, $chargeAmount, $cardType, $lastFour, $eventResult, $randomStuffString);
                //$TenantID = $dbObject->GetTenantIDFromStripeID($StripeCustomerID);
                /*$RK_TenantID = $dbObject->FindCustomerID($StripeCustomerID, $RK_EmailAddress);*/
                if ($RK_TenantID > 0 && $RK_TenantID != "") {
                    $dbObject->saveMonthlyStripeResult($RK_TenantID, $StripeCustomerID, $RK_EmailAddress, $eventType, $chargeID, $chargeDate, $chargeAmount, $cardType, $lastFour, $eventResult, $randomStuffString, "");
                    $dbObject->resumeTenantReporting($RK_TenantID);
                    $dbObject->logUserEvent("$".$chargeAmount." Stripe charge paid, unknown reason why. ", $RK_TenantID);
                }
            } else {
                $myLogger->logDebug("FOUND previous payment.");
                $dbObject->updateStripeResult($RK_ChargeID, $chargeID, $chargeDate, $chargeAmount, $cardType, $lastFour, $eventResult, $randomStuffString);
                $myLogger->logDebug("saved stripe result.  chargeID : ".$chargeID.", chargeDate: ".$chargeDate.", amount:".$chargeAmount.", type:".$cardType.", last4: ".$lastFour);
                $dbObject->logUserEvent("$".$chargeAmount." Stripe charge paid for registration", $RK_TenantID);
                $dbObject->resumeTenantReporting($RK_TenantID);
            }
        } else {
            //$myLogger->logDebug("debugging charge.succeeded. 50");
            $InvoiceID = $event_json->data->object->invoice;
            /* we're going to handle this with an invoice handler */
            $myLogger->logDebug("about to save stripe result.  chargeID : ".$chargeID.", chargeDate: ".$chargeDate.", amount:".$chargeAmount.", type:".$cardType.", last4: ".$lastFour.", invoice: ".$InvoiceID." TenantID: ".$RK_TenantID);
            $eventType = "Subscription";
            
            if ($RK_TenantID > 0 && $RK_TenantID != "") {
                $dbObject->saveMonthlyStripeResult($RK_TenantID, $StripeCustomerID, $RK_EmailAddress, $eventType, $chargeID, $chargeDate, $chargeAmount, $cardType, $lastFour, $eventResult, $event_json->data->object->metadata, $InvoiceID);
                //$myLogger->logDebug("actually saved stripe result.  ");
                $dbObject->logUserEvent("$".$chargeAmount." Stripe charge paid for subscription.  InvoiceID: ".$InvoiceID, $RK_TenantID);
                //$myLogger->logDebug("logged user event.  ");
                $dbObject->resumeTenantReporting($RK_TenantID);
                $myLogger->logDebug("resumed tenant reporting for tenant.  ".$RK_TenantID);
            }
        }     
    }
} 

if ($event_json->type=='charge.refunded') {
    $handled=true;
    
    $randomStuffString = serialize($event_json->data->object->metadata);
    
    $eventType = $event_json->type;
    $eventResult = 'Refunded';

    $chargeID = $event_json->data->object->id;
    $chargeDate = $event_json->data->object->created;
    $tempDate = date("Y-m-d H:i:s", substr($chargeDate, 0, 10));
    $chargeAmountTemp = $event_json->data->object->amount_refunded;
    $chargeAmount = floatval(($chargeAmountTemp)/100);
    $cardType = $event_json->data->object->card->type;
    $lastFour = $event_json->data->object->card->last4;
    $StripeCustomerID = $event_json->data->object->customer;
    $TenantID = $dbObject->getTenantIDFromStripeID($StripeCustomerID);
    //$myLogger->logDebug("refund event - data object: ".print_r($event_json->data->object, true));
    $RK_EmailAddress = "";
    if (property_exists($event_json->data->object, 'receipt_email')) {
        $RK_EmailAddress = $event_json->data->object->receipt_email;
    }
    // for refunds, we ALWAYS want to save a new record, with a negative amount.
    
    if ($event_json->data->object->invoice == null || $event_json->data->object->invoice == "") {
        // then this is a one-time charge
        if ($event_json->data->object->invoice == null) {
            $myLogger->logDebug("refund event null invoice");
        } else {
            $myLogger->logDebug("refund event invoice == ''");
        }
        
        $eventType = "Registration";
        $myLogger->logDebug("STRIPE_event_C ".$eventType." chargeID : ".$chargeID.", RK UserID:".$TenantID." chargeDate: ".$chargeDate.", source charge date:".$tempDate.", amount:".$chargeAmount.", source amount:".$chargeAmountTemp.", type:".$cardType.", last4: ".$lastFour);
        $myLogger->logDebug("inserting a new refund item.");
        
        if ($TenantID > 0) {
            $dbObject->saveStripeResultByTenantID($TenantID, $StripeCustomerID, $eventType, $chargeID, $chargeDate, (-1 * $chargeAmount), $cardType, $lastFour, $eventResult, $randomStuffString);
            $dbObject->logUserEvent("$".$chargeAmount." Stripe charge refunded for registration.", $TenantID);
        }
    } else {
        $eventType = "Subscription";
        $InvoiceID = $event_json->data->object->invoice;
        $myLogger->logDebug("refund event invoice ID:".$InvoiceID);
        
        if ($TenantID > 0) {
            $dbObject->saveMonthlyStripeResult($TenantID, $StripeCustomerID, $RK_EmailAddress, $eventType, $chargeID, $chargeDate, (-1 * $chargeAmount), $cardType, $lastFour, $eventResult, $randomStuffString, $InvoiceID);
            $dbObject->logUserEvent("$".$chargeAmount." Stripe charge refunded for subscription.", $TenantID);
        }   
    }
} 
if ($event_json->type=='charge.failed') {
    // mark the tenant as non-reporting or delete them all together.
    $handled=true;
    
    $randomStuffString = serialize($event_json->data->object->metadata);
    
    $eventType = $event_json->type;
    $chargeID = $event_json->data->object->id;
    $chargeDate = $event_json->data->object->created;
    $tempDate = date("Y-m-d H:i:s", substr($chargeDate, 0, 10));
    $chargeAmountTemp = $event_json->data->object->amount;
    $chargeAmount = floatval(($chargeAmountTemp)/100);
    $cardType = $event_json->data->object->card->type;
    $lastFour = $event_json->data->object->card->last4;
    
    $RK_EmailAddress = "";
    if (property_exists($event_json->data->object, 'receipt_email')) {
        $RK_EmailAddress = $event_json->data->object->receipt_email;
    }

    $StripeCustomerID = $event_json->data->object->customer;
    
    $failureCode = $event_json->data->object->failure_code;
    $eventResult = 'Failed. '.$failureCode;
    
    //$RK_TenantID = $event_json->data->object->metadata->TenantID;

    if (property_exists($event_json->data->object->metadata, 'TenantID')) {
        $RK_TenantID = $event_json->data->object->metadata->TenantID;
    } else {
        $RK_TenantID = "";        
    }
    if ($RK_TenantID == "PENDING") {
        // pretty much do nothing
        $myLogger->logDebug("Ignored stripe charge event because it's for a pending tenant.  Pending ID:".$event_json->data->object->metadata->SubmissionID);
    } else {
        $TenantID = $dbObject->getTenantIDFromStripeID($StripeCustomerID);
        if ($event_json->data->object->invoice == null) {
            // then this is a one-time charge
            $eventType = "Registration";
            if (property_exists($event_json->data->object->metadata, 'RK_ChargeID')) {
                $RK_ChargeID = $event_json->data->object->metadata->RK_ChargeID;
            } else {
                $RK_ChargeID = "";
            }
            $myLogger->logDebug("STRIPE_event_C ".$eventType." chargeID : ".$chargeID.", RK ChargeID:".$RK_ChargeID.", RK UserID:".$TenantID." chargeDate: ".$chargeDate.", source charge date:".$tempDate.", amount:".$chargeAmount.", source amount:".$chargeAmountTemp.", type:".$cardType.", last4: ".$lastFour);
            $sql = "select count(*) from StripePayments where StripePaymentID = ?";
        
            $stmt = $dbObject->connection->prepare($sql);
            $stmt->bindParam(1, $RK_ChargeID);
            $stmt->execute();
            $paymentCount = $stmt->fetch(PDO::FETCH_NUM);
            if ($paymentCount[0] == 0) {
                $myLogger->logDebug("no previous payment found, inserting a new one with the stripe customer ID if we can.");
                if ($TenantID > 0) {
                    $dbObject->saveStripeResultByTenantID($TenantID, $StripeCustomerID, $eventType, $chargeID, $chargeDate, $chargeAmount, $cardType, $lastFour, $eventResult, $randomStuffString);
                    $dbObject->logUserEvent("$".$chargeAmount." Stripe charge FAILED for registration", $TenantID);
                }
            } else {
                $myLogger->logDebug("FOUND previous payment.");
                $dbObject->updateStripeResult($RK_ChargeID, $chargeID, $chargeDate, $chargeAmount, $cardType, $lastFour, $eventResult, $randomStuffString);
                $myLogger->logDebug("saved stripe result.  chargeID : ".$chargeID.", chargeDate: ".$chargeDate.", amount:".$chargeAmount.", type:".$cardType.", last4: ".$lastFour);
                $dbObject->logUserEvent("$".$chargeAmount." Stripe charge FAILED for registration", $TenantID);    
            }
        } else {
            // get the invoice ID and the month for it.
            $eventType = "Subscription";
            $InvoiceID = $event_json->data->object->invoice;
            $myLogger->logDebug("FAILED charge event invoice ID:".$InvoiceID);
            
            if ($TenantID > 0) {
                $dbObject->saveMonthlyStripeResult($TenantID, $StripeCustomerID, $RK_EmailAddress, $eventType, $chargeID, $chargeDate, $chargeAmount, $cardType, $lastFour, $eventResult, $randomStuffString, $InvoiceID);
                $dbObject->logUserEvent("$".$chargeAmount." Stripe charge FAILED for subscription.  InvoiceID: ".$InvoiceID, $TenantID);
                // we want to the let the invoice handler take care of this for us.
                //$dbObject->UpdateStripeInvoiceDetails($InvoiceID,"Failed", $chargeAmount, $chargeDate, $chargeID); 
            }
        }
        $dbObject->suspendTenantReporting($TenantID);
    }
} 

if ($event_json->type=='charge.updated') {
    // just want to log it, really.
    $handled=true;
    $eventType = $event_json->type;
    $chargeID = $event_json->data->object->id;
    $chargeDate = $event_json->data->object->created;
    $tempDate = date("Y-m-d H:i:s", substr($chargeDate, 0, 10));
    $chargeAmountTemp = $event_json->data->object->amount;
    $chargeAmount = floatval(($chargeAmountTemp));
    $cardType = $event_json->data->object->card->type;
    $lastFour = $event_json->data->object->card->last4;
    //$RK_ChargeID = $event_json->data->object->metadata->RK_ChargeID;
    if (property_exists($event_json->data->object->metadata, 'RK_ChargeID')) {
        $RK_ChargeID = $event_json->data->object->metadata->RK_ChargeID;
    } else {
        $RK_ChargeID = "";
    }
    $TenantID = $dbObject->getTenantIDFromStripeID($StripeCustomerID);
    
    //$RK_EmailAddress = $event_json->data->object->receipt_email;
    $myLogger->logDebug("STRIPE_event_C charge updated.  Not doing anything about it. ChargeID : ".$chargeID.", RK ChargeID:".$RK_ChargeID.", RK UserID:".$TenantID." chargeDate: ".$chargeDate.", source charge date:".$tempDate.", amount:".$chargeAmount.", source amount:".$chargeAmountTemp.", type:".$cardType.", last4: ".$lastFour);
    $dbObject->logUserEvent("$".$chargeAmount." Stripe charge updated for registration. ChargeID : ".$chargeID.", RK ChargeID:".$RK_ChargeID.", RK UserID:".$TenantID." chargeDate: ".$chargeDate.", source charge date:".$tempDate.", amount:".$chargeAmount.", source amount:".$chargeAmountTemp.", type:".$cardType.", last4: ".$lastFour, $tenantID);
    // logUserEvent
} 




if ($handled == false) {
    $myLogger->logDebug("STRIPE_event_C  unhandled EVENT CONTENTS: ".print_r($event_json, true));
}


?>
