Update
This commit is contained in:
@@ -37,6 +37,32 @@ public static function getPricingPageOneTime()
|
||||
return $one_time;
|
||||
}
|
||||
|
||||
public static function getSubscriptionPlanByStripePriceID($stripe_price_id)
|
||||
{
|
||||
$plans = self::getSubscriptions('subscription_plans', true, true);
|
||||
|
||||
foreach ($plans as $plan) {
|
||||
|
||||
|
||||
if ($plan['id'] == 'free') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$stripe_price_ids = self::getPlanSystemProperty($plan, 'stripe.stripe_price_ids');
|
||||
|
||||
if (is_array($stripe_price_ids)) {
|
||||
if (isset($stripe_price_ids['month']) && in_array($stripe_price_id, $stripe_price_ids['month'])) {
|
||||
return $plan;
|
||||
}
|
||||
if (isset($stripe_price_ids['year']) && in_array($stripe_price_id, $stripe_price_ids['year'])) {
|
||||
return $plan;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function getPlanSystemProperty($plan, $property)
|
||||
{
|
||||
// Get the environment (test or prod)
|
||||
@@ -50,10 +76,10 @@ public static function getPlanSystemProperty($plan, $property)
|
||||
// Inject environment into the path
|
||||
// stripe.product_id.month becomes system.stripe.product_id.{env}.month
|
||||
array_splice($propertyParts, 2, 0, $environment);
|
||||
$fullPath = 'system.'.implode('.', $propertyParts);
|
||||
$fullPath = 'system.' . implode('.', $propertyParts);
|
||||
} else {
|
||||
// For non-stripe properties, just prepend 'system.'
|
||||
$fullPath = 'system.'.$property;
|
||||
$fullPath = 'system.' . $property;
|
||||
}
|
||||
|
||||
return data_get($plan, $fullPath);
|
||||
|
||||
89
app/Helpers/FirstParty/Purchase/SubscriptionHelper.php
Normal file
89
app/Helpers/FirstParty/Purchase/SubscriptionHelper.php
Normal file
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
namespace App\Helpers\FirstParty\Purchase;
|
||||
|
||||
use App\Helpers\FirstParty\Stripe\StripeHelper;
|
||||
use App\Models\Plan;
|
||||
use App\Models\UserPlan;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Laravel\Cashier\Events\WebhookReceived;
|
||||
|
||||
class SubscriptionHelper
|
||||
{
|
||||
public static function handleSubscriptionWebhookEvents(WebhookReceived $event)
|
||||
{
|
||||
switch ($event->payload['type']) {
|
||||
case 'customer.subscription.created':
|
||||
case 'customer.subscription.updated':
|
||||
self::handleSubscriptionUpsert($event);
|
||||
break;
|
||||
|
||||
case 'customer.subscription.deleted':
|
||||
self::handleSubscriptionDelete($event);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private static function handleSubscriptionUpsert(WebhookReceived $event)
|
||||
{
|
||||
$object = $event->payload['data']['object'];
|
||||
|
||||
//dump($object);
|
||||
|
||||
$ignore_statuses = ['incomplete_expired', 'incomplete'];
|
||||
|
||||
if (in_array($object['status'], $ignore_statuses)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$user = StripeHelper::getUserByStripeID($object['customer']);
|
||||
|
||||
if ($user) {
|
||||
foreach ($object['items']['data'] as $line_item) {
|
||||
|
||||
//dump($line_item);
|
||||
|
||||
$stripe_price_id = $line_item['plan']['id'];
|
||||
$current_period_end = $line_item['current_period_end'];
|
||||
|
||||
$cancel_at = null;
|
||||
$canceled_at = null;
|
||||
|
||||
if ($line_item['subscription'] == $object['id']) {
|
||||
$cancel_at = $object['cancel_at'];
|
||||
$canceled_at = $object['canceled_at'];
|
||||
}
|
||||
|
||||
$subscription_config = PurchaseHelper::getSubscriptionPlanByStripePriceID($stripe_price_id);
|
||||
|
||||
$plan = Plan::where('tier', $subscription_config['id'])->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' => $current_period_end,
|
||||
'cancel_at' => $cancel_at,
|
||||
'canceled_at' => $canceled_at,
|
||||
]);
|
||||
} else {
|
||||
$user_plan = UserPlan::create([
|
||||
'user_id' => $user->id,
|
||||
'plan_id' => $plan->id,
|
||||
'current_period_end' => $current_period_end,
|
||||
'cancel_at' => $cancel_at,
|
||||
'canceled_at' => $canceled_at,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static function handleSubscriptionDelete(WebhookReceived $event)
|
||||
{
|
||||
// /
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,31 +2,13 @@
|
||||
|
||||
namespace App\Helpers\FirstParty\Stripe;
|
||||
|
||||
use App\Models\User;
|
||||
use Laravel\Cashier\Events\WebhookReceived;
|
||||
|
||||
class StripeHelper
|
||||
{
|
||||
public static function handleSubscriptionWebhookEvents(WebhookReceived $event)
|
||||
public static function getUserByStripeID($customer_id)
|
||||
{
|
||||
switch ($event->payload['type']) {
|
||||
case 'customer.subscription.created':
|
||||
case 'customer.subscription.updated':
|
||||
self::handleSubscriptionUpsert($event);
|
||||
break;
|
||||
|
||||
case 'customer.subscription.deleted':
|
||||
self::handleSubscriptionDelete($event);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private static function handleSubscriptionUpsert(WebhookReceived $event)
|
||||
{
|
||||
///
|
||||
}
|
||||
|
||||
private static function handleSubscriptionDelete(WebhookReceived $event)
|
||||
{
|
||||
///
|
||||
return User::where('stripe_id', $customer_id)->first();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user