gopay/payments-sdk-php

GoPay的PHP SDK,用于 Payments REST API


README

License Latest Stable Version Total Downloads Monthly Downloads Dependency Status

需求

  • 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可以用于不同的客户端(clientIdgatewayUrl)。因此,传递给方法的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