mirror of
https://github.com/myronblair/tomsjavajive
synced 2026-06-30 17:50:32 -05:00
Fix webhook: add checkout.session.completed; propagate metadata to payment intent
This commit is contained in:
+44
-24
@@ -9,7 +9,7 @@ require_once __DIR__ . '/../includes/stripe.php';
|
||||
|
||||
header('Content-Type: application/json');
|
||||
|
||||
$payload = file_get_contents('php://input');
|
||||
$payload = file_get_contents('php://input');
|
||||
$sigHeader = $_SERVER['HTTP_STRIPE_SIGNATURE'] ?? '';
|
||||
|
||||
// Verify webhook signature (if secret is configured)
|
||||
@@ -31,31 +31,51 @@ if (!empty(STRIPE_WEBHOOK_SECRET) && STRIPE_WEBHOOK_SECRET !== 'whsec_your_webho
|
||||
}
|
||||
|
||||
$eventType = $event['type'] ?? '';
|
||||
$data = $event['data']['object'] ?? [];
|
||||
$data = $event['data']['object'] ?? [];
|
||||
|
||||
switch ($eventType) {
|
||||
case 'payment_intent.succeeded':
|
||||
$paymentIntentId = $data['id'] ?? '';
|
||||
$orderId = $data['metadata']['order_id'] ?? '';
|
||||
|
||||
if ($orderId) {
|
||||
|
||||
case 'checkout.session.completed':
|
||||
// Stripe Checkout (hosted page) — metadata is on the session
|
||||
$orderId = $data['metadata']['order_id'] ?? '';
|
||||
$paymentIntentId = $data['payment_intent'] ?? '';
|
||||
if ($orderId && ($data['payment_status'] ?? '') === 'paid') {
|
||||
db()->update('orders',
|
||||
[
|
||||
'payment_status' => 'paid',
|
||||
'order_status' => 'confirmed'
|
||||
'payment_status' => 'paid',
|
||||
'order_status' => 'confirmed',
|
||||
'stripe_payment_intent' => $paymentIntentId,
|
||||
],
|
||||
'order_id = :id',
|
||||
['id' => $orderId]
|
||||
);
|
||||
|
||||
// Send confirmation email
|
||||
$order = db()->fetch("SELECT * FROM orders WHERE order_id = :id", ['id' => $orderId]);
|
||||
if ($order) {
|
||||
sendOrderConfirmationEmail($order);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
case 'payment_intent.succeeded':
|
||||
// Payment Intent flow (embedded checkout) — metadata.order_id set directly on PI
|
||||
$paymentIntentId = $data['id'] ?? '';
|
||||
$orderId = $data['metadata']['order_id'] ?? '';
|
||||
if ($orderId) {
|
||||
db()->update('orders',
|
||||
[
|
||||
'payment_status' => 'paid',
|
||||
'order_status' => 'confirmed',
|
||||
],
|
||||
'order_id = :id',
|
||||
['id' => $orderId]
|
||||
);
|
||||
$order = db()->fetch("SELECT * FROM orders WHERE order_id = :id", ['id' => $orderId]);
|
||||
if ($order) {
|
||||
sendOrderConfirmationEmail($order);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case 'payment_intent.payment_failed':
|
||||
$orderId = $data['metadata']['order_id'] ?? '';
|
||||
if ($orderId) {
|
||||
@@ -66,14 +86,14 @@ switch ($eventType) {
|
||||
);
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
case 'charge.refunded':
|
||||
$paymentIntentId = $data['payment_intent'] ?? '';
|
||||
if ($paymentIntentId) {
|
||||
db()->update('orders',
|
||||
[
|
||||
'payment_status' => 'refunded',
|
||||
'order_status' => 'refunded'
|
||||
'order_status' => 'refunded',
|
||||
],
|
||||
'stripe_payment_intent = :pi',
|
||||
['pi' => $paymentIntentId]
|
||||
@@ -89,9 +109,9 @@ echo json_encode(['received' => true]);
|
||||
* Send order confirmation email
|
||||
*/
|
||||
function sendOrderConfirmationEmail($order) {
|
||||
$items = json_decode($order['items'], true) ?? [];
|
||||
$items = json_decode($order['items'], true) ?? [];
|
||||
$shippingAddress = json_decode($order['shipping_address'], true) ?? [];
|
||||
|
||||
|
||||
$itemsHtml = '';
|
||||
foreach ($items as $item) {
|
||||
$itemsHtml .= sprintf(
|
||||
@@ -101,22 +121,22 @@ function sendOrderConfirmationEmail($order) {
|
||||
$item['total']
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
$html = <<<HTML
|
||||
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
|
||||
<div style="background: #8B4513; color: white; padding: 20px; text-align: center;">
|
||||
<h1 style="margin: 0;">Tom's Java Jive</h1>
|
||||
</div>
|
||||
|
||||
|
||||
<div style="padding: 30px; background: #FDFBF7;">
|
||||
<h2>Order Confirmed!</h2>
|
||||
<p>Thank you for your order, {$order['customer_name']}!</p>
|
||||
|
||||
|
||||
<div style="background: white; padding: 20px; border-radius: 8px; margin: 20px 0;">
|
||||
<p><strong>Order #:</strong> {$order['order_number']}</p>
|
||||
<p><strong>Total:</strong> \${$order['total']}</p>
|
||||
</div>
|
||||
|
||||
|
||||
<h3>Order Details</h3>
|
||||
<table style="width: 100%; border-collapse: collapse;">
|
||||
{$itemsHtml}
|
||||
@@ -125,23 +145,23 @@ function sendOrderConfirmationEmail($order) {
|
||||
<td style="text-align:right;"><strong>\${$order['total']}</strong></td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
|
||||
<h3>Shipping To</h3>
|
||||
<p>
|
||||
{$shippingAddress['address']}<br>
|
||||
{$shippingAddress['city']}, {$shippingAddress['state']} {$shippingAddress['zip']}
|
||||
</p>
|
||||
|
||||
|
||||
<p style="color: #666; font-size: 14px;">
|
||||
We'll send you tracking information once your order ships.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
|
||||
<div style="padding: 20px; text-align: center; color: #666; font-size: 12px;">
|
||||
<p>Tom's Java Jive | Premium Coffee</p>
|
||||
</div>
|
||||
</div>
|
||||
HTML;
|
||||
|
||||
|
||||
sendEmail($order['customer_email'], "Order Confirmed - #{$order['order_number']}", $html);
|
||||
}
|
||||
|
||||
@@ -121,6 +121,7 @@ class StripeAPI {
|
||||
if (!empty($options['metadata'])) {
|
||||
foreach ($options['metadata'] as $key => $value) {
|
||||
$data["metadata[$key]"] = $value;
|
||||
$data["payment_intent_data[metadata][$key]"] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user