Cash.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. private function tixian_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/account')->act($config, $merchant, $info);
  166. }
  167. }
  168. return false;
  169. }
  170. # 将测试数据改成待入账
  171. public function test_edit_api()
  172. {
  173. $data = Dever::db('pay/yspay_cash')->select(array('order_num' => 'test123', 'status' => 1));
  174. if ($data) {
  175. foreach ($data as $k => $v) {
  176. $this->up($v['id'], 2);
  177. }
  178. }
  179. return 'ok';
  180. }
  181. # 资金划付
  182. public function huafu_commit_api()
  183. {
  184. $where = array('status' => 2);
  185. $account_id = Dever::input('account_id');
  186. if ($account_id) {
  187. $where['account_id'] = $account_id;
  188. }
  189. $merchant_id = Dever::input('merchant_id');
  190. if ($merchant_id) {
  191. $where['merchant_id'] = $merchant_id;
  192. }
  193. $data = Dever::db('pay/yspay_cash')->select($where);
  194. if ($data) {
  195. foreach ($data as $k => $v) {
  196. $this->up($v['id'], 3);
  197. }
  198. }
  199. return 'ok';
  200. }
  201. # 随机获取平台商户
  202. public function getMid($account_id)
  203. {
  204. $merchant = Dever::db('pay/yspay_merchant')->select(array('type' => 2, 'account_id' => $account_id, 'status' => 1));
  205. if ($merchant) {
  206. $key = array_rand($merchant);
  207. if (isset($merchant[$key])) {
  208. return $merchant[$key];
  209. }
  210. }
  211. return false;
  212. }
  213. public function tixian($id, $name, $data)
  214. {
  215. $merchant_id = Dever::param('merchant_id', $data);
  216. $cash = Dever::param('cash', $data);
  217. $merchant = Dever::db('pay/yspay_merchant')->one($merchant_id);
  218. if ($merchant) {
  219. $cash = $cash*$this->num;
  220. $yue = $merchant['hf_cash'] - $merchant['hf_tx_cash'] - $this->num*$this->bfj;
  221. if ($yue < $cash) {
  222. Dever::alert('余额不足');
  223. }
  224. $info['merchant_id'] = $merchant_id;
  225. $info['hf_cash'] = $cash;
  226. $info['order_num'] = Dever::order('TX');
  227. $status = $this->huafu_act($info);
  228. if ($status == 1) {
  229. Dever::db('pay/yspay_merchant')->updateTxCash(array('where_id' => $merchant_id, 'hf_tx_cash' => $cash));
  230. }
  231. }
  232. return false;
  233. }
  234. }