neolikotsi / payfast
Laravel 5.4+|6+|7+|8+ 包,用于通过 payfast.co.za 处理 ITN 支付
Requires
- billowapp/show-me-the-money: ^0.1.2|^0.2|^0.4
- illuminate/http: ^5.4|^6.0|^7.0|^8.0
- illuminate/support: ^5.4|^6.0|^7.0|^8.0
Requires (Dev)
- orchestra/testbench: ^3.0|^4.6|^5.0|^6.0
- phpunit/phpunit: ^8.5|^9.1
- symfony/var-dumper: ^3.0
- dev-master
- 0.6.0
- 0.5.1
- 0.5.0
- 0.4.0
- 0.3.7
- 0.3.6
- 0.3.5
- 0.3.4
- 0.3.3
- 0.3.2
- 0.3.1
- 0.3.0
- 0.2.0
- 0.1.29
- v0.1.28
- v0.1.27
- v0.1.26
- v0.1.25
- v0.1.24
- v0.1.23
- v0.1.22
- v0.1.21
- v0.1.20
- v0.1.19
- v0.1.18
- v0.1.17
- v0.1.16
- v0.1.15
- v0.1.14
- v0.1.13
- v0.1.12
- v0.1.11
- v0.1.10
- v0.1.9
- v0.1.8
- v0.1.7
- 0.1.6
- v0.1.5
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1
- dev-upgrade/laravel-7
- dev-feat/add-test
This package is auto-updated.
Last update: 2024-09-08 21:09:06 UTC
README
一个简单的 Laravel 支付处理类,用于通过 payfast.co.za 进行支付。此包仅支持 ITN 交易。Laravel Payfast 使用风险自负。
安装
将 Laravel Payfast 添加到 composer.json
composer require neolikotsi/payfast
将 PayfastServiceProvider 添加到 config/app.php 中的 providers 数组
'providers' => [ // 'NeoLikotsi\PayfastServiceProvider' ];
配置
发布默认配置文件。
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. 'passphrase' => env('PF_PASSPHRASE'), // SECURITY PASSPHRASE '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 NeoLikotsi\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(config('payfast.PF_CONFIRMATION_EMAIL')); // Optionally make this a subscription $payfast->setIsSubscription(); // 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 NeoLikotsi\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)->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 返回的 POST 变量可以按以下方式访问
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 表单。有 3 种配置可用于提交按钮。
$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.
测试
composer test
许可证
MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件。