<?php
/********************************************************************************
 * (c)1337 aheadware.com - All rights reserved
 ********************************************************************************/
require_once __DIR__ . '/../../../src/server/config/boot_global.php';
require_once __DIR__ . '/../../../src/server/config/boot_local.php';

/** @var Francis_Utils_Log $logger */
$logger = TB_Server_Utils_Log::get();

try
{
    TB_Server_Core_Premium::initBraintree();

    if( isset( $_POST[ "bt_signature" ] ) &&
        isset( $_POST[ "bt_payload"] ) )
    {
        $webhookNotification = Braintree_WebhookNotification::parse(
            $_POST["bt_signature"],
            $_POST["bt_payload"]
        );

        // Example values for webhook notification properties
        $messageKind = $webhookNotification->kind; // "subscription_went_past_due"
        $messageDate = $webhookNotification->timestamp->format('D M j G:i:s T Y'); // "Sun Jan 1 00:00:00 UTC 2012"
        $subscriptionId = NULL;

        if ( $webhookNotification->subscription && $webhookNotification->subscription->id )
        {
            $subscriptionId = $webhookNotification->subscription->id;
        }

        $premiumLog = 'PREMIUM' . PHP_EOL;
        $premiumLog .= $messageDate . PHP_EOL;
        $premiumLog .= $messageKind . PHP_EOL;
        $premiumLog .= $subscriptionId;

        $logger->log( $premiumLog );

        if ( $messageKind == Braintree_WebhookNotification::SUBSCRIPTION_CHARGED_SUCCESSFULLY )
        {
            $billingPeriodStartDt = new DateTime();
            $billingPeriodEndDt = new DateTime();
            $billingPeriodEndDt->add( new DateInterval( 'P1M' ) );

            if ( isset( $webhookNotification->subscription ) &&
                 isset( $webhookNotification->subscription->billingPeriodStartDate ) &&
                 isset( $webhookNotification->subscription->billingPeriodEndDate ) )
            {
                $billingPeriodStartDt = $webhookNotification->subscription->billingPeriodStartDate;
                $billingPeriodEndDt = $webhookNotification->subscription->billingPeriodEndDate;
            }
            else
            {
                $logger->log( 'PREMIUM: No billing perion start dates found.' );
            }

            /** @var TB_Shared_Ent_TeamData_PremiumLog $entPremiumLog */
            $entPremiumLog = TB_Shared_Ent_TeamData_PremiumLog::getByBraintreeSubscriptionId( $subscriptionId );
            if ( $entPremiumLog instanceof TB_Shared_Ent_TeamData_PremiumLog )
            {
                $entInvoice = TB_Server_Core_Premium::createInvoiceFromPremiumLog( $entPremiumLog, $billingPeriodStartDt, $billingPeriodEndDt );
                if ( !( $entInvoice instanceof TB_Shared_Ent_TeamData_Invoice ) )
                {
                    TB_Server_Utils_Log::get()->log( "PRMEIUM: Could not create invoice for premium log: " . $subscriptionId );
                }
            }
            else
            {
                TB_Server_Utils_Log::get()->log( 'PREMIUM: Premium Log not found: ' . $subscriptionId );
            }
        }
    }
    else
    {
        $logger->log ( 'PREMIUM: Missing bt_signature and/or bt_payload' );
    }
}
catch ( Exception $e )
{
    $logger->log( $e );
}