slevomat / csob-gateway
CSOB支付网关客户端
6.1.0
2024-03-04 09:38 UTC
Requires
- php: ^8.1
- ext-mbstring: *
- ext-openssl: *
Requires (Dev)
- ext-curl: *
- guzzlehttp/guzzle: ^6.4.1 || ^7.0.1
- php-parallel-lint/php-parallel-lint: ^1.3.1
- phpstan/phpstan: ^1.7.15
- phpstan/phpstan-phpunit: ^1.1.1
- phpstan/phpstan-strict-rules: ^1.2.3
- phpunit/phpunit: ^9.5.21
- psr/log: ^3.0.0
- slevomat/coding-standard: ^8.0.0
- squizlabs/php_codesniffer: ^3.7.1
Suggests
- ext-curl: cURL driver for usage of CurlDriver
- guzzlehttp/guzzle: HTTP client for usage of GuzzleHttpClient
- psr/log: Common interface for logging of requests - implementations: https://packagist.org.cn/providers/psr/log-implementation
README
此仓库提供了对ČSOB支付网关的客户端库。
库支持 eAPI 1.9 的所有端点(除了 NEJsplátku(贷款@商店))。欢迎提交拉取请求。
旧版可用版本(非活跃维护)
- 版本 5.* 支持 PHP 7.2 和 eAPI 1.8
- 版本 4.* 支持 PHP 7.2 和 eAPI 1.7
- 版本 3.* 支持 PHP 7 和 eAPI 1.6。
- 版本 2.* 支持 PHP 7 和 eAPI 1.5。
- 版本 1.* 支持 PHP 5.6 和 eAPI 1.5。
安装
安装 slevomat/csob-gateway 的最佳方法是使用 Composer
> composer require slevomat/csob-gateway
使用方法
首先,您需要在网关中初始化支付并将客户重定向到其界面。
警告:请注意,所有价格都是以货币单位的百分之一表示。这意味着当您想初始化 1.9 欧元的支付时,您应该在这里传递整数 190。
$apiClient = new ApiClient( new CurlDriver(), new CryptoService( $privateKeyFile, $bankPublicKeyFile ), 'https://api.platebnibrana.csob.cz/api/v1.8' ); $requestFactory = new RequestFactory('012345'); // cart has to have at least 1 but most of 2 items $cart = new Cart(Currency::EUR); $cart->addItem('Nákup', 1, 1.9 * 100); $customer = new Customer( 'Jan Novák', 'jan.novak@example.com', mobilePhone: '+420.800300300', customerAccount: new CustomerAccount( new DateTimeImmutable('2022-01-12T12:10:37+01:00'), new DateTimeImmutable('2022-01-15T15:10:12+01:00'), ), customerLogin: new CustomerLogin( CustomerLoginAuth::ACCOUNT, new DateTimeImmutable('2022-01-25T13:10:03+01:00'), ), ); $order = new Order( OrderType::PURCHASE, OrderAvailability::NOW, null, OrderDelivery::SHIPPING, OrderDeliveryMode::SAME_DAY, addressMatch: true, billing: new OrderAddress( 'Karlova 1', null, null, 'Praha', '11000', null, Country::CZE, ), ); $paymentResponse = $requestFactory->createInitPayment( 123, PayOperation::PAYMENT, PayMethod::CARD, true, $returnUrl, HttpMethod::POST, $cart, $customer, $order, 'some-base64-encoded-merchant-data', '123', Language::CZ, 1800, 1, 2, )->send($apiClient); $payId = $paymentResponse->getPayId(); $processPaymentResponse = $requestFactory->createProcessPayment($payId)->send($apiClient); // redirect to gateway header('Location: ' . $processPaymentResponse->getGatewayLocationUrl());
客户从网关返回后,将被重定向到 $returnUrl
,您必须在这里处理支付。
try { $receivePaymentResponse = $requestFactory->createReceivePaymentRequest()->send($apiClient, $_POST /* $_GET */); if ($receivePaymentResponse->getPaymentStatus() === PaymentStatus::S7_AWAITING_SETTLEMENT) { // payment was successful! } } catch (VerificationFailedException | InvalidSignatureException $e) { // request was not send from csob api }
请参考 CSOB 文档,了解您应该检查哪些状态,它们都作为 PaymentStatus::S* 常量可用。
自定义 ApiClientDriver
API 调用通过 ApiClientDriver
接口进行。库包含两个默认的驱动实现 - CurlDriver 和 GuzzleDriver。您也可以通过实现 ApiClientDriver
接口并传递给 ApiClient
构造函数来创建自己的驱动。
CurlDriver
通过 curl
PHP 扩展进行通信,GuzzleDriver
使用 guzzlehttp/guzzle 库。如果您想使用 GuzzleDriver,您需要在 composer.json 中引入 guzzlehttp/guzzle
包。