Best laravel framework open-source packages.

Payfast onsite subscriptions

A Payfast Onsite Payments implementation for Laravel designed to ease subscription billing. Livewire views included.
Updated 8 months ago

About PayFast Onsite Subscriptions

GitHub release (latest by date) Tests GitHub

A PayFast Onsite Payments implementation for Laravel designed to ease subscription billing. Livewire views are included.

Requirements:

If you want to use Laravel Nova, version 4 is required for the Subscription and Receipt resources.

Installation

Install the package via composer:

composer require fintech-systems/payfast-onsite-subscriptions

Publish Configuration and Views

Publish the config file with:

php artisan vendor:publish --provider="FintechSystems\PayFast\PayFastServiceProvider" --tag="config"

Publish the Success and Cancelled views and the Livewire components for subscriptions and receipts.

php artisan vendor:publish --provider="FintechSystems\PayFast\PayFastServiceProvider" --tag="views"

Nova Integration

Optionally publish Laravel Nova Subscription and Receipts Resources and Actions

php artisan vendor:publish --provider="FintechSystems\PayFast\PayFastServiceProvider" --tag="nova-resources"

Migrations

A migration is needed to create Customers, Orders, Receipts and Subscriptions tables:

php artisan migrate

Example Configuration

config/payfast.php:

<?php

return [
    'merchant_id' => env('PAYFAST_MERCHANT_ID', '10004002'),
    'merchant_key' => env('PAYFAST_MERCHANT_KEY', 'q1cd2rdny4a53'),
    'passphrase' => env('PAYFAST_PASSPHRASE', 'payfast'),
    'testmode' => env('PAYFAST_TESTMODE', true),        
    'return_url' => env('PAYFAST_RETURN_URL', config('app.url') . '/payfast/return'),
    'cancel_url' => env('PAYFAST_CANCEL_URL', config('app.url') . '/payfast/cancel'),
    'notify_url' => env('PAYFAST_NOTIFY_URL', config('app.url') . '/payfast/notify'),
    'card_update_link_css' => env('CARD_UPDATE_LINK_CSS', 'inline-flex items-center px-4 py-2 bg-gray-800 border border-transparent rounded-md font-semibold text-xs text-white uppercase tracking-widest hover:bg-gray-700 active:bg-gray-900 focus:outline-none focus:border-gray-900 focus:ring focus:ring-gray-300 disabled:opacity-25 transition'),
    'card_updated_return_url' => env('CARD_UPDATED_RETURN_URL', config('app.url') . '/user/profile'),
    'plans' => [
        3 => [
            'name' => 'Monthly R 99',
            'start_date' => \Carbon\Carbon::now()->addDay()->format('Y-m-d'),
            'payfast_frequency' => 3,
            'initial_amount' => 5.99,
            'recurring_amount' => 5.99,
        ],
        6 => [
            'name' => 'Yearly R 1089',
            'start_date' => \Carbon\Carbon::now()->format('Y-m-d'),
            'payfast_frequency' => 6,
            'initial_amount' => 6.89,
            'recurring_amount' => 6.89,
        ]
    ],
    'cancelation_reasons' => [
        'Too expensive',
        'Lacks features',
        'Not what I expected',
    ],
];

Livewire setup

Views

The Livewire views are modelled to blend into a Laravel Jetstream user profile page.

Adding a billing menu

In app.blade.php below in the Account Management sections (e.g. below profile):

<x-dropdown-link href="/user/billing">
    Billing
</x-dropdown-link>

Also look for the responsive part and add this:

<x-responsive-nav-link href="/user/billing" :active="request()->routeIs('profile.billing')">
    Billing
</x-responsive-nav-link>

Adding the subscriptions and receipts views

When calling the Livewire component, you can override any PayFast form field by specifying a mergeFields array.

Example modification Jetstream Livewire's resources/views/profiles/show.php:

Replace $user->name with your first name and last name fields.

<!-- Subscriptions -->
<div class="mt-10 sm:mt-0">    
    @livewire('subscriptions', ['mergeFields' => [
            'name_first' => $user->name,
            'name_last' => $user->name,
            'item_description' => 'Subscription to Online Service'
        ]] )        
</div>

<x-section-border />
<!-- End Subscriptions -->

<!-- Receipts -->
    <div class="mt-10 sm:mt-0">
        @livewire('receipts')
    </div>

<x-section-border />
<!-- End Receipts -->

Usage

Examples

  • Generate a payment link
  • Create an ad-hoc token optionally specifying the amount
  • Cancel a subscription
  • Update a card
use FintechSystems\PayFast\Facades\PayFast;

Route::get('/payment', function() {
    return PayFast::payment(5,'Order #1');
});

Route::get('/cancel-subscription', function() {
    return PayFast::cancelSubscription('73d2a218-695e-4bb5-9f62-383e53bef68f');
});

Route::get('/create-subscription', function() {
    return PayFast::createSubscription(
        Carbon::now()->addDay()->format('Y-m-d'),
        5, // Amount
        6 // Frequency (6 = annual, 3 = monthly)
    );
});

Route::get('/create-adhoc-token', function() {
    return PayFast::createAdhocToken(5);
});

Route::get('/fetch-subscription', function() {
    return PayFast::fetchSubscription('21189d52-12eb-4108-9c0e-53343c7ac692');
});

Route::get('/update-card', function() {
    return PayFast::updateCardLink('40ab3194-20f0-4814-8c89-4d2a6b5462ed');
});

Testing

vendor/bin/phpunit

In your main project add this:

"repositories": [
        {
            "type": "path",
            "url": "../payfast-onsite-subscriptions"
        }
],

Then do this to symlink the library:

composer require fintechsystems/payfast-onsite-subscriptions:dev-main

If you want to test trials, use this one liner to activate a billable user and a trial using Tinker:

$user = User::find(x)->createAsCustomer(['trial_ends_at' => now()->addDays(30)]);

Changelog

Please see CHANGELOG for more information on what has changed recently.

Screenshots

Livewire Subscriptions and Receipts Components

Credits

License

The MIT License (MIT). Please see License File for more information.