UdunService.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. namespace crmeb\services;
  3. use app\models\system\UdunCallback;
  4. use app\models\user\UserMoney;
  5. use app\Request;
  6. class UdunService
  7. {
  8. private $api_key = '';
  9. private $merchant_number = '';
  10. private $request_url_hn = 'https://hk06-hk-node.uduncloud.com';//香港网关
  11. private $request_url_hk = 'https://hk06-node.uduncloud.com';//华南网关
  12. private $head = [
  13. 'X-AjaxPro-Method' => 'ShowList',
  14. 'Content-Type' => 'application/json; charset=utf-8',
  15. 'Content-Length' => ''
  16. ];
  17. /**
  18. * @return UdunService
  19. */
  20. public static function init(): UdunService
  21. {
  22. $instance = new self();
  23. $instance->api_key = sys_config('udun_key', '', true);
  24. $instance->merchant_number = sys_config('udun_id', '', true);
  25. // var_dump(sys_config('udun_id', '', true));
  26. // var_dump(sys_config('udun_key', '', true));
  27. return $instance;
  28. }
  29. // 获取商户支持的币种信息
  30. public static function supportCoins($showBalance = true)
  31. {
  32. $instance = self::init();
  33. $body = array(
  34. 'merchantId' => $instance->merchant_number,
  35. 'showBalance' => $showBalance
  36. );
  37. $body = json_encode($body);
  38. $timestamp = time();
  39. $nonce = rand(100000, 999999);
  40. $url = $instance->request_url_hk . '/mch/support-coins';
  41. $key = $instance->api_key;
  42. $sign = md5($body . $key . $nonce . $timestamp);
  43. $data = array(
  44. 'timestamp' => $timestamp,
  45. 'nonce' => $nonce,
  46. 'sign' => $sign,
  47. 'body' => $body
  48. );
  49. $data_string = json_encode($data);
  50. return $instance->http_post($url, $data_string);
  51. }
  52. // 生成地址
  53. public static function createAddress($coinType, $callUrl)
  54. {
  55. $instance = self::init();
  56. $body = array(
  57. 'merchantId' => $instance->merchant_number,
  58. 'coinType' => $coinType,
  59. 'callUrl' => $callUrl,
  60. );
  61. // var_dump($body);
  62. $body = '[' . json_encode($body) . ']';
  63. $timestamp = time();
  64. $nonce = rand(100000, 999999);
  65. $url = $instance->request_url_hk . '/mch/address/create';
  66. $key = $instance->api_key;
  67. $sign = md5($body . $key . $nonce . $timestamp);
  68. $data = array(
  69. 'timestamp' => $timestamp,
  70. 'nonce' => $nonce,
  71. 'sign' => $sign,
  72. 'body' => $body
  73. );
  74. // var_dump($data);
  75. $data_string = json_encode($data);
  76. return $instance->http_post($url, $data_string);
  77. }
  78. // 校验地址合法性
  79. public static function checkAddress($mainCoinType, $address)
  80. {
  81. $instance = self::init();
  82. $body = array(
  83. 'merchantId' => $instance->merchant_number,
  84. 'mainCoinType' => $mainCoinType,
  85. 'address' => $address,
  86. );
  87. $body = '[' . json_encode($body) . ']';
  88. $timestamp = time();
  89. $nonce = rand(100000, 999999);
  90. $url = $instance->request_url_hk . '/mch/check/address';
  91. $key = $instance->api_key;
  92. $sign = md5($body . $key . $nonce . $timestamp);
  93. $data = array(
  94. 'timestamp' => $timestamp,
  95. 'nonce' => $nonce,
  96. 'sign' => $sign,
  97. 'body' => $body
  98. );
  99. $data_string = json_encode($data);
  100. return $instance->http_post($url, $data_string);
  101. }
  102. // 发送提币申请
  103. public static function withdraw($mainCoinType, $coinType, $amount, $address, $callUrl, $businessId, $memo)
  104. {
  105. $instance = self::init();
  106. $body = array(
  107. 'merchantId' => $instance->merchant_number,
  108. 'mainCoinType' => $mainCoinType,
  109. 'address' => $address,
  110. 'amount' => $amount,
  111. 'coinType' => $coinType,
  112. 'callUrl' => $callUrl,
  113. 'businessId' => $businessId,
  114. 'memo' => $memo
  115. );
  116. $body = '[' . json_encode($body) . ']';
  117. $timestamp = time();
  118. $nonce = rand(100000, 999999);
  119. $url = $instance->request_url_hk . '/mch/withdraw';
  120. $key = $instance->api_key;
  121. $sign = md5($body . $key . $nonce . $timestamp);
  122. $data = array(
  123. 'timestamp' => $timestamp,
  124. 'nonce' => $nonce,
  125. 'sign' => $sign,
  126. 'body' => $body
  127. );
  128. $data_string = json_encode($data);
  129. return $instance->http_post($url, $data_string);
  130. }
  131. // 代付
  132. public static function proxypay($mainCoinType, $coinType, $amount, $address, $callUrl, $businessId, $memo)
  133. {
  134. $instance = self::init();
  135. $body = array(
  136. 'merchantId' => $instance->merchant_number,
  137. 'mainCoinType' => $mainCoinType,
  138. 'address' => $address,
  139. 'amount' => $amount,
  140. 'coinType' => $coinType,
  141. 'callUrl' => $callUrl,
  142. 'businessId' => $businessId,
  143. 'memo' => $memo
  144. );
  145. $body = '[' . json_encode($body) . ']';
  146. $timestamp = time();
  147. $nonce = rand(100000, 999999);
  148. $url = $instance->request_url_hk . '/mch/withdraw/proxypay';
  149. $key = $instance->api_key;
  150. $sign = md5($body . $key . $nonce . $timestamp);
  151. $data = array(
  152. 'timestamp' => $timestamp,
  153. 'nonce' => $nonce,
  154. 'sign' => $sign,
  155. 'body' => $body
  156. );
  157. $data_string = json_encode($data);
  158. return $instance->http_post($url, $data_string);
  159. }
  160. public function http_post($url, $data_string)
  161. {
  162. $ch = curl_init();
  163. curl_setopt($ch, CURLOPT_URL, $url);
  164. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  165. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  166. 'X-AjaxPro-Method:ShowList',
  167. 'Content-Type: application/json; charset=utf-8',
  168. 'Content-Length: ' . strlen($data_string))
  169. );
  170. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  171. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  172. curl_setopt($ch, CURLOPT_POST, 1);
  173. curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
  174. $data = curl_exec($ch);
  175. curl_close($ch);
  176. // var_dump($url);
  177. // var_dump(array(
  178. // 'X-AjaxPro-Method:ShowList',
  179. // 'Content-Type: application/json; charset=utf-8',
  180. // 'Content-Length: ' . strlen($data_string)));
  181. // var_dump($data_string);
  182. return $data;
  183. }
  184. public static function Notify($call_back_data)
  185. {
  186. $instance = self::init();
  187. $key = $instance->api_key;
  188. // $key = 'eed98c702c8ec15bb7bfba808a0b4607';
  189. $sign = md5($call_back_data['body'] . $key . $call_back_data['nonce'] . $call_back_data['timestamp']);
  190. if ($call_back_data['sign'] == $sign) {
  191. $body = json_decode($call_back_data['body'], true);
  192. UdunCallback::create(
  193. [
  194. 'name' => $body['mainCoinType'],
  195. 'name_alias' => $body['coinType'],
  196. 'time' => (int)$call_back_data['timestamp'] / 1000,
  197. 'sign' => $call_back_data['sign'],
  198. 'hash' => $body['txId'],
  199. // 'from' => $data['data']['from'],
  200. 'to' => $body['address'],
  201. 'memo' => $body['memo'],
  202. 'amount' => bcdiv($body['amount'], bcpow(10, $body['decimals']), 8),
  203. 'fee_amount' => bcdiv($body['fee'], bcpow(10, $body['decimals']), 8),
  204. 'add_time' => (int)$call_back_data['timestamp'] / 1000,
  205. 'type' => $body['tradeType'],
  206. 'state' => $body['status'],
  207. ]
  208. );
  209. //$body->tradeType 1充币回调 2提币回调
  210. if ($body['tradeType'] == 1) {
  211. //$body->status 0待审核 1审核成功 2审核驳回 3交易成功 4交易失败
  212. if ($body['status'] == 3) {
  213. //业务处理
  214. $money = sys_data('money_type');
  215. $money_type = '';
  216. foreach ($money as $v) {
  217. if ($v['money_address'] == $body['mainCoinType'] && $v['coin_type'] == $body['coinType']) {
  218. $money_type = $v['code'];
  219. }
  220. }
  221. if (!$money_type) {
  222. return "success";
  223. }
  224. $user = UserMoney::where('address', $body['address'])->value('uid');
  225. if ($user) {
  226. UserMoney::incomeMoney($user, $money_type, bcdiv($body['amount'], bcpow(10, $body['decimals']), 8), 'recharge', '用户充值', '用户充值' . bcdiv($body['amount'], bcpow(10, $body['decimals']), 8) . $money_type);
  227. }
  228. }
  229. //无论业务方处理成功与否(success,failed),回调都认为成功
  230. return "success";
  231. } elseif ($body['tradeType'] == 2) {
  232. //$body->status 0待审核 1审核成功 2审核驳回 3交易成功 4交易失败
  233. if ($body['status'] == 0) {
  234. //业务处理
  235. } else if ($body['status'] == 1) {
  236. //业务处理
  237. } else if ($body['status'] == 2) {
  238. //业务处理
  239. } else if ($body['status'] == 3) {
  240. //业务处理
  241. } else if ($body['status'] == 4) {
  242. //业务处理
  243. }
  244. //无论业务方处理成功与否(success,failed),回调都认为成功
  245. return "success";
  246. }
  247. } else {
  248. echo 'sign error';
  249. 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);
  250. }
  251. }
  252. }