sharifur/payfast

Laravel 10+ 包,用于通过 payfast.co.za 处理 ITN 支付

v2.0.3 2024-09-18 11:03 UTC

This package is auto-updated.

Last update: 2024-09-22 12:00:26 UTC


README

这是一个非常简单的 Laravel 支付处理类,用于通过 payfast.co.za 进行支付。此包仅支持 ITN 交易。使用 Laravel Payfast 需自行承担风险。

重要通知

这是 billowapp/payfast 包的克隆版本,这个包已经很久没有更新了,所以我创建了一个分支,为 Laravel 9 提供支持,并在 Packagist 上发布,以便我可以在我的私有包中使用它。所有功劳归原作者 "Warren Hansen"。

安装

将 Laravel Payfast 添加到 composer.json 中

composer require sharifur/payfast

配置

发布默认配置文件。

php artisan vendor:publish

重要:您需要编辑 App\Http\Middleware\VerifyCsrfToken,将处理 ITN 响应的路由添加到 $excepted 数组中。验证通过 ITN 响应完成。

/*
|--------------------------------------------------------------------------
| Merchant Settings
|--------------------------------------------------------------------------
| All Merchant settings below are for example purposes only. for more info
| see www.payfast.co.za. The Merchant ID and Merchant Key can be obtained
| from your payfast.co.za account.
|
*/

[

    'testing' => true, // Set to false when in production.

    'currency' => 'ZAR', // ZAR is the only supported currency at this point.

    'merchant' => [
        'merchant_id' => env('PF_MERCHANT_ID', '10000100'), // TEST Credentials. Replace with your merchant ID from Payfast.
        'merchant_key' => env('PF_MERCHANT_KEY', '46f0cd694581a'), // TEST Credentials. Replace with your merchant key from Payfast.
        'return_url' => env('PF_RETURN_URL', 'http://your-domain.co.za/success'), // Redirect URL on Success.
        'cancel_url' => env('PF_CANCEL_URL', 'http://your-domain.co.za/cancel'), // Redirect URL on Cancellation.
        'notify_url' => env('PF_ITN_URL', 'http://your-domain.co.za/itn'), // ITN URL.
    ],

];

用法

创建支付会返回一个准备好的 HTML 表单,可以直接 POST 到 payfast。当客户提交表单时,他们将被重定向到 payfast 完成支付。支付成功后,客户将被返回到指定的 'return_url',如果取消支付,他们将返回到指定的 'cancel_url'。

注意

如果您想使用订阅,请确保在配置文件中设置您的商户密码。这对于订阅是必需的。

use Billow\Contracts\PaymentProcessor;

Class PaymentController extends Controller
{

    public function confirmPayment(PaymentProcessor $payfast)
    {
        // Eloqunet example.
        $cartTotal = 9999;
        $order = Order::create([
            'm_payment_id' => '001', // A unique reference for the order.
            'amount'       => $cartTotal
        ]);

        // Build up payment Paramaters.
        $payfast->setBuyer('first name', 'last name', 'email');
        $payfast->setAmount($order->amount);
        $payfast->setItem('item-title', 'item-description');
        $payfast->setMerchantReference($order->m_payment_id);

        // Optionally send confirmation email to seller
        $payfast->setEmailConfirmation();
        $payfast->setConfirmationAddress(env('PAYFAST_CONFIRMATION_EMAIL'));

        // Optionally make this a subscription
        $payfast->setSubscriptionType();    // will default to 1
        $payfast->setFrequency();           // will default to 3 = monthly if not set
        $payfast->setCycles();              // will default to 0 = indefinite if not set

        // Return the payment form.
        return $payfast->paymentForm('Place Order');
    }

}

ITN 响应

Payfast 将发送一个 POST 请求通知商户(您)交易的状态。这将允许您根据从 Payfast 返回的适当状态更新订单状态。您不必强制使用 'm_payment_id' 键来存储您的商户参考,但这将是 Payfast 返回给您的键,以便进一步验证。

use Billow\Contracts\PaymentProcessor;

Class PaymentController extends Controller
{

    public function itn(Request $request, PaymentProcessor $payfast)
    {
        // Retrieve the Order from persistance. Eloquent Example.
        $order = Order::where('m_payment_id', $request->get('m_payment_id'))->firstOrFail(); // Eloquent Example

        // Verify the payment status.
        $status = $payfast->verify($request, $order->amount, $order->m_payment_id)->status();

        // Handle the result of the transaction.
        switch( $status )
        {
            case 'COMPLETE': // Things went as planned, update your order status and notify the customer/admins.
                break;
            case 'FAILED': // We've got problems, notify admin and contact Payfast Support.
                break;
            case 'PENDING': // We've got problems, notify admin and contact Payfast Support.
                break;
            default: // We've got problems, notify admin to check logs.
                break;
        }
    }

}

以下是如何访问 Payfast 返回的响应变量

 return $payfast->responseVars();

Payfast 返回的变量

[
    'm_payment_id' => '',
    'pf_payment_id' => '',
    'payment_status' => '',
    'item_name' => '',
    'item_description' => '',
    'amount_gross' => '',
    'amount_fee' => '',
    'amount_net' => '',
    'custom_str1' => '',
    'custom_str2' => '',
    'custom_str3' => '',
    'custom_str4' => '',
    'custom_str5' => '',
    'custom_int1' => '',
    'custom_int2' => '',
    'custom_int3' => '',
    'custom_int4' => '',
    'custom_int5' => '',
    'name_first' => '',
    'name_last' => '',
    'email_address' => '',
    'merchant_id' => '',
    'signature' => '',
];

金额

购物车总价可以通过两种方式设置,作为字符串值

    $cartTotal = '99.99';

    $payfast->setAmount($cartTotal);

或作为整数。在整数的情况下,购物车总价必须以分的形式传递,如下所示

$cartTotal = 9999; // Laravel Payfast will parse this value and format it accordingly. See sebastianbergmann/money
$payfast->setAmount($cartTotal);

支付表单

默认情况下,paymentForm() 方法将返回一个包含提交按钮的编译好的 HTML 表单。有三种配置可用于提交按钮。

$payfast->paymentForm() // Default Text: 'Pay Now'

$payfast->paymentForm(false) // No submit button, handy for submitting the form via javascript

$payfast->paymentForm('Confirm and Pay') // Override Default Submit Button Text.