Creating subscription form using Laravel and yabacon paystack

Open sidebar

onoja.stanleyugo@gmail.com

Create subscription form using Laravel and yabacon paystack

To create a subscription form using Laravel and Yabacon Paystack, follow these steps:

  1. Install the Laravel framework by running the command: composer create-project –prefer-dist laravel/laravel subscription-form
  2. Install the Paystack package by running the command: composer require yabacon/paystack-php
  3. In the .env file, add your Paystack API keys:

PAYSTACK_SECRET_KEY=your_secret_key PAYSTACK_PUBLIC_KEY=your_public_key

  1. In the config/paystack.php file, add the following:

‘public_key’ => env(‘PAYSTACK_PUBLIC_KEY’), ‘secret_key’ => env(‘PAYSTACK_SECRET_KEY’),

  1. In the routes/web.php file, create a route for the subscription form and a route for the subscription callback:

Route::get(‘/subscription-form’, ‘SubscriptionController@showForm’); Route::post(‘/subscription-form’, ‘SubscriptionController@processSubscription’); Route::post(‘/subscription-callback’, ‘SubscriptionController@subscriptionCallback’);

  1. In the app/Http/Controllers directory, create a SubscriptionController.php file and add the following code:

namespace App\Http\Controllers;

use Illuminate\Http\Request; use Yabacon\Paystack;

class SubscriptionController extends Controller { public function showForm() { return view(‘subscription-form’); }

Copy codepublic function processSubscription(Request $request)
{
    $paystack = new Paystack();

    try {
        $subscription = $paystack->subscription()->create([
            'customer' => $request->customer_email,
            'plan' => $request->plan_id,
        ]);

        if($subscription->status) {
            // subscription successful, redirect to callback route
            return redirect()->route('subscription-callback', ['reference' => $subscription->data->id]);
        } else {
            // subscription failed, display error message
            return back()->with('error', $subscription->message);
        }
    } catch (\Exception $e) {
        return back()->with('error', $e->getMessage());
    }
}

public function subscriptionCallback(Request $request)
{
    $paystack = new Paystack();

    try {
        $subscription = $paystack->subscription()->verify($request->reference);

        if($subscription->status) {
            // subscription verified, redirect to success page
            return redirect()->route('subscription-success');
        } else {
            // subscription verification failed, display error message
            return back()->with('error', $subscription->message);
        }
    } catch (\Exception $e) {
        return back()->with('error', $e->getMessage());
    }
}

}

  1. In the resources/views directory, create a subscription-form.blade.php file and add the following code:

<!DOCTYPE html> <html> <head> <title>Subscription Form</title> </head> <

2 / 2

onoja.stanleyugo@gmail.com

Complete the blade files

<body> @if ($errors->any()) <div> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <form action=”{{ route(‘subscription-form’) }}” method=”POST”> @csrf <label for=”customer_email”>Email:</label> <input type=”email” name=”customer_email” required> <br> <label for=”plan_id”>Plan:</label> <select name=”plan_id” required> <option value=”plan_abc123″>Plan ABC123</option> <option value=”plan_def456″>Plan DEF456</option> <option value=”plan_ghi789″>Plan GHI789</option> </select> <br> <input type=”submit” value=”Subscribe”> </form> </body> </html>

  1. In the resources/views directory, create a subscription-success.blade.php file and add the following code:

<!DOCTYPE html> <html> <head> <title>Subscription Success</title> </head> <body> <p>Your subscription was successful!</p> </body> </html>

onoja.stanleyugo@gmail.com

Continue

  1. In the routes/web.php file, add a route for the subscription success page:

Route::get(‘/subscription-success’, ‘SubscriptionController@showSuccessPage’);

  1. In the app/Http/Controllers/SubscriptionController.php file, add the following method:

public function showSuccessPage() { return view(‘subscription-success’); }

  1. Run the Laravel development server by running the command: php artisan serve
  2. Open your web browser and go to http://localhost:8000/subscription-form to access the subscription form. Fill in the form and submit it to complete the subscription process.

Leave a comment