Mt4Deposit.php 10 KB

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