Cash.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. <?php namespace Pay\Yspay;
  2. use Dever;
  3. class Cash
  4. {
  5. # 百分制
  6. private $num = 100;
  7. # 备付金
  8. private $bfj = 100;
  9. # 入账:待确认
  10. public function add($merchant_id, $amount, $order_num, $source_order_num, $fenzhang = 0)
  11. {
  12. $merchant = Dever::db('pay/yspay_merchant')->one($merchant_id);
  13. if ($merchant) {
  14. $data = array();
  15. $data['account_id'] = $merchant['account_id'];
  16. $data['merchant_id'] = $merchant['id'];
  17. $data['order_num'] = $order_num;
  18. $data['source_order_num'] = $source_order_num;
  19. $info = Dever::db('pay/yspay_cash')->find($data);
  20. if ($amount && $amount > 0) {
  21. $this->getCash($amount, $merchant, $data, $fenzhang);
  22. }
  23. $data['status'] = 1;
  24. if ($info) {
  25. $data['where_id'] = $id = $info['id'];
  26. Dever::db('pay/yspay_cash')->update($data);
  27. } else {
  28. $id = Dever::db('pay/yspay_cash')->insert($data);
  29. }
  30. return $id;
  31. }
  32. return false;
  33. }
  34. # 获取账户信息
  35. public function getInfo($merchant_id)
  36. {
  37. $merchant = Dever::db('pay/yspay_merchant')->one($merchant_id);
  38. $yue = $merchant['hf_cash'] - $merchant['hf_tx_cash'];
  39. $yue = $yue/$this->num;
  40. $beifujin = 0;
  41. if ($yue >= $this->bfj) {
  42. $beifujin = $this->bfj;
  43. }
  44. $tixian = $yue - $beifujin;
  45. $info = '当前账户余额:¥' . Dever::number($yue, 2) . '&nbsp;&nbsp;&nbsp;&nbsp;备付金:¥'.Dever::number($beifujin, 2).'&nbsp;&nbsp;&nbsp;&nbsp;可提现余额:¥' . Dever::number($tixian, 2);
  46. return $info;
  47. }
  48. # 修改状态
  49. public function up($id, $status = 2, $amount = false, $fenzhang = 0)
  50. {
  51. if ($amount) {
  52. $amount = $amount * 100;
  53. }
  54. if (is_numeric($id)) {
  55. $where['id'] = $id;
  56. } else {
  57. $where['source_order_num'] = $id;
  58. }
  59. $info = Dever::db('pay/yspay_cash')->find($where);
  60. if (!$info) {
  61. $where['source_order_num'] = $id . '_O';
  62. $info = Dever::db('pay/yspay_cash')->find($where);
  63. }
  64. if ($info) {
  65. $merchant = Dever::db('pay/yspay_merchant')->one($info['merchant_id']);
  66. if ($merchant) {
  67. $update = array('status' => $status, 'where_id' => $info['id']);
  68. if ($status == 2) {
  69. if ($amount && $amount > 0 && $info['ycash'] != $amount) {
  70. $this->getCash($amount, $merchant, $update, $fenzhang);
  71. }
  72. $update['fdate'] = time();
  73. }
  74. $state = Dever::db('pay/yspay_cash')->update($update);
  75. if ($state) {
  76. if ($status == 2) {
  77. $info = Dever::db('pay/yspay_cash')->find(array('id' => $info['id'], 'clear' => true));
  78. $total = Dever::db('pay/yspay_cash')->getTotal(array('status' => 2, 'merchant_id' => $info['merchant_id']));
  79. if ($total) {
  80. $name = '';
  81. $admin = Dever::load('manage/auth.info');
  82. if ($admin) {
  83. $name = $admin['username'];
  84. }
  85. Dever::db('pay/yspay_merchant')->update(array('where_id' => $info['merchant_id'], 'cash' => $total['cash'], 'hf_cash' => $total['hf_cash'], 'fz_cash' => $total['fz_cash']));
  86. Dever::load('pay/yspay/merchant')->log($info['merchant_id'], $info['account_id'], 1, $info['hf_cash'], $name);
  87. }
  88. $info = Dever::db('pay/yspay_cash')->find(array('id' => $info['id'], 'clear' => true));
  89. if ($info['fz_cash'] && $info['fz_cash'] > 0 && $info['status'] == 2 && $info['fenzhang_status'] != 2) {
  90. $state = $this->fenzhang_act($info);
  91. if ($state == 1) {
  92. $update = array();
  93. $update['where_id'] = $info['id'];
  94. if ($state == 1) {
  95. $update['fenzhang_status'] = 2;
  96. } else {
  97. $update['fenzhang_status'] = 3;
  98. }
  99. $update['rdate'] = time();
  100. Dever::db('pay/yspay_cash')->update($update);
  101. }
  102. }
  103. }
  104. return $state;
  105. }
  106. }
  107. }
  108. return false;
  109. }
  110. private function getCash($amount, $merchant, &$data, $fenzhang = 0)
  111. {
  112. $fenzhang = $fenzhang*$this->num;
  113. $yspay = Dever::db('pay/yspay')->one(array('account_id' => $merchant['account_id']));
  114. if ($yspay) {
  115. $cash_per = 0;
  116. if ($merchant['type'] == 1 && $yspay['type'] == 2) {
  117. $cash_per = $merchant['cash_per'];
  118. if (!$cash_per) {
  119. if ($yspay && $yspay['cash_per']) {
  120. $cash_per = $yspay['cash_per'];
  121. }
  122. }
  123. if (!$cash_per || $cash_per <= 0) {
  124. $cash_per = 0;
  125. } else {
  126. $cash_per = $cash_per/100;
  127. }
  128. }
  129. $per = $yspay['per']/100;
  130. $cash_jy_per = $yspay['cash_jy_per']/100;
  131. $data['ycash'] = $amount;
  132. $data['yl_cash'] = round($data['ycash'] * $per);
  133. $data['pt_cash'] = round($data['ycash'] * $cash_jy_per);
  134. $data['cash'] = $data['ycash'] - $data['yl_cash'] - $data['pt_cash'];
  135. /*
  136. if ($yspay['cash_type'] == 1) {
  137. $fz_cash = $data['ycash'];
  138. } else {
  139. $fz_cash = $data['cash'];
  140. }
  141. $data['fz_cash'] = $fz_cash*$cash_per;
  142. */
  143. $data['fz_cash'] = 0;
  144. if ($fenzhang && $fenzhang > 0) {
  145. $data['fz_cash'] += $fenzhang;
  146. $data['hf_cash'] = $data['cash'] - $data['fz_cash'];
  147. } else {
  148. $data['hf_cash'] = $data['cash'];
  149. unset($data['fz_cash']);
  150. }
  151. }
  152. }
  153. private function huafu_act($info)
  154. {
  155. $merchant = Dever::db('pay/yspay_merchant')->one($info['merchant_id']);
  156. if ($merchant) {
  157. $config = Dever::db('pay/yspay')->one(array('account_id' => $merchant['account_id']));
  158. if ($config) {
  159. return Dever::load('pay/yspay/multi')->huafu($config, $merchant, $info);
  160. }
  161. }
  162. return false;
  163. }
  164. private function fenzhang_act($info)
  165. {
  166. $merchant = Dever::db('pay/yspay_merchant')->one($info['merchant_id']);
  167. if ($merchant) {
  168. $config = Dever::db('pay/yspay')->one(array('account_id' => $merchant['account_id']));
  169. if ($config) {
  170. $yspay_main = Dever::db('pay/yspay_main')->one($merchant['yspay_main_id']);
  171. return Dever::load('pay/yspay/multi')->fenzhang($config, $yspay_main, $merchant, $info);
  172. }
  173. }
  174. return false;
  175. }
  176. # 重新分账
  177. public function fz_api()
  178. {
  179. $where = array();
  180. $where['status'] = 2;
  181. $where['fenzhang_status'] = 2;
  182. $no_order_num = Dever::input('no_order_num');
  183. $order_num = Dever::input('order_num');
  184. if ($order_num) {
  185. $where['order_num'] = $order_num;
  186. }
  187. $data = Dever::db('pay/yspay_cash')->select($where);
  188. if ($data) {
  189. $test = Dever::input('test');
  190. if ($test == 1) {
  191. print_r($data);die;
  192. }
  193. foreach ($data as $info)
  194. {
  195. if ($no_order_num && strstr($no_order_num, $info['order_num'])) {
  196. continue;
  197. } elseif ($info['fz_cash'] && $info['fz_cash'] > 0 && $info['status'] == 2 && $info['fenzhang_status'] == 2) {
  198. $this->fenzhang_act($info);
  199. }
  200. }
  201. }
  202. return 'ok';
  203. }
  204. /*
  205. private function tixian_act($info)
  206. {
  207. $merchant = Dever::db('pay/yspay_merchant')->one($info['merchant_id']);
  208. if ($merchant) {
  209. $config = Dever::db('pay/yspay')->one(array('account_id' => $merchant['account_id']));
  210. if ($config) {
  211. return Dever::load('pay/yspay/account')->act($config, $merchant, $info);
  212. }
  213. }
  214. return false;
  215. }*/
  216. # 将测试数据改成待入账
  217. public function test_edit_api()
  218. {
  219. $order_num = Dever::input('order_num');
  220. /*
  221. $price = Dever::input('price');
  222. $fenzhang = Dever::input('fenzhang');
  223. Dever::load('pay/yspay/cash')->up($order_num, 2, $price, $fenzhang);
  224. */
  225. $info = Dever::db('pay/yspay_cash')->find(array('source_order_num' => $order_num, 'clear' => true));
  226. if ($info['fz_cash'] && $info['fz_cash'] > 0 && $info['status'] == 2 && $info['fenzhang_status'] != 2) {
  227. $state = $this->fenzhang_act($info);
  228. if ($state == 1) {
  229. $update = array();
  230. $update['where_id'] = $info['id'];
  231. if ($state == 1) {
  232. $update['fenzhang_status'] = 2;
  233. } else {
  234. $update['fenzhang_status'] = 3;
  235. }
  236. $update['rdate'] = time();
  237. Dever::db('pay/yspay_cash')->update($update);
  238. }
  239. }
  240. return 'ok';
  241. }
  242. # 查询余额
  243. public function test_yue_api()
  244. {
  245. $info['merchant_id'] = Dever::input('merchant_id');
  246. if ($info) {
  247. $merchant = Dever::db('pay/yspay_merchant')->one($info['merchant_id']);
  248. if ($merchant) {
  249. $config = Dever::db('pay/yspay')->one(array('account_id' => $merchant['account_id']));
  250. if ($config) {
  251. return Dever::load('pay/yspay/multi')->yue($config, $merchant);
  252. }
  253. }
  254. }
  255. return 'ok';
  256. }
  257. # 直接划付
  258. public function test_tixian_api()
  259. {
  260. $info['merchant_id'] = Dever::input('merchant_id');
  261. $info['hf_cash'] = Dever::input('hf_cash');
  262. $info['order_num'] = Dever::order('TX');
  263. if ($info) {
  264. $merchant = Dever::db('pay/yspay_merchant')->one($info['merchant_id']);
  265. if ($merchant) {
  266. $config = Dever::db('pay/yspay')->one(array('account_id' => $merchant['account_id']));
  267. if ($config) {
  268. return Dever::load('pay/yspay/multi')->huafu($config, $merchant, $info);
  269. }
  270. }
  271. }
  272. return 'ok';
  273. }
  274. # 查询订单
  275. public function test_query_api()
  276. {
  277. $info['merchant_id'] = Dever::input('merchant_id');
  278. $orderType = Dever::input('orderType', 1);
  279. $transDate = Dever::input('transDate');
  280. $page = Dever::input('page', 1);
  281. if ($info) {
  282. $merchant = Dever::db('pay/yspay_merchant')->one($info['merchant_id']);
  283. if ($merchant) {
  284. $config = Dever::db('pay/yspay')->one(array('account_id' => $merchant['account_id']));
  285. if ($config) {
  286. return Dever::load('pay/yspay/multi')->query($config, $merchant, $orderType, $transDate, $page);
  287. }
  288. }
  289. }
  290. return 'ok';
  291. }
  292. # 资金划付
  293. public function huafu_commit_api()
  294. {
  295. $where = array('status' => 2);
  296. $account_id = Dever::input('account_id');
  297. if ($account_id) {
  298. $where['account_id'] = $account_id;
  299. }
  300. $merchant_id = Dever::input('merchant_id');
  301. if ($merchant_id) {
  302. $where['merchant_id'] = $merchant_id;
  303. }
  304. $data = Dever::db('pay/yspay_cash')->select($where);
  305. if ($data) {
  306. foreach ($data as $k => $v) {
  307. $this->up($v['id'], 3);
  308. }
  309. }
  310. return 'ok';
  311. }
  312. # 随机获取平台商户
  313. public function getMid($account_id, $yspay_main_id = 1)
  314. {
  315. $merchant = Dever::db('pay/yspay_merchant')->select(array('type' => 2, 'account_id' => $account_id, 'yspay_main_id' => $yspay_main_id, 'status' => 1));
  316. if ($merchant) {
  317. $key = array_rand($merchant);
  318. if (isset($merchant[$key])) {
  319. return $merchant[$key];
  320. }
  321. }
  322. return false;
  323. }
  324. public function tixian($id, $name, $data)
  325. {
  326. $merchant_id = Dever::param('merchant_id', $data);
  327. $cash = Dever::param('cash', $data);
  328. $merchant = Dever::db('pay/yspay_merchant')->one($merchant_id);
  329. if ($merchant) {
  330. $admin = Dever::load('manage/auth.info');
  331. $this->tixian_act($merchant, $cash, $admin['username']);
  332. }
  333. return false;
  334. }
  335. public function tixian_act($merchant, $cash, $name = '')
  336. {
  337. if ($cash <= 0) {
  338. Dever::alert('提现金额应大于0');
  339. }
  340. $cash = $cash*$this->num;
  341. $yue = $merchant['hf_cash'] - $merchant['hf_tx_cash'] - $this->num*$this->bfj;
  342. $cash = intval($cash);
  343. $yue = intval($yue);
  344. if ($yue < $cash) {
  345. Dever::alert('余额不足');
  346. }
  347. $info['merchant_id'] = $merchant['id'];
  348. $info['hf_cash'] = $cash;
  349. $info['order_num'] = Dever::order('TX');
  350. $status = $this->huafu_act($info);
  351. if ($status == 1) {
  352. Dever::db('pay/yspay_merchant')->updateTxCash(array('where_id' => $merchant['id'], 'hf_tx_cash' => $cash));
  353. Dever::load('pay/yspay/merchant')->log($merchant['id'], $merchant['account_id'], 3, $cash, $name);
  354. }
  355. }
  356. public function total_api()
  357. {
  358. $merchant_id = Dever::input('mid');
  359. $total = Dever::db('pay/yspay_cash')->getTotal(array('status' => 2, 'merchant_id' => $merchant_id));
  360. if ($total) {
  361. Dever::db('pay/yspay_merchant')->update(array('where_id' => $merchant_id, 'cash' => $total['cash'], 'hf_cash' => $total['hf_cash'], 'fz_cash' => $total['fz_cash']));
  362. }
  363. return 'ok';
  364. }
  365. }