Update
This commit is contained in:
73
app/Helpers/FirstParty/Purchase/CreditsPurchaseHelper.php
Normal file
73
app/Helpers/FirstParty/Purchase/CreditsPurchaseHelper.php
Normal file
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace App\Helpers\FirstParty\Purchase;
|
||||
|
||||
use App\Helpers\FirstParty\Credits\CreditsHelper;
|
||||
use App\Helpers\FirstParty\Credits\CreditsService;
|
||||
use App\Helpers\FirstParty\Stripe\StripeHelper;
|
||||
use App\Models\PaymentWebhookProcessedLog;
|
||||
use Laravel\Cashier\Events\WebhookReceived;
|
||||
|
||||
class CreditsPurchaseHelper
|
||||
{
|
||||
public static function handleWebhookEvents(WebhookReceived $event)
|
||||
{
|
||||
|
||||
switch (StripeHelper::getEventType($event)) {
|
||||
case 'checkout.session.completed':
|
||||
self::handleCheckoutSessionCompleted($event);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private static function handleCheckoutSessionCompleted($event)
|
||||
{
|
||||
|
||||
$object = StripeHelper::getEventObject($event);
|
||||
|
||||
// Check if the webhook has already been processed
|
||||
$payment_webhook_processed_log = PaymentWebhookProcessedLog::where('provider', 'stripe')
|
||||
->where('log_id', $object['id'])
|
||||
->where('log_type', 'checkout_session')
|
||||
->first();
|
||||
|
||||
if (! is_null($payment_webhook_processed_log)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the user associated with the checkout session
|
||||
$user = StripeHelper::getUserByStripeID($object['customer']);
|
||||
|
||||
if ($user) {
|
||||
|
||||
// Get the line items from the checkout session
|
||||
StripeHelper::setStripeApiKey();
|
||||
$lineItems = \Stripe\Checkout\Session::allLineItems($object['id']);
|
||||
|
||||
// dd($lineItems);
|
||||
|
||||
foreach ($lineItems->data as $lineItem) {
|
||||
$stripe_price_id = $lineItem->price->id;
|
||||
|
||||
// Get the credit pack associated with the price ID
|
||||
$credit_pack = CreditsHelper::getCreditPackByStripePriceId($stripe_price_id);
|
||||
|
||||
if (is_null($credit_pack)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$total_credits = PurchaseHelper::getPlanSystemProperty($credit_pack, 'credits');
|
||||
|
||||
// Deposit the credits to the user's account
|
||||
CreditsService::depositAlacarte($user->id, $total_credits, "Purchased {$credit_pack['name']}");
|
||||
}
|
||||
}
|
||||
|
||||
// Mark the webhook as processed
|
||||
PaymentWebhookProcessedLog::create([
|
||||
'provider' => 'stripe',
|
||||
'log_id' => $object['id'],
|
||||
'log_type' => 'checkout_session',
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,7 @@ public static function getPricingPageSubscriptions()
|
||||
|
||||
$subscriptions = self::filterShowInPricing($subscriptions);
|
||||
|
||||
$subscriptions = self::setSystemPlan($subscriptions, 'stripe.current_stripe_price_id.month');
|
||||
$subscriptions = self::setSystemPlan($subscriptions, 'stripe.current_stripe_price_id.month', 'stripe_monthly_price_id');
|
||||
|
||||
$subscriptions = self::filterUnsetSystem($subscriptions);
|
||||
|
||||
@@ -28,7 +28,7 @@ public static function getPricingPageOneTime()
|
||||
|
||||
$one_time = PurchaseHelper::filterShowInPricing($one_time);
|
||||
|
||||
$one_time = PurchaseHelper::setSystemPlan($one_time, 'stripe.current_stripe_price_id');
|
||||
$one_time = PurchaseHelper::setSystemPlan($one_time, 'stripe.current_stripe_price_id', 'stripe_price_id');
|
||||
|
||||
$one_time = PurchaseHelper::filterUnsetSystem($one_time);
|
||||
|
||||
@@ -136,11 +136,11 @@ public static function getOneTimePurchases(?bool $enabled, $system = false)
|
||||
return $tmp_otp;
|
||||
}
|
||||
|
||||
public static function setSystemPlan($arr, $property)
|
||||
public static function setSystemPlan($arr, $property, $key_name)
|
||||
{
|
||||
foreach ($arr as $key => $item) {
|
||||
|
||||
$arr[$key]['stripe_monthly_price_id'] = PurchaseHelper::getPlanSystemProperty($item, $property);
|
||||
$arr[$key][$key_name] = PurchaseHelper::getPlanSystemProperty($item, $property);
|
||||
|
||||
// $stripe_monthly_price_id = PurchaseHelper::getPlanSystemProperty($subscription, 'stripe.product_id.month');
|
||||
|
||||
@@ -173,7 +173,7 @@ public static function filterShowInPricing($arr)
|
||||
});
|
||||
}
|
||||
|
||||
private static function getStripeEnvironment()
|
||||
public static function getStripeEnvironment()
|
||||
{
|
||||
$env = App::environment();
|
||||
|
||||
|
||||
@@ -9,9 +9,9 @@
|
||||
|
||||
class SubscriptionHelper
|
||||
{
|
||||
public static function handleSubscriptionWebhookEvents(WebhookReceived $event)
|
||||
public static function handleWebhookEvents(WebhookReceived $event)
|
||||
{
|
||||
switch ($event->payload['type']) {
|
||||
switch (StripeHelper::getEventType($event)) {
|
||||
case 'customer.subscription.created':
|
||||
case 'customer.subscription.updated':
|
||||
self::handleSubscriptionUpsert($event);
|
||||
@@ -25,7 +25,7 @@ public static function handleSubscriptionWebhookEvents(WebhookReceived $event)
|
||||
|
||||
private static function handleSubscriptionUpsert(WebhookReceived $event)
|
||||
{
|
||||
$object = $event->payload['data']['object'];
|
||||
$object = StripeHelper::getEventObject($event);
|
||||
|
||||
// dump($object);
|
||||
|
||||
@@ -83,6 +83,34 @@ private static function handleSubscriptionUpsert(WebhookReceived $event)
|
||||
|
||||
private static function handleSubscriptionDelete(WebhookReceived $event)
|
||||
{
|
||||
// /
|
||||
$object = StripeHelper::getEventObject($event);
|
||||
|
||||
$user = StripeHelper::getUserByStripeID($object['customer']);
|
||||
|
||||
if ($user) {
|
||||
|
||||
$plan = Plan::where('tier', 'free')->first();
|
||||
|
||||
if ($plan) {
|
||||
$user_plan = UserPlan::where('user_id', $user->id)->first();
|
||||
|
||||
if ($user_plan) {
|
||||
$user_plan->update([
|
||||
'plan_id' => $plan->id,
|
||||
'current_period_end' => null,
|
||||
'cancel_at' => null,
|
||||
'canceled_at' => null,
|
||||
]);
|
||||
} else {
|
||||
$user_plan = UserPlan::create([
|
||||
'user_id' => $user->id,
|
||||
'plan_id' => $plan->id,
|
||||
'current_period_end' => null,
|
||||
'cancel_at' => null,
|
||||
'canceled_at' => null,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user