43 lines
1.4 KiB
PHP
43 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace Modules\Platform\app\Http\Controllers;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use Modules\Company\Models\Company;
|
|
use Modules\Platform\app\Models\Payment;
|
|
|
|
class StripeWebhookController extends Controller
|
|
{
|
|
/**
|
|
* Receive a Stripe webhook event.
|
|
*/
|
|
public function __invoke(Request $request)
|
|
{
|
|
$payload = $request->all();
|
|
$eventType = data_get($payload, 'type');
|
|
|
|
if ($eventType === 'payment_intent.succeeded') {
|
|
$intent = data_get($payload, 'data.object', []);
|
|
$company = Company::where('stripe_customer_id', data_get($intent, 'customer'))->first();
|
|
|
|
if ($company) {
|
|
Payment::updateOrCreate(
|
|
['stripe_payment_intent_id' => data_get($intent, 'id')],
|
|
[
|
|
'company_id' => $company->id,
|
|
'amount' => (int) data_get($intent, 'amount_received', 0),
|
|
'currency' => (string) data_get($intent, 'currency', 'gbp'),
|
|
'status' => 'paid',
|
|
'description' => 'Stripe payment received',
|
|
'paid_at' => now(),
|
|
'metadata' => $intent,
|
|
],
|
|
);
|
|
}
|
|
}
|
|
|
|
return response()->json(['received' => true]);
|
|
}
|
|
}
|