'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); } } }