tamkeen-tech / laravel-payfort
laravel Payfort 包
Requires
- php: >=8
- guzzlehttp/guzzle: ^7.4
- laravel/framework: ^9|^10.0
Requires (Dev)
- orchestra/testbench: ^7
- phpstan/phpstan: ^1.11
README
帮助您将Payfort集成到您的应用程序中。目前它支持 自定义商户页面集成
,请参考此 链接 了解更多信息,此包还支持使用多个商户账户。
当前此包支持以下操作列表
- AUTHORIZATION/PURCHASE
- TOKENIZATION
- CAPTURE
- REFUND
- INSTALLMENTS
- VOID
- CHECK_STATUS
- APPLE_PAY
请务必阅读并理解 payfort
文档。
目前它只支持 Laravel 9。
安装
您需要运行以下命令
composer require tamkeen-tech/laravel-payfort
要发布配置,请运行此命令
php artisan vendor:publish --tag payfort-config
这将生成一个 config/payfort.php
,包含默认配置
return [ 'gateway_host' => env('PAYFORT_GATEWAY_HOST', 'https://checkout.payfort.com/'), 'gateway_sandbox_host' => env('PAYFORT_GATEWAY_SAND_BOX_HOST', 'https://sbcheckout.payfort.com/'), 'merchants' => [ 'default' => [ 'merchant_identifier' => env('PAYFORT_MERCHANT_IDENTIFIER', null), 'access_code' => env('PAYFORT_ACESS_CODE', null), 'SHA_request_phrase' => env('PAYFORT_SHAR_REQUEST_PHARSE', null), 'SHA_response_phrase' => env('PAYFORT_SHAR_RESPONSE_PHRASE', null), ], ], 'sandbox_mode' => env('PAYFORT_SANDBOX_MODE', true), 'SHA_type' => env('PAYFORT_SHA_TYPE', 'sha256'), 'language' => env('PAYFORT_LANGUAGE', 'en'), ];
然后您可以将您的 .env
文件更新为正确的凭证
PAYFORT_SANDBOX_MODE=true # Defines wether to activate the payfort sandbox enviroment or not. PAYFORT_MERCHANT_IDENTIFIER=test # The payfort merchant account identifier PAYFORT_ACCESS_CODE=test # The payfort account access code PAYFORT_SHA_TYPE=sha256 # The payfort account sha type. sha256/sha512 PAYFORT_SHA_REQUEST_PHRASE=test # The payfort account sha request phrase PAYFORT_SHA_RESPONSE_PHRASE=test # The payfort account sha response phrase
使用方法
一旦您确定了凭证和配置,您就可以开始使用 payfort 操作。
令牌化请求
要显示令牌化页面,您可以在控制器方法中添加以下代码
Payfort::tokenization( 1000, # Bill amount 'redirect_url', # the recirect to url after tokenization true # either to return form html or not (optional) );
AUTHORIZATION/PURCHASE
要发送购买或授权命令,您可以在控制器中从 payfort 的令牌化请求返回后添加以下代码
$response = Payfort::purchase( [], # Request body coming from the tokenization 100, # Bill amount 'test@test.ts', # User email 'redirect_url', # The return back url after purchase [] # installment data (optional) );
$response = Payfort::authorize( [], # Request body coming from the tokenization 100, # Bill amount 'test@test.ts', # User email 'redirect_url' # The return back url after purchase );
要处理 3Ds 重定向,您可以使用以下代码片段
if ($response->should3DsRedirect()) { return redirect()->away($response->get3DsUri()); }
其中 $response
是来自购买或授权的响应。
如果交易成功完成,您可以使用以下方式获取交易 fort id
$response->getResponseFortId();
或者使用此方式获取使用的支付方式
$response->getResponsePaymentMethod()
处理响应
要处理来自 payfort 的响应并确保其有效性,您可以使用以下代码片段
Payfort::processResponse( [] # the response array );
如果响应无效,它将抛出异常 \TamkeenTech\Payfort\Exceptions\PaymentFailed
。
如果交易成功完成,您可以使用以下方式获取交易 fort id
$response->getResponseFortId();
或者使用此方式获取使用的支付方式
$response->getResponsePaymentMethod()
Capture
仅在授权后使用,要发送捕获命令,请使用以下代码
Payfort::capture( 'fort_id', # fort id for the payment transaction 100.0 # bill amount );
Void
仅在授权后使用,要发送取消命令,请使用以下代码
Payfort::void( 'fort_id' # fort id for the payment transaction );
Refund
仅在购买后使用,要发送退款命令,请使用以下代码
Payfort::refund( 'fort_id', # fort id for the payment transaction 1000 # amount to be reunded must not exceed the bill amount );
商户额外
Payfort 支持将额外的字段发送到请求中,它们将返回给您作为响应,因此要向任何命令添加商户额外信息,请执行以下操作
Payfort::setMerchantExtra('test')->tokenization( 1000, # Bill amount 'redirect_url', # the recirect to url after tokenization true # either to return form html or not (optional) );
您可以在任何命令之前使用此方法 setMerchantExtra
,并且您可以添加最多 5 个额外信息。
Apple pay
要使用 Apple Pay 服务,您只需执行以下操作
Payfort::applePay( [], # fort params (please make sure to match the fort params mentioned below) 1000, # bill amount 'test@test.ts', # User email 'PURCHASE', # command to be sent to apple pay either PURCHASE or AUTHORIZE );
一个重要的注意事项,您需要匹配以下 fort 参数结构
[ "paymentData" => [ "data" => "apple_data", "header" => [ 'ephemeralPublicKey' => 'apple_ephemeralPublicKey', 'publicKeyHash' => 'apple_publicKeyHash', 'transactionId' => 'apple_transactionId', ], "signature" => "signature", ], "paymentMethod" => [ 'displayName' => 'apple_displayName', 'network' => 'apple_network', 'type' => 'apple_type', ], ]
日志记录
要记录您对 payfort 的请求,您可以监听此事件 \TamkeenTech\Payfort\Events\PayfortMessageLog
,它将包含发送的数据和响应
以下是如何使用它的示例
$log = app(PayfortLog::class); $log->contract_id = data_get($event->request, 'merchant_extra', data_get($event->response, 'merchant_extra', null)); if (isset($event->response['card_number'])) { $last_four_digits = substr($event->response['card_number'], -4); $log->card_number = '************'.$last_four_digits; } if (isset($event->response['amount'])) { $log->amount = floatval($event->response['amount'] / 100); } if (isset($event->response['response_message'])) { $log->response_message = data_get($event->response, 'response_message'); } if (isset($event->response['merchant_reference'])) { $log->merchant_reference = $event->response['merchant_reference']; } if (isset($event->request['merchant_reference'])) { $log->merchant_reference = $event->request['merchant_reference']; } $log->fort_id = data_get($event->response, 'fort_id'); $log->payment_option = data_get($event->response, 'payment_option'); $log->command = data_get($event->response, 'command', data_get($event->response, 'service_command')); $log->response_code = data_get($event->response, 'response_code'); $log->request = $event->request ? json_encode($event->request) : ""; $log->response = json_encode($event->response); $log->save();
许可
MIT 许可证(MIT)。有关更多信息,请参阅许可文件。