zoge / yii2-barion
Yii2 Barion 扩展
This package is auto-updated.
Last update: 2024-09-09 17:48:28 UTC
README
BarionPHP 是一个紧凑的 PHP 库,用于通过 Barion 智能网关 管理在线电子货币和卡片支付。它允许您仅用几行代码即可接受信用卡和电子货币支付。
BarionPHP 让您
- 轻松启动在线即时或预订支付
- 获取有关特定支付的详细信息
- 完全或部分完成正在进行中的预订支付,并提供自动退款支持
- 完全或部分退款已完成支付交易
只需几行简单代码即可完成所有操作!
版本历史
- 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 已启用(使用 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 HUF 的一个产品支付。
1. 创建请求对象
要启动在线支付,您必须创建一个或多个 支付交易 对象,向它们添加交易 项目,然后将这些交易组合在一个 支付 对象中。
首先,创建一个 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 Smart Gateway 生成的费用交易。在这种情况下,这些是网关使用费和银行卡处理费。
注意:相关费用交易的数量和类型取决于请求调用者的身份 - 请参阅https://docs.barion.com 的文档或联系我们的销售部门。
注意:RequestSuccessful 参数仅表示 HTTP 请求-响应通信本身已成功完成。它不涉及支付过程的任何部分。如果在请求处理过程中发生任何失败,API 错误消息将在 Errors 数组中找到。
3. 将用户重定向到 Barion Smart Gateway
您可以使用响应中的 PaymentId 值将用户重定向到 Barion Smart Gateway。您必须在 Id 查询字符串参数中提供此标识符。完整的重定向 URL 看起来像这样
https://secure.barion.com/Pay?id=64157032-d3dc-4296-aeda-fd4b0994c64e
用户现在可以在 Barion Smart Gateway 中完成支付。
获取支付信息
在这个例子中,我们将获取上面创建的支付的详细信息。
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) 用户账户完成的。
基本故障排除
以下是一些您在联系我们的支持之前可能想要检查的常见错误
1. 发送请求时我收到 "用户身份验证失败" 错误
- 检查您是否将正确的 POSkey 发送到正确的环境,例如,如果您想调用 TEST 环境中的 API,请使用您在 TEST 网站上注册的商店的 POSkey。
- 检查发送的数据是否实际上是有效的 JSON 字符串,不包含任何特殊字符、分隔符、换行符或无效编码。
2. 在 TEST 环境中我收到 "商店关闭" 错误消息
- 登录到 Barion Test 网站后,检查您的商店是否已打开。请注意,您必须填写您商店的每一项数据,然后将其发送以供批准。在此之后,批准将自动完成,您的商店将处于 Open 状态。这仅适用于 TEST 环境。
更多示例
要查看更多关于 Barion 库使用的示例,请参阅存储库的 examples 文件夹中的示例文件。
© 2017 Barion Payment Inc.