cruisedotco / trustly-client-php
实现与Trustly公共API进行在线银行电子支付的通信。更多信息请访问https://trustly.com
Requires
- php: >=5.2.0
- ext-bcmath: *
- ext-curl: *
- ext-json: *
- ext-mbstring: *
- ext-openssl: *
- guzzlehttp/guzzle: ^6.3
Requires (Dev)
- phpunit/phpunit: 4.8
This package is not auto-updated.
Last update: 2024-09-20 20:36:37 UTC
README
这是使用PHP与Trustly API通信的示例实现。它实现了标准的支付API,并为后台办公室使用的API调用提供了占位符。
有关Trustly API内部结构的完整文档,请访问我们的开发者网站:http://trustly.com/developer。有关软件流程和调用模式的全部信息都可以在该网站上找到。此代码中的文档将仅涵盖代码本身,而不是如何使用Trustly API。
此代码按原样提供,用作灵感、参考或将它直接应用到您的项目中使用。
如果您在代码中发现问题或想对其进行扩展,请随意进行分支并给我们发送一个拉取请求。
此代码应在PHP 5(>= 5.2.0)上运行。所需的PHP模块有:bcmath、openssl、curl、mbstring和json。
概述
提供的代码为调用Trustly API提供了包装器。使用您的商家标准创建API调用实例,并使用该类中的占位符调用API。API默认与https://trustly.com通信,要与之通信test.trustly.com,则覆盖构造函数中的host
参数。
在处理传入的通知时,API的handleNotification()
方法可以帮助解析和验证消息签名,使用notificationResponse()
构建合适的响应对象。
以下示例代表调用的一些基本使用情况。围绕此代码进行的最小错误处理是在处理过程中检查以下异常。
-
Trustly_ConnectionException
当无法与Trustly API通信时抛出。这可能是由于互联网或其他服务错误。
-
Trustly_DataException
在API返回数据出现各种问题时抛出。例如,当响应消息包含与发送消息不同的UUID或当响应结构不完整时。
-
Trustly_SignatureException
当无法验证消息的真实性时发出。如果捕获到这个异常,通信中的数据应该被废弃,因为它可能是伪造的。
示例存款调用
require_once('Trustly.php');
/* Change 'test.trustly.com' to 'trustly.com' below to use the live environment */
$api = new Trustly\Api\Trustly_Api_Signed(
$trustly_rsa_private_key,
$trustly_username,
$trustly_password,
'test.trustly.com'
);
/* Guzzle will handle our remote requests. */
$api->setGuzzle(new \GuzzleHttp\Client());
$deposit = $api->deposit(
"$base_url/php/example.php/notification", /* NotificationURL */
'john.doe@example.com', /* EndUserID */
$messageid, /* MessageID */
'en_US', /* Locale */
$amount, /* Amount */
$currency, /* Currency */
'SE', /* Country */
NULL, /* MobilePhone */
NULL, /* FirstName */
NULL, /* LastName */
NULL, /* NationalIdentificationNumber */
'Test', /* ShopperStatement */
$ip, /* IP */
"$base_url/success.html", /* SuccessURL */
"$base_url/fail.html", /* FailURL */
NULL, /* TemplateURL */
"0", /* URLTarget */
NULL, /* SuggestedMinAmount */
NULL, /* SuggestedMaxAmount */
'trustly-client-php example/1.0' /* IntegrationModule */
FALSE, /* HoldNotifications */
'john.doe@example.com', /* Email */
'SE', /* ShippingAddressCountry */
'12345', /* ShippingAddressPostalCode */
'ExampleCity', /* ShippingAddressCity */
'123 Main St' /* ShippingAddressLine1 */
'C/O Careholder', /* ShippingAddressLine2 */
NULL /* ShippingAddress */
);
$iframe_url= $deposit->getData('url');
示例通知处理
$request = $api->handleNotification($notification_body);
# FIXME Handle the incoming notification data here
$notifyresponse = $api->notificationResponse($request, TRUE);
echo $notifyresponse->json();
示例实现
在example/子目录中是一个简单实现的客户端,它使用该代码向Trustly发起存款调用并处理传入的通知。代码注释良好,包含有关需要调用的函数以及一些注意事项的信息。
使用内置的php webserver进行测试(在example/www目录中执行php -S localhost:8000)。在测试之前,您需要修改example/www/php/example.php
和example/example.private.pem
以包含您的处理账户信息。
贡献/开发
请随意提出更改的拉取请求。任何更改都需要涵盖单元测试。
非常感谢,很高兴开源。