From c199e0a9dd586ef15af86d799f7242b3eab078fd Mon Sep 17 00:00:00 2001 From: Matt Young Date: Thu, 29 Jan 2026 23:48:07 -0600 Subject: [PATCH] Create new contacts on payment --- app/Http/Controllers/StripeController.php | 39 +++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/StripeController.php b/app/Http/Controllers/StripeController.php index a0db0fc..08ac5fd 100644 --- a/app/Http/Controllers/StripeController.php +++ b/app/Http/Controllers/StripeController.php @@ -87,8 +87,7 @@ class StripeController extends Controller $feeAmount = $paymentIntent->latest_charge?->balance_transaction?->fee ?? 0; - $email = $session->customer_details?->email; - $contact = $email ? Contact::where('email', $email)->first() : null; + $contact = $this->findOrCreateContact($session, $invoice); Payment::create([ 'invoice_id' => $invoice->id, @@ -125,6 +124,42 @@ class StripeController extends Controller ]); } + protected function findOrCreateContact($session, Invoice $invoice): ?Contact + { + $email = $session->customer_details?->email; + + if (! $email) { + return null; + } + + $contact = Contact::where('email', $email)->first(); + + if ($contact) { + return $contact; + } + + $name = $session->customer_details?->name ?? ''; + $lastSpacePos = strrpos($name, ' '); + + if ($lastSpacePos !== false) { + $firstName = substr($name, 0, $lastSpacePos); + $lastName = substr($name, $lastSpacePos + 1); + } else { + $firstName = $name; + $lastName = ''; + } + + $contact = Contact::create([ + 'first_name' => $firstName, + 'last_name' => $lastName, + 'email' => $email, + ]); + + $invoice->client->contacts()->attach($contact); + + return $contact; + } + public function checkoutTutorial() { Stripe::setApiKey(config('stripe.sk'));