lunalabs-srl / slimpay-php
一个简单的PHP包,用于将SlimPay结账集成到您的应用程序中,支持iframe和重定向结账
Requires
- php: >=7.2
- guzzlehttp/guzzle: ~7.0
Requires (Dev)
- phpunit/phpunit: ^9.0
This package is auto-updated.
Last update: 2024-09-30 01:23:06 UTC
README
一个简单的PHP包,用于将您的应用程序中的 SlimPay结账 集成,支持 iframe 和 重定向 结账。
安装
您可以直接使用以下命令安装包:
composer require lunalabs-srl/slimpay-php
测试包
创建一个包含以下内容的简单PHP文件
<?php require_once __DIR__ . '/vendor/autoload.php'; use LunaLabs\SlimPayPhp\SlimPayPhp; use LunaLabs\SlimPayPhp\SlimPayNotification; use LunaLabs\SlimPayPhp\Exceptions\SlimPayPhpException; // Slimpay credentials. $slimpayConfig = [ 'creditor' => 'xxxxxxxxxxx', 'appId' => 'xxxxxxxxxxxx', 'appSecret' => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', 'baseUri' => 'https://api.preprod.slimpay.com', 'profileUri' => 'https://api.slimpay.net', 'apiVersion' => 'v1', 'mode' => 'iframe' // iframe|redirect ]; // Instance. $slimpay = new SlimPayPhp($slimpayConfig);
使用SlimPay iframe或重定向进行信用卡结账
要创建信用卡的SlimPay结账,您必须初始化一个如下所示的数组并调用 checkout 方法。使用您自己的数据自定义数据和返回URL(failureUrl,successUrl和cancelUrl)。
$data = [ 'started' => true, 'locale' => 'it', 'paymentScheme' => 'CARD', 'creditor' => ['reference' => 'xxxxxxxxxxx'], 'items' => [['type' => 'cardAlias']], 'subscriber' => [ 'reference' => 'yourUniqueUserId', 'givenName' => 'John', 'familyName' => 'Doe', 'email' => 'john.doe@domain.com', 'telephone' => '+393470000000', ], 'failureUrl' => 'http://yourdomain.com/failure.php', 'successUrl' => 'http://yourdomain.com/success.php', 'cancelUrl' => 'http://yourdomain.com/cancel.php' ]; try { // This call returns a response with the order representation // in which you can find the unique ID. Save it to keep the order // reference (for example, if the order fails). $response = $slimpay->checkout($data); $slimpay->showCheckoutPage($response); } catch (SlimPayPhpException $e) { // In case of error, you can handle the formatted object response through some useful properties. header('Content-Type: application/json'); echo json_encode($e->errorFormatter()); }
如果响应中有 用户批准链接,您将被重定向到 SlimPay结账页面。一旦您填写了结账表单并输入了您的信用卡详细信息,就会将一个详细的响应发送到您在SlimPay应用程序中设置的 服务器通知URI,其中包含用于检索 信用卡ID 和 参考ID 的 get-card-alias链接,以便将它们发送到您的支付网关以完成流程。 服务器通知参考
$creditCardAlias = $slimpay->getResource('https://api.slimpay.net/card-aliases/00000000-0000-0000-0000-000000000000');
在这个响应中,您将找到用于已使用的信用卡的 id、reference 和 status,现在您可以将这些数据存储在支付网关中。
使用SlimPay iframe或重定向进行SEPA结账
要创建SEPA直接借记的SlimPay iframe,您必须初始化一个如下所示的数组并调用 checkout 方法。使用您自己的数据自定义数据和返回URL(failureUrl,successUrl和cancelUrl)。
$data = [ 'started' => true, 'creditor' => ['reference' => 'xxxxxxxxxxx'], 'subscriber' => ['reference' => 'yourUniqueUserId'], 'items' => [[ 'type' => 'signMandate', 'mandate' => [ 'signatory' => [ 'givenName' => 'John', 'familyName' => 'Doe', 'email' => 'john.doe@domain.com', 'telephone' => '+393470000000', 'billingAddress' => [ 'street1' => 'Address street 123', 'postalCode' => '01234', 'city' => 'CityName', 'country' => 'IT' ], ] ] ]], 'failureUrl' => 'http://yourdomain.com/failure.php', 'successUrl' => 'http://yourdomain.com/success.php', 'cancelUrl' => 'http://yourdomain.com/cancel.php' ]; try { // This call returns a response with the order representation // in which you can find the unique ID. Save it to keep the order // reference (for example, if the order fails). $response = $slimpay->checkout($data); $slimpay->showCheckoutPage($response); } catch (SlimPayPhpException $e) { // In case of error, you can handle the formatted object response through some useful properties. header('Content-Type: application/json'); echo json_encode($e->errorFormatter()); }
如果响应中有 用户批准链接,您将被重定向到 SlimPay结账页面。一旦您填写了结账表单并输入了您的IBAN,就会将一个详细的响应发送到您在SlimPay应用程序中设置的 服务器通知URI,其中包含 结账状态 和用于检索创建的授权的链接(get-mandate)。通过调用此链接(例如: https://api.slimpay.net/mandates/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx),您可以检索 参考ID 和 UMR (RUM)号码,以便在您的支付网关中为该用户创建支付方式。
通过认证调用获取资源
要检索资源,您可以使用 getResource() 方法,如下所示传递URL。
try { $response = $slimpay->getResource('https://api.slimpay.net/RESOURCE-NAME/00000000-0000-0000-0000-000000000000'); } catch (SlimPayPhpException $e) { // In case of error, you can handle the formatted object response through some useful properties. header('Content-Type: application/json'); echo json_encode($e->errorFormatter()); }
服务器通知URL
可以处理 SlimPay服务器通知。要实例化通知处理器,请编写以下这些行
$notification = new SlimPayNotification(); $response = $notification->getResponse();
如果您想 记录通知响应,您可以作为参数注入自定义记录器。请注意,您的记录器必须包含一个像下面这样的 "write()" 方法,如下所示。
class Log { public function write($input) { $path = "./slimpay_notifications.log"; error_log(json_encode($input), 3, $path); } } $customLog = new Log; $notification = new SlimPayNotification($customLog); $response = $notification->getResponse();
待办事项
身份验证
- 身份验证流程
信用卡结账
- 创建订单
- 结账重定向
- 嵌入式iframe
- 获取通知响应以检索卡和参考ID
SEPA结账
- 创建订单
- Iframe重定向
- 获取通知响应以检索RUM
测试
- 单元测试