Cash.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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'] = round($data['ycash'] * $per);
  129. $data['pt_cash'] = round($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. $yspay_main = Dever::db('pay/yspay_main')->one($merchant['yspay_main_id']);
  166. return Dever::load('pay/yspay/multi')->fenzhang($config, $yspay_main, $merchant, $info);
  167. }
  168. }
  169. return false;
  170. }
  171. /*
  172. private function tixian_act($info)
  173. {
  174. $merchant = Dever::db('pay/yspay_merchant')->one($info['merchant_id']);
  175. if ($merchant) {
  176. $config = Dever::db('pay/yspay')->one(array('account_id' => $merchant['account_id']));
  177. if ($config) {
  178. return Dever::load('pay/yspay/account')->act($config, $merchant, $info);
  179. }
  180. }
  181. return false;
  182. }*/
  183. # 将测试数据改成待入账
  184. public function test_edit_api()
  185. {
  186. $order_num = Dever::input('order_num');
  187. /*
  188. $price = Dever::input('price');
  189. $fenzhang = Dever::input('fenzhang');
  190. Dever::load('pay/yspay/cash')->up($order_num, 2, $price, $fenzhang);
  191. */
  192. $info = Dever::db('pay/yspay_cash')->find(array('source_order_num' => $order_num, 'clear' => true));
  193. if ($info['fz_cash'] && $info['fz_cash'] > 0 && $info['status'] == 2 && $info['fenzhang_status'] != 2) {
  194. $state = $this->fenzhang_act($info);
  195. if ($state == 1) {
  196. $update = array();
  197. $update['where_id'] = $info['id'];
  198. if ($state == 1) {
  199. $update['fenzhang_status'] = 2;
  200. } else {
  201. $update['fenzhang_status'] = 3;
  202. }
  203. $update['rdate'] = time();
  204. Dever::db('pay/yspay_cash')->update($update);
  205. }
  206. }
  207. return 'ok';
  208. }
  209. # 查询余额
  210. public function test_yue_api()
  211. {
  212. $info['merchant_id'] = Dever::input('merchant_id');
  213. if ($info) {
  214. $merchant = Dever::db('pay/yspay_merchant')->one($info['merchant_id']);
  215. if ($merchant) {
  216. $config = Dever::db('pay/yspay')->one(array('account_id' => $merchant['account_id']));
  217. if ($config) {
  218. return Dever::load('pay/yspay/multi')->yue($config, $merchant);
  219. }
  220. }
  221. }
  222. return 'ok';
  223. }
  224. # 查询订单
  225. public function test_query_api()
  226. {
  227. $info['merchant_id'] = Dever::input('merchant_id');
  228. $orderType = Dever::input('orderType', 1);
  229. $transDate = Dever::input('transDate');
  230. $page = Dever::input('page', 1);
  231. if ($info) {
  232. $merchant = Dever::db('pay/yspay_merchant')->one($info['merchant_id']);
  233. if ($merchant) {
  234. $config = Dever::db('pay/yspay')->one(array('account_id' => $merchant['account_id']));
  235. if ($config) {
  236. return Dever::load('pay/yspay/multi')->query($config, $merchant, $orderType, $transDate, $page);
  237. }
  238. }
  239. }
  240. return 'ok';
  241. }
  242. # 资金划付
  243. public function huafu_commit_api()
  244. {
  245. $where = array('status' => 2);
  246. $account_id = Dever::input('account_id');
  247. if ($account_id) {
  248. $where['account_id'] = $account_id;
  249. }
  250. $merchant_id = Dever::input('merchant_id');
  251. if ($merchant_id) {
  252. $where['merchant_id'] = $merchant_id;
  253. }
  254. $data = Dever::db('pay/yspay_cash')->select($where);
  255. if ($data) {
  256. foreach ($data as $k => $v) {
  257. $this->up($v['id'], 3);
  258. }
  259. }
  260. return 'ok';
  261. }
  262. # 随机获取平台商户
  263. public function getMid($account_id, $yspay_main_id = 1)
  264. {
  265. $merchant = Dever::db('pay/yspay_merchant')->select(array('type' => 2, 'account_id' => $account_id, 'yspay_main_id' => $yspay_main_id, 'status' => 1));
  266. if ($merchant) {
  267. $key = array_rand($merchant);
  268. if (isset($merchant[$key])) {
  269. return $merchant[$key];
  270. }
  271. }
  272. return false;
  273. }
  274. public function tixian($id, $name, $data)
  275. {
  276. $merchant_id = Dever::param('merchant_id', $data);
  277. $cash = Dever::param('cash', $data);
  278. $merchant = Dever::db('pay/yspay_merchant')->one($merchant_id);
  279. if ($merchant) {
  280. $admin = Dever::load('manage/auth.info');
  281. $this->tixian_act($merchant, $cash, $admin['username']);
  282. }
  283. return false;
  284. }
  285. public function tixian_act($merchant, $cash, $name = '')
  286. {
  287. if ($cash <= 0) {
  288. Dever::alert('提现金额应大于0');
  289. }
  290. $cash = $cash*$this->num;
  291. $yue = $merchant['hf_cash'] - $merchant['hf_tx_cash'] - $this->num*$this->bfj;
  292. if ($yue < $cash) {
  293. Dever::alert('余额不足');
  294. }
  295. $info['merchant_id'] = $merchant['id'];
  296. $info['hf_cash'] = $cash;
  297. $info['order_num'] = Dever::order('TX');
  298. $status = $this->huafu_act($info);
  299. if ($status == 1) {
  300. Dever::db('pay/yspay_merchant')->updateTxCash(array('where_id' => $merchant['id'], 'hf_tx_cash' => $cash));
  301. Dever::load('pay/yspay/merchant')->log($merchant['id'], $merchant['account_id'], 3, $cash, $name);
  302. }
  303. }
  304. public function total_api()
  305. {
  306. $merchant_id = Dever::input('mid');
  307. $total = Dever::db('pay/yspay_cash')->getTotal(array('status' => 2, 'merchant_id' => $merchant_id));
  308. if ($total) {
  309. Dever::db('pay/yspay_merchant')->update(array('where_id' => $merchant_id, 'cash' => $total['cash'], 'hf_cash' => $total['hf_cash'], 'fz_cash' => $total['fz_cash']));
  310. }
  311. return 'ok';
  312. }
  313. }