Cash.php 8.3 KB

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