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