dizpay / dizpay-php-sdk
DizPay API 2.0 的官方 PHP 库
Requires
- guzzlehttp/guzzle: ^6.3
This package is auto-updated.
Last update: 2024-09-27 14:23:38 UTC
README
这是一个开源的 PHP SDK,允许您从 PHP 应用程序访问 DizPay API。
目录
简介
DizPay API 允许您将 BTC、ETH、LTC、DASH、USDT、GUSD、PAX、TUSD 和 USDC 支付集成到您的在线应用程序中。使用 DizPay PHP SDK,您可以将 DizPay 集成到多个 PHP 框架中,例如 Laravel、Lumen 和 ThinkPHP。
在开始使用 DizPay 之前,请确保您已从 DizPay 网站注册并获取您的 App ID 和 App Key。
DizPay SDK API 被分组为
- 资产 API
- 结算 API
- 订单 API
- 汇率 API
您可以使用这些 API 发送请求并从 DizPay 获取响应。有关每个 API 请求和响应的详细信息,请参阅 API 参考手册。
先决条件
PHP 5.6 或更高版本
安装
使用 composer
安装
composer require dizpay/dizpay-php-sdk:dev-master
使用方法
在您的 PHP 文件中使用以下代码行来设置 DizPay
require_once __DIR__.'/../vendor/autoload.php'; $profile = new \DizPay\DizPayProfile('YOUR_APP_ID', 'YOUR_APP_KEY');
完成设置后,您可以使用上面创建的 profile
变量向 DizPay API 发送请求。
使用 DizPay SDK 有两种方式:checkout
和 API
。
-
checkout
是默认集成方法,您可以使用 DizPay UI。 -
API
是原生方法,您需要实现自己的 UI。
结算
要使用 checkout
,调用 checkout::invoice
方法。它返回一个付款 URL 地址。只需将提供的 URL 添加到您的网页上,使其对所有用户都可用。
用户完成支付后,DizPay 通过同步(通过 success_url
)和异步(通过 notify_url
)通知向您发送相应的结果。
- 同步通知:它不带任何参数,当它到达时,您应该始终将其视为成功。
- 异步通知:DizPay 在创建 API 时通过设置在
notify_url
键中的地址向您发送一个POST
通知 payStatus/amount/orderNo。
API
使用此方法,您需要使用 DizPay API 参考手册 实现自己的收银程序。
创建 API 实例有两种方式。
- 通过工厂
$profile = new \DizPay\DizPayProfile('YOUR_APP_ID', 'YOUR_APP_KEY'); $assets = \DizPay\DizPayAPIFactory::assets($profile); $checkout = \DizPay\DizPayAPIFactory::checkout($profile); $orders = \DizPay\DizPayAPIFactory::orders($profile); $rates = \DizPay\DizPayAPIFactory::rates($profile);
- 通过
new
操作符
$profile = new \DizPay\DizPayProfile('YOUR_APP_ID', 'YOUR_APP_KEY'); $assets = new \DizPay\Api\Assets($profile); $checkout = new \DizPay\Api\Checkout($profile); $orders = new \DizPay\Api\Orders($profile); $rates = new \DizPay\Api\Rates($profile);
API 包含两个属性
baseUri
prefix
然后,每个 API 调用都发送到 DizPay,格式为: baseUri + prefix + endPoint
,其中 endPoint
由方法名解析。
然后,PHP 魔术方法 __call
将方法名(SDK API 名称,为大写字母)转换为 snakecase 并用作实际请求端点。
例如,$order->createChargeOrder(...)
将转换为 create_charge_order
。
因此,最终的请求发送如下
curl -X POST -H 'Content-Type: application/json' -H 'Accept: application/json' -d <YOUR_FORM_DATA> https://api.dizpay.com/v2/member/orders/create_charge_order
源代码
// Source Code at src/DizPay/Common/BaseAPI.php public function __call($name, $arguments) { $endPoint = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $name)); $payloads = $this->preparedPayload($arguments ? $arguments[0] : []); $this->response = $this->httpClient->post($this->prefix . '/' . $endPoint, [ 'json' => $payloads, ]); return $this; }
示例代码
以下代码行展示了如何使用在设置过程中创建的 profile
变量获取示例发票
$checkout = \DizPay\DizPayAPIFactory::checkout($profile); $invoice = $checkout->invoice( [ 'order_number' => uniqid('TEST_ORDER:'), # required 'name' => 'DizPay Inc.', // 'Your Merchant Name, eg: DizPay Inc.', 'description' => 'Add crypto to your account.', // 'optional, default is: Add crypto to your {{ Domain or App Name }} account.', 'logo_url'=> 'https://cdn.shopifycloud.com/hatchful-web/assets/c3a241ae6d1e03513dfed6f5061f4a4b.png', 'payer_info' => 'email', // one of in ['', 'email', 'mobile'] 'pricing_type' => 'fixed_price', // one of in ['fixed_price', 'no_price'], 'currency' => 'USD', // required, split by commas, valid option is USD | CNY | GBP | BTC | ETH | LTC | DASH | USDT | TUSD | GUSD | PAX | USDC 'amount' => '100', // string, required 'rate' => 'huobi', // optional, default is coinmarketcap, one of in ['coinmarketcap', 'okex', 'binance', 'huobi'] 'crypto' => 'USDC,TUSD,PAX,GUSD,USDT,ETH,BTC,LTC,DASH', // required, split by commas, valid option is BTC | ETH | LTC | DASH | USDT | TUSD | GUSD | PAX | USDC 'locale' => 'auto', // language, one of in ['auto', 'en', 'cn', 'ru', 'ko', 'jp'] 'success_url' => 'https://example.com/diz-pay-result?type=success', // optional, redirect to the merchant URL after successful payment. 'cancel_url' => 'https://example.com/diz-pay-result?type=failed', // optional, edirect to a failure URL when the charge failed to complete. The buyer cancels the order or the payment expired. 'notify_url' => 'https://demo.dizpay.com/webhook', // optional, Send information to the callback URL when charge has been confirmed and the associated payment is completed. 'extra' => 'another-params', ] ); echo 'Checkout API Call Result:' . ($invoice->isSuccessful() ? 'Successful' : 'Failed') .PHP_EOL . 'Response:' . $invoice;
为了帮助您开始,您可以查看其他示例 API 调用。
响应代码
一旦您发送请求,DizPay API 会在响应中发送一个代码。最常见的返回响应代码是
如果响应代码为 400
,DizPay API 会返回以下错误代码