Skip to content

Commit

Permalink
feat: early fraud warning webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
andrasbacsai committed Jul 5, 2024
1 parent 88ab385 commit 42fb8ab
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions app/Http/Controllers/Webhook/Stripe.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,34 @@ public function events(Request $request)
$type = data_get($event, 'type');
$data = data_get($event, 'data.object');
switch ($type) {
case 'radar.early_fraud_warning.created':
$stripe = new \Stripe\StripeClient(config('subscription.stripe_api_key'));
$id = data_get($data, 'id');
$charge = data_get($data, 'charge');
if ($charge) {
$stripe->refunds->create(['charge' => $charge]);
}
$pi = data_get($data, 'payment_intent');
$piData = $stripe->paymentIntents->retrieve($pi, []);
$customerId = data_get($piData, 'customer');
$subscription = Subscription::where('stripe_customer_id', $customerId)->first();
if (! $subscription) {
Sleep::for(5)->seconds();
$subscription = Subscription::where('stripe_customer_id', $customerId)->firstOrFail();
}
if (! $subscription) {
Sleep::for(5)->seconds();
$subscription = Subscription::where('stripe_customer_id', $customerId)->firstOrFail();
}
if ($subscription) {
$subscriptionId = data_get($subscription, 'stripe_subscription_id');
$stripe->subscriptions->cancel($subscriptionId, []);
$subscription->update([
'stripe_invoice_paid' => false,
]);
}
send_internal_notification("Early fraud warning created Refunded, subscription canceled. Charge: {$charge}, id: {$id}, pi: {$pi}, customer: {$customerId}, subscription: {$subscriptionId}");
break;
case 'checkout.session.completed':
$clientReferenceId = data_get($data, 'client_reference_id');
if (is_null($clientReferenceId)) {
Expand Down

0 comments on commit 42fb8ab

Please sign in to comment.