/** * {@inheritdoc} */ public function handle($user, $plan, $fromRegistration, array $data) { $subscription = $user->newSubscription('default', $plan->id); // Here we will check if we need to skip trial or set trial days on the subscription // when creating it on the provider. By default, we will skip the trial when this // interaction is not from egistration since they have already usually trialed. if (!$fromRegistration) { $subscription->skipTrial(); } elseif ($plan->trialDays > 0) { $subscription->trialDays($plan->trialDays); } if (isset($data['coupon'])) { $subscription->withCoupon($data['coupon']); } // Next, we need to check if this application is storing billing addresses and if so // we will update the billing address in the database so that any tax information // on the user will be up to date via the taxPercentage method on the billable. if (Spark::collectsBillingAddress()) { Spark::call(UserRepository::class . '@updateBillingAddress', [$user, $data]); } // If this application collects European VAT, we will store the VAT ID that was sent // with the request. It is used to determine if the VAT should get charged at all // when billing the customer. When it is present, VAT is not typically charged. if (Spark::collectsEuropeanVat()) { Spark::call(UserRepository::class . '@updateVatId', [$user, array_get($data, 'vat_id')]); } // Here we will create the actual subscription on the service and fire off the event // letting other listeners know a user has subscribed, which will allow any hooks // to fire that need to send the subscription data to any external metrics app. $subscription->create($data[$this->token]); event(new UserSubscribed($user = $user->fresh(), $plan, $fromRegistration)); return $user; }
/** * {@inheritdoc} */ public function handle($team, $plan, array $data) { $subscription = $team->newSubscription('default', $plan->id); // Here we will fill the trial days for this team subscription. We will also set any // coupon on the subscription so that the team can receive a discount on the team // subscription. Then we will almost be ready to create the final subscription. $subscription->trialDays($plan->trialDays); if (isset($data['coupon'])) { $subscription->withCoupon($data['coupon']); } // Next, we need to check if this application is storing billing addresses and if so // we will update the billing address in the database so that any tax information // on the team will be up to date via the taxPercentage method on the billable. if (Spark::collectsBillingAddress()) { Spark::call(TeamRepository::class . '@updateBillingAddress', [$team, $data]); } // If this application collects European VAT, we will store the VAT ID that was sent // with the request. It is used to determine if the VAT should get charged at all // when billing the customer. When it is present, VAT is not typically charged. if (Spark::collectsEuropeanVat()) { Spark::call(TeamRepository::class . '@updateVatId', [$team, array_get($data, 'vat_id')]); } // Here we will create the actual subscription on the service and fire off the event // letting other listeners know a team has subscribed, which will allow any hooks // to fire that need to send the subscription data to any external metrics app. $subscription->create($data[$this->token]); event(new TeamSubscribed($team = $team->fresh(), $plan)); return $team; }
/** * Get the tax percentage to apply to the subscription. * * @return int */ public function taxPercentage() { if (!Spark::collectsEuropeanVat()) { return 0; } $vatCalculator = new VatCalculator(); $vatCalculator->setBusinessCountryCode(Spark::homeCountry()); return $vatCalculator->getTaxRateForCountry($this->card_country, !empty($this->vat_id)) * 100; }
/** * Get the default JavaScript variables for Spark. * * @return array */ public static function scriptVariables() { return ['braintreeMerchantId' => config('services.braintree.merchant_id'), 'braintreeToken' => Spark::billsUsingBraintree() ? BraintreeClientToken::generate() : null, 'cardUpFront' => Spark::needsCardUpFront(), 'collectsBillingAddress' => Spark::collectsBillingAddress(), 'collectsEuropeanVat' => Spark::collectsEuropeanVat(), 'csrfToken' => csrf_token(), 'currencySymbol' => Cashier::usesCurrencySymbol(), 'env' => config('app.env'), 'roles' => Spark::roles(), 'state' => Spark::call(InitialFrontendState::class . '@forUser', [Auth::user()]), 'stripeKey' => config('services.stripe.key'), 'userId' => Auth::id(), 'usesApi' => Spark::usesApi(), 'usesBraintree' => Spark::billsUsingBraintree(), 'usesTeams' => Spark::usesTeams(), 'usesStripe' => Spark::billsUsingStripe()]; }