klev-o/crypto-pay-api

简单的Crypto Pay支付系统实现(@CryptoBot)

1.3.0 2024-08-30 10:20 UTC

This package is auto-updated.

Last update: 2024-08-30 10:23:04 UTC


README

CryptoPayApi

klev-o/crypto-pay-api

简单的Crypto Pay API实现,支持php版本 ^7.4。基于官方Crypto Pay Api

License Packagist Downloads GitHub release (latest by date including pre-releases) Scrutinizer code quality (GitHub/Bitbucket) GitHub last commit

📖 简介

完全面向对象且代码简单。所有可用类型和方法均通过类进行描述,所有字段的文档都已提供。对于每个类,都指明了文档的URL,您可以在其中研究细节等。

🛠 安装

在您的命令行中运行此命令

composer require klev-o/crypto-pay-api

🔌 使用方法

常见

首先,您需要创建CryptoPay类的对象,并将您的应用程序的API密钥传递给构造函数

<?php

require_once '../vendor/autoload.php';

$api = new \Klev\CryptoPayApi\CryptoPay('YOUR_APP_TOKEN');

要创建应用程序并获得API令牌,请打开@CryptoBot@CryptoTestnetBot(测试网),发送/pay命令,然后转到“创建应用程序”。

您可以将第二个参数$ isTestnet传递给CryproPay构造函数,使所有请求都通过测试网进行。这对于任何实验都很有用)

<?php

require_once '../vendor/autoload.php';

//the second parameter true - activates the testnet
$api = new \Klev\CryptoPayApi\CryptoPay('YOUR_APP_TOKEN', true);

然后您可以调用所有可能的方法。为了检查一切是否正常工作,您可以调用$api->getMe()方法,它将返回有关您的应用程序的基本信息。

<?php

require_once '../vendor/autoload.php';

$api = new \Klev\CryptoPayApi\CryptoPay('YOUR_APP_TOKEN');

$result = $api->getMe();

print_r($result)

如果一切正常,您将看到以下内容

//Display if everything is working well
Array
(
    [app_id] => 12345 //your id will be different
    [name] => Some App //your name will be different
    [payment_processing_bot_username] => CryptoBot
)

对于更复杂的方法,使用值对象,例如,以下是如何创建发票的示例

<?php

use Klev\CryptoPayApi\Methods\CreateInvoice;
use Klev\CryptoPayApi\CryptoPay;
use Klev\CryptoPayApi\Enums\PaidBtnName;

require_once '../vendor/autoload.php';

$api = new CryptoPay('YOUR_APP_TOKEN');

$data = new CreateInvoice('TON', '0.53');
$data->allow_anonymous = false;
$data->paid_btn_name = PaidBtnName::OPEN_CHANNEL;

$createdInvoice = $api->createInvoice($data)

如您所见,必需参数通过构造函数传递,而可选参数可以直接设置。此外,对于期望“其中之一”的值,还创建了位于命名空间Klev\CryptoPayApi\Enums的特殊枚举。

按照相同的原则,创建了针对getInvoice()Transfer()方法的对象

更详细的示例可以在demo目录中找到

所有可用方法都可以在$api对象中查看,或参考官方文档

Webhooks

使用Webhook,您可以接收应用程序的更新

请注意,必须在您的应用程序中首先激活拦截器。可以通过以下方式完成:打开@CryptoBot@CryptoTestnetBot(测试网),发送/pay命令,然后转到“我的应用程序”,选择您的应用程序,打开“Webhooks”,然后点击“启用Webhooks”。更多信息请在此查看

要接收更新,您需要使用$api->getWebhookUpdates()方法并订阅以监听必要的活动。目前只有一种事件(或更新类型)可用 - invoice_paid。它还有一个枚举 - PaidType::INVOICE_PAID

<?php

use Klev\CryptoPayApi\Methods\CreateInvoice;
use Klev\CryptoPayApi\CryptoPay;
use Klev\CryptoPayApi\Enums\PaidType;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;

require_once '../vendor/autoload.php';

//The logger does not have to be created, it is used only for an example
$log = new Logger('App');
$log->pushHandler(new StreamHandler('../var/logs/app.log'));

$api = new CryptoPay('YOUR_APP_TOKEN');
$api->setEnableEvents(true);

//subscribe to an event
$api->on(PaidType::INVOICE_PAID, static function(Update $update) use ($log) {
    //do something with the data
    $log->info('webhook data: ', (array)$update);
});

$api->getWebhookUpdates();

您可以注册多个处理程序来处理同一事件。

您也可以直接从$api->getWebhookUpdates()方法获取结果,而无需订阅事件

默认情况下,事件是禁用的。要启用它们,您需要使用方法$api->setEnableEvents(true);

📟 高级

同样,您可以使用任何与可调用类型对应的内容作为事件处理程序。以下是一个示例

假设我们已安装composer require php-di/php-di,尽管您可以使用任何其他psr兼容的

<?php

use Klev\CryptoPayApi\CryptoPay;
use Klev\CryptoPayApi\Enums\PaidType;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Psr\Log\LoggerInterface;

require_once '../vendor/autoload.php';

$api = new CryptoPay('YOUR_APP_TOKEN');

//Use Container builder
$builder = new DI\ContainerBuilder();

$builder->addDefinitions([
    //specify the rules on how to create an object
    LoggerInterface::class => function(\DI\Container $c) {
        $log = new Logger('App');
        $log->pushHandler(new StreamHandler('../var/logs/app.log'));
        return $log;
    },
    //specify the rules on how to create an object
    InvoicePaidListener::class => function(\DI\Container $c) {
        return new InvoicePaidListener($c->get(LoggerInterface::class));
    }
]);
$container = $builder->build();

//Instead of using an anonymous function, we can now use a custom class, into which,
//if necessary, we can pull everything we need (working with the database, sending by mail, etc.)
class InvoicePaidListener
{
    private Logger $log;
    
    public function __construct(Logger $log)
    {
        $this->log = $log;
    }
    public function __invoke(Update $update)
    {
        $this->log->info('payload', (array)$update);
    }

}

//Now the event subscription looks more concise
$api->on(PaidType::INVOICE_PAID, $container->get(InvoicePaidListener::class));

$api->getWebhookUpdates();

您可以使用您的依赖注入容器将所有必要的功能从您的代码中传递到处理程序

🎁 赞助

如果您喜欢该项目,请支持它。资金将用于食物。

🧨故障排除

请,如果您发现任何错误或不完全正确的地方,请在此问题页面报告。

最后...

祝您愉快地使用机器人 🤖