Cash.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php namespace Pay\Yspay;
  2. use Dever;
  3. class Cash
  4. {
  5. # 千分制
  6. private $num = 1000;
  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) . '&nbsp;&nbsp;&nbsp;&nbsp;备付金:¥'.Dever::number($beifujin).'&nbsp;&nbsp;&nbsp;&nbsp;可提现余额:¥' . Dever::number($tixian);
  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. $total = Dever::db('pay/yspay_cash')->getTotal(array('status' => 2, 'merchant_id' => $info['merchant_id']));
  74. if ($total) {
  75. 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']));
  76. }
  77. $info = Dever::db('pay/yspay_cash')->find(array('id' => $info['id'], 'clear' => true));
  78. if ($info['fz_cash'] && $info['fz_cash'] > 0) {
  79. $state = $this->fenzhang_act($info);
  80. if ($state == 1) {
  81. $update = array();
  82. $update['where_id'] = $info['id'];
  83. if ($state == 1) {
  84. $update['fenzhang_status'] = 2;
  85. } else {
  86. $update['fenzhang_status'] = 3;
  87. }
  88. $update['rdate'] = time();
  89. Dever::db('pay/yspay_cash')->update($update);
  90. }
  91. }
  92. }
  93. return $state;
  94. }
  95. }
  96. }
  97. return false;
  98. }
  99. private function getCash($amount, $merchant, &$data, $fenzhang = 0)
  100. {
  101. $amount = $amount*10;
  102. $yspay = Dever::db('pay/yspay')->one(array('account_id' => $merchant['account_id']));
  103. if ($yspay) {
  104. $cash_per = 0;
  105. if ($merchant['type'] == 1 && $yspay['type'] == 2) {
  106. $cash_per = $merchant['cash_per'];
  107. if (!$cash_per) {
  108. if ($yspay && $yspay['cash_per']) {
  109. $cash_per = $yspay['cash_per'];
  110. }
  111. }
  112. if (!$cash_per || $cash_per <= 0) {
  113. $cash_per = 0;
  114. } else {
  115. $cash_per = $cash_per/100;
  116. }
  117. }
  118. $per = $yspay['per']/100;
  119. $cash_jy_per = $yspay['cash_jy_per']/100;
  120. $data['ycash'] = $amount;
  121. $data['yl_cash'] = $data['ycash'] * $per;
  122. $data['pt_cash'] = $data['ycash'] * $cash_jy_per;
  123. $data['cash'] = $data['ycash'] - $data['yl_cash'] - $data['pt_cash'];
  124. if ($yspay['cash_type'] == 1) {
  125. $fz_cash = $data['ycash'];
  126. } else {
  127. $fz_cash = $data['cash'];
  128. }
  129. //$data['fz_cash'] = $fz_cash*$cash_per;
  130. $data['fz_cash'] = 0;
  131. if ($fenzhang && $fenzhang > 0) {
  132. $data['fz_cash'] += $fenzhang;
  133. }
  134. $data['hf_cash'] = $data['cash'] - $data['fz_cash'];
  135. }
  136. }
  137. private function huafu_act($info)
  138. {
  139. $merchant = Dever::db('pay/yspay_merchant')->one($info['merchant_id']);
  140. if ($merchant) {
  141. $config = Dever::db('pay/yspay')->one(array('account_id' => $merchant['account_id']));
  142. if ($config) {
  143. return Dever::load('pay/yspay/multi')->huafu($config, $merchant, $info);
  144. }
  145. }
  146. return false;
  147. }
  148. private function fenzhang_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')->fenzhang($config, $merchant, $info);
  155. }
  156. }
  157. return false;
  158. }
  159. /*
  160. private function tixian_act($info)
  161. {
  162. $merchant = Dever::db('pay/yspay_merchant')->one($info['merchant_id']);
  163. if ($merchant) {
  164. $config = Dever::db('pay/yspay')->one(array('account_id' => $merchant['account_id']));
  165. if ($config) {
  166. return Dever::load('pay/yspay/account')->act($config, $merchant, $info);
  167. }
  168. }
  169. return false;
  170. }*/
  171. # 将测试数据改成待入账
  172. public function test_edit_api()
  173. {
  174. $data = Dever::db('pay/yspay_cash')->select(array('order_num' => 'test123', 'status' => 1));
  175. if ($data) {
  176. foreach ($data as $k => $v) {
  177. $this->up($v['id'], 2);
  178. }
  179. }
  180. return 'ok';
  181. }
  182. # 资金划付
  183. public function huafu_commit_api()
  184. {
  185. $where = array('status' => 2);
  186. $account_id = Dever::input('account_id');
  187. if ($account_id) {
  188. $where['account_id'] = $account_id;
  189. }
  190. $merchant_id = Dever::input('merchant_id');
  191. if ($merchant_id) {
  192. $where['merchant_id'] = $merchant_id;
  193. }
  194. $data = Dever::db('pay/yspay_cash')->select($where);
  195. if ($data) {
  196. foreach ($data as $k => $v) {
  197. $this->up($v['id'], 3);
  198. }
  199. }
  200. return 'ok';
  201. }
  202. # 随机获取平台商户
  203. public function getMid($account_id)
  204. {
  205. $merchant = Dever::db('pay/yspay_merchant')->select(array('type' => 2, 'account_id' => $account_id, 'status' => 1));
  206. if ($merchant) {
  207. $key = array_rand($merchant);
  208. if (isset($merchant[$key])) {
  209. return $merchant[$key];
  210. }
  211. }
  212. return false;
  213. }
  214. public function tixian($id, $name, $data)
  215. {
  216. $merchant_id = Dever::param('merchant_id', $data);
  217. $cash = Dever::param('cash', $data);
  218. $merchant = Dever::db('pay/yspay_merchant')->one($merchant_id);
  219. if ($merchant) {
  220. $this->tixian_act();
  221. }
  222. return false;
  223. }
  224. public function tixian_act($merchant, $cash)
  225. {
  226. $cash = $cash*$this->num;
  227. $yue = $merchant['hf_cash'] - $merchant['hf_tx_cash'] - $this->num*$this->bfj;
  228. if ($yue < $cash) {
  229. Dever::alert('余额不足');
  230. }
  231. $info['merchant_id'] = $merchant_id;
  232. $info['hf_cash'] = $cash;
  233. $info['order_num'] = Dever::order('TX');
  234. $status = $this->huafu_act($info);
  235. if ($status == 1) {
  236. Dever::db('pay/yspay_merchant')->updateTxCash(array('where_id' => $merchant_id, 'hf_tx_cash' => $cash));
  237. }
  238. }
  239. }