apirone / apirone-sdk-php
Apiron SDK
Requires
- apirone/apirone-api-php: ^1.0
- apirone/php-qr-code: ^1.0
README
Apirone SDK PHP
此库提供用于处理 Apirone 发票的类。轻松集成加密货币支付。
要求
- PHP 7.4 或更高版本,带有 cURL 和 JSON 扩展。已测试至 8.2。
- MySQL 或 MariaDB
- Apirone API PHP
安装和使用准备
使用 composer 安装库。
composer require apirone/apirone-sdk-php
资产配置
将 src/assets
文件夹复制到您的公共 HTML 目录。在发票显示页面,您需要添加来自资产文件夹的 css/styles.css
和 js/script.js
。您还可以使用最小化版本。
数据库和日志处理器
要将库连接到您的数据库引擎,您需要创建两个回调函数并将它们传递给库。
php MySQLi 的数据库处理器示例
// Our MySQL engine example $conn = new mysqli('host', 'user', 'pass', 'database'); $conn->select_db('database'); // DB MySQL handler example $db_handler = static function($query) { global $conn; $result = $conn->query($query, MYSQLI_STORE_RESULT); if (!$result) { return $conn->error; } if (gettype($result) == 'boolean') { return $result; } return $result->fetch_all(MYSQLI_ASSOC); };
对于日志记录,您可以使用回调函数或 Psr/Log/LoggerInterface 实现的日志记录器。
日志回调示例
$logger = static function($level, $message, $context) { print_r([$level, $message, $context]); };
Psr/Log 示例
$logger = new /Psr/Log/LoggerInterface();
将处理器设置到库中
use Apirone\SDK\Invoice; Invoice::db($db_handler, 'table_prefix_'); Invoice::setLogger($logger);
创建发票表
use Apirone\SDK\Service\InvoiceDb; // Create the invoice table if it doesn't exist InvoiceDb::install('table_prefix', $charset = 'urf8', $collate = 'utf_general_ci');
您可以在一个公共文件中设置账户类,并将其包含到您的代码中
// Setup invoice handlers and Settings Invoice::db($db_handler, $table_prefix); Invoice::log($log_handler); Invoice::settings(Settings::fromFile('/absolute/path/to/settings.json'));
设置
设置类用于创建账户、管理货币设置以及保存和恢复设置对象。
创建一个新的设置对象
use Apirone\SDK\Model\Settings; $settings = Settings::init(); $settings->createAccount(); // Save the settings object to the file $settings->toFile('absolute/path/to/settings.json'); // Get settings as JSON-object to save it to the database $json = $settings->toJson();
加载现有的设置对象
$fromFile = Settings::fromFile('/absolute/path/to/settings.json'); // For example you have get_option function $json = get_option('apirone_settings'); $fromJson = Settings::fromJson($json);
创建发票
use Apirone\SDK\Invoice; use Apirone\SDK\Model\Settings; // Setup invoice handlers and Settings Invoice::db($db_handler, $table_prefix); Invoice::log($log_handler); Invoice::settings(Settings::fromFile('/absolute/path/to/settings.json')); $invoice = Invoice::init('btc', 25000); $invoice->callbackUrl('https://example.com/callback_page'); $invoice->lifetime(1800); $invoice->crete(); // You can see the created invoice JSON data $json = $invoice->toJson();
获取现有发票
use Apirone\SDK\Invoice; use Apirone\SDK\Model\Settings; // Setup invoice handlers and Settings Invoice::db($db_handler, $table_prefix); Invoice::log($log_handler); Invoice::settings(Settings::fromFile('/absolute/path/to/settings.json')); $id = '9qAs2OQao43VWz72'; $invoice = Invoice::getInvoice($id);
回调处理器
Apirone 服务通过 回调 与库交互。要获取发票状态,您需要在代码中创建一个 URL 并添加一个回调处理器。此地址必须在创建新发票时设置。
如果您需要根据发票状态处理订单状态,您可以创建一个回调函数来处理状态更改。
// Create an order process function $order_status_handler = static function ($invoice) { // You need to set the order_id when creating an invoice. $order_id = $invoice->order(); // ... Write here your business logic } // ... Setup invoice handlers and settings Invoice::callbackHandler($order_status_handler);
显示发票
渲染加载器
要显示发票,请使用 renderLoader()
函数。还应在页面模板中添加来自资产文件夹的样式文件和 js 脚本。
use Apirone\SDK\Invoice; use Apirone\SDK\Model\Settings; // Setup invoice handlers and Settings Invoice::db($db_handler, $table_prefix); Invoice::log($log_handler); Invoice::settings(Settings::fromFile('/absolute/path/to/settings.json')); // Set invoice data url - render ajsx response page Invoice::dataUrl('render_ajax_response.php'); // In case when $invoice_id does not set function try to find id from request params - $_GET['invoice'] $ = Invoice::renderLoader(); // If you got the required invoice ID any other way, just pass it to the function $id = 'MyInvoiceId'; Invoice::renderLoader($id);
渲染 AJAX 响应
要更新发票数据,您需要创建一个 URL 并将 renderAjaxResponse()
函数添加到代码中。此 URL 用作 Invoice::dataUrl()
参数。
use Apirone\SDK\Invoice; use Apirone\SDK\Model\Settings; // Setup invoice handlers and Settings Invoice::db($db_handler, $table_prefix); Invoice::log($log_handler); Invoice::settings(Settings::fromFile('/absolute/path/to/settings.json')); Invoice::renderAjax();
示例和游乐场
在 examples
文件夹中,您可以找到库使用的现成实现。
您还可以运行游乐场。要运行游乐场,您需要安装 Docker 和 Docker Compose。
为此,您需要将 docker-compose.yml
文件从 docker
文件夹复制到库根目录并运行命令
docker-compose up -d --build
在容器启动后,游乐场将在浏览器中的 https:// 可用
支持
许可
MIT 许可
版权所有(c) © 2017-2023. Apirone。保留所有权利。
特此授予任何人免费获取此软件及其相关文档文件(“软件”)的副本的许可,用于在软件上不受限制地处理,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件副本,并允许向软件提供的人这样做,但受以下条件约束
上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。
本软件按“原样”提供,不提供任何明示或暗示的保证,包括但不限于适销性、适用于特定目的和非侵权的保证。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任负责,无论源于合同行为、侵权或其他,无论是否与软件或软件的使用或其他相关。