fintechsystems/payfast-onsite-subscriptions

Payfast订阅计费服务的Laravel服务提供者和Livewire blades

v2.1 2023-11-19 09:15 UTC

README

GitHub release (latest by date) Tests GitHub

Laravel的Payfast在线支付实现,旨在简化订阅计费。包含Livewire视图。

要求

  • PHP 8.1
  • Laravel 9.x或更高版本
  • Payfast沙盒账户
  • Payfast账户

如果您想使用Laravel Nova,则需要版本4的SubscriptionReceipt资源。

安装

通过composer安装包

composer require fintech-systems/payfast-onsite-subscriptions

发布配置和视图

使用以下命令发布配置文件

php artisan vendor:publish --provider="FintechSystems\Payfast\PayfastServiceProvider" --tag="config"

发布成功和取消视图以及订阅和收据的Livewire组件。

php artisan vendor:publish --provider="FintechSystems\Payfast\PayfastServiceProvider" --tag="views"

这些文件是

banner.blade.php
billing.blade.php
cancel.blade.php
pricing.blade.php
receipts.blade.php
subscriptions.blade.php
success.blade.php

要在页面上包含定价组件,执行以下操作

在您的页眉中

@vite(['resources/css/app.css', 'resources/js/app.js'])

在您的视图中

@include('payfast::components.pricing')

最终您将得到一个如下所示的页面:定价组件

Nova集成

可选发布Laravel Nova订阅和收据资源及操作

php artisan vendor:publish --provider="FintechSystems\Payfast\PayfastServiceProvider" --tag="nova-resources"

迁移

需要迁移来创建客户、订单、收据和订阅表

php artisan migrate

示例配置

config/payfast.php:

<?php

return [
    'merchant_id' => env('PAYFAST_MERCHANT_ID', '10004002'),
    'merchant_key' => env('PAYFAST_MERCHANT_KEY', 'q1cd2rdny4a53'),
    'passphrase' => env('PAYFAST_PASSPHRASE', 'payfast'),
    'test_mode' => env('PAYFAST_TEST_MODE', 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设置

视图

Livewire视图设计为与Laravel Jetstream用户个人资料页面融合。

添加计费菜单

在下面的app.blade.php中的账户管理部分(例如,在个人资料下方)

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

也查找响应部分并添加以下内容

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

添加订阅和收据视图

调用Livewire组件时,您可以通过指定一个mergeFields数组来覆盖任何PayFast表单字段

示例修改Jetstream Livewire的resources/views/profiles/show.php

$user->name替换为您的名字和姓氏字段。

<!-- 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 -->

使用

示例

  • 生成支付链接
  • 创建一个可选指定金额的临时令牌
  • 取消订阅
  • 更新卡片
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');
});

测试

如何确定用户的订阅何时结束

$user->subscription('default')->ends_at = [过去日期]

vendor/bin/phpunit

在您的主要项目中添加以下内容

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

然后这样做来创建库的符号链接

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

如果您想测试试用,请使用以下单行命令激活一个可收费用户和一个试用,使用Tinker

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

要检查用户是否在试用中(如订阅 blades中使用),请这样做


变更日志

请参阅CHANGELOG以获取有关最近更改的更多信息。

屏幕截图

Livewire Subscriptions and Receipts Components

鸣谢

许可

MIT许可(MIT)。请参阅许可文件以获取更多信息。