zerosuxx/barion-web-php

Barion PHP 库

dev-master 2018-04-13 21:53 UTC

This package is auto-updated.

Last update: 2024-09-05 04:02:21 UTC


README

BarionPHP 是一个用于通过 Barion Smart Gateway 管理在线电子货币和信用卡支付的紧凑型 PHP 库。它允许您仅用几行代码就接受信用卡和电子货币支付。

BarionPHP 让您

  • 轻松开始在线即时或预约支付
  • 获取特定支付的详细信息
  • 完全或部分完成正在进行的预约支付,支持自动退款
  • 完全或部分退款完成的支付交易

所有这些只需几行简单的代码!

版本历史

  • 1.2.9 2017年5月16日。
  • 1.2.8 2017年4月13日。
  • 1.2.7 2017年2月14日。
  • 1.2.5 2016年11月7日。
  • 1.2.4 2016年5月25日。
  • 1.2.3 2016年1月14日。
  • 1.2.2 2016年1月11日。
  • 1.1.0 2015年11月27日。
  • 1.0.1 2015年11月26日。
  • 1.0.0 2015年11月17日。

有关版本更改的详细信息,请参阅 changelog.txt 文件。

系统要求

  • PHP 5.6 或更高版本
  • cURL 模块已启用(至少 v7.18.1)
  • SSL 已启用(使用至少 0.9.8f 版本的 OpenSSL 的系统)

安装

barion 库的内容复制到目标文件夹。确保在运行您的 PHP 脚本时可以访问该路径。

基本用法

在您的 PHP 脚本中包含 BarionClient

require_once 'library/BarionClient.php';

然后实例化一个 Barion 客户端。为此,您必须提供三个参数。

首先,Barion 中注册的在线商店的秘密密钥(称为 POSKey)

$myPosKey = "9c165cfc-cbd1-452f-8307-21a3a9cee664";

API 版本号(默认为 2)

$apiVersion = 2;

要连接的环境。这可以是测试系统或生产环境。

// Test environment
$environment = BarionEnvironment::Test;

// Production environment
$environment = BarionEnvironment::Prod;

使用这些参数,您可以创建 BarionClient 类的实例

$BC = new BarionClient($myPosKey, $apiVersion, $environment);

如果您遇到 SSL 连接问题,则可以将第四个参数设置为 true:$useBundledRootCerts 这将使用捆绑的根证书列表而不是服务器提供的证书。

示例

开始在线支付

让我们看看使用 Barion 库的基本示例。我们将开始一个即时在线支付,用户可以为价值 1 000 HUF 的产品付款。

1. 创建请求对象

要开始在线支付,您必须创建一个或多个 Payment Transaction 对象,将交易 Items 添加到它们中,然后在 Payment 对象中将这些交易组合在一起。

首先,创建一个 ItemModel

$item = new ItemModel();
$item->Name = "TestItem";
$item->Description = "A test product";
$item->Quantity = 1;
$item->Unit = "piece";
$item->UnitPrice = 1000;
$item->ItemTotal = 1000;
$item->SKU = "ITEM-01";

然后创建一个 PaymentTransactionModel 并将上述 Item 添加到其中

$trans = new PaymentTransactionModel();
$trans->POSTransactionId = "TRANS-01";
$trans->Payee = "webshop@example.com";
$trans->Total = 1000;
$trans->Currency = Currency::HUF;
$trans->Comment = "Test transaction containing the product";
$trans->AddItem($item);

最后,创建一个 PreparePaymentRequestModel 并将上述 PaymentTransactionModel 添加到其中

$ppr = new PreparePaymentRequestModel();
$ppr->GuestCheckout = true;
$ppr->PaymentType = PaymentType::Immediate;
$ppr->FundingSources = array(FundingSourceType::All);
$ppr->PaymentRequestId = "PAYMENT-01";
$ppr->PayerHint = "user@example.com";
$ppr->Locale = UILocale::EN;
$ppr->OrderNumber = "ORDER-0001";
$ppr->Currency = Currency::HUF;
$ppr->ShippingAddress = "12345 NJ, Example ave. 6.";
$ppr->RedirectUrl = "http://webshop.example.com/afterpayment";
$ppr->CallbackUrl = "http://webshop.example.com/processpayment";
$ppr->AddTransaction($trans);

此时,完整的请求对象如下所示

PreparePaymentRequestModel Object
(
    [PaymentType] => Immediate
    [ReservationPeriod =>
    [PaymentWindow] => 00:30:00
    [GuestCheckout] => 1
    [FundingSources] => Array
        (
            [0] => All
        )
    [PaymentRequestId] => PAYMENT-01
    [PayerHint] => user@example.com
    [Transactions] => Array
        (
            [0] => PaymentTransactionModel Object
                (
                    [POSTransactionId] => TRANS-01
                    [Payee] => webshop@example.com
                    [Total] => 1000
                    [Currency] => HUF
                    [Comment] => Test transaction containing the product
                    [Items] => Array
                        (
                            [0] => ItemModel Object
                                (
                                    [Name] => TestItem
                                    [Description] => A test product
                                    [Quantity] => 1
                                    [Unit] => piece
                                    [UnitPrice] => 1000
                                    [ItemTotal] => 1000
                                    [SKU] => ITEM-01
                                )
                        )
                    [PayeeTransactions] =>
                )
        )
    [Locale] => en-US
    [Currency] => HUF
    [OrderNumber] => ORDER-0001
    [ShippingAddress] => 12345 NJ, Example ave. 6.
    [InitiateRecurrence] =>
    [RecurrenceId] =>
    [RedirectUrl] => http://webshop.example.com/afterpayment
    [CallbackUrl] => http://webshop.example.com/processpayments
    [POSKey] =>
)

注意:用于身份验证的秘密 POSKey 不是请求对象的一部分。Barion 客户端类会自动将此值注入到发送到 Barion API 的每个请求中。

2. 调用 Barion API

现在,您可以使用您刚才创建的请求模型调用 Barion 客户端的 PreparePayment 方法

$myPayment = $BC->PreparePayment($ppr);

Barion API 现在准备一个可以由任何人支付的支付实体。

$myPayment 变量包含从 Barion API 收到的响应,它是一个 PreparePaymentResponseModel 对象的实例。它应该看起来像这样

PreparePaymentResponseModel Object
(
    [PaymentId] => 64157032-d3dc-4296-aeda-fd4b0994c64e
    [PaymentRequestId] => PAYMENT-01
    [Status] => Prepared
    [Transactions] => Array
        (
            [0] => TransactionResponseModel Object
                (
                    [POSTransactionId] => TRANS-01
                    [TransactionId] => fb06f46e-7a55-4da5-9a62-992089b3dd23
                    [Status] => Prepared
                    [TransactionTime] => 2015-11-12T09:24:14.074
                    [RelatedId] =>
                )
            [1] => TransactionResponseModel Object
                (
                    [POSTransactionId] =>
                    [TransactionId] => 49a9c395-833a-445f-82dd-b1d12784b3ef
                    [Status] => Prepared
                    [TransactionTime] => 2015-11-12T09:24:14.262
                    [RelatedId] =>
                )
            [2] => TransactionResponseModel Object
                (
                    [POSTransactionId] =>
                    [TransactionId] => c91a0006-4b6b-41ed-bdbd-5cfb3d67528b
                    [Status] => Prepared
                    [TransactionTime] => 2015-11-12T09:24:14.262
                    [RelatedId] =>
                )
        )
    [QRUrl] => https://api.barion.com/qr/generate?paymentId=64157032-d3dc-4296-aeda-fd4b0994c64e&size=Large
    [RecurrenceResult] => None
    [PaymentRedirectUrl] => https://secure.barion.com/Pay?id=64157032-d3dc-4296-aeda-fd4b0994c64e
    [Errors] => Array
        (
        )
    [RequestSuccessful] => 1
)

RequestSuccessful参数表明请求已成功发送,响应已成功接收。PaymentId包含你在Barion系统中刚刚创建的支付的公共标识符,而Transactions数组包含与这笔支付相关的交易。第一个元素是你在之前构造的交易,其余两个是Barion智能网关生成的费用交易。在这种情况下,这些是网关使用费和银行卡处理费。

注意:相关费用交易的数量和类型取决于请求调用者的身份,请参阅https://docs.barion.com的文档或联系我们的销售部门。

注意:RequestSuccessful参数仅表示HTTP请求-响应通信本身已成功完成。它不涉及支付过程的任何部分。如果在请求处理过程中出现任何错误,API错误信息将在Errors数组中找到。

3. 将用户重定向到Barion智能网关

您可以使用响应中的PaymentId值将用户重定向到Barion智能网关。您必须使用该标识符提供Id查询字符串参数。完整的重定向URL看起来像这样

https://secure.barion.com/Pay?id=64157032-d3dc-4296-aeda-fd4b0994c64e

现在用户可以在Barion智能网关完成支付。

获取支付信息

在这个例子中,我们将获取上面创建的支付的详细信息。

1. 创建请求对象

要请求支付详情,您只需要一个参数:支付标识符。这就是我们之前用于重定向用户的PaymentId

64157032-d3dc-4296-aeda-fd4b0994c64e

2. 调用 Barion API

要请求支付详情,我们调用Barion客户端类的GetPaymentState方法,并带上上述标识符

$paymentDetails = $BC->GetPaymentState("64157032-d3dc-4296-aeda-fd4b0994c64e");

$paymentDetails变量包含从Barion API收到的响应,它是一个PaymentStateResponseModel对象的实例。它应该看起来像这样

PaymentStateResponseModel Object
(
    [PaymentId] => 64157032-d3dc-4296-aeda-fd4b0994c64e
    [PaymentRequestId] => PAYMENT-01
    [POSId] =>
    [POSName] => ExampleWebshop
    [Status] => Prepared
    [PaymentType] => Immediate
    [FundingSource] =>
    [AllowedFundingSources] => Array
        (
            [0] => All
        )
    [GuestCheckout] => 1
    [CreatedAt] => 2015-11-12T09:47:12.173
    [ValidUntil] => 2015-11-12T10:17:12.173
    [CompletedAt] =>
    [ReservedUntil] =>
    [Total] => 1000
    [Currency] => HUF
    [Transactions] => Array
        (
            [0] => TransactionDetailModel Object
                (
                    [TransactionId] => fb06f46e-7a55-4da5-9a62-992089b3dd23
                    [POSTransactionId] => TRANS-01
                    [TransactionTime] => 2015-11-12T09:47:12.189
                    [Total] => 1000
                    [Currency] => HUF
                    [Payer] => UserModel Object
                        (
                            [Name] =>
                            [Email] =>
                        )
                    [Payee] => UserModel Object
                        (
                            [Name] => Example Webshop Technologies Ltd.
                            [Email] => webshop@example.com
                        )
                    [Comment] => Test transaction containing the product
                    [Status] => Prepared
                    [TransactionType] => Unspecified
                    [Items] => Array
                        (
                            [0] => ItemModel Object
                                (
                                    [Name] => TestItem
                                    [Description] => A test product
                                    [Quantity] => 1
                                    [Unit] => piece
                                    [UnitPrice] => 1000
                                    [ItemTotal] => 1000
                                    [SKU] => ITEM-01
                                )
                        )
                    [RelatedId] =>
                    [POSId] => 04ed8c89-c9bd-4c17-92f6-a0964587bbff
                    [PaymentId] => 64157032-d3dc-4296-aeda-fd4b0994c64e
                )
            [1] => TransactionDetailModel Object
                (
                    [TransactionId] => 49a9c395-833a-445f-82dd-b1d12784b3ef
                    [POSTransactionId] =>
                    [TransactionTime] => 2015-11-12T09:47:12.205
                    [Total] => 50
                    [Currency] => HUF
                    [Payer] => UserModel Object
                        (
                            [Name] => Example Webshop Technologies Ltd.
                            [Email] => webshop@example.com
                        )
                    [Payee] => UserModel Object
                        (
                            [Name] =>
                            [Email] =>
                        )
                    [Comment] =>
                    [Status] => Prepared
                    [TransactionType] => GatewayFee
                    [Items] => Array
                        (
                        )
                    [RelatedId] =>
                    [POSId] => 04ed8c89-c9bd-4c17-92f6-a0964587bbff
                    [PaymentId] => 64157032-d3dc-4296-aeda-fd4b0994c64e
                )
            [2] => TransactionDetailModel Object
                (
                    [TransactionId] => c91a0006-4b6b-41ed-bdbd-5cfb3d67528b
                    [POSTransactionId] =>
                    [TransactionTime] => 2015-11-12T09:47:12.205
                    [Total] => 10
                    [Currency] => HUF
                    [Payer] => UserModel Object
                        (
                            [Name] => Example Webshop Technologies Ltd.
                            [Email] => webshop@example.com
                        )
                    [Payee] => UserModel Object
                        (
                            [Name] =>
                            [Email] =>
                        )
                    [Comment] =>
                    [Status] => Prepared
                    [TransactionType] => CardProcessingFee
                    [Items] => Array
                        (
                        )
                    [RelatedId] =>
                    [POSId] => 04ed8c89-c9bd-4c17-92f6-a0964587bbff
                    [PaymentId] => 64157032-d3dc-4296-aeda-fd4b0994c64e
                )
        )
    [RecurrenceResult] =>
    [Errors] => Array
        (
        )
    [RequestSuccessful] => 1
)

支付处于准备状态,这意味着它仍在等待支付。如果我们等待用户完成支付并再次发送请求,我们应该得到一个略有不同的结果,包含更多信息

PaymentStateResponseModel Object
(
    [PaymentId] => 64157032-d3dc-4296-aeda-fd4b0994c64e
    [PaymentRequestId] => PAYMENT-01
    [POSId] =>
    [POSName] => ExampleWebshop
    [Status] => Succeeded
    [PaymentType] => Immediate
    [FundingSource] => BankCard
    [FundingInformation] => FundingInformationModel Object
        (
            [BankCard] => BankCardModel Object
                (
                    [MaskedPan] => 1234
                    [BankCardType] => MasterCard
                    [ValidThruYear] => 2019
                    [ValidThruMonth] => 9
                )

            [AuthorizationCode] => 123456
        )
    [AllowedFundingSources] => Array
        (
            [0] => All
        )
    [GuestCheckout] => 1
    [CreatedAt] => 2015-11-12T09:47:12.173
    [ValidUntil] => 2015-11-12T10:17:12.173
    [CompletedAt] => 2015-11-12T10:09:35.525
    [ReservedUntil] =>
    [Total] => 1000
    [Currency] => HUF
    [Transactions] => Array
        (
            [0] => TransactionDetailModel Object
                (
                    [TransactionId] => fb06f46e-7a55-4da5-9a62-992089b3dd23
                    [POSTransactionId] => TRANS-01
                    [TransactionTime] => 2015-11-12T09:47:12.189
                    [Total] => 1000
                    [Currency] => HUF
                    [Payer] => UserModel Object
                        (
                            [Name] => John Doe
                            [Email] => user@example.com
                        )
                    [Payee] => UserModel Object
                        (
                            [Name] => Example Webshop Technologies Ltd.
                            [Email] => webshop@example.com
                        )
                    [Comment] => Test transaction containing the product
                    [Status] => Succeeded
                    [TransactionType] => CardPayment
                    [Items] => Array
                        (
                            [0] => ItemModel Object
                                (
                                    [Name] => TestItem
                                    [Description] => A test product
                                    [Quantity] => 1
                                    [Unit] => piece
                                    [UnitPrice] => 1000
                                    [ItemTotal] => 1000
                                    [SKU] => ITEM-01
                                )
                        )
                    [RelatedId] =>
                    [POSId] => 04ed8c89-c9bd-4c17-92f6-a0964587bbff
                    [PaymentId] => 64157032-d3dc-4296-aeda-fd4b0994c64e
                )
            [1] => TransactionDetailModel Object
                (
                    [TransactionId] => 49a9c395-833a-445f-82dd-b1d12784b3ef
                    [POSTransactionId] =>
                    [TransactionTime] => 2015-11-12T09:47:12.205
                    [Total] => 50
                    [Currency] => HUF
                    [Payer] => UserModel Object
                        (
                            [Name] => Example Webshop Technologies Ltd.
                            [Email] => webshop@example.com
                        )
                    [Payee] => UserModel Object
                        (
                            [Name] =>
                            [Email] =>
                        )
                    [Comment] =>
                    [Status] => Succeeded
                    [TransactionType] => GatewayFee
                    [Items] => Array
                        (
                        )
                    [RelatedId] =>
                    [POSId] => 04ed8c89-c9bd-4c17-92f6-a0964587bbff
                    [PaymentId] => 64157032-d3dc-4296-aeda-fd4b0994c64e
                )
            [2] => TransactionDetailModel Object
                (
                    [TransactionId] => c91a0006-4b6b-41ed-bdbd-5cfb3d67528b
                    [POSTransactionId] =>
                    [TransactionTime] => 2015-11-12T09:47:12.205
                    [Total] => 10
                    [Currency] => HUF
                    [Payer] => UserModel Object
                        (
                            [Name] => Example Webshop Technologies Ltd.
                            [Email] => webshop@example.com
                        )
                    [Payee] => UserModel Object
                        (
                            [Name] =>
                            [Email] =>
                        )
                    [Comment] =>
                    [Status] => Succeeded
                    [TransactionType] => CardProcessingFee
                    [Items] => Array
                        (
                        )
                    [RelatedId] =>
                    [POSId] => 04ed8c89-c9bd-4c17-92f6-a0964587bbff
                    [PaymentId] => 64157032-d3dc-4296-aeda-fd4b0994c64e
                )
        )
    [RecurrenceResult] =>
    [Errors] => Array
        (
        )
    [RequestSuccessful] => 1
)

如您所见,支付状态现在是成功,这意味着支付已成功完成。 FundingSource参数显示支付是用银行卡完成的。有关银行卡的信息可在FundingInformation属性中找到。此外,第一个交易的Payer参数显示支付是由John Doe (user@example.com)用户账户完成的。

更多示例

要查看更多关于Barion库使用的示例,请参阅存储库中examples文件夹中的示例文件。

© 2017 Barion Payment Inc.