Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Mithrandir69's avatar

how to send customer details to database ?

<?php
require_once(__DIR__.'/../../../vendor/autoload.php');


\Stripe\Stripe::setApiKey('sk_test_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
$session = \Stripe\Checkout\Session::create([
    'payment_method_types' => ['card'],
    'line_items' => [[
      'price_data' => [
        'currency' => 'usd',
        'product_data' => [
          'name' => 'T-shirt',
        ],
        'unit_amount' => 2000,
      ],
      'quantity' => 1,
    ]],
    'mode' => 'payment',
    'success_url' => 'https://example.com/success',
    'cancel_url' => 'https://example.com/cancel',
  ]);

?>

and this is my webhook.blade.php:

<?php
//THis webhook is for tutorial
        include('database/mydb.php');
        require_once 'stripe-php-master/init.php';

// Set your secret key. Remember to switch to your live secret key in production!
// See your keys here: https://dashboard.stripe.com/account/apikeys
\Stripe\Stripe::setApiKey('sk_test_xxxxxxxxxxxxxxxxxxxxxxxxxxxx');

// If you are testing your webhook locally with the Stripe CLI you
// can find the endpoint's secret by running `stripe listen`
// Otherwise, find your endpoint's secret in your webhook settings in the Developer Dashboard
$endpoint_secret = 'whsec_xxxxxxxxxxxxxxxxxxxxxxx';

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

try {
    $event = \Stripe\Webhook::constructEvent(
        $payload, $sig_header, $endpoint_secret
    );
} catch(\UnexpectedValueException $e) {
    // Invalid payload
    http_response_code(400);
    exit();
} catch(\Stripe\Exception\SignatureVerificationException $e) {
    // Invalid 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
        handlePaymentIntentSucceeded($paymentIntent);
        break;

    case 'charge.succeeded':
    $stmt = $con->prepare("INSERT INTO payments (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();
        break;


    if (!$stmt) {
      # code...
      echo 'There was an error'.mysqli_error($con);
    }
    $stmt->close();
    $con->close();
        break;


    // ... handle other event types
    default:
        echo 'Received unknown event type ' . $event->type;
}

http_response_code(200);
?>
0 likes
7 replies
martinbean's avatar

can u just tell me where is the problem ?

@Mithrandir69 Not unless you actually tell us what the problem is.

All you’ve done is copy some code from the Stripe docs. You’ve not told us what’s not working. We’re not clairvoyant.

You need to tell us what’s happening, and what you expect instead.

1 like

Please or to participate in this conversation.