i need to store the event insert into database i don’t what i miss please help me i used PHP 8.0 this events are created and respond 200 OK payment_intent.succeeded,payment_method.attached, charge.succeeded,payment_intent.succeeded and also Webhook url is correct please help?
<?php
//secret.php
// Keep your Stripe API key protected by including it as an environment variable
// or in a private script that does not publicly expose the source code.
// This is your test secret API key.
$stripeSecretKey = 'sk_test_51MqG2jBbneKn9awSabftRMnZeMxZZZsBO6uOAP1RFzeY6we3FLCF1yM3pcuwZycYQE5GJAzEDKf9fierGVHL48yS00UbfOkJbT';
<?php
//webhook.php
require_once '../vendor/autoload.php';
require_once '../secrets.php';
require('./include/config.php');
StripeStripe::setApiKey($stripeSecretKey);
// Replace this endpoint secret with your endpoint's unique secret
// If you are testing with the CLI, find the secret by running 'stripe listen'
// If you are using an endpoint defined with the API or dashboard, look in your webhook settings
// at https://dashboard.stripe.com/webhooks
$endpoint_secret = 'whsec_oolku8vdsvyWii1aL5xEffkFMMZH6z6P';
$payload = @file_get_contents('php://input');
$event = null;
try {
$event = StripeEvent::constructFrom(
json_decode($payload, true)
);
} catch(UnexpectedValueException $e) {
// Invalid payload
echo '⚠️ Webhook error while parsing basic request.';
http_response_code(400);
exit();
}
if ($endpoint_secret) {
// 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 = StripeWebhook::constructEvent(
$payload, $sig_header, $endpoint_secret
);
} catch(StripeExceptionSignatureVerificationException $e) {
// Invalid signature
echo '⚠️ Webhook error while validating signature.';
http_response_code(400);
exit();
}
}
$id = $event->data->object->id;
$amount = $event->data->object->amount_captured;
$currency = $event->data->object->currency;
$cus_email = $event->data->object->receipt_email;
$name = $event->data->object->billing_details->name;
// Handle the event
switch ($event->type) {
case 'payment_intent.succeeded':
$paymentIntent = $event->data->object; // contains a StripePaymentIntent
// Then define and call a method to handle the successful payment intent.
// handlePaymentIntentSucceeded($paymentIntent);
break;
case 'payment_method.attached':
$paymentMethod = $event->data->object; // contains a StripePaymentMethod
// Then define and call a method to handle the successful attachment of a PaymentMethod.
// handlePaymentMethodAttached($paymentMethod);
break;
case 'charge.succeeded':
// $paymentMethod = $event->data->object; // contains a StripePaymentMethod
// Then define and call a method to handle the successful attachment of a PaymentMethod.
// mysqli_query($con, "INSERT INTO tbl_chargeSuccess (txn_id, amount, currency) VALUES ($id, $amount, $currency)");
$stmt = $con->prepare("INSERT INTO tbl_chargeSuccess (txn_id, amount, currency, cus_email, name) VALUES (?, ?, ?, ?, ?)");
$stmt->bind_param("sdsss", $id, $amount, $currency, $cus_email, $name);
$stmt->execute();
if (!$stmt) {
# code...
echo 'There was an error'.mysqli_error($con);
}
$stmt->close();
$con->close();
// handlePaymentMethodAttached($paymentMethod);
break;
default:
// Unexpected event type
error_log('Received unknown event type');
}
http_response_code(200);
i need to store the event insert into database i don’t what i miss please help me i used PHP 8.0 this events are created and respond 200 OK payment_intent.succeeded,payment_method.attached, charge.succeeded,payment_intent.succeeded and also Webhook url is correct please help?