first commit

This commit is contained in:
Your Name
2026-01-19 14:19:22 +08:00
commit fe2d9c1868
4777 changed files with 665503 additions and 0 deletions

View File

@@ -0,0 +1 @@
/vendor

View File

@@ -0,0 +1,111 @@
# udun-sdk-php
udun-sdk-php
## 安装
### 方式1命令安装
```php
composer require uduncloud/udun-wallet-sdk
```
### 方式2composer 配置安装
1,在composer.json添加如下配置
```php
{
"require":{
"uduncloud/udun-wallet-sdk": "^1.0"
}
}
```
2,执行命令
```
composer install
```
## 使用
1,新建UdunController.php
```
use udun\Dispatch\UdunDispatch;
class UdunController{
protected $udunDispatch ;
public function __construct()
{
// 控制器初始化
$this->initialize();
}
protected function initialize(){
$this->udunDispatch = new UdunDispatch([
'merchant_no' => 30000, //商户号
'api_key' => 'c789xxxxxxxxxxxxxxxxx388ecxxx',//apikey
'gateway_address'=>'https://sig11.udun.io', //节点
'callUrl'=>'https://localhost/callUrl', //回调地址
'debug' => false //调试模式
]);
}
}
```
2,在需要使用到接口的类继承 UdunController
```
##使用示例
namespace xxxx;
class Index extends UdunController{
//查询支持的交易对
public function supportCoins()
{
$result = $this->udunDispatch->supportCoins(true);
return json($result);
}
//创建地址
public function createAddress()
{
$result = $this->udunDispatch->createAddress('195');
return json($result);
}
//验证地址合法性
public function checkAddress()
{
$result = $this->udunDispatch->checkAddress('195','TEpK1aWkjDue6j8reeeMqG7hdJ5tRytyAF');
return json($result);
}
//查询地址是否存在
public function existAddress()
{
$result = $this->udunDispatch->existAddress('195','TEpK1aWkjDue6j8reeeMqG7hdJ5tRytyAF');
return json($result);
}
//申请提币
public function withdraw()
{
$result = $this->udunDispatch->withdraw('sn00001','195','195','TEpK1aWkjDue6j8reeeMqG7hdJ5tRytyAF',10);
return json($result);
}
//交易回调
public function callback()
{
$result = $this->callback();
return json($result);
}
}
```
## 其他
```
##curl: (35) OpenSSL SSL_connect: SSL_ERROR_SYSCALL in connection to raw.githubusercontent.com:443
如果提示以上错误需要添加ca证书
##php.ini 打开ssl
extension=php_openssl.dll;
##证书路径
openssl.cafile=D:\cacert.pem
```

View File

@@ -0,0 +1,24 @@
{
"name": "uduncloud/udun-wallet-sdk",
"description": "udun-wallet-sdk",
"version": "1.0.0",
"type": "1",
"license": "MIT",
"authors": [
{
"name": "udun wallet"
}
],
"require": {
"hanson/foundation-sdk": "^5.0",
"php": ">=7.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5"
},
"autoload": {
"psr-4": {
"Udun\\Dispatch\\": "src/"
}
}
}

