Platon 集成

2.0.1 2023-05-24 12:37 UTC

README

PHP Composer Latest Stable Version Total Downloads codecov

Platon SDK for PHP.
变更日志 | 使用示例

API

安装

使用 composer

composer require wearesho-team/platon

使用

配置

为了配置,您必须使用 ConfigInterface。可用的实现

  • Config - 简单实体。示例
<?php

use Wearesho\Bobra\Platon;

$config = new Platon\Config("Key", "Pass", "PaymentType");
<?php

use Wearesho\Bobra\Platon;

$config = new Platon\EnvironmentConfig();

环境配置

生成支付配置

使用 Client 获取支付配置

<?php

use Wearesho\Bobra\Platon;
use Wearesho\Bobra\Payments;

$config = new Platon\EnvironmentConfig();
$client = new Platon\Client($config);

$payment = $client->createPayment(
    new Payments\UrlPair(
        'http://redirect-url-on-success',
        'http://redirect-url-on-fail'
    ),
    new Platon\Transaction(
        $serviceId = 1,
        $amount = 500,
        $paymentType = 'paymentType',
        $description = 'description for payment',
        $ext = [
            0 => 'some-info'            
        ], // optional, will be returned in notification
        $currency = 'UAH', // optional 
        $formId = 'Form id for front-end' // optional
    )
);

渲染表单

<?php

$config = $payment->jsonSerialize(); // ['action' => 'URL', 'data' => 'url']

*您应将 data 发送到 action URL。

通知

使用 Notification\Server 从 Platon 请求创建支付实例

<?php

use Wearesho\Bobra\Platon;

class PlatonController
{
    public function actionHandle()
    {
        // You can use several platon configs in different cases.
        // Handler will get that one that matches `key` param from platon`s request.
        // All platon configs should be passed into ConfigProvider.
        $configProvider = new Platon\Notification\ConfigProvider([
            new Platon\Config('First key', 'first pass', 'CC'),    
            new Platon\Config('Second key', 'second pass', 'CC'),    
        ]);

        $server = new Platon\Notification\Server($configProvider);

        try {
            $payment = $server->handle($_POST);   
        } catch (Platon\Notification\InvalidSignException $exception) {
            // handle invalid sign
        } catch (\InvalidArgumentException $exception) {
            // When received data is incorrect or some required fields are empty
        }

        // You can use returned Payment instance to save transaction data.
    }
}

信息

获取账户余额信息。使用 Info\ConfigInterface 进行配置。 EnvironmentConfig 可用

<?php

use Wearesho\Bobra\Platon;

$publicKey = readline("Public Key: ");
$privateKey = readline("Private Key: ");

$config = new Platon\Info\Config($publicKey, $privateKey);
$client = new GuzzleHttp\Client();

$repository = new Platon\Info\Repository($config, $client);

// All items can be converted to string and JSON formats
$responses = $repository->get();

详见 Info\Response

作者

许可证

MIT