Create new contacts on payment
This commit is contained in:
parent
9e8e046985
commit
c199e0a9dd
|
|
@ -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'));
|
||||
|
|
|
|||
Loading…
Reference in New Issue