2799
vendor/uduncloud/udun-wallet-sdk/composer.lock generated vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,41 @@
<?php
require_once __DIR__.'/../vendor/autoload.php';
$merchant_no = 30000; //商户号
$api_key= 'c789324a4XXXXXXX88ec8a3872';// apikey
$gateway_address= 'https://sig11.udun.io'; //节点
$callUrl = "https://localhost/callUrl"; //回调地址
/** @var \udun\Dispatch\UdunDispatch $result */
$result = new \Udun\Dispatch\UdunDispatch([
'merchant_no' => $merchant_no,
'api_key' => $api_key,
'gateway_address'=>$gateway_address,
'callUrl'=>$callUrl,
'debug' => true
]);
//查询支持的交易对
//var_dump($result->supportCoins(true));
//返回内容 { ["code"]=> int(200) ["data"]=> array(6) { [0]=> array(10) { ["coinType"]=> string(1) "0" ["symbol"]=> string(3) "BTC" ["tokenStatus"]=> int(0) ["mainCoinType"]=> string(1) "0" ["balance"]=> int(0) ["decimals"]=> string(1) "8" ["name"]=> string(3) "BTC" ["logo"]=> string(46) "https://img.udresource.com/public/coin/BTC.png" ["coinName"]=> string(7) "Bitcoin" ["mainSymbol"]=> string(0) "" } [1]=> array(10) { ["coinType"]=> string(2) "31" ["symbol"]=> string(4) "OMNI" ["tokenStatus"]=> int(1) ["mainCoinType"]=> string(1) "0" ["balance"]=> int(0) ["decimals"]=> string(1) "8" ["name"]=> string(9) "USDT-OMNI" ["logo"]=> string(79) "https://img.udresource.com/public/coin/27a1cb00-7905-4975-b485-208639311346.png" ["coinName"]=> string(9) "USDT-OMNI" ["mainSymbol"]=> string(3) "BTC" } [2]=> array(9) { ["coinType"]=> string(3) "195" ["symbol"]=> string(3) "TRX" ["tokenStatus"]=> int(0) ["mainCoinType"]=> string(3) "195" ["balance"]=> int(0) ["decimals"]=> string(1) "6" ["name"]=> string(3) "TRX" ["logo"]=> string(46) "https://img.udresource.com/public/coin/TRX.png" ["coinName"]=> string(4) "TRON" } [3]=> array(10) { ["coinType"]=> string(34) "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t" ["symbol"]=> string(7) "TRCUSDT" ["tokenStatus"]=> int(1) ["mainCoinType"]=> string(3) "195" ["balance"]=> int(0) ["decimals"]=> string(1) "6" ["name"]=> string(10) "USDT-TRC20" ["logo"]=> string(79) "https://img.udresource.com/public/coin/d1dc3cb6-3918-42e0-990d-42383b379237.png" ["coinName"]=> string(10) "USDT-TRC20" ["mainSymbol"]=> string(3) "TRX" } [4]=> array(10) { ["coinType"]=> string(42) "0xdac17f958d2ee523a2206206994597c13d831ec7" ["symbol"]=> string(4) "USDT" ["tokenStatus"]=> int(1) ["mainCoinType"]=> string(2) "60" ["balance"]=> int(0) ["decimals"]=> string(1) "6" ["name"]=> string(10) "USDT-ERC20" ["logo"]=> string(47) "https://img.udresource.com/public/coin/USDT.png" ["coinName"]=> string(4) "USDT" ["mainSymbol"]=> string(3) "ETH" } [5]=> array(10) { ["coinType"]=> string(2) "60" ["symbol"]=> string(3) "ETH" ["tokenStatus"]=> int(0) ["mainCoinType"]=> string(2) "60" ["balance"]=> int(0) ["decimals"]=> string(2) "18" ["name"]=> string(3) "ETH" ["logo"]=> string(46) "https://img.udresource.com/public/coin/ETH.png" ["coinName"]=> string(8) "Ethereum" ["mainSymbol"]=> string(0) "" } } ["message"]=> string(7) "SUCCESS" }
//创建地址
//var_dump($result->createAddress('195'));
//返回内容: { ["code"]=> int(200) ["data"]=> array(2) { ["coinType"]=> int(195) ["address"]=> string(34) "TEpK1aWkjDue6j8reeeMqG7hdJ5tRytyAF" } ["message"]=> string(7) "SUCCESS" }
//{ ["code"]=> int(200) ["data"]=> array(2) { ["coinType"]=> int(195) ["address"]=> string(34) "TFtLBfPJQBzhthjkjG78WxdcE7kEdJPjer" } ["message"]=> string(7) "SUCCESS" }
//验证地址合法性
//var_dump($result->checkAddress('195','TEpK1aWkjDue6j8reeeMqG7hdJ5tRytyAF'));
//返回内容: { ["code"]=> int(200) ["message"]=> string(7) "SUCCESS" ["data"]=> NULL }
//var_dump($result->existAddress('195','TEpK1aWkjDue6j8reeeMqG7hdJ5tRytyAF'));
//返回内容: { ["code"]=> int(200) ["message"]=> string(7) "SUCCESS" ["data"]=> NULL }
//申请提币
//var_dump($result->withdraw('sn00001','195','195','TEpK1aWkjDue6j8reeeMqG7hdJ5tRytyAF',10));
//返回内容: { ["code"]=> int(200) ["message"]=> string(7) "SUCCESS" }
//回调测试
// var_dump($result->callback());
//返回内容body=%7B%22address%22%3A%22TEpK1aWkjDue6j8reeeMqG7hdJ5tRytyAF%22%2C%22amount%22%3A%2210000000%22%2C%22blockHigh%22%3A%22-1%22%2C%22businessId%22%3A%22sn00001%22%2C%22coinType%22%3A%22195%22%2C%22decimals%22%3A%226%22%2C%22fee%22%3A%220%22%2C%22mainCoinType%22%3A%22195%22%2C%22memo%22%3A%22%22%2C%22status%22%3A2%2C%22tradeId%22%3A%22998006370676506624%22%2C%22tradeType%22%3A2%2C%22txId%22%3A%22%22%7D&nonce=eHSrfr&sign=679eeebb2bf221e16fe49fefef635d61&timestamp=1658370940327

