Mt4Deposit.php 10 KB

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