| Server IP : 162.253.224.18 / Your IP : 216.73.216.135 Web Server : Apache System : Linux s18.infinitysrv.com 3.10.0-962.3.2.lve1.5.89.el7.x86_64 #1 SMP Thu Jul 9 15:55:31 UTC 2026 x86_64 User : dejavumk ( 1184) PHP Version : 7.4.33 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : OFF | Pkexec : OFF Directory : /home/dejavumk/nutricoach.dejavumkt.info/app/Billing/ |
Upload File : |
<?php
namespace App\Billing;
use App\Billing\Payment;
use Carbon\Carbon;
use App\DetailPaypal;
use App\CardDetail;
trait Billable
{
/**
* Fetch a user by their conektaId.
*
* @param string $conektaId
* @return User
*/
public static function byConektaId($conektaId)
{
return static::with(['card'])->whereHas('card', function($q) use($conektaId) {$q->where('customer_id', '=', $conektaId);})->first();
}
public static function byAgreementId($agreement_id)
{
return static::with(['paypal'])->whereHas('paypal', function($q) use($agreement_id) {$q->where('agreement_id', '=', $agreement_id);})->first();
}
/**
* Activate the user's subscription.
*
* @param string $customerId
* @param int $subscription_id
* @return bool
*/
public function activate($customerId, $plan_id, $subscription_id, $provider)
{
$start_date = Carbon::now();
$renewal_date = Carbon::now()->addDays(30);
$card = new CardDetail;
$card->customer_id = $customerId;
$card->subscription_conekta_id = $subscription_id;
// $card ->save();
$subscription = $this->forceFill([
// 'id_customer' => $customerId,
'active' => true,
'plan_id' => $plan_id,
'start_date' => $start_date,
'renewal_date' => $renewal_date,
'provider' => $provider
]); //->card()->save($card);
$subscription->card()->save($card);
return $subscription->save();
}
public function activate_paypal($plan_id, $provider, $agreement_id, $email, $payer_id)
{
$start_date = Carbon::now();
$renewal_date = Carbon::now()->addDays(30);
$paypal = new DetailPaypal;
$paypal->agreement_id = $agreement_id;
$paypal->email = $email;
$paypal->payer_id = $payer_id;
$subscription = $this->forceFill([
'active' => true,
'plan_id' => $plan_id,
'start_date' => $start_date,
'renewal_date' => $renewal_date,
'provider' => 'paypal'
]); //->paypal()->save($paypal);
$subscription->paypal()->save($paypal);
return $subscription->save();
}
/**
* Deactivate the user's subscription.
*
* @param mixed $endDate
* @return bool
*/
public function deactivate($endDate = null)
{
$endDate = $endDate ?: \Carbon\Carbon::now()->addDays(3);
return $this->forceFill([
'active' => false,
'subscription_end_at' => $endDate,
'plan_id' => 0,
// 'start_date' => null,
'renewal_date' => null,
// 'subscription_id' => null,
'provider' => null
])->save();
}
/**
* Fetch a Subscription instance.
*
* @return Subscription
*/
public function subscription()
{
return new Subscription($this);
}
public function isOnGracePeriod()
{
$end_at = $this->subscription_end_at;
$end_at = Carbon::parse($end_at);
if (! $end_at) {
return false;
}
return Carbon::now()->lt($end_at);
}
/**
* Determine if the user is subscribed.
*
* @return boolean
*/
public function isSubscribed()
{
return !! $this->active;
}
public function isActive()
{
return ! is_null($this->subscription_end_at) || $this->isOnGracePeriod();
}
public function hasCanceled()
{
return ! is_null($this->subscription_end_at);
}
public function hasNotSubscribe()
{
return ! is_null($this->provider);
}
/**
* Fetch the user's payments.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function payments()
{
return $this->hasMany(Payment::class);
}
}