# Onboarding Accounts

### Laravel Hosted Interface

Create a controller to manage on-boarding process. The example below registers an Express account for the store and is for platforms with the frontend hosted within Laravel. API led applications would take a slightly different approach.

```php
namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Store;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use URL;

class StripeController extends Controller
{

    /**
     * Creates an onboarding link and redirects the store there.
     *
     * @param Request $request
     * @return RedirectResponse
     */
    public function board(Request $request): RedirectResponse
    {
        return $this->handleBoardingRedirect($request->store());
    }

    /**
     * Handles returning from completing the onboarding process.
     *
     * @param Request $request
     * @return RedirectResponse
     */
    public function returning(Request $request): RedirectResponse
    {
        return $this->handleBoardingRedirect($request->store());
    }

    /**
     * Handles refreshing of onboarding process.
     *
     * @param Request $request
     * @return RedirectResponse
     */
    public function refresh(Request $request): RedirectResponse
    {
        return $this->handleBoardingRedirect($request->store());
    }

    /**
     * Handles the redirection logic of Stripe onboarding for the given store. Will 
     * create account and redirect store to onboarding process or redirect to account 
     * dashboard if they have already completed the process.
     *
     * @param Store $Store
     * @return RedirectResponse
     */
    private function handleBoardingRedirect(Store $Store): RedirectResponse
    {
        // Redirect to dashboard if onboarding is already completed.
        if ($store->hasStripeAccount() && $store->hasCompletedOnboarding()) {
            return $store->redirectToAccountDashboard();
        }

        // Delete account if already exists and create new express account with 
        // weekly payouts.
        $store->deleteAndCreateStripeAccount('express', [
            'settings' => [
                'payouts' => [ 
                    'schedule' => [ 
                        'interval' => 'weekly', 
                        'weekly_anchor' => 'friday',
                    ]
                ]
            ]
        ]);
        
        // If you wish to acceess the new connected account via the related model
        // you must refresh the model using the refresh() model method.
        $store->refresh();

        // Redirect to Stripe account onboarding, with return and refresh url, otherwise.
        return $store->redirectToAccountOnboarding(
            URL::to('/api/stripe/return?api_token=' . $store->api_token),
            URL::to('/api/stripe/refresh?api_token=' . $store->api_token)
        );
    }

}
```

### JSON API Approach

If your application is powered by a JSON API and your redirects will be happening on a separate SPA Frontend, then this approach is the one to use.

```php
public function generateOnboardingURL(Request $request, $id){

    $validated = $request->validate([
            "returnURL" => ['required'],
            "refreshURL" => ['required']
        ]);

    $store = Store::find($id);

    return response()->json((object)[
        "Url" => $store->accountOnboardingUrl($validated['returnURL']), $validated['refreshURL'])
    ]);

}
```

You would then hit this endpoint from your frontend or mobile app and direct the user to URL returned by the endpoint.