View File

@@ -0,0 +1,70 @@
<?php
namespace Udun\Dispatch;
use Hanson\Foundation\AbstractAPI;
class Api extends AbstractAPI
{
//商户号
protected $merchant_no;
//apikey
protected $api_key;
//节点地址
protected $gateway_address;
//回调地址
protected $callUrl;
public function __construct( $merchant_no, string $api_key,string $gateway_address, string $callUrl)
{
$this->merchant_no = $merchant_no;
$this->api_key = $api_key;
$this->gateway_address = $gateway_address;
$this->callUrl = $callUrl;
}
/**
* @param string $method
* @param array $params
* @return result
* @throws UdunDispatchException
*/
public function request(string $method, array $body)
{
$time = time();
$nonce = rand(100000, 999999);
if($method=='/mch/support-coins'){
$body = json_encode($body);
}else{
$body = '['.json_encode($body).']';
}
$sign = $this->signature($body,$time,$nonce);
$params = array(
'timestamp' => $time,
'nonce' => $nonce,
'sign' => $sign,
'body' => $body
);
$http = $this->getHttp();
$response = $http->json($this->gateway_address. $method, $params);
$result = json_decode(strval($response->getBody()), true);
$this->checkErrorAndThrow($result);
return $result;
}
public function signature($body,$time,$nonce)
{
return md5($body.$this->api_key.$nonce.$time);
}
/**
* @param $result
* @throws UdunDispatchException
*/
private function checkErrorAndThrow($result)
{
if (!$result || $result['code'] != 200) {
throw new UdunDispatchException($result['code'], $result['message']);
}
}
}
?>

View File

