gopay / payments-sdk-php
GoPay的PHP SDK,用于 Payments REST API
Requires
- php: >=8.1
- guzzlehttp/guzzle: ^7.7.0
- symfony/deprecation-contracts: ^3.3.0
Requires (Dev)
- hamcrest/hamcrest-php: *
- phpspec/prophecy: ~1.0
- phpspec/prophecy-phpunit: ^2.0
- phpunit/phpunit: 9.3.7
- dev-master
- 1.10.1
- 1.10.0
- 1.9.0
- 1.8.0
- v1.7.3
- v1.7.2
- v1.7.1
- v1.7.0
- v1.6.2
- v1.6.1
- v1.6.0
- v1.5.7
- v1.5.6
- v1.5.5
- v1.5.4
- v1.5.3
- v1.5.2
- v1.5.1
- v1.5.0
- v1.4.7
- v1.4.6
- v1.4.5
- v1.4.4
- v1.4.3
- v1.4.2
- v1.4.1
- v1.4.0
- v1.3.6
- v1.3.5
- v1.3.4
- v1.3.3
- v1.3.2
- v1.3.1
- v1.3.0
- v1.2.3
- v1.2.2
- v1.2.1
- v1.2.0
- v1.1.1
- v1.1.0
- v1.0.1
- v1.0.0
- dev-feature/user-agent-with-sdk-version
- dev-hotfix/GPMAIN-7759-bnpl
- dev-hotfix/GPOMA-134-php-capture-na-nizsi-castku
- dev-supercash-batch
- dev-hotfix/GPOMA-70-php-api-revize
This package is not auto-updated.
Last update: 2024-09-24 07:49:35 UTC
README
需求
- PHP >= 8.1
- 启用扩展
curl
,json
安装
安装SDK最简单的方法是使用 Composer
composer require gopay/payments-sdk-php
基本用法
// minimal configuration $gopay = GoPay\Api::payments([ 'goid' => 'my goid', 'clientId' => 'my id', 'clientSecret' => 'my secret', 'gatewayUrl' => 'gateway url' ]); // full configuration $gopay = GoPay\Api::payments([ 'goid' => 'my goid', 'clientId' => 'my id', 'clientSecret' => 'my secret', 'gatewayUrl' => 'gateway url', 'scope' => GoPay\Definition\TokenScope::ALL, 'language' => GoPay\Definition\Language::CZECH, 'timeout' => 30 ]);
配置
必填字段
可选字段
可用方法
SDK响应?我的调用成功了吗?
SDK返回包装后的API响应。每个方法都返回 GoPay\Http\Response
对象。json/__toString
的结构应与 文档 中相同。SDK不会抛出异常。如果您遇到异常,请创建一个问题。
$response = $gopay->createPayment([/* define your payment */]); if ($response->hasSucceed()) { echo "hooray, API returned {$response}"; return $response->json['gw_url']; // url for initiation of gateway } else { // errors format: https://doc.gopay.com/en/?shell#http-result-codes echo "oops, API returned {$response->statusCode}: {$response}"; }
必填字段和允许的值是否经过验证?
不。 API 非常详细地验证字段,因此无需在SDK中重复验证。这只会引入新的错误类型。或者我们不得不完美地模拟API错误消息。这就是为什么SDK只调用API,其行为在doc.gopay.com中得到了很好的文档说明。
高级用法
支付网关的初始化
// create payment and pass url to template $response = $gopay->createPayment([/* define your payment */]); if ($response->hasSucceed()) { $gatewayUrl => $response->json['gw_url'], $embedJs => $gopay->urlToEmbedJs() // render template }
内联网关
<form action="<?= $gatewayUrl ?>" method="post" id="gopay-payment-button"> <button name="pay" type="submit">Pay</button> <script type="text/javascript" src="<?= $embedJs ?>"></script> </form>
重定向网关
<form action="<?= $gatewayUrl ?>" method="post"> <button name="pay" type="submit">Pay</button> </form>
使用JavaScript异步初始化
枚举 (代码列表)
您可以使用预定义的枚举来代替硬编码银行代码字符串。请参阅 create-payment 示例 中的枚举用法
框架集成
缓存访问令牌
访问令牌在30分钟后过期,因此为每次请求使用新令牌代价高昂。不幸的是,这是GoPay\Token\InMemoryTokenCache
的默认行为。但是,您可以实现自己的缓存并将令牌存储在Memcache、Redis、文件中……这取决于您。
您的缓存必须实现GoPay\Token\TokenCache
接口。请注意,存在两个作用域(TokenScope
)并且SDK可以用于不同的客户端(clientId
,gatewayUrl
)。因此,传递给方法的client
是当前环境的唯一标识符(string
),它为此环境构建。以下是将令牌缓存到文件的示例实现:
// register cache in optional service configuration $gopay = GoPay\payments( [/* your config */], ['cache' => new PrimitiveFileCache()] );
<?php use GoPay\Token\TokenCache; use GoPay\Token\AccessToken; class PrimitiveFileCache implements TokenCache { public function setAccessToken($client, AccessToken $t) { file_put_contents(__DIR__ . "/{$client}", serialize($t)); } public function getAccessToken($client) { $file = __DIR__ . "/{$client}"; if (file_exists($file)) { return unserialize(file_get_contents($file)); } return null; } }
记录HTTP通信
您可以记录与API通信中的每个请求和响应。请参阅以下可用的记录器。或者,您可以实现自己的记录器,只需实现GoPay\Http\Log\Logger
接口。
// register logger in optional service configuration $gopay = GoPay\payments( [/* your config */], ['logger' => new GoPay\Http\Log\PrintHttpRequest()] );
贡献
许可证
版权(c)2015 GoPay.com。MIT许可证,有关详细信息请参阅 LICENSE。