123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- <?php
- namespace crmeb\services;
- use app\models\system\UdunCallback;
- use app\models\user\UserMoney;
- use app\Request;
- class UdunService
- {
- private $api_key = '';
- private $merchant_number = '';
- private $request_url_hn = 'https://hk06-hk-node.uduncloud.com';//香港网关
- private $request_url_hk = 'https://hk06-node.uduncloud.com';//华南网关
- private $head = [
- 'X-AjaxPro-Method' => 'ShowList',
- 'Content-Type' => 'application/json; charset=utf-8',
- 'Content-Length' => ''
- ];
- /**
- * @return UdunService
- */
- public static function init(): UdunService
- {
- $instance = new self();
- $instance->api_key = sys_config('udun_key', '', true);
- $instance->merchant_number = sys_config('udun_id', '', true);
- // var_dump(sys_config('udun_id', '', true));
- // var_dump(sys_config('udun_key', '', true));
- return $instance;
- }
- // 获取商户支持的币种信息
- public static function supportCoins($showBalance = true)
- {
- $instance = self::init();
- $body = array(
- 'merchantId' => $instance->merchant_number,
- 'showBalance' => $showBalance
- );
- $body = json_encode($body);
- $timestamp = time();
- $nonce = rand(100000, 999999);
- $url = $instance->request_url_hk . '/mch/support-coins';
- $key = $instance->api_key;
- $sign = md5($body . $key . $nonce . $timestamp);
- $data = array(
- 'timestamp' => $timestamp,
- 'nonce' => $nonce,
- 'sign' => $sign,
- 'body' => $body
- );
- $data_string = json_encode($data);
- return $instance->http_post($url, $data_string);
- }
- // 生成地址
- public static function createAddress($coinType, $callUrl)
- {
- $instance = self::init();
- $body = array(
- 'merchantId' => $instance->merchant_number,
- 'coinType' => $coinType,
- 'callUrl' => $callUrl,
- );
- // var_dump($body);
- $body = '[' . json_encode($body) . ']';
- $timestamp = time();
- $nonce = rand(100000, 999999);
- $url = $instance->request_url_hk . '/mch/address/create';
- $key = $instance->api_key;
- $sign = md5($body . $key . $nonce . $timestamp);
- $data = array(
- 'timestamp' => $timestamp,
- 'nonce' => $nonce,
- 'sign' => $sign,
- 'body' => $body
- );
- // var_dump($data);
- $data_string = json_encode($data);
- return $instance->http_post($url, $data_string);
- }
- // 校验地址合法性
- public static function checkAddress($mainCoinType, $address)
- {
- $instance = self::init();
- $body = array(
- 'merchantId' => $instance->merchant_number,
- 'mainCoinType' => $mainCoinType,
- 'address' => $address,
- );
- $body = '[' . json_encode($body) . ']';
- $timestamp = time();
- $nonce = rand(100000, 999999);
- $url = $instance->request_url_hk . '/mch/check/address';
- $key = $instance->api_key;
- $sign = md5($body . $key . $nonce . $timestamp);
- $data = array(
- 'timestamp' => $timestamp,
- 'nonce' => $nonce,
- 'sign' => $sign,
- 'body' => $body
- );
- $data_string = json_encode($data);
- return $instance->http_post($url, $data_string);
- }
- // 发送提币申请
- public static function withdraw($mainCoinType, $coinType, $amount, $address, $callUrl, $businessId, $memo)
- {
- $instance = self::init();
- $body = array(
- 'merchantId' => $instance->merchant_number,
- 'mainCoinType' => $mainCoinType,
- 'address' => $address,
- 'amount' => $amount,
- 'coinType' => $coinType,
- 'callUrl' => $callUrl,
- 'businessId' => $businessId,
- 'memo' => $memo
- );
- $body = '[' . json_encode($body) . ']';
- $timestamp = time();
- $nonce = rand(100000, 999999);
- $url = $instance->request_url_hk . '/mch/withdraw';
- $key = $instance->api_key;
- $sign = md5($body . $key . $nonce . $timestamp);
- $data = array(
- 'timestamp' => $timestamp,
- 'nonce' => $nonce,
- 'sign' => $sign,
- 'body' => $body
- );
- $data_string = json_encode($data);
- return $instance->http_post($url, $data_string);
- }
- // 代付
- public static function proxypay($mainCoinType, $coinType, $amount, $address, $callUrl, $businessId, $memo)
- {
- $instance = self::init();
- $body = array(
- 'merchantId' => $instance->merchant_number,
- 'mainCoinType' => $mainCoinType,
- 'address' => $address,
- 'amount' => $amount,
- 'coinType' => $coinType,
- 'callUrl' => $callUrl,
- 'businessId' => $businessId,
- 'memo' => $memo
- );
- $body = '[' . json_encode($body) . ']';
- $timestamp = time();
- $nonce = rand(100000, 999999);
- $url = $instance->request_url_hk . '/mch/withdraw/proxypay';
- $key = $instance->api_key;
- $sign = md5($body . $key . $nonce . $timestamp);
- $data = array(
- 'timestamp' => $timestamp,
- 'nonce' => $nonce,
- 'sign' => $sign,
- 'body' => $body
- );
- $data_string = json_encode($data);
- return $instance->http_post($url, $data_string);
- }
- public function http_post($url, $data_string)
- {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_HTTPHEADER, array(
- 'X-AjaxPro-Method:ShowList',
- 'Content-Type: application/json; charset=utf-8',
- 'Content-Length: ' . strlen($data_string))
- );
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
- $data = curl_exec($ch);
- curl_close($ch);
- // var_dump($url);
- // var_dump(array(
- // 'X-AjaxPro-Method:ShowList',
- // 'Content-Type: application/json; charset=utf-8',
- // 'Content-Length: ' . strlen($data_string)));
- // var_dump($data_string);
- return $data;
- }
- public static function Notify($call_back_data)
- {
- $instance = self::init();
- $key = $instance->api_key;
- // $key = 'eed98c702c8ec15bb7bfba808a0b4607';
- $sign = md5($call_back_data['body'] . $key . $call_back_data['nonce'] . $call_back_data['timestamp']);
- if ($call_back_data['sign'] == $sign) {
- $body = json_decode($call_back_data['body'], true);
- UdunCallback::create(
- [
- 'name' => $body['mainCoinType'],
- 'name_alias' => $body['coinType'],
- 'time' => (int)$call_back_data['timestamp'] / 1000,
- 'sign' => $call_back_data['sign'],
- 'hash' => $body['txId'],
- // 'from' => $data['data']['from'],
- 'to' => $body['address'],
- 'memo' => $body['memo'],
- 'amount' => bcdiv($body['amount'], bcpow(10, $body['decimals']), 8),
- 'fee_amount' => bcdiv($body['fee'], bcpow(10, $body['decimals']), 8),
- 'add_time' => (int)$call_back_data['timestamp'] / 1000,
- 'type' => $body['tradeType'],
- 'state' => $body['status'],
- ]
- );
- //$body->tradeType 1充币回调 2提币回调
- if ($body['tradeType'] == 1) {
- //$body->status 0待审核 1审核成功 2审核驳回 3交易成功 4交易失败
- if ($body['status'] == 3) {
- //业务处理
- $money = sys_data('money_type');
- $money_type = '';
- foreach ($money as $v) {
- if ($v['money_address'] == $body['mainCoinType'] && $v['coin_type'] == $body['coinType']) {
- $money_type = $v['code'];
- }
- }
- if (!$money_type) {
- return "success";
- }
- $user = UserMoney::where('address', $body['address'])->value('uid');
- if ($user) {
- UserMoney::incomeMoney($user, $money_type, bcdiv($body['amount'], bcpow(10, $body['decimals']), 8), 'recharge', '用户充值', '用户充值' . bcdiv($body['amount'], bcpow(10, $body['decimals']), 8) . $money_type);
- }
- }
- //无论业务方处理成功与否(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";
- }
- } else {
- echo 'sign error';
- file_put_contents("call_back_data.txt", "\n" . date('Y-m-d H:i:s') . "\n sign error: \n" . $sign . "\n" . $call_back_data['sign'] . "\n", FILE_APPEND);
- }
- }
- }
|