trustly/trustly-client-php

此包的最新版本(2021.1.0)没有可用的许可信息。

实现与Trustly公共API进行在线银行电子支付的通信。更多信息请访问https://trustly.com

2021.1.0 2018-11-06 08:55 UTC

This package is not auto-updated.

Last update: 2024-09-21 14:12:54 UTC


README

这是使用PHP与Trustly API进行通信的示例实现。它实现了标准支付API,并提供了对后端使用的API的调用占位符。

有关Trustly API内部结构的完整文档,请访问我们的开发者网站:https://eu.developers.trustly.com/。有关软件流程和调用模式的全部信息都可以在该网站上找到。本代码中的文档将仅涵盖代码本身,而不是如何使用Trustly API。

此代码按原样提供,您可以将其用作灵感、参考或直接将其用于自己的项目。

如果您在代码中发现问题或希望扩展它,请自由地将其分叉并发送给我们pull request。

此代码应在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_Signed(
                $trustly_rsa_private_key,
                $trustly_username,
                $trustly_password,
                'test.trustly.com'
            );

$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 */
                'Sam',                                      /* FirstName */
                'Trautman',                                 /* LastName */
                NULL,                                       /* NationalIdentificationNumber */
                NULL,                                       /* ShopperStatement */
                $ip,                                        /* IP */
                "$base_url/success.html",                   /* SuccessURL */
                "$base_url/fail.html",                      /* FailURL */
                NULL,                                       /* TemplateURL */
                NULL,                                       /* 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进行存款调用并处理传入的通知。代码有良好的注释,并包含有关需要调用的函数和注意事项的信息。

代码在Linux/OSX上可运行,Apache版本为v2.2/2.4。使用example/example.sh脚本来控制示例环境。在测试之前,您需要修改example/www/php/example.phpexample/example.private.pem以包含您的处理帐户信息。