Mt4Deposit.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. namespace common\lib;
  3. use backend\helpers\MailHelper;
  4. use backend\models\Mt4DepositLog;
  5. use common\lib\Mt4ManagerApi;
  6. /**
  7. * Class Mt4Deposit
  8. * @package common\helps
  9. */
  10. class Mt4Deposit
  11. {
  12. const TYPE_DEPOSIT = 1; // 入金
  13. const TYPE_WITHDRAW = 2; // 出金
  14. /**
  15. * soap服务url
  16. * @var string
  17. */
  18. public $url = 'http://43.239.176.170:20001/';
  19. /**
  20. * @var array
  21. public $options = [
  22. 'cache_wsdl' => WSDL_CACHE_NONE,
  23. ];*/
  24. /**
  25. * @var array
  26. */
  27. public $emails = [];
  28. /**
  29. * 错误码
  30. * @var array
  31. */
  32. public $errorCodes = [
  33. 0 => 'RET_OK|正确',
  34. 1 => 'RET_OK_NONE|正确,但是不做操作',
  35. 2 => 'RET_ERROR|一般性错误',
  36. 3 => 'RET_INVALID_DATA|无效数据',
  37. 4 => 'RET_TECH_PROBLEM|服务器技术问题',
  38. 5 => 'RET_OLD_VERSION|客户端版本过低',
  39. 6 => 'RET_NO_CONNECT|无连接',
  40. 7 => 'RET_NOT_ENOUGH_RIGHTS|权限不足',
  41. 8 => 'RET_TOO_FREQUENT|请求太频繁',
  42. 9 => 'RET_MALFUNCTION|交易操作故障',
  43. 64 => 'RET_ACCOUNT_DISABLED|被禁用账号',
  44. 65 => 'RET_BAD_ACCOUNT_INFO|无效账户',
  45. -1 => 'MT4ManWSEC_COMM|一般性错误',
  46. -2 => 'MT4ManWSEC_CONN|连接和登录 MT4 服务器错误',
  47. -3 => 'MT4ManWSEC_UNKN|未知错误',
  48. -17 => 'MT4ManWSEC_FREEAPI|API 释放错误',
  49. -18 => 'MT4ManWSEC_APIERR|API 错误',
  50. -33 => 'MT4ManWSEC_INVLDPRM|输入参数不允许',
  51. // 自定义错误信息
  52. 1000 => '参数非法',
  53. 1100 => '入金失败',
  54. 1101 => '入金异常',
  55. 1200 => '查询失败',
  56. 1201 => '查询异常',
  57. 1300 => '出金失败',
  58. 1301 => '出金异常',
  59. ];
  60. /**
  61. * 入金
  62. * @param int $mt4Id mt4id
  63. * @param float $usdMoney 美元
  64. * @param string $comment 注释
  65. * @return array
  66. */
  67. public function deposit($mt4Id, $usdMoney, $comment = null)
  68. {
  69. if (empty($mt4Id) || empty($usdMoney)) {
  70. // 参数非法
  71. return $this->retResult(1000, $this->errorCodes[1000], func_get_args());
  72. }
  73. /*
  74. $params = [
  75. 'login' => $mt4Id,
  76. 'money' => $usdMoney,
  77. ];
  78. if ($comment) {
  79. $params['comment'] = $comment;
  80. }
  81. $client = new \SoapClient($this->url, $this->options);
  82. $result = $client->deposit($params);
  83. */
  84. $comment = ($comment) ? $comment : 'Deposit';
  85. $api = new Mt4ManagerApi();
  86. $result = $api->balance('deposit', $mt4Id, $usdMoney, $comment);
  87. $result = json_decode($result, true);
  88. if ($result) {
  89. $errMsg = isset($this->errorCodes[$result['ret']]) ? trim($this->errorCodes[$result['ret']]) : '未知错误';
  90. if ($result['ret'] == 0) {
  91. // 入金成功
  92. $this->log(self::TYPE_DEPOSIT, $mt4Id, $usdMoney, $comment, $result['ret'], $errMsg, $result['order']);
  93. return $this->retResult(0, $errMsg, $result);
  94. } else {
  95. // 入金失败
  96. $this->log(self::TYPE_DEPOSIT, $mt4Id, $usdMoney, $comment, $result['ret'], $errMsg);
  97. $this->sendMail('入金失败报警', [
  98. 'mt4Id' => $mt4Id,
  99. 'usdMoney' => $usdMoney,
  100. 'comment' => $comment,
  101. 'errCode' => $result['ret'],
  102. 'errMsg' => $errMsg,
  103. ]);
  104. return $this->retResult(1100, $errMsg, $result);
  105. }
  106. } else {
  107. // 入金异常
  108. $this->log(self::TYPE_DEPOSIT, $mt4Id, $usdMoney, $comment, 1101, $this->errorCodes[1101]);
  109. $this->sendMail('入金异常报警', [
  110. 'mt4Id' => $mt4Id,
  111. 'usdMoney' => $usdMoney,
  112. 'orderNo' => $comment,
  113. 'errCode' => 1101,
  114. 'errMsg' => $this->errorCodes[1101],
  115. ]);
  116. return $this->retResult(1101, $this->errorCodes[1101], $result);
  117. }
  118. }
  119. /**
  120. * 入金
  121. * @param int $mt4Id mt4id
  122. * @param float $cnyMoney 人民币(元)
  123. * @param float $usdRate 美元汇率
  124. * @param int $orderNo 订单号
  125. * @return array
  126. */
  127. public function depositByCNY($mt4Id, $cnyMoney, $usdRate, $orderNo = null)
  128. {
  129. if (empty($mt4Id) || empty($cnyMoney) || empty($usdRate)) {
  130. // 参数非法
  131. return $this->retResult(1000, $this->errorCodes[1000], func_get_args());
  132. }
  133. $usdMoney = $cnyMoney / $usdRate;
  134. return $this->deposit($mt4Id, $usdMoney, $orderNo);
  135. }
  136. /**
  137. * 获取账户余额
  138. * @param int $mt4Id mt4id
  139. * @return array
  140. */
  141. public function getbalance($mt4Id)
  142. {
  143. if (empty($mt4Id)) {
  144. // 参数非法
  145. return $this->retResult(1000, $this->errorCodes[1000], func_get_args());
  146. }
  147. $params = [
  148. 'login' => $mt4Id,
  149. ];
  150. $client = new \SoapClient($this->url, $this->options);
  151. $result = $client->getbalance($params);
  152. if ($result) {
  153. if ($result->result == 0) {
  154. // 查询成功
  155. return $this->retResult(0, $this->errorCodes[$result->result], $result);
  156. } else {
  157. // 查询失败
  158. return $this->retResult(1200, $this->errorCodes[$result->result], $result);
  159. }
  160. } else {
  161. // 查询异常
  162. return $this->retResult(1201, $this->errorCodes[1201], $result);
  163. }
  164. }
  165. /**
  166. * 出金
  167. * @param int $mt4Id mt4id
  168. * @param float $usdMoney 美元
  169. * @param string $comment 注释
  170. * @return array
  171. */
  172. public function withdraw($mt4Id, $usdMoney, $comment = null)
  173. {
  174. if (empty($mt4Id) || empty($usdMoney)) {
  175. // 参数非法
  176. return $this->retResult(1000, $this->errorCodes[1000], func_get_args());
  177. }
  178. /*
  179. $params = [
  180. 'login' => $mt4Id,
  181. 'money' => $usdMoney,
  182. ];
  183. if ($comment) {
  184. $params['comment'] = $comment;
  185. }
  186. $client = new \SoapClient($this->url, $this->options);
  187. $result = $client->withdraw($params);
  188. */
  189. $comment = ($comment) ? $comment : 'Withdraw';
  190. $api = new Mt4ManagerApi();
  191. $result = $api->balance('withdraw', $mt4Id, $usdMoney, $comment);
  192. $result = json_decode($result, true);
  193. if ($result) {
  194. $errMsg = isset($this->errorCodes[$result['ret']]) ? trim($this->errorCodes[$result['ret']]) : '未知错误';
  195. if ($result['ret'] == 0) {
  196. // 出金成功
  197. $this->log(self::TYPE_WITHDRAW, $mt4Id, $usdMoney, $comment, $result['ret'], $errMsg, $result['order']);
  198. return $this->retResult(0, $errMsg, $result);
  199. } else {
  200. // 出金失败
  201. $this->log(self::TYPE_WITHDRAW, $mt4Id, $usdMoney, $comment, $result['ret'], $errMsg);
  202. $this->sendMail('出金失败报警', [
  203. 'mt4Id' => $mt4Id,
  204. 'usdMoney' => $usdMoney,
  205. 'comment' => $comment,
  206. 'errCode' => $result['ret'],
  207. 'errMsg' => $errMsg,
  208. ]);
  209. return $this->retResult(1300, $errMsg, $result);
  210. }
  211. } else {
  212. // 出金异常
  213. $this->log(self::TYPE_WITHDRAW, $mt4Id, $usdMoney, $comment, 1301, $this->errorCodes[1301]);
  214. $this->sendMail('出金异常报警', [
  215. 'mt4Id' => $mt4Id,
  216. 'usdMoney' => $usdMoney,
  217. 'orderNo' => $comment,
  218. 'errCode' => 1301,
  219. 'errMsg' => $this->errorCodes[1301],
  220. ]);
  221. return $this->retResult(1301, $this->errorCodes[1301], $result);
  222. }
  223. }
  224. /**
  225. * 出金
  226. * @param int $mt4Id mt4id
  227. * @param float $cnyMoney 人民币
  228. * @param float $usdRate 美元汇率
  229. * @param null|int $orderNo 订单号
  230. * @return array
  231. */
  232. public function withdrawByCNY($mt4Id, $cnyMoney, $usdRate, $orderNo = null)
  233. {
  234. if (empty($mt4Id) || empty($cnyMoney) || empty($usdRate)) {
  235. // 参数非法
  236. return $this->retResult(1000, $this->errorCodes[1000], func_get_args());
  237. }
  238. $usdMoney = $cnyMoney / $usdRate;
  239. return $this->withdraw($mt4Id, $usdMoney, $orderNo);
  240. }
  241. /**
  242. * 返回结果
  243. * @param int $errCode 错误码
  244. * @param string $errMsg 错误信息
  245. * @param array $data 返回数据
  246. * @return array
  247. */
  248. public function retResult($errCode, $errMsg = '', $data = [])
  249. {
  250. return ['errCode' => $errCode, 'errMsg' => $errMsg, 'data' => (array)$data];
  251. }
  252. /**
  253. * 记录日志
  254. * @param int $type
  255. * @param int $mt4Id
  256. * @param float $usdMoney
  257. * @param string $orderId
  258. * @param string $errCode
  259. * @param string $errMsg
  260. * @param string $mt4Order
  261. * @return bool
  262. */
  263. private function log($type, $mt4Id, $usdMoney, $orderId, $errCode, $errMsg, $mt4Order = '')
  264. {
  265. $data = [
  266. 'type' => intval($type),
  267. 'mt4_id' => trim($mt4Id),
  268. 'order_id' => trim($orderId),
  269. 'mt4_order_id' => trim($mt4Order),
  270. 'usd_money' => trim($usdMoney),
  271. 'err_code' => trim($errCode),
  272. 'err_msg' => trim($errMsg),
  273. 'create_time' => time(),
  274. ];
  275. $model = new Mt4DepositLog();
  276. $model->setAttributes($data);
  277. return $model->save();
  278. }
  279. /**
  280. * 发送邮件
  281. * @param string $subject
  282. * @param string|array $body
  283. */
  284. private function sendMail($subject, $body)
  285. {
  286. $html = '';
  287. if (is_array($body)) {
  288. $html .= '<table>';
  289. foreach ($body as $key => $value) {
  290. $html .= "<tr><td>{$key} : </td><td>{$value}</td></tr>";
  291. }
  292. $html .= '</table>';
  293. } else {
  294. $html = trim($body);
  295. }
  296. MailHelper::sendMail($subject, $this->emails, [], 'do.not.reply', $html);
  297. }
  298. }