Create a donation website using Laravel and Yabacon Paystack

Write code for a donation website using Laravel yabacon paystack

To create a donation website using Laravel and Yabacon Paystack, you will need to perform the following steps:

  1. Install Laravel and set up a new project:
Copy codecomposer create-project --prefer-dist laravel/laravel donation-website
  1. Install the Paystack Laravel package:
Copy codecomposer require yabacon/paystack-laravel
  1. Set up your Paystack API keys in your .env file:
Copy codePAYSTACK_SECRET_KEY=sk_test_your_secret_key
PAYSTACK_PUBLIC_KEY=pk_test_your_public_key
  1. In your config/services.php file, add the following lines to register the Paystack service:
Copy code'paystack' => [
    'secret_key' => env('PAYSTACK_SECRET_KEY'),
    'public_key' => env('PAYSTACK_PUBLIC_KEY'),
],
  1. Create a route and a controller to handle the donation form submission:
Copy codeRoute::post('/donate', 'DonationController@store')->name('donate');
Copy code<?php

namespace App\Http\Controllers;

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

class DonationController extends Controller
{
    public function store(Request $request)
    {
        // Validate the form data
        $request->validate([
            'name' => 'required',
            'email' => 'required|email',
            'amount' => 'required|numeric',
        ]);

        // Initialize the Paystack API client
        $paystack = new Paystack();

        // Set up the transaction data
        $transactionData = [
            'amount' => $request->amount * 100, // Paystack requires amounts in kobo
            'email' => $request->email,
            'metadata' => [
                'name' => $request->name,
            ],
        ];

        // Initialize the transaction
        $transaction = $paystack->transaction->initialize($transactionData);

        // Check if the transaction was successful
        if ($transaction->status) {
            // Redirect the user to the payment page
            return redirect($transaction->data->authorization_url);
        } else {
            // There was an error initializing the transaction
            // You can add error handling here
            return redirect()->back()->withErrors([$transaction->message]);
        }
    }
}
  1. Create a view for the donation form:
Copy code<form action="{{ route('donate') }}" method="post">
    @csrf
    <label for="name">Name:</label>
    <input type="text" name="name" id="name" required>
    <br>
    <label for="email">Email:</label>
    <input type="email" name="email" id="email" required>
    <br>
    <label for="amount">Amount:</label>
    <input type="number" name="amount" id="

Complete the code

To complete the code for the donation website, you will need to create a route and a controller action to handle the payment callback from Paystack, as well as a view to display a success or failure message to the user.

First, create a route and a controller action to handle the payment callback:

Copy codeRoute::get('/donate/callback', 'DonationController@callback')->name('donate.callback');

Copy codepublic function callback(Request $request)
{
    // Initialize the Paystack API client
    $paystack = new Paystack();

    // Get the reference from the request
    $reference = $request->get('reference');

    // Verify the transaction
    $transaction = $paystack->transaction->verify([
        'reference' => $reference,
    ]);

    // Check if the transaction was successful
    if ($transaction->status) {
        // Transaction was successful
        // You can save the transaction details in your database here

        // Redirect the user to the success page
        return view('donate.success');
    } else {
        // Transaction failed
        // You can add error handling here

        // Redirect the user to the failure page
        return view('donate.failure');
    }
}

Next, create the success and failure views:

Copy code<!-- donate/success.blade.php -->
<h1>Donation Successful</h1>
<p>Thank you for your donation!</p>

<!-- donate/failure.blade.php -->
<h1>Donation Failed</h1>
<p>There was an error processing your donation. Please try again later.</p>

That’s it! You should now have a working donation website using Laravel and Paystack.

Write code for saving the transaction details in the payment callback

To save the transaction details in the payment callback, you will need to store the data in a database. Here is an example of how you can save the transaction details in a MySQL database using Laravel’s Eloquent ORM:

Copy codepublic function callback(Request $request)
{
    // Initialize the Paystack API client
    $paystack = new Paystack();

    // Get the reference from the request
    $reference = $request->get('reference');

    // Verify the transaction
    $transaction = $paystack->transaction->verify([
        'reference' => $reference,
    ]);

    // Check if the transaction was successful
    if ($transaction->status) {
        // Transaction was successful
        // Save the transaction details in the database

        $data = $transaction->data;

        // Create a new donation record
        $donation = new Donation([
            'name' => $data->metadata->name,
            'email' => $data->customer->email,
            'amount' => $data->amount / 100, // Convert amount back to NGN
            'reference' => $data->reference,
            'status' => $data->status,
        ]);

        // Save the donation to the database
        $donation->save();

        // Redirect the user to the success page
        return view('donate.success');
    } else {
        // Transaction failed
        // You can add error handling here

        // Redirect the user to the failure page
        return view('donate.failure');
    }
}

In this example, Donation is a model that represents a donation record in the database. You will need to create this model and set up the database connection in your Laravel project to be able to save the transaction details.

Here is an example of the Donation model:

Copy code<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Donation extends Model
{
    protected $fillable = [
        'name', 'email', 'amount', 'reference', 'status',
    ];
}

You will also need to create a migrations file to create the donations table in the database:

Copy codeuse Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateDonationsTable extends Migration
{
    public function up()
    {
        Schema::create('donations', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email');
            $table->decimal('amount', 10, 2);
            $table->string('reference');
            $table->string('status');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('donations');
    }
}

To run the migration and create the donations table, run the following command:

Copy codephp artisan migrate

With these changes, the transaction details will be saved in the donations table in the database every time a successful donation is made.

Complete the code for the donation form

To complete the code for the donation form, you will need to create a view file that contains the HTML form and any necessary form validation or error handling. Here is an example of what the donation form view file could look like:

Copy code<!-- donate/form.blade.php -->

<h1>Make a Donation</h1>

@if ($errors->any())
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

<form action="{{ route('donate') }}" method="post">
    @csrf
    <div class="form-group">
        <label for="name">Name:</label>
        <input type="text" name="name" id="name" class="form-control" required>
    </div>
    <div class="form-group">
        <label for="email">Email:</label>
        <input type="email" name="email" id="email" class="form-control" required>
    </div>
    <div class="form-group">
        <label for="amount">Amount:</label>
        <input type="number" name="amount" id="amount" class="form-control" required>
    </div>
    <button type="submit" class="btn btn-primary">Donate</button>
</form>

To display the form in a view, you can use Laravel’s blade syntax to include the form view file:

Copy code<!-- donate/index.blade.php -->

@extends('layouts.app')

@section('content')
    @include('donate.form')
@endsection

This will display the donation form on the donate/index route. You can customize the form and the layout as needed.

Leave a comment