@@ -0,0 +1,162 @@
<?php
namespace Udun\Dispatch;
class Clients extends Api
{
// 获取商户支持的币种信息
/**
* showBalance Boolean 是否查詢余額false不獲取true獲取
* */
public function supportCoins($showBalance = true)
{
$body = array(
'merchantId' => $this->merchant_no,//商户号
'showBalance' => $showBalance
);
return $this->request('/mch/support-coins', $body);
}
/**
* 创建地址
* mainCoinType 主币种编号
* walletId 錢包編號,默認根據主錢包生成地址
* alias 地址別名
*/
public function createAddress($mainCoinType,$walletId=null,$alias=null)
{
$body = array(
'merchantId' => $this->merchant_no,
'mainCoinType' => $mainCoinType,
'callUrl' => $this->callUrl
);
if(!empty($walletId)){
$body['walletId']=$walletId;
}
if(!empty($alias)){
$body['alias']=$alias;
}
return $this->request('/mch/address/create', $body);
}
/**
* 验证地址的合法性
* mainCoinType 主币种编号
* address 地址
*
* */
public function checkAddress($mainCoinType,$address)
{
$body = array(
'merchantId' => $this->merchant_no,//商户号
'mainCoinType' => $mainCoinType,
'address' => $address
);
return $this->request('/mch/check/address', $body);
}
/**
* 验证地址是否存在
* mainCoinType 主币种编号
* address 地址
*
* */
public function existAddress($mainCoinType,$address)
{
$body = array(
'merchantId' => $this->merchant_no,//商户号
'mainCoinType' => $mainCoinType,
'address' => $address
);
return $this->request('/mch/exist/address', $body);
}
/**
* 申请提币
* businessId 业务编号,必须唯一
* mainCoinType 主币种编号
* coinType 子币种编号
* address 地址
* amount 提币数量
* memo 备注
* */
public function withdraw($businessId,$mainCoinType,$coinType,$address,$amount,$memo='')
{
$body = array(
'merchantId' => $this->merchant_no,//商户号
'mainCoinType' => $mainCoinType,
'coinType' => $coinType,
'address' => $address,
'businessId' => $businessId,
'amount' => $amount,
'callUrl' => $this->callUrl //回调地址
);
if(!empty($memo)){
$body['memo']=$memo;
}
return $this->request('/mch/withdraw', $body);
}
//自定义日志
function printLog($msg) {
if (!is_dir('log')){
mkdir('log',0777,true);
}
$path="log/".date('Y-m-d').".log";
file_put_contents($path, "" . date('Y-m-d H:i:s') . "" . $msg . "\r\n\r\n", FILE_APPEND);
}
//回调函数
public function callback(){
$body = $_POST['body'];
$nonce = $_POST['nonce'];
$timestamp = $_POST['timestamp'];
$sign = $_POST['sign'];
//接收回调参数 用于日志记录
//$content = file_get_contents('php://input');
//$this->printLog("回调接收内容:".$content);
//验证签名
$signCheck = $this->signature($body,$timestamp,$nonce);
if ($sign != $signCheck) {
throw new UdunDispatchException(-1, '签名错误');
return ;
}
$body = json_decode($body);
//$this->printLog("回调接收内容(tradeType):".$body->tradeType);
//$body->tradeType 1充币回调 2提币回调
if ($body->tradeType == 1) {
//$body->status 0待审核 1审核成功 2审核驳回 3交易成功 4交易失败
if($body->status == 3){
//业务处理
}
//无论业务方处理成功与否success,failed回调都认为成功
return "success";
} elseif ($body->tradeType == 2) {
//$body->status 0待审核 1审核成功 2审核驳回 3交易成功 4交易失败
if($body->status == 0){
//业务处理
}
else if($body->status == 1){
//业务处理
}
else if($body->status == 2){
//业务处理
}
else if($body->status == 3){
//业务处理
}
else if($body->status == 4){
//业务处理
}
//无论业务方处理成功与否success,failed回调都认为成功
return "success";
}
}
}

View File

@@ -0,0 +1,26 @@
<?php
namespace Udun\Dispatch;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
class ClientsServiceProvider implements ServiceProviderInterface
{
/**
* Registers services on the given container.
*
* This method should only be used to configure services and parameters.
* It should not get services.
*
* @param Container $pimple A container instance
*/
public function register(Container $pimple)
{
$pimple['clients'] = function ($pimple) {
return new Clients($pimple['config']->get('merchant_no'), $pimple['config']->get('api_key'), $pimple['config']->get('gateway_address'), $pimple['config']->get('callUrl'));
};
}
}

View File

@@ -0,0 +1,28 @@
<?php
namespace Udun\Dispatch;
use Hanson\Foundation\Foundation;
class UdunDispatch extends Foundation
{
private $clients;
protected $providers = [
ClientsServiceProvider::class
];
public function __construct($config)
{
parent::__construct($config);
$this->clients = new Clients($config['merchant_no'], $config['api_key'], $config['gateway_address'], $config['callUrl']);
}
public function __call($name, $arguments)
{
return $this->clients->{$name}(...$arguments);
}
}

View File

@@ -0,0 +1,11 @@
<?php
namespace Udun\Dispatch;
class UdunDispatchException extends \Exception
{
public function __construct($code,$msg){
var_dump("code:".$code);
var_dump("msg:".$msg);
}
}