<commit_before><?php
require ASSETS_PLUGIN_DIR.DS.'vendor'.DS.'autoload.php';

/**
 * Stripe webhook https://stripe.com/docs/webhooks
 */
function Assets_stripeWebhook_response_content ($params) {
	$payload = @file_get_contents('php://input');
	$event = null;
	$endpoint_secret = Q_Config::expect("Assets", "payments", "stripe", "webhookSecret");

	try {
		$event = \Stripe\Event::constructFrom(json_decode($payload, true));
	} catch(\UnexpectedValueException $e) {
		// Invalid payload

		Assets_stripeWebhook_log('Webhook error while parsing basic request.', $e);
		http_response_code(400);
		exit();
	}

	// Only verify the event if there is an endpoint secret defined
	// Otherwise use the basic decoded event
	$sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE'];
	try {
		$event = \Stripe\Webhook::constructEvent($payload, $sig_header, $endpoint_secret);
	} catch(\Stripe\Exception\SignatureVerificationException $e) {
		// Invalid signature
		Assets_stripeWebhook_log('Webhook error while validating signature.', $e);
		http_response_code(400);
		exit();
	}

	// Handle the event
	switch ($event->type) {
		case 'payment_intent.succeeded':
			$paymentIntent = $event->data->object; // contains a \Stripe\PaymentIntent

			// need to response http code 200 to stripe endpoint regardless of processing our engine
			// because regardless of our exceptions we got payment_intent.succeeded event
			// otherwise stripe will block our webhook
			try {
				Assets_stripeWebhook_log('Payment success!', $paymentIntent);

				$amount = (int)Q::ifset($paymentIntent, "amount", null);
				$amount /= 100; // amount in cents, need to convert to dollars
				$currency = Q::ifset($paymentIntent, "currency", null);
				$metadata = Q::ifset($paymentIntent, "metadata", array());

				// check app
				if (Q::app() != Q::ifset($metadata, "app", null)) {
					break;
				}

				// set user to metadata
				$userId = Q::ifset($metadata, "userId", null);
				if ($userId) {
					$metadata["user"] = Users::fetch($userId, true);
				}

				// set payment id to metadata
				$chargeId = Q::ifset($paymentIntent, "id", null);
				if ($chargeId) {
					$metadata["chargeId"] = $chargeId;
				}

				// set stream to metadata
				$publisherId = Q::ifset($metadata, "publisherId", null);
				$streamName = Q::ifset($metadata, "streamName", null);
				if ($publisherId && $streamName) {
					$metadata["stream"] = Streams::fetchOne($publisherId, $publisherId, $streamName);
				}

				Assets::charge("stripe", $amount, $currency, $metadata);
			} catch (Exception $e) {
				Assets_stripeWebhook_log("Exception occur during process payment intent", $e);
			}

			break;
		default:
			Assets_stripeWebhook_log('Received unknown event type ' . $event->type);
	}

	http_response_code(200); // PHP 5.4 or greater
	exit;
}

function Assets_stripeWebhook_log ($title, $message=null) {
	Q::log('______________________________________________', "Stripe.webhook");
	Q::log($title, "Stripe.webhook");<commit_msg>Increated logs length for stripe webhook<commit_after><?php
require ASSETS_PLUGIN_DIR.DS.'vendor'.DS.'autoload.php';

/**
 * Stripe webhook https://stripe.com/docs/webhooks
 */
function Assets_stripeWebhook_response_content ($params) {
	$payload = @file_get_contents('php://input');
	$event = null;
	$endpoint_secret = Q_Config::expect("Assets", "payments", "stripe", "webhookSecret");

	try {
		$event = \Stripe\Event::constructFrom(json_decode($payload, true));
	} catch(\UnexpectedValueException $e) {
		// Invalid payload

		Assets_stripeWebhook_log('Webhook error while parsing basic request.', $e);
		http_response_code(400);
		exit();
	}

	// Only verify the event if there is an endpoint secret defined
	// Otherwise use the basic decoded event
	$sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE'];
	try {
		$event = \Stripe\Webhook::constructEvent($payload, $sig_header, $endpoint_secret);
	} catch(\Stripe\Exception\SignatureVerificationException $e) {
		// Invalid signature
		Assets_stripeWebhook_log('Webhook error while validating signature.', $e);
		http_response_code(400);
		exit();
	}

	// Handle the event
	switch ($event->type) {
		case 'payment_intent.succeeded':
			$paymentIntent = $event->data->object; // contains a \Stripe\PaymentIntent

			// need to response http code 200 to stripe endpoint regardless of processing our engine
			// because regardless of our exceptions we got payment_intent.succeeded event
			// otherwise stripe will block our webhook
			try {
				Assets_stripeWebhook_log('Payment success!', $paymentIntent);

				$amount = (int)Q::ifset($paymentIntent, "amount", null);
				$amount /= 100; // amount in cents, need to convert to dollars
				$currency = Q::ifset($paymentIntent, "currency", null);
				$metadata = Q::ifset($paymentIntent, "metadata", array());

				// check app
				if (Q::app() != Q::ifset($metadata, "app", null)) {
					break;
				}

				// set user to metadata
				$userId = Q::ifset($metadata, "userId", null);
				if ($userId) {
					$metadata["user"] = Users::fetch($userId, true);
				}

				// set payment id to metadata
				$chargeId = Q::ifset($paymentIntent, "id", null);
				if ($chargeId) {
					$metadata["chargeId"] = $chargeId;
				}

				// set stream to metadata
				$publisherId = Q::ifset($metadata, "publisherId", null);
				$streamName = Q::ifset($metadata, "streamName", null);
				if ($publisherId && $streamName) {
					$metadata["stream"] = Streams::fetchOne($publisherId, $publisherId, $streamName);
				}

				Assets::charge("stripe", $amount, $currency, $metadata);
			} catch (Exception $e) {
				Assets_stripeWebhook_log("Exception occur during process payment intent", $e);
			}

			break;
		default:
			Assets_stripeWebhook_log('Received unknown event type ' . $event->type);
	}

	http_response_code(200); // PHP 5.4 or greater
	exit;
}

function Assets_stripeWebhook_log ($title, $message=null) {
	Q::log('______________________________________________', "Stripe.webhook");
	Q::log($title, "Stripe.webhook", array(
		"maxLength" => 2048
	));
{'fraction_non_alphanumeric': 0.14876994680851063, 'fraction_numerical': 0.006316489361702127, 'mean_word_length': 4.343694493783303, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 12, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '420783', 'n_tokens_mistral': 2181, 'n_tokens_neox': 1885, 'n_words': 459}