snufkin / barion-web-php
Barion PHP 库
Requires
- php: ^5.6
- php-mod/curl: ^1.6
Requires (Dev)
- phpunit/phpunit: ^6.0
This package is not auto-updated.
Last update: 2024-09-28 20:40:52 UTC
README
BarionPHP 是一个紧凑的 PHP 库,用于通过 Barion Smart Gateway 管理在线电子货币和卡支付。它允许您只需几行代码即可接受信用卡和电子货币支付。
BarionPHP 让您可以
- 轻松启动在线即时或预订支付
- 获取特定支付的详细信息
- 完全或部分完成正在进行的预订支付,支持自动退款
- 完全或部分退款完成的支付交易
所有这些只需几行简单的代码!
版本历史
- 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.4 或更高版本
- cURL 模块已启用(至少 v7.18.1)
- SSL 已启用(至少使用 OpenSSL 0.9.8f 版本的系统)
安装
将 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 福林的单一产品。
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
)
支付处于《Prepared》状态,这意味着它仍在等待支付。如果我们等待用户完成支付并再次发送请求,我们应该得到一个略有不同的结果,包含更多信息
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
)
如您所见,支付状态现在是《Succeeded》,这意味着支付已成功完成。《FundingSource》参数显示支付是通过银行卡完成的。有关银行卡的信息可在《FundingInformation》属性中找到。此外,第一个交易的《Payer》参数显示支付是由John Doe (user@example.com)用户账户完成的。
更多示例
要查看更多关于Barion库使用的示例,请参阅存储库中examples文件夹中的示例文件。
© 2017 Barion Payment Inc.