io-digital / payfast
使用 Payfast 在 Laravel 中处理在线支付
Requires
- illuminate/http: ^5.1
- illuminate/support: ^5.1
- mathiasverraes/money: ^1.3
Requires (Dev)
- phpunit/phpunit: 4.*
- scrutinizer/ocular: ~1.1
- squizlabs/php_codesniffer: ~2.3
README
这是一个简单的 Laravel 5 支付处理类,用于通过 payfast.co.za 进行支付。此包仅支持 ITN 交易。
从 billowapp/payfast 分支而来。
此仓库添加了所需的缺失验证步骤。
仍在开发中。
安装
将 Laravel 5 Payfast 添加到您的 composer.json
composer require io-digital/payfast
将 PayfastServiceProvider 添加到 config/app.php 中的 providers 数组
'providers' => [ // IoDigital\Payfast\PayfastServiceProvider::class, ];
在您的 .env 中添加以下键
PAYFAST_MERCHANT_ID=your-id
PAYFAST_MERCHANT_KEY=your-key
重要:为了可靠地处理 ITN 回调,建议使用 ngrok。这将使您的 config/payfast.php 文件中的 URL 看起来像
'return_url' => 'https://xxxxxxxx.ngrok.io/success',
配置
发布默认配置文件。
php artisan vendor:publish
重要:您需要编辑 App\Http\Middleware\VerifyCsrfToken,通过将处理 ITN 响应的路由添加到 $except 数组中。验证是通过 ITN 响应完成的。
protected $except = [ '/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' => '10000100', // TEST Credentials. Replace with your merchant ID from Payfast. 'merchant_key' => '46f0cd694581a', // TEST Credentials. Replace with your merchant key from Payfast. 'return_url' => 'http://your-domain.co.za/success', // The URL the customer should be redirected to after a successful payment. 'cancel_url' => 'http://your-domain.co.za/cancelled', // The URL the customer should be redirected to after a payment is cancelled. 'notify_url' => 'http://your-domain.co.za/itn', // The URL to which Payfast will post return variables. ] ];
使用方法
创建一个支付会返回一个准备好的 HTML 表单,可 POST 到 payfast。当客户提交表单时,他们将被重定向到 payfast 完成支付。支付成功后,客户将被返回到指定的 'return_url',如果取消,则返回到指定的 'cancel_url'。
use IoDigital\Payfast\Contracts\PaymentProcessor; Class PaymentController extends Controller { public function confirmPayment(PaymentProcessor $payfast) { // Eloquent 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'); //PS ADD DIVISION BY 100 FOR CENTS AS THE NEW DEPENDENCY "mathiasverraes/money": "^1.3" DOESN'T DO CONVERSION payfast->setAmount($purchase->amount / 100); $payfast->setItem('item-title', 'item-description'); $payfast->setMerchantReference($order->m_payment_id); // Return the payment form. return $payfast->paymentForm('Place Order'); } }
ITN 响应
Payfast 将发送一个 POST 请求通知商家(您)交易的状态。这将允许您根据从 Payfast 返回的适当状态更新您的订单状态。您不必强制使用 'm_payment_id' 键来存储您的商家参考,但这是 Payfast 会返回给您以进行进一步验证的键。
use IoDigital\Payfast\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 try { $verification = $payfast->verify($request, $order->amount); $status = $payfast->status(); } catch (\Exception $e) { Log::error('PAYFAST ERROR: ' . $e->getMessage()); $status = false; } // Verify the payment status. $status = (int) $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 = 9999; // "mathiasverraes/money": "^1.3" doesn't convert from cents correctly yet // That's why a manual division by 100 is necessary $payfast->setAmount($cartTotal / 100);
支付表单
默认情况下,getPaymentForm() 方法将返回一个包含提交按钮的编译后的 HTML 表单。有 3 种配置可用于提交按钮。
$payfast->getPaymentForm() // Default Text: 'Pay Now' $payfast->getPaymentForm(false) // No submit button, handy for submitting the form via javascript $payfast->getPaymentForm('Confirm and Pay') // Override Default Submit Button Text.
待办事项
- 单元测试
- 添加 Facade 类
- 允许自定义整数/字符串
- Curl 请求到 Payfast(验证)-> 需要进行更多测试