Search Here

Laravel Paypal Donation Payment Form Integration with Deploying to Production

Laravel Paypal Donation Payment Form Integration with Deploying to Production

In this post, you’ll learn to take donation payments from the Paypal payment gateway using the Laravel Framework. The reason why you have to use the donation form for taking donations is that there will be no shipping details that need to be given by the donor while donating.

Paypal provides a simple interface for creating a donation form and that is what discussed in this post of how you can create a donation form of PayPal using HTML and submit it to the PayPal server and get a response from PayPal.

Table of Contents

Creating a Sandbox Account

Steps to create a Sandbox account for testing the Paypal Payment Gateway.

Step 1:

Go topaypal.com sign in into your account.

Step 2:

Open Paypal Developers Site and goto Sandbox Test Accounts.

Sandbox accounts for paypal

Sandbox accounts for PayPal

Step 3:

Click on the Create button a modal model pops up and there you’ll have two options of account type select Business (Merchant Account) and then region you like. I’ll go with the United States of America.

Creating Sandbox business account for paypal

Creating a Sandbox business account for PayPal

All the sandbox accounts are displayed under the Create Account button.

List of all the Sandbox account created including personal and business accounts

List of all the Sandbox account created including personal and business accounts

Creating a Donation Form in HTML

In resources\views\donation\form.blade.php.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Paypal Donation Form</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</head>
<body>
    <div class="jumbotron">
        <form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
            @csrf
            
            <div class="row">
                
                <div class="col-md-4 offset-md-4">
                    <h4 class="text-center" >Paypal Donation Form</h4>
                </div>

                <div class="col-md-4 offset-md-4">
                    <label for="">CMD</label>
                    <input type="text" class="form-control" name="cmd" value="_donations" />

                    <label for="">business</label>
                    <input type="text" class="form-control" name="business" value="sb-cut488888@business.example.com" />

                    <label for="">Item Name</label>
                    <input type="text" class="form-control" name="item_name" value="Donation" />

                    <label for="">Currency Code</label>
                    <input type="text" class="form-control" name="currency_code" value="USD" />
                    
                    <label for="">Notify URL</label>
                    <input type='text' class="form-control" name='notify_url' value='{{ route("donation.notify") }}'>

                    <label for="">Cancel Return URL</label>
                    <input type='text' class="form-control" name='cancel_return' value='{{ route("donation.cancelled") }}'>

                    <label for="">Return</label>
                    <input type='text' class="form-control" name='return' value='{{ route("donation.success") }}'>

                    <label for="">Custom Text Field</label>
                    <textarea class="form-control" name="custom"></textarea>

                </div>

                <div class="col-md-4 offset-md-4">
                    <label for="">Donation Amount</label>
                    <input type="number" name="amount" class="form-control" value="100" >
                </div>

                <div class="col-md-4 offset-md-4">
                    <br>
                    <input type="submit" class="btn btn-info" value="Donation" >
                </div>
            </div>
        </form>
    </div>
</body>
</html>    

Request Body

  • Post URL: https://www.sandbox.paypal.com/cgi-bin/webscr for sandbox testing and for production use https://www.paypal.com/cgi-bin/webscr.
  • business: Account name which is the email ID of the Business account.
  • item_name: The name of the item, in this case, it is donation
  • currency_code: Currency code specifies in which currency the customer or donor must pay.
  • notify_url: Notification of Payment
  • cancel_return: The redirect URL if the payment was canceled.
  • return: The redirect URL if the payment was successfully executed. This will return data from PayPal to our application.
  • custom: A text field for receiving any extra data when sent from the application.
  • amount: The amount to be charged to the customer or donor.

Getting data after success or failure of payment

Let’s prepare our controllers and routes for receiving information related to payments from PayPal.

In app\Http\Controllers\DonationController.php.

<?php
namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Utils\Drive;
use Illuminate\Http\Request;
use DirectoryIterator;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use ZipArchive;

class DonationController extends Controller{

    public function donationForm( Request $request ){
        $info = [];
        return view( 'donation.form', $info );
    }

    public function donationSuccess( Request $request ){
        dump( "Donation Success" );
        dd( $request->all() );
    }

    public function donationCancelled( Request $request ){
        dump( "Donation Cancelled" );
        dd( $request->all() );
    }

    public function donationNotify( Request $request ){
        dump( "Donation Notify" );
        dd( $request->all() );
    }
}

Mapping DonationController methods with web routes

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\DonationController;

// Paypal Donation Form
Route::get( 'donation-form',  [ DonationController::class, 'donationForm' ] );
Route::get( 'donation/success',  [ DonationController::class, 'donationSuccess' ] )->name('donation.success');
Route::get( 'donation/cancelled',  [ DonationController::class, 'donationCancelled' ] )->name('donation.cancelled');
Route::get( 'donation/notify_url',  [ DonationController::class, 'donationNotify' ] )->name('donation.notify');

Output

Paypal donation form

Paypal donation choosing payment options via PayPal or card payment

Donation checkout page for card payment

Donation checkout page for card payment

Page after successful donation of payment

Page after successful donation of payment

Paypal response after successful payment of donations.

Paypal response after successful payment of donations.

Moving to Production Environment

For moving to production mode and to receive real payments replace the <form action=”https://www.sandbox.paypal.com/cgi-bin/webscr” method=”post”></form> with <form action=”https://www.paypal.com/cgi-bin/webscr” method=”post”></form> you just need to remove the sandbox.
Apart from this, you need to do one more step i.e within form field business add your original email to which you want to receive the donation amount.
Replace

<input type="text" class="form-control" name="business" value="sb-cut43i3706853@business.example.com" />

With

<input type="text" class="form-control" name="business" value="your email address used for paypal original not sandbox email" />

By doing the above steps you can make your PayPal payment gateway to accept real payments.

Conclusion

That’s all for this post on Laravel Paypal Donation Payment Form Integration with Deploying to Production and if you like our content do visit regularly and for any queries comment on this post.

Related Posts

Summary
Review Date
Reviewed Item
Laravel Paypal Donation Payment Form Integration with Deploying to Production
Author Rating
51star1star1star1star1star
Software Name
Laravel Framework
Software Name
Windows Os, Mac Os, Ubuntu Os
Software Category
Web Development