Cash.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. $merchant = Dever::db('pay/yspay_merchant')->one($info['merchant_id']);
  62. if ($merchant) {
  63. $update = array('status' => $status, 'where_id' => $info['id']);
  64. if ($status == 2) {
  65. if ($amount && $amount > 0 && $info['ycash'] != $amount) {
  66. $this->getCash($amount, $merchant, $update, $fenzhang);
  67. }
  68. $update['fdate'] = time();
  69. }
  70. $state = Dever::db('pay/yspay_cash')->update($update);
  71. if ($state) {
  72. if ($status == 2) {
  73. $info = Dever::db('pay/yspay_cash')->find(array('id' => $info['id'], 'clear' => true));
  74. $total = Dever::db('pay/yspay_cash')->getTotal(array('status' => 2, 'merchant_id' => $info['merchant_id']));
  75. if ($total) {
  76. $name = '';
  77. $admin = Dever::load('manage/auth.info');
  78. if ($admin) {
  79. $name = $admin['username'];
  80. }
  81. 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']));
  82. Dever::load('pay/yspay/merchant')->log($info['merchant_id'], $info['account_id'], 1, $info['hf_cash'], $name);
  83. }
  84. $info = Dever::db('pay/yspay_cash')->find(array('id' => $info['id'], 'clear' => true));
  85. if ($info['fz_cash'] && $info['fz_cash'] > 0 && $info['status'] == 2 && $info['fenzhang_status'] != 2) {
  86. $state = $this->fenzhang_act($info);
  87. if ($state == 1) {
  88. $update = array();
  89. $update['where_id'] = $info['id'];
  90. if ($state == 1) {
  91. $update['fenzhang_status'] = 2;
  92. } else {
  93. $update['fenzhang_status'] = 3;
  94. }
  95. $update['rdate'] = time();
  96. Dever::db('pay/yspay_cash')->update($update);
  97. }
  98. }
  99. }
  100. return $state;
  101. }
  102. }
  103. }
  104. return false;
  105. }
  106. private function getCash($amount, $merchant, &$data, $fenzhang = 0)
  107. {
  108. $fenzhang = $fenzhang*$this->num;
  109. $yspay = Dever::db('pay/yspay')->one(array('account_id' => $merchant['account_id']));
  110. if ($yspay) {
  111. $cash_per = 0;
  112. if ($merchant['type'] == 1 && $yspay['type'] == 2) {
  113. $cash_per = $merchant['cash_per'];
  114. if (!$cash_per) {
  115. if ($yspay && $yspay['cash_per']) {
  116. $cash_per = $yspay['cash_per'];
  117. }
  118. }
  119. if (!$cash_per || $cash_per <= 0) {
  120. $cash_per = 0;
  121. } else {
  122. $cash_per = $cash_per/100;
  123. }
  124. }
  125. $per = $yspay['per']/100;
  126. $cash_jy_per = $yspay['cash_jy_per']/100;
  127. $data['ycash'] = $amount;
  128. $data['yl_cash'] = floor($data['ycash'] * $per);
  129. $data['pt_cash'] = floor($data['ycash'] * $cash_jy_per);
  130. $data['cash'] = $data['ycash'] - $data['yl_cash'] - $data['pt_cash'];
  131. /*
  132. if ($yspay['cash_type'] == 1) {
  133. $fz_cash = $data['ycash'];
  134. } else {
  135. $fz_cash = $data['cash'];
  136. }
  137. $data['fz_cash'] = $fz_cash*$cash_per;
  138. */
  139. $data['fz_cash'] = 0;
  140. if ($fenzhang && $fenzhang > 0) {
  141. $data['fz_cash'] += $fenzhang;
  142. $data['hf_cash'] = $data['cash'] - $data['fz_cash'];
  143. } else {
  144. unset($data['fz_cash']);
  145. }
  146. }
  147. }
  148. private function huafu_act($info)
  149. {
  150. $merchant = Dever::db('pay/yspay_merchant')->one($info['merchant_id']);
  151. if ($merchant) {
  152. $config = Dever::db('pay/yspay')->one(array('account_id' => $merchant['account_id']));
  153. if ($config) {
  154. return Dever::load('pay/yspay/multi')->huafu($config, $merchant, $info);
  155. }
  156. }
  157. return false;
  158. }
  159. private function fenzhang_act($info)
  160. {
  161. $merchant = Dever::db('pay/yspay_merchant')->one($info['merchant_id']);
  162. if ($merchant) {
  163. $config = Dever::db('pay/yspay')->one(array('account_id' => $merchant['account_id']));
  164. if ($config) {
  165. return Dever::load('pay/yspay/multi')->fenzhang($config, $merchant, $info);
  166. }
  167. }
  168. return false;
  169. }
  170. /*
  171. private function tixian_act($info)
  172. {
  173. $merchant = Dever::db('pay/yspay_merchant')->one($info['merchant_id']);
  174. if ($merchant) {
  175. $config = Dever::db('pay/yspay')->one(array('account_id' => $merchant['account_id']));
  176. if ($config) {
  177. return Dever::load('pay/yspay/account')->act($config, $merchant, $info);
  178. }
  179. }
  180. return false;
  181. }*/
  182. # 将测试数据改成待入账
  183. public function test_edit_api()
  184. {
  185. $order_num = Dever::input('order_num');
  186. /*
  187. $price = Dever::input('price');
  188. $fenzhang = Dever::input('fenzhang');
  189. Dever::load('pay/yspay/cash')->up($order_num, 2, $price, $fenzhang);
  190. */
  191. $info = Dever::db('pay/yspay_cash')->find(array('source_order_num' => $order_num, 'clear' => true));
  192. if ($info['fz_cash'] && $info['fz_cash'] > 0 && $info['status'] == 2 && $info['fenzhang_status'] != 2) {
  193. $state = $this->fenzhang_act($info);
  194. if ($state == 1) {
  195. $update = array();
  196. $update['where_id'] = $info['id'];
  197. if ($state == 1) {
  198. $update['fenzhang_status'] = 2;
  199. } else {
  200. $update['fenzhang_status'] = 3;
  201. }
  202. $update['rdate'] = time();
  203. Dever::db('pay/yspay_cash')->update($update);
  204. }
  205. }
  206. return 'ok';
  207. }
  208. # 查询余额
  209. public function test_yue_api()
  210. {
  211. $info['merchant_id'] = Dever::input('merchant_id');
  212. if ($info) {
  213. $merchant = Dever::db('pay/yspay_merchant')->one($info['merchant_id']);
  214. if ($merchant) {
  215. $config = Dever::db('pay/yspay')->one(array('account_id' => $merchant['account_id']));
  216. if ($config) {
  217. return Dever::load('pay/yspay/multi')->yue($config, $merchant);
  218. }
  219. }
  220. }
  221. return 'ok';
  222. }
  223. # 查询订单
  224. public function test_query_api()
  225. {
  226. $info['merchant_id'] = Dever::input('merchant_id');
  227. $orderType = Dever::input('orderType', 1);
  228. $transDate = Dever::input('transDate');
  229. $page = Dever::input('page', 1);
  230. if ($info) {
  231. $merchant = Dever::db('pay/yspay_merchant')->one($info['merchant_id']);
  232. if ($merchant) {
  233. $config = Dever::db('pay/yspay')->one(array('account_id' => $merchant['account_id']));
  234. if ($config) {
  235. return Dever::load('pay/yspay/multi')->query($config, $merchant, $orderType, $transDate, $page);
  236. }
  237. }
  238. }
  239. return 'ok';
  240. }
  241. # 资金划付
  242. public function huafu_commit_api()
  243. {
  244. $where = array('status' => 2);
  245. $account_id = Dever::input('account_id');
  246. if ($account_id) {
  247. $where['account_id'] = $account_id;
  248. }
  249. $merchant_id = Dever::input('merchant_id');
  250. if ($merchant_id) {
  251. $where['merchant_id'] = $merchant_id;
  252. }
  253. $data = Dever::db('pay/yspay_cash')->select($where);
  254. if ($data) {
  255. foreach ($data as $k => $v) {
  256. $this->up($v['id'], 3);
  257. }
  258. }
  259. return 'ok';
  260. }
  261. # 随机获取平台商户
  262. public function getMid($account_id)
  263. {
  264. $merchant = Dever::db('pay/yspay_merchant')->select(array('type' => 2, 'account_id' => $account_id, 'status' => 1));
  265. if ($merchant) {
  266. $key = array_rand($merchant);
  267. if (isset($merchant[$key])) {
  268. return $merchant[$key];
  269. }
  270. }
  271. return false;
  272. }
  273. public function tixian($id, $name, $data)
  274. {
  275. $merchant_id = Dever::param('merchant_id', $data);
  276. $cash = Dever::param('cash', $data);
  277. $merchant = Dever::db('pay/yspay_merchant')->one($merchant_id);
  278. if ($merchant) {
  279. $admin = Dever::load('manage/auth.info');
  280. $this->tixian_act($merchant, $cash, $admin['username']);
  281. }
  282. return false;
  283. }
  284. public function tixian_act($merchant, $cash, $name = '')
  285. {
  286. if ($cash <= 0) {
  287. Dever::alert('提现金额应大于0');
  288. }
  289. $cash = $cash*$this->num;
  290. $yue = $merchant['hf_cash'] - $merchant['hf_tx_cash'] - $this->num*$this->bfj;
  291. if ($yue < $cash) {
  292. Dever::alert('余额不足');
  293. }
  294. $info['merchant_id'] = $merchant['id'];
  295. $info['hf_cash'] = $cash;
  296. $info['order_num'] = Dever::order('TX');
  297. $status = $this->huafu_act($info);
  298. if ($status == 1) {
  299. Dever::db('pay/yspay_merchant')->updateTxCash(array('where_id' => $merchant['id'], 'hf_tx_cash' => $cash));
  300. Dever::load('pay/yspay/merchant')->log($merchant['id'], $merchant['account_id'], 3, $cash, $name);
  301. }
  302. }
  303. }