octonull / barion-web-php
用于Barion Smart Gateway API集成的PHP库
Requires
- php: >=7.2
- ext-curl: *
This package is not auto-updated.
Last update: 2024-09-28 04:01:58 UTC
README
BarionPHP 是一个用于通过 Barion Smart Gateway 管理在线电子货币和卡片支付的小型PHP库。它允许您只需几行代码即可接受信用卡和电子货币支付。
BarionPHP 可以让您
- 轻松启动在线即时或预约支付
- 获取指定支付的详细信息
- 完全或部分完成进行中的预约支付,并支持自动退款
- 完全或部分退款已完成的支付交易
只需几行简单的代码即可实现所有这些功能!
版本历史
- 1.4.4 2021年2月17日
- 1.4.3 2020年12月11日
- 1.4.2 2019年8月15日.
- 1.4.1 2019年8月14日.
- 1.4.0 2019年8月8日.
- 1.3.2 2019年8月5日.
- 1.3.1 2019年3月20日.
- 1.3.0 2019年3月12日.
- 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
这将使用捆绑的根证书列表而不是服务器提供的证书。
基本流程
使用此库管理支付流程分为两个步骤
1. 开始支付
1.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->RedirectUrl = "http://webshop.example.com/afterpayment"; $ppr->CallbackUrl = "http://webshop.example.com/processpayment"; $ppr->AddTransaction($trans);
注意:用于认证的秘密 POSKey 不是请求对象的一部分。Barion客户端类会自动将此值注入到发送到Barion API的每个请求中。
1.2. 调用Barion API
现在您可以使用您刚才创建的请求模型调用Barion客户端的 PreparePayment 方法
$myPayment = $BC->PreparePayment($ppr);
Barion API 现在可以准备一个可以被任何人支付的付款实体。
$myPayment 变量存储从 Barion API 收到的响应,这是一个 PreparePaymentResponseModel 对象的实例。
1.3. 将用户重定向到 Barion Smart Gateway
您可以使用响应中的 PaymentId 值将用户重定向到 Barion Smart Gateway。您必须在 Id 查询字符串参数中提供此标识符。完整的重定向 URL 看起来像这样
https://secure.barion.com/Pay?id=<your_payment_id>
用户现在可以在 Barion Smart Gateway 完成付款。
2. 获取付款信息
在这个示例中,我们将获取上面创建的付款的详细信息。
2.1. 创建请求对象
要请求付款的详细信息,您只需要一个参数:付款标识符。这就是我们之前用来重定向用户的 PaymentId。
64157032-d3dc-4296-aeda-fd4b0994c64e
2.2. 调用 Barion API
要请求付款详情,我们使用上面的标识符调用 Barion 客户端类的 GetPaymentState 方法
$paymentDetails = $BC->GetPaymentState("64157032-d3dc-4296-aeda-fd4b0994c64e");
根据响应中收到的付款状态和参数,商店现在可以决定付款是否成功。
基本故障排除
在联系我们的支持之前,这里有一些常见的错误您可能想要再次检查
1. 在发送我的请求时,我得到 "用户身份验证失败" 错误
- 检查您是否将正确的 POSkey 发送到正确的环境,例如,如果您想在测试环境中调用 API,请使用您在测试网站上注册的商店的 POSkey。
- 检查发送的数据是否确实是有效的 JSON 字符串,不包含任何特殊字符、分隔符、换行符或无效编码。
2. 在测试环境中,我收到 "商店已关闭" 错误消息
- 在登录 Barion 测试网站后,检查您的商店是否已打开。请注意,您必须填写您商店的每项数据,然后将其发送到审批。在此之后,审批将自动完成,您的商店将处于打开状态。这仅适用于测试环境。
更多示例
要查看有关 Barion 库使用方式的更多示例,请参阅存储库的 docs 和 examples 文件夹。
© 2017 Barion Payment Inc.