Mt4Deposit.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. namespace common\lib;
  3. use backend\helpers\MailHelper;
  4. use backend\models\Mt4DepositLog;
  5. /**
  6. * Class Mt4Deposit
  7. * @package common\helps
  8. */
  9. class Mt4Deposit
  10. {
  11. const TYPE_DEPOSIT = 1; // 入金
  12. const TYPE_WITHDRAW = 2; // 出金
  13. /**
  14. * soap服务url
  15. * @var string
  16. */
  17. public $url = 'http://172.31.23.210:28888/';
  18. /**
  19. * @var array
  20. */
  21. public $options = [
  22. 'cache_wsdl' => WSDL_CACHE_NONE,
  23. ];
  24. /**
  25. * @var array
  26. */
  27. public $emails = ['176462329@qq.com', '339140399@qq.com'];
  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. $params = [
  74. 'login' => $mt4Id,
  75. 'money' => $usdMoney,
  76. ];
  77. if ($comment) {
  78. $params['comment'] = $comment;
  79. }
  80. $client = new \SoapClient($this->url, $this->options);
  81. $result = $client->deposit($params);
  82. if ($result) {
  83. $errMsg = isset($this->errorCodes[$result->result]) ? trim($this->errorCodes[$result->result]) : '未知错误';
  84. if ($result->result == 0) {
  85. // 入金成功
  86. $this->log(self::TYPE_DEPOSIT, $mt4Id, $usdMoney, $comment, $result->result, $errMsg, $result->order);
  87. return $this->retResult(0, $errMsg, $result);
  88. } else {
  89. // 入金失败
  90. $this->log(self::TYPE_DEPOSIT, $mt4Id, $usdMoney, $comment, $result->result, $errMsg);
  91. $this->sendMail('【爱客】 入金失败报警', [
  92. 'mt4Id' => $mt4Id,
  93. 'usdMoney' => $usdMoney,
  94. 'comment' => $comment,
  95. 'errCode' => $result->result,
  96. 'errMsg' => $errMsg,
  97. ]);
  98. return $this->retResult(1100, $errMsg, $result);
  99. }
  100. } else {
  101. // 入金异常
  102. $this->log(self::TYPE_DEPOSIT, $mt4Id, $usdMoney, $comment, 1101, $this->errorCodes[1101]);
  103. $this->sendMail('【爱客】 入金异常报警', [
  104. 'mt4Id' => $mt4Id,
  105. 'usdMoney' => $usdMoney,
  106. 'orderNo' => $comment,
  107. 'errCode' => 1101,
  108. 'errMsg' => $this->errorCodes[1101],
  109. ]);
  110. return $this->retResult(1101, $this->errorCodes[1101], $result);
  111. }
  112. }
  113. /**
  114. * 入金
  115. * @param int $mt4Id mt4id
  116. * @param float $cnyMoney 人民币(元)
  117. * @param float $usdRate 美元汇率
  118. * @param int $orderNo 订单号
  119. * @return array
  120. */
  121. public function depositByCNY($mt4Id, $cnyMoney, $usdRate, $orderNo = null)
  122. {
  123. if (empty($mt4Id) || empty($cnyMoney) || empty($usdRate)) {
  124. // 参数非法
  125. return $this->retResult(1000, $this->errorCodes[1000], func_get_args());
  126. }
  127. $usdMoney = $cnyMoney / $usdRate;
  128. return $this->deposit($mt4Id, $usdMoney, $orderNo);
  129. }
  130. /**
  131. * 获取账户余额
  132. * @param int $mt4Id mt4id
  133. * @return array
  134. */
  135. public function getbalance($mt4Id)
  136. {
  137. if (empty($mt4Id)) {
  138. // 参数非法
  139. return $this->retResult(1000, $this->errorCodes[1000], func_get_args());
  140. }
  141. $params = [
  142. 'login' => $mt4Id,
  143. ];
  144. $client = new \SoapClient($this->url, $this->options);
  145. $result = $client->getbalance($params);
  146. if ($result) {
  147. if ($result->result == 0) {
  148. // 查询成功
  149. return $this->retResult(0, $this->errorCodes[$result->result], $result);
  150. } else {
  151. // 查询失败
  152. return $this->retResult(1200, $this->errorCodes[$result->result], $result);
  153. }
  154. } else {
  155. // 查询异常
  156. return $this->retResult(1201, $this->errorCodes[1201], $result);
  157. }
  158. }
  159. /**
  160. * 出金
  161. * @param int $mt4Id mt4id
  162. * @param float $usdMoney 美元
  163. * @param string $comment 注释
  164. * @return array
  165. */
  166. public function withdraw($mt4Id, $usdMoney, $comment = null)
  167. {
  168. if (empty($mt4Id) || empty($usdMoney)) {
  169. // 参数非法
  170. return $this->retResult(1000, $this->errorCodes[1000], func_get_args());
  171. }
  172. $params = [
  173. 'login' => $mt4Id,
  174. 'money' => $usdMoney,
  175. ];
  176. if ($comment) {
  177. $params['comment'] = $comment;
  178. }
  179. $client = new \SoapClient($this->url, $this->options);
  180. $result = $client->withdraw($params);
  181. if ($result) {
  182. $errMsg = isset($this->errorCodes[$result->result]) ? trim($this->errorCodes[$result->result]) : '未知错误';
  183. if ($result->result == 0) {
  184. // 出金成功
  185. $this->log(self::TYPE_WITHDRAW, $mt4Id, $usdMoney, $comment, $result->result, $errMsg, $result->order);
  186. return $this->retResult(0, $errMsg, $result);
  187. } else {
  188. // 出金失败
  189. $this->log(self::TYPE_WITHDRAW, $mt4Id, $usdMoney, $comment, $result->result, $errMsg);
  190. $this->sendMail('【爱客】 出金失败报警', [
  191. 'mt4Id' => $mt4Id,
  192. 'usdMoney' => $usdMoney,
  193. 'comment' => $comment,
  194. 'errCode' => $result->result,
  195. 'errMsg' => $errMsg,
  196. ]);
  197. return $this->retResult(1300, $errMsg, $result);
  198. }
  199. } else {
  200. // 出金异常
  201. $this->log(self::TYPE_WITHDRAW, $mt4Id, $usdMoney, $comment, 1301, $this->errorCodes[1301]);
  202. $this->sendMail('【爱客】 出金异常报警', [
  203. 'mt4Id' => $mt4Id,
  204. 'usdMoney' => $usdMoney,
  205. 'orderNo' => $comment,
  206. 'errCode' => 1301,
  207. 'errMsg' => $this->errorCodes[1301],
  208. ]);
  209. return $this->retResult(1301, $this->errorCodes[1301], $result);
  210. }
  211. }
  212. /**
  213. * 出金
  214. * @param int $mt4Id mt4id
  215. * @param float $cnyMoney 人民币
  216. * @param float $usdRate 美元汇率
  217. * @param null|int $orderNo 订单号
  218. * @return array
  219. */
  220. public function withdrawByCNY($mt4Id, $cnyMoney, $usdRate, $orderNo = null)
  221. {
  222. if (empty($mt4Id) || empty($cnyMoney) || empty($usdRate)) {
  223. // 参数非法
  224. return $this->retResult(1000, $this->errorCodes[1000], func_get_args());
  225. }
  226. $usdMoney = $cnyMoney / $usdRate;
  227. return $this->withdraw($mt4Id, $usdMoney, $orderNo);
  228. }
  229. /**
  230. * 返回结果
  231. * @param int $errCode 错误码
  232. * @param string $errMsg 错误信息
  233. * @param array $data 返回数据
  234. * @return array
  235. */
  236. public function retResult($errCode, $errMsg = '', $data = [])
  237. {
  238. return ['errCode' => $errCode, 'errMsg' => $errMsg, 'data' => (array)$data];
  239. }
  240. /**
  241. * 记录日志
  242. * @param int $type
  243. * @param int $mt4Id
  244. * @param float $usdMoney
  245. * @param string $orderId
  246. * @param string $errCode
  247. * @param string $errMsg
  248. * @param string $mt4Order
  249. * @return bool
  250. */
  251. private function log($type, $mt4Id, $usdMoney, $orderId, $errCode, $errMsg, $mt4Order = '')
  252. {
  253. $data = [
  254. 'type' => intval($type),
  255. 'mt4_id' => trim($mt4Id),
  256. 'order_id' => trim($orderId),
  257. 'mt4_order_id' => trim($mt4Order),
  258. 'usd_money' => trim($usdMoney),
  259. 'err_code' => trim($errCode),
  260. 'err_msg' => trim($errMsg),
  261. 'create_time' => time(),
  262. ];
  263. $model = new Mt4DepositLog();
  264. $model->setAttributes($data);
  265. return $model->save();
  266. }
  267. /**
  268. * 发送邮件
  269. * @param string $subject
  270. * @param string|array $body
  271. */
  272. private function sendMail($subject, $body)
  273. {
  274. $html = '';
  275. if (is_array($body)) {
  276. $html .= '<table>';
  277. foreach ($body as $key => $value) {
  278. $html .= "<tr><td>{$key} : </td><td>{$value}</td></tr>";
  279. }
  280. $html .= '</table>';
  281. } else {
  282. $html = trim($body);
  283. }
  284. MailHelper::sendMail($subject, $this->emails, [], 'do.not.reply', $html);
  285. }
  286. }