<?php

require 'config.php';

// The library needs to be configured with your account's secret key.
// Ensure the key is kept out of any version control system you might be using.
$stripe = new \Stripe\StripeClient(STRIPE_API_KEY);

$payload = @file_get_contents('php://input');
$sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE'];
$event = null;

try {
	$event = \Stripe\Webhook::constructEvent(
		$payload,
		$sig_header,
		STRIPE_ENDPOINT
	);
} catch (\UnexpectedValueException $e) {
	// Invalid payload
	http_response_code(400);
	exit();
} catch (\Stripe\Exception\SignatureVerificationException $e) {
	// Invalid signature
	http_response_code(400);
	exit();
}

// Handle the event
switch ($event->type) {
	case 'payment_intent.created':
		payment::paymentInit($event->data->object);
		break;
	case 'checkout.session.completed':
		payment::paymentUpdate($event->data->object);
		break;
}

http_response_code(200);
