/**
  * Return the subscription helper object associated with this invoice item.
  *
  * @return \DorellJames\Billing\SubscriptionBillableTrait\Subscription
  */
 public function subscription()
 {
     if (empty($this->subscription_id)) {
         return null;
     }
     if (null !== $this->subscription) {
         return $this->subscription ? $this->subscription : null;
     }
     $this->subscription = false;
     if ($subscription = $this->model->subscriptions()->find($this->subscription_id)) {
         $this->subscription = $subscription;
     }
     return $this->subscription ? $this->subscription : null;
 }
Example #2
0
 /**
  * Create a new Stripe subscription.
  *
  * @param  string|null  $token
  * @param  array  $options
  * @return \Laravel\Cashier\Subscription
  */
 public function create($token = null, array $options = [])
 {
     $customer = $this->getStripeCustomer($token, $options);
     $subscription = $customer->subscriptions->create($this->buildPayload());
     if ($this->skipTrial) {
         $trialEndsAt = null;
     } else {
         $trialEndsAt = $this->trialDays ? Carbon::now()->addDays($this->trialDays) : null;
     }
     return $this->owner->subscriptions()->create(['name' => $this->name, 'stripe_id' => $subscription->id, 'stripe_plan' => $this->plan, 'quantity' => $this->quantity, 'trial_ends_at' => $trialEndsAt, 'ends_at' => null]);
 }
 /**
  * Create a new Braintree subscription.
  *
  * @param  string|null  $token
  * @param  array  $customerOptions
  * @param  array  $subscriptionOptions
  * @return \Laravel\Cashier\Subscription
  */
 public function create($token = null, array $customerOptions = [], array $subscriptionOptions = [])
 {
     $payload = $this->getSubscriptionPayload($this->getBraintreeCustomer($token, $customerOptions), $subscriptionOptions);
     if ($this->coupon) {
         $payload = $this->addCouponToPayload($payload);
     }
     $response = BraintreeSubscription::create($payload);
     if (!$response->success) {
         throw new Exception('Braintree failed to create subscription: ' . $response->message);
     }
     if ($this->skipTrial) {
         $trialEndsAt = null;
     } else {
         $trialEndsAt = $this->trialDays ? Carbon::now()->addDays($this->trialDays) : null;
     }
     return $this->user->subscriptions()->create(['name' => $this->name, 'braintree_id' => $response->subscription->id, 'braintree_plan' => $this->plan, 'quantity' => 1, 'trial_ends_at' => $trialEndsAt, 'ends_at' => null]);
 }
 /**
  * Create a new Braintree subscription.
  *
  * @param string|null $nonce
  * @param array       $customerOptions
  * @param array       $subscriptionOptions
  *
  * @return \LimeDeck\CashierBraintree\Subscription
  * @throws \Exception
  */
 public function create($nonce = null, array $customerOptions = [], array $subscriptionOptions = [])
 {
     $customer = $this->getBraintreeCustomer($nonce, $customerOptions);
     $plan = $this->findPlanById($this->plan);
     $planPriceWithTax = $this->planPriceWithTax($plan, $this->getTaxPercentageForPayload());
     $subscriptionOptions = array_merge($subscriptionOptions, ['price' => $planPriceWithTax, 'paymentMethodToken' => $customer->paymentMethods[0]->token, 'planId' => $this->plan, 'trialDuration' => $this->trialDays ?: 0, 'trialDurationUnit' => 'day', 'trialPeriod' => $this->trialDays ? true : false]);
     if ($this->coupon) {
         $coupon = $this->findCouponById($this->coupon);
         $amount = $this->couponPercentage ? $coupon->amount / 100 * $planPriceWithTax : $coupon->amount;
         $subscriptionOptions['discounts'] = ['add' => [['inheritedFromId' => $this->coupon, 'amount' => $this->formatAmount($amount)]]];
     }
     $result = BraintreeSubscription::create($subscriptionOptions);
     if (!$result->success) {
         throw new Exception('Subscription was not created');
     }
     return $this->user->subscriptions()->create(['name' => $this->name, 'braintree_id' => $result->subscription->id, 'braintree_plan' => $this->plan, 'quantity' => 1, 'trial_ends_at' => $this->trialDays ? Carbon::now()->addDays($this->trialDays) : null, 'ends_at' => null]);
 }
 /**
  * Retrieve a hosted page and register a user based on the result of the payment.
  *
  * @param $id
  * @return null
  * @throws UserMismatchException
  */
 public function registerFromHostedPage($id)
 {
     $result = ChargeBee_HostedPage::retrieve($id);
     // TODO: Check if subscription was successful or failed.
     // Check if the ID of the model is the same as the ID of the model that performed the payment
     if (!(int) base64_decode($result->hostedPage()->passThruContent) === $this->model->id) {
         throw new UserMismatchException('The user who performed the payment is not the user you are trying to attach the subscription to');
     }
     $subscriptionId = $result->hostedPage()->content['subscription']['id'];
     $result = ChargeBee_Subscription::retrieve($subscriptionId);
     $subscription = $result->subscription();
     $addons = $subscription->addons;
     $card = $result->card();
     $subscription = $this->model->subscriptions()->create(['subscription_id' => $subscription->id, 'plan_id' => $subscription->planId, 'next_billing_at' => $subscription->currentTermEnd, 'trial_ends_at' => $subscription->trialEnd, 'quantity' => $subscription->planQuantity, 'last_four' => $card->last4]);
     if ($addons) {
         foreach ($addons as $addon) {
             $subscription->addons()->create(['quantity' => $addon->quantity, 'addon_id' => $addon->id]);
         }
     }
     return $subscription;
 }