Update
This commit is contained in:
65
app/Helpers/FirstParty/Purchase/WatermarkUsageHelper.php
Normal file
65
app/Helpers/FirstParty/Purchase/WatermarkUsageHelper.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace App\Helpers\FirstParty\Purchase;
|
||||
|
||||
use App\Helpers\FirstParty\Stripe\StripeHelper;
|
||||
use App\Models\UserUsage;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Laravel\Cashier\Events\WebhookReceived;
|
||||
|
||||
class WatermarkUsageHelper
|
||||
{
|
||||
public static function handleWatermarkUsageWebhookEvents(WebhookReceived $event)
|
||||
{
|
||||
switch ($event->payload['type']) {
|
||||
case 'invoice.paid':
|
||||
self::handleInvoicePaid($event);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private static function handleInvoicePaid(WebhookReceived $event)
|
||||
{
|
||||
$object = $event->payload['data']['object'];
|
||||
|
||||
//dump($object);
|
||||
|
||||
$accept_statuses = ['paid', 'partially_paid'];
|
||||
|
||||
if (!in_array($object['status'], $accept_statuses)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$user = StripeHelper::getUserByStripeID($object['customer']);
|
||||
|
||||
if ($user) {
|
||||
foreach ($object['lines']['data'] as $line_item) {
|
||||
$stripe_price_id = $line_item['pricing']['price_details']['price'];
|
||||
|
||||
//dd($stripe_price_id);
|
||||
|
||||
$subscription_config = PurchaseHelper::getSubscriptionPlanByStripePriceID($stripe_price_id);
|
||||
|
||||
if ($subscription_config) {
|
||||
|
||||
if ($subscription_config['type'] == 'subscription_plans') {
|
||||
$user_usage = UserUsage::where('user_id', $user->id)->first();
|
||||
|
||||
if ($user_usage) {
|
||||
$user_usage->update([
|
||||
'non_watermark_videos_left' => $subscription_config['system']['non_watermark_videos'],
|
||||
]);
|
||||
} else {
|
||||
$user_usage = UserUsage::create([
|
||||
'user_id' => $user->id,
|
||||
'non_watermark_videos_left' => $subscription_config['system']['non_watermark_videos'],
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
//dd($subscription_config);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user