Skip to content

Instantly share code, notes, and snippets.

@GMory
Last active June 29, 2017 17:34
Show Gist options
  • Save GMory/0a9cfd09f48d271cec1c6e00048fe075 to your computer and use it in GitHub Desktop.
Save GMory/0a9cfd09f48d271cec1c6e00048fe075 to your computer and use it in GitHub Desktop.
Laravel Spark: Allow invited users to register without requirement for plan
// resources/views/vendor/spark/auth/register-common.blade.php
// Add this check for invitation
<!-- Plan Selection -->
<div class="row" v-if="paidPlans.length > 0 && !invitation">
//resources/assets/js/spark-components/auth/register-stripe.js
methods: {
/**
* [Overriding Vendor's registration method] Attempt to register with the application.
*/
register() {
this.cardForm.errors.forget();
this.registerForm.busy = true;
this.registerForm.errors.forget();
if(this.invitation != null) {
return this.sendRegistration();
}
if ( ! Spark.cardUpFront || this.registerForm.invitation || this.selectedPlan.price == 0) {
return this.sendRegistration();
}
Stripe.card.createToken(this.stripePayload(), (status, response) => {
if (response.error) {
this.cardForm.errors.set({number: [response.error.message]})
this.registerForm.busy = false;
} else {
this.registerForm.stripe_token = response.id;
this.sendRegistration();
}
});
},
}
// app/Http/Requests/RegisterRequest.php
// Create this new request that registrations will go through
<?php
namespace App\Http\Requests;
use Laravel\Spark\Spark;
use App\Http\Requests\Request;
use Laravel\Spark\Contracts\Interactions\Auth\CreateUser;
use Laravel\Spark\Http\Requests\Auth\StripeRegisterRequest;
class RegisterRequest extends StripeRegisterRequest
{
public function plan()
{
if ($this->plan) {
return Spark::plans()->merge(Spark::teamPlans())->where('id', $this->plan)->first();
}
}
public function baseValidator()
{
$validator = Spark::interact(
CreateUser::class.'@validator', [$this]
);
$allPlanIdList = Spark::activePlanIdList().','.Spark::activeTeamPlanIdList();
$validator->sometimes('plan', 'required|in:'.$allPlanIdList, function () {
return Spark::needsCardUpFront() && ! $this->invitation;
});
return $validator;
}
}
// app/Providers/SparkServiceProvider.php
// Register this new request
public function register()
{
$this->app->singleton(RegisterRequestContract::class, RegisterRequest::class);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment