mehedi-iitdu/laravel-paystack

此包已被弃用且不再维护。作者建议使用 https://github.com/unicodeveloper/laravel-paystack 包代替。

Laravel 5 的 Paystack 包

1.0.2 2016-12-24 08:05 UTC

This package is auto-updated.

Last update: 2021-09-07 08:21:21 UTC


README

Latest Stable Version License Build Status Quality Score Total Downloads

用于无缝工作于 Paystack 的 Laravel 5 包

安装

需要 PHP 5.4+ 或 HHVM 3.3+ 以及 Composer。

要获取 Laravel Paystack 的最新版本,只需要求它即可

composer require unicodeveloper/laravel-paystack

或者将以下行添加到您的 composer.json 文件的 require 块中。

"unicodeveloper/laravel-paystack": "1.0.*"

然后您需要运行 composer installcomposer update 来下载它并更新自动加载器。

一旦安装了 Laravel Paystack,您需要注册服务提供者。打开 config/app.php 并将以下内容添加到 providers 键中。

如果您使用 Laravel >= 5.5,则可以跳过此步骤并转到 配置

  • Mehedi\Paystack\PaystackServiceProvider::class

此外,还可以这样注册 Facade

'aliases' => [
    ...
    'Paystack' => Mehedi\Paystack\Facades\Paystack::class,
    ...
]

配置

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

php artisan vendor:publish --provider="Mehedi\Paystack\PaystackServiceProvider"

将一个名为 paystack.php 的配置文件(包含一些合理的默认值)放置在您的 config 目录中

<?php

return [

    /**
     * Public Key From Paystack Dashboard
     *
     */
    'publicKey' => getenv('PAYSTACK_PUBLIC_KEY'),

    /**
     * Secret Key From Paystack Dashboard
     *
     */
    'secretKey' => getenv('PAYSTACK_SECRET_KEY'),

    /**
     * Paystack Payment URL
     *
     */
    'paymentUrl' => getenv('PAYSTACK_PAYMENT_URL'),

    /**
     * Optional email address of the merchant
     *
     */
    'merchantEmail' => getenv('MERCHANT_EMAIL'),

];

## 一般支付流程

尽管有多种支付订单的方法,但大多数支付网关都希望你在结账过程中遵循以下流程

### 1. 客户被重定向到支付提供商网站 客户完成结账过程并准备付款后,客户必须被重定向到支付提供商的网站。

通过提交包含一些隐藏字段的表单来完成重定向。表单必须提交到支付提供商的网站。隐藏字段至少指定了必须支付的金额、订单 ID 和哈希。

哈希是通过隐藏表单字段和非公开密钥计算得出的。支付提供商使用此哈希来验证请求是否有效。

### 2. 客户在支付提供商网站上付款 客户到达支付提供商的网站,可以选择支付方式。所有必要的支付步骤都由支付提供商处理。

### 3. 客户被重定向回网站 客户支付订单后,将被重定向回网站。在重定向请求到商店网站时,会返回一些值。这些值通常是订单 ID、支付结果和哈希。

哈希是通过返回字段和一些非公开密钥计算得出的。此哈希用于验证请求是否有效且来自支付提供商。确保彻底检查此哈希至关重要。

用法

打开您的 .env 文件,并添加您的公钥、密钥、商家电子邮件和支付 URL,如下所示

PAYSTACK_PUBLIC_KEY=xxxxxxxxxxxxx
PAYSTACK_SECRET_KEY=xxxxxxxxxxxxx
PAYSTACK_PAYMENT_URL=https://api.paystack.co
MERCHANT_EMAIL=unicodeveloper@gmail.com

按照以下方式设置路由和控制器方法

注意:请确保您已在 Paystack 控制台(https://dashboard.paystack.co/#/settings/developer)中注册了 /payment/callback,如下所示

payment-callback

// Laravel 5.1.17 and above
Route::post('/pay', 'PaymentController@redirectToGateway')->name('pay'); 

或者

Route::post('/pay', [
    'uses' => 'PaymentController@redirectToGateway',
    'as' => 'pay'
]);
Route::get('/payment/callback', 'PaymentController@handleGatewayCallback');

或者

// Laravel 5.0
Route::get('payment/callback', [
    'uses' => 'PaymentController@handleGatewayCallback'
]); 
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Paystack;

class PaymentController extends Controller
{

    /**
     * Redirect the User to Paystack Payment Page
     * @return Url
     */
    public function redirectToGateway()
    {
        return Paystack::getAuthorizationUrl()->redirectNow();
    }

    /**
     * Obtain Paystack payment information
     * @return void
     */
    public function handleGatewayCallback()
    {
        $paymentDetails = Paystack::getPaymentData();

        dd($paymentDetails);
        // Now you have the payment details,
        // you can store the authorization_code in your db to allow for recurrent subscriptions
        // you can then redirect or do whatever you want
    }
}

让我简单解释一下这个包提供的流畅方法。

/**
 *  This fluent method does all the dirty work of sending a POST request with the form data
 *  to Paystack Api, then it gets the authorization Url and redirects the user to Paystack
 *  Payment Page. I abstracted all of it, so you don't have to worry about that.
 *  Just eat your cookies while coding!
 */
Paystack::getAuthorizationUrl()->redirectNow();

/**
 * This fluent method does all the dirty work of verifying that the just concluded transaction was actually valid,
 * It verifies the transaction reference with Paystack Api and then grabs the data returned from Paystack.
 * In that data, we have a lot of good stuff, especially the `authorization_code` that you can save in your db
 * to allow for easy recurrent subscription.
 */
Paystack::getPaymentData();

/**
 * This method gets all the customers that have performed transactions on your platform with Paystack
 * @returns array
 */
Paystack::getAllCustomers();

/**
 * This method gets all the plans that you have registered on Paystack
 * @returns array
 */
Paystack::getAllPlans();

/**
 * This method gets all the transactions that have occurred
 * @returns array
 */
Paystack::getAllTransactions();

/**
 * This method generates a unique super secure cryptograhical hash token to use as transaction reference
 * @returns string
 */
Paystack::genTranxRef();

/**
* This method creates a subaccount to be used for split payments 
* @return array
*/
Paystack::createSubAccount();


/**
* This method fetches the details of a subaccount  
* @return array
*/
Paystack::fetchSubAccount();


/**
* This method lists the subaccounts associated with your paystack account 
* @return array
*/
Paystack::listSubAccounts();

/**
* This method Updates a subaccount to be used for split payments 
* @return array
*/
Paystack::updateSubAccount();

一个示例表单看起来像这样

<form method="POST" action="{{ route('pay') }}" accept-charset="UTF-8" class="form-horizontal" role="form">
        <div class="row" style="margin-bottom:40px;">
          <div class="col-md-8 col-md-offset-2">
            <p>
                <div>
                    Lagos Eyo Print Tee Shirt
                    ₦ 2,950
                </div>
            </p>
            <input type="hidden" name="email" value="otemuyiwa@gmail.com"> {{-- required --}}
            <input type="hidden" name="orderID" value="345">
            <input type="hidden" name="amount" value="800"> {{-- required in kobo --}}
            <input type="hidden" name="quantity" value="3">
            <input type="hidden" name="metadata" value="{{ json_encode($array = ['key_name' => 'value',]) }}" > {{-- For other necessary things you want to add to your payload. it is optional though --}}
            <input type="hidden" name="reference" value="{{ Paystack::genTranxRef() }}"> {{-- required --}}
            <input type="hidden" name="key" value="{{ config('paystack.secretKey') }}"> {{-- required --}}
            {{ csrf_field() }} {{-- works only when using laravel 5.1, 5.2 --}}

             <input type="hidden" name="_token" value="{{ csrf_token() }}"> {{-- employ this in place of csrf_field only in laravel 5.0 --}}


            <p>
              <button class="btn btn-success btn-lg btn-block" type="submit" value="Pay Now!">
              <i class="fa fa-plus-circle fa-lg"></i> Pay Now!
              </button>
            </p>
          </div>
        </div>
</form>

当点击提交按钮时,客户将被重定向到 Paystack 网站。

因此,现在我们已经将客户重定向到 Paystack。客户在那里执行了一些操作(希望他们已经支付了订单)并且现在被重定向回我们的商店网站。

Paystack 将将客户重定向到 Paystack 控制台 Web Hooks 部分的回调 URL 中指定的路由的 URL。

我们必须验证重定向到我们网站的是否是一个有效的请求(我们不想有人恶意放置未支付的订单)。

在处理来自支付提供者的请求的控制器中,我们有

Paystack::getPaymentData() - 这个函数调用验证方法并确保它是一个有效的交易,否则它将抛出一个异常。

您可以使用以下详细信息进行测试

Card Number: 4123450131001381
Expiry Date: any date in the future
CVV: 883

待办事项

  • 向回头客收费
  • 添加全面的测试
  • 实现交易仪表板以查看您的 Laravel 应用程序中的所有交易

贡献

请随意fork此包,并通过提交拉取请求来增强功能进行贡献。

如何感谢我?

为何不在 GitHub 仓库上 star?我渴望得到关注!为何不在 Twitter 或 HackerNews 上分享此存储库的链接?传播一下!

别忘了在推特上关注我

谢谢!Prosper Otemuyiwa。

许可

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