Last active
May 15, 2026 01:01
-
-
Save codigoconjuan/a13221fc05e442e8b146ae38010692cb to your computer and use it in GitHub Desktop.
Métodos Auxiliares para Suscripciones CashTrackr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private function getNextBillingDate($subscription): ?string | |
| { | |
| return cache()->remember( | |
| "stripe.next_billing.{$subscription->id}", | |
| now()->addHours(1), | |
| function () use ($subscription) { | |
| try { | |
| $stripe = $subscription->asStripeSubscription(); | |
| $periodEnd = $stripe->items->data[0]->current_period_end ?? null; | |
| return $periodEnd | |
| ? Carbon::createFromTimestamp($periodEnd)->toIso8601String() | |
| : null; | |
| } catch (\Exception $e) { | |
| logger()->error('Error obteniendo next billing date', [ | |
| 'error' => $e->getMessage(), | |
| 'subscription_id' => $subscription->id, | |
| ]); | |
| return null; | |
| } | |
| } | |
| ); | |
| } | |
| private function buildStatusLabel($subscription, ?string $nextBillingDate): array | |
| { | |
| if ($subscription->ended()) { | |
| return [ | |
| 'text' => 'Suscripción terminada', | |
| 'description' => 'Terminó el', | |
| 'date' => $subscription->ends_at?->toIso8601String(), | |
| 'color' => 'gray', | |
| ]; | |
| } | |
| if ($subscription->onGracePeriod()) { | |
| return [ | |
| 'text' => 'Cancelada', | |
| 'description' => 'Acceso hasta', | |
| 'date' => $subscription->ends_at?->toIso8601String(), | |
| 'color' => 'orange', | |
| ]; | |
| } | |
| if ($subscription->hasIncompletePayment() || $subscription->pastDue()) { | |
| if ($this->latestInvoiceIsPaid($subscription)) { | |
| return [ | |
| 'text' => 'Suscripción Activa', | |
| 'description' => 'Tu próximo cobro será el ', | |
| 'color' => 'green', | |
| 'date' => $nextBillingDate, | |
| ]; | |
| } | |
| if ($subscription->hasIncompletePayment()) { | |
| return [ | |
| 'text' => 'Pago por confirmar', | |
| 'description' => 'Completa la verificación de tu tarjeta', | |
| 'date' => null, | |
| 'color' => 'red', | |
| ]; | |
| } | |
| return [ | |
| 'text' => 'Pago pendiente', | |
| 'description' => 'Actualiza tu método de pago para continuar', | |
| 'date' => null, | |
| 'color' => 'red', | |
| ]; | |
| } | |
| return [ | |
| 'text' => 'Suscripción Activa', | |
| 'description' => 'Tu próximo cobro será el ', | |
| 'color' => 'green', | |
| 'date' => $nextBillingDate, | |
| ]; | |
| } | |
| private function latestInvoiceIsPaid( $subscription): bool | |
| { | |
| try { | |
| $stripeSub = $subscription->asStripeSubscription(); | |
| if (!$stripeSub->latest_invoice) { | |
| return false; | |
| } | |
| $invoice = \Laravel\Cashier\Cashier::stripe() | |
| ->invoices | |
| ->retrieve($stripeSub->latest_invoice); | |
| return $invoice->status === 'paid'; | |
| } catch (\Exception $e) { | |
| logger()->error('Error verificando invoice', [ | |
| 'error' => $e->getMessage(), | |
| 'subscription_id' => $subscription->id, | |
| ]); | |
| return false; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment