File size: 5,846 Bytes
5f32fbd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | <?php
namespace App\Console\Commands;
// use App\Models\Subscriptions;
use App\Events\BankTransferEvent;
use App\Events\FreePaymentEvent;
use App\Events\IyzicoLifetimeEvent;
use App\Events\PaypalLifetimeEvent;
use App\Events\PaystackLifetimeEvent;
use App\Events\StripeLifetimeEvent;
use Illuminate\Console\Command;
use Laravel\Cashier\Subscription as Subscriptions;
class CheckSubscriptionEnd extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'subscription:check-end';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Check subscription end dates and take appropriate actions';
/**
* Execute the console command.
*/
public function handle()
{
$now = now();
$allSubscriptions = Subscriptions::where(function ($query) use ($now) {
$query->where('stripe_status', 'bank_approved')
->where('ends_at', '<=', $now);
})->orWhere(function ($query) use ($now) {
$query->where('stripe_status', 'bank_renewed')
->where('ends_at', '<=', $now);
})->orWhere(function ($query) use ($now) {
$query->where('stripe_status', 'free_approved')
->where('ends_at', '<=', $now);
})
->orWhere(function ($query) use ($now) {
$query->where('stripe_status', 'stripe_approved')
->where('ends_at', '<=', $now);
})->orWhere(function ($query) use ($now) {
$query->where('stripe_status', 'paypal_approved')
->where('ends_at', '<=', $now);
})->orWhere(function ($query) use ($now) {
$query->where('stripe_status', 'iyzico_approved')
->where('ends_at', '<=', $now);
})->orWhere(function ($query) use ($now) {
$query->where('stripe_status', 'paystack_approved')
->where('ends_at', '<=', $now);
})->get();
// free
$cancelFreeSubscriptions = $allSubscriptions
->where('stripe_status', 'free_approved')
->where('auto_renewal', false);
$renewFreeSubscriptions = $allSubscriptions
->where('stripe_status', 'free_approved')
->where('auto_renewal', true);
$cancelFreeSubscriptionIds = $cancelFreeSubscriptions->pluck('stripe_id')->toArray();
$renewFreeSubscriptionIds = $renewFreeSubscriptions->pluck('stripe_id')->toArray();
if (! empty($cancelFreeSubscriptionIds)) {
event(new FreePaymentEvent('free_expired', $cancelFreeSubscriptionIds));
}
if (! empty($renewFreeSubscriptionIds)) {
event(new FreePaymentEvent('free_renewed', $renewFreeSubscriptionIds));
}
// bank
$cancelBankSubscriptions = $allSubscriptions
->where('auto_renewal', false)
->where(function ($query) {
$query->where('stripe_status', 'bank_approved')
->orWhere('stripe_status', 'bank_renewed');
});
$renewBankSubscriptions = $allSubscriptions
->where('auto_renewal', true)
->where(function ($query) {
$query->where('stripe_status', 'bank_approved')
->orWhere('stripe_status', 'bank_renewed');
});
$renewBankSubscriptionIds = $renewBankSubscriptions->pluck('stripe_id')->toArray();
$cancelBankSubscriptionIds = $cancelBankSubscriptions->pluck('stripe_id')->toArray();
if (! empty($cancelBankSubscriptionIds)) {
event(new BankTransferEvent('bank_expired', $cancelBankSubscriptionIds));
}
if (! empty($renewBankSubscriptionIds)) {
event(new BankTransferEvent('bank_renewed', $renewBankSubscriptionIds));
}
// strip
$renewStripeLifeTimeSubscriptions = $allSubscriptions
->where('stripe_status', 'stripe_approved')
->where('auto_renewal', true);
$renewStripeLiftimeSubscriptionIds = $renewStripeLifeTimeSubscriptions->pluck('stripe_id')->toArray();
if (! empty($renewStripeLiftimeSubscriptionIds)) {
event(new StripeLifetimeEvent('stripe_approved', $renewStripeLiftimeSubscriptionIds));
}
// paypal
$renewPaypalLifeTimeSubscriptions = $allSubscriptions
->where('stripe_status', 'paypal_approved')
->where('auto_renewal', true);
$renewPaypalLiftimeSubscriptionIds = $renewPaypalLifeTimeSubscriptions->pluck('stripe_id')->toArray();
if (! empty($renewPaypalLiftimeSubscriptionIds)) {
event(new PaypalLifetimeEvent('paypal_approved', $renewPaypalLiftimeSubscriptionIds));
}
// iyzico
$renewIyzicoLifeTimeSubscriptions = $allSubscriptions
->where('stripe_status', 'iyzico_approved')
->where('auto_renewal', true);
$renewIyzicoLiftimeSubscriptionIds = $renewIyzicoLifeTimeSubscriptions->pluck('stripe_id')->toArray();
if (! empty($renewIyzicoLiftimeSubscriptionIds)) {
event(new IyzicoLifetimeEvent('iyzico_approved', $renewIyzicoLiftimeSubscriptionIds));
}
// paystack
$renewPaystackLifeTimeSubscriptions = $allSubscriptions
->where('stripe_status', 'iyzico_approved')
->where('auto_renewal', true);
$renewPaystackLiftimeSubscriptionIds = $renewPaystackLifeTimeSubscriptions->pluck('stripe_id')->toArray();
if (! empty($renewPaystackLiftimeSubscriptionIds)) {
event(new PaystackLifetimeEvent('paystack_approved', $renewPaystackLiftimeSubscriptionIds));
}
$this->info('Subscription check event run successfully.');
}
}
|