<?php
include('db_con.php');

$apiData = file_get_contents('php://input');
$apiData = json_decode($apiData);


$customer = $apiData->data->object->customer;
$status = $apiData->data->object->status;
$end = $apiData->data->object->lines->data[0]->period->end;

// echo "Customer: $customer\n";
// echo "Status: $status\n";
// echo "End: $end\n";

$sql = $db_con->prepare("SELECT id FROM stripe_user_data WHERE cu_name = :customer");
$sql->bindValue(':customer', $customer);
$sql->execute();

$numRows = $sql->rowCount();

if($numRows == 0){
    $stmt = $db_con->prepare("INSERT INTO stripe_user_data (cu_name, exp_date) VALUES (:cu_name, :exp_date)");
    $stmt->bindValue(':cu_name', $customer);
    $stmt->bindValue(':exp_date', $end);
    $stmt->execute();
}else{
    $stripe_user = $sql->fetch(PDO::FETCH_ASSOC);
    $stripe_user_id = $stripe_user['id'];
    $stmt = $db_con->prepare("UPDATE stripe_user_data SET exp_date = :exp_date WHERE id = :id");
    $stmt->bindValue(':id', $stripe_user_id);
    $stmt->bindValue(':exp_date', $end);
    $stmt->execute();
}


?>