Add code to get fee amount on webook.

This commit is contained in:
Matt Young 2026-01-29 22:09:45 -06:00
parent f626157ab4
commit c1dd36d7fd
1 changed files with 12 additions and 3 deletions

View File

@ -3,6 +3,7 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Enums\PaymentStatus; use App\Enums\PaymentStatus;
use App\Enums\PaymentMethod;
use App\Models\Invoice; use App\Models\Invoice;
use App\Models\Payment; use App\Models\Payment;
use Illuminate\Http\Request; use Illuminate\Http\Request;
@ -49,7 +50,7 @@ class StripeController extends Controller
$signature = $request->header('Stripe-Signature'); $signature = $request->header('Stripe-Signature');
try { try {
$event = Webhook::constructEvent( $event = \Stripe\Webhook::constructEvent(
$payload, $payload,
$signature, $signature,
config('services.stripe.webhook_secret') config('services.stripe.webhook_secret')
@ -64,6 +65,14 @@ class StripeController extends Controller
$invoice = Invoice::find($session->metadata->invoice_id); $invoice = Invoice::find($session->metadata->invoice_id);
if ($invoice) { if ($invoice) {
// Retrieve PaymentIntent with expanded charge and balance_transaction
$paymentIntent = \Stripe\PaymentIntent::retrieve([
'id' => $session->payment_intent,
'expand' => ['latest_charge.balance_transaction'],
]);
$feeAmount = $paymentIntent->latest_charge->balance_transaction->fee;
Payment::create([ Payment::create([
'invoice_id' => $invoice->id, 'invoice_id' => $invoice->id,
'payment_date' => now(), 'payment_date' => now(),
@ -71,8 +80,8 @@ class StripeController extends Controller
'payment_method' => PaymentMethod::CARD, 'payment_method' => PaymentMethod::CARD,
'reference' => $session->payment_intent, 'reference' => $session->payment_intent,
'stripe_payment_intent_id' => $session->payment_intent, 'stripe_payment_intent_id' => $session->payment_intent,
'amount' => $session->amount_total / 100, // Stripe sends cents 'amount' => $session->amount_total / 100,
'fee_amount' => 0, // Can retrieve actual fee via separate API call if needed 'fee_amount' => $feeAmount / 100,
]); ]);
} }
} }