jakabj16/barion-web-php

Barion web API for Yii 2

安装: 10

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 42

类型:yii2-extension

dev-master 2018-01-31 12:37 UTC

This package is auto-updated.

Last update: 2024-09-06 04:09:08 UTC


README

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

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 已启用(使用 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. 创建请求对象

要启动在线支付,您必须创建一个或多个 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.