<?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_CARD, 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 : ".print_r($event_json,true));    


if ($event_json->type=='customer.card.created' || $event_json->type=='customer.card.updated' || $event_json->type=='customer.card.deleted') {
    $handled=true;
    $myLogger->logDebug(" event : ".$event_json->type);    
    $Action = substr($event_json->type, 14);
    $CardObject = $event_json->data->object;
    $StripeCustomerID = $CardObject->customer;
    $LastFour = "";
    $CardType = "";
    $CardBrand = "";
    if (property_exists($CardObject, "brand") && $CardObject->brand !== null) {
        $CardBrand = $CardObject->brand;
    }
    if (property_exists($CardObject, "last4") && $CardObject->last4 !== null) {
        $CardLastFour = $CardObject->last4;
    }
    if (property_exists($CardObject, "funding") && $CardObject->funding !== null) {
        $CardType = " (".$CardObject->funding.")";
    }
    
    
    
    $MemberID = $dbObject->getMemberIDFromStripeID($StripeCustomerID);
    //$dbObject->updateStripeCardInfo($CardObject);
    if (isset($MemberID)) {
        $dbObject->logUserEvent("Stripe ".$Action." card (".$CardType."/".$CardBrand.") ending in ".$LastFour." to customer.", $MemberID);
    } else {
        $myLogger->logDebug(" unable to find member for event: ".$event_json->type.", Stripe CustomerID: ".$StripeCustomerID);    
    }
}

if ($event_json->type=='payment_method.attached' || $event_json->type=='payment_method.card_automatically_updated' || $event_json->type=='payment_method.detached' || $event_json->type=='payment_method.updated') {
	$handled=true;
	$Action = substr($event_json->type, 15);
	$PaymentMethodObject = $event_json->data->object;
	$StripeCustomerID = $PaymentMethodObject->customer;
	$MemberID = $dbObject->getMemberIDFromStripeID($StripeCustomerID);
	
	if ($PaymentMethodObject->type = "card" && property_exists($PaymentMethodObject, "card") && $PaymentMethodObject->card != null &&  $PaymentMethodObject->card != "" ){
		$CardObject = $PaymentMethodObject->card;
		$LastFour = "";
		$CardType = "";
		$CardBrand = "";
		$CardExpMonth = "";
		$CardExpYear = "";
		if (property_exists($CardObject, "brand") && $CardObject->brand !== null) {
			$CardBrand = $CardObject->brand;
		}
		if (property_exists($CardObject, "last4") && $CardObject->last4 !== null) {
			$CardLastFour = $CardObject->last4;
		}
		if (property_exists($CardObject, "funding") && $CardObject->funding !== null) {
			$CardType = $CardObject->funding;
		}
		if (property_exists($CardObject, "exp_month") && $CardObject->exp_month !== null) {
			$CardExpMonth = $CardObject->exp_month;
		}
		if (property_exists($CardObject, "exp_year") && $CardObject->exp_year !== null) {
			$CardExpYear = $CardObject->exp_year;
		}
		
		
		
		//$dbObject->updateStripeCardInfo_PaymentMethod($CardObject, $StripeCustomerID);
		if (isset($MemberID)) {
			$dbObject->logUserEvent("Stripe ".$Action." card (".$CardType."/".$CardBrand.", expires ".$CardExpMonth."/".$CardExpYear.") ending in ".$CardLastFour." to customer.", $MemberID);
			$myLogger->logDebug("Stripe ".$Action." card (".$CardType."/".$CardBrand.", expires ".$CardExpMonth."/".$CardExpYear.") ending in ".$CardLastFour." to customer ".$StripeCustomerID.", Member ID ".$MemberID);
			
		} else {
			$myLogger->logDebug(" unable to find member for event: ".$event_json->type.", Stripe CustomerID: ".$StripeCustomerID);    
		}
	} else {
			$dbObject->logUserEvent("Stripe ".$Action." payment method (".$PaymentMethodObject->type." ".$PaymentMethodObject->id.") to customer.", $MemberID);
			$myLogger->logDebug("Stripe ".$Action." payment method (".$PaymentMethodObject->type." ".$PaymentMethodObject->id.") to customer ".$StripeCustomerID.", Member ID ".$MemberID);
	}
}

if ($handled == false) {
    $myLogger->logDebug(" unhandled EVENT CONTENTS: ".print_r($event_json, true));
}


?>
