stripe / stripe-php

PHP library for the Stripe API.
https://stripe.com
MIT License
3.75k stars 849 forks source link

Php Webhook issue #1662

Closed Urban-Orange closed 8 months ago

Urban-Orange commented 8 months ago

Describe the bug

On this file I have named charge.php, after a test purchase on my website, it comes up with this error: Webhook Error: Unable to extract timestamp and signatures from header

To Reproduce

<?php require_once 'vendor/autoload.php'; require_once 'includes/dbh.inc.php'; require_once 'secrets.php';

// Set your Stripe API secret key \Stripe\Stripe::setApiKey($stripeSecretKey);

// Retrieve the request's body and parse it as JSON $input = file_get_contents('php://input'); $event = json_decode($input);

// Verify the signature to ensure the webhook is from Stripe $endpoint_secret = 'whsec_hpSS....'; // Replace with your actual endpoint secret

$stripe_signature = $_SERVER['HTTP_STRIPE_SIGNATURE'];

try { $event = \Stripe\Webhook::constructEvent($input, $stripe_signature, $endpoint_secret); } catch (\Exception $e) { http_response_code(400); exit('Webhook Error: ' . $e->getMessage()); }

// Handle the event switch ($event->type) { case 'payment_intent.succeeded': $paymentIntent = $event->data->object; // Handle successful payment intent handlePaymentIntentSucceeded($paymentIntent); break; default: http_response_code(400); exit('Unsupported webhook event received'); }

// Function to handle the successful payment intent function handlePaymentIntentSucceeded($paymentIntent) { global $conn;

// Extract necessary information from the payment intent
$userId = $paymentIntent->metadata->userId; // Assuming you passed user ID as metadata
$raffleId = 1; // Assuming you have a specific raffle ID

// Check if the user has already purchased a ticket
$sql = "SELECT * FROM user_raffles WHERE user_id = ? AND raffle_id = ?";
$stmt = mysqli_prepare($conn, $sql);
mysqli_stmt_bind_param($stmt, "ii", $userId, $raffleId);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$userInRaffle = mysqli_fetch_assoc($result);
mysqli_stmt_close($stmt);

// If the user is already in the raffle, exit
if ($userInRaffle) {
    return;
}

// Decrement spots available in the raffle
$sql = "UPDATE raffles SET spots_left = spots_left - 1 WHERE id = ?";
$stmt = mysqli_prepare($conn, $sql);
mysqli_stmt_bind_param($stmt, "i", $raffleId);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);

// Insert user into raffle
$sql = "INSERT INTO user_raffles (user_id, raffle_id) VALUES (?, ?)";
$stmt = mysqli_prepare($conn, $sql);
mysqli_stmt_bind_param($stmt, "ii", $userId, $raffleId);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);

// Close the database connection
mysqli_close($conn);

// Return a 200 response to acknowledge receipt of the event
http_response_code(200);

} ?>

Expected behavior

Expected for a successful purchase and then the user to be redircted back to my website.

Code snippets

No response

OS

Windows

PHP version

8.3/4

Library version

stripe-php latest version

API version

2023-10-16

Additional context

No response

remi-stripe commented 8 months ago

@Urban-Orange Github issues are limited to bugs with the SDK itself. This question is an integration support question better directed at our support team for 1:1 help: https://support.stripe.com/contact/email