Cash.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php namespace Pay\Yspay;
  2. use Dever;
  3. class Cash
  4. {
  5. # 入账:待确认
  6. public function add($merchant_id, $amount, $order_num, $source_order_num, $fenzhang = 0)
  7. {
  8. $merchant = Dever::db('pay/yspay_merchant')->one($merchant_id);
  9. if ($merchant) {
  10. $data = array();
  11. $data['account_id'] = $merchant['account_id'];
  12. $data['merchant_id'] = $merchant['id'];
  13. $data['order_num'] = $order_num;
  14. $data['source_order_num'] = $source_order_num;
  15. $info = Dever::db('pay/yspay_cash')->find($data);
  16. if ($amount && $amount > 0) {
  17. $this->getCash($amount, $merchant, $data, $fenzhang);
  18. }
  19. $data['status'] = 1;
  20. if ($info) {
  21. $data['where_id'] = $id = $info['id'];
  22. Dever::db('pay/yspay_cash')->update($data);
  23. } else {
  24. $id = Dever::db('pay/yspay_cash')->insert($data);
  25. }
  26. return $id;
  27. }
  28. return false;
  29. }
  30. # 修改状态
  31. public function up($id, $status = 2, $amount = false, $fenzhang = 0)
  32. {
  33. if ($amount) {
  34. $amount = $amount * 100;
  35. }
  36. if (is_numeric($id)) {
  37. $where['id'] = $id;
  38. } else {
  39. $where['source_order_num'] = $id;
  40. }
  41. $info = Dever::db('pay/yspay_cash')->find($where);
  42. if ($info) {
  43. $merchant = Dever::db('pay/yspay_merchant')->one($info['merchant_id']);
  44. if ($merchant) {
  45. $update = array('status' => $status, 'where_id' => $info['id']);
  46. if ($status == 2) {
  47. if ($amount && $amount > 0 && $info['ycash'] != $amount) {
  48. $this->getCash($amount, $merchant, $update, $fenzhang);
  49. }
  50. $update['fdate'] = time();
  51. }
  52. $state = Dever::db('pay/yspay_cash')->update($update);
  53. if ($state) {
  54. if ($status == 2) {
  55. $total = Dever::db('pay/yspay_cash')->getTotal(array('status' => 2, 'merchant_id' => $info['merchant_id']));
  56. if ($total) {
  57. 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']));
  58. }
  59. $info = Dever::db('pay/yspay_cash')->find(array('id' => $info['id'], 'clear' => true));
  60. if ($info['fz_cash'] && $info['fz_cash'] > 0) {
  61. $state = $this->fenzhang_act($info);
  62. if ($state == 1) {
  63. $update = array();
  64. $update['where_id'] = $info['id'];
  65. if ($state == 1) {
  66. $update['fenzhang_status'] = 2;
  67. } else {
  68. $update['fenzhang_status'] = 3;
  69. }
  70. $update['rdate'] = time();
  71. Dever::db('pay/yspay_cash')->update($update);
  72. }
  73. }
  74. }
  75. return $state;
  76. }
  77. }
  78. }
  79. return false;
  80. }
  81. private function getCash($amount, $merchant, &$data, $fenzhang = 0)
  82. {
  83. $yspay = Dever::db('pay/yspay')->one(array('account_id' => $merchant['account_id']));
  84. if ($yspay) {
  85. $cash_per = 0;
  86. if ($merchant['type'] == 1 && $yspay['type'] == 2) {
  87. $cash_per = $merchant['cash_per'];
  88. if (!$cash_per) {
  89. if ($yspay && $yspay['cash_per']) {
  90. $cash_per = $yspay['cash_per'];
  91. }
  92. }
  93. if (!$cash_per || $cash_per <= 0) {
  94. $cash_per = 0;
  95. } else {
  96. $cash_per = $cash_per/100;
  97. }
  98. }
  99. $per = $yspay['per']/100;
  100. $cash_jy_per = $yspay['cash_jy_per']/100;
  101. $data['ycash'] = $amount/100;
  102. $data['yl_cash'] = round($data['ycash'] * $per, 2);
  103. $data['pt_cash'] = round($data['ycash'] * $cash_jy_per, 2);
  104. $data['cash'] = round($data['ycash'] - $data['yl_cash'] - $data['pt_cash'], 2);
  105. if ($yspay['cash_type'] == 1) {
  106. $fz_cash = $data['ycash'];
  107. } else {
  108. $fz_cash = $data['cash'];
  109. }
  110. //$data['fz_cash'] = round($fz_cash*$cash_per, 2);
  111. $data['fz_cash'] = 0;
  112. if ($fenzhang && $fenzhang > 0) {
  113. $data['fz_cash'] += $fenzhang;
  114. }
  115. $data['hf_cash'] = round($data['cash'] - $data['fz_cash'], 2);
  116. $data['ycash'] *= 100;
  117. $data['yl_cash'] *= 100;
  118. $data['pt_cash'] *= 100;
  119. $data['cash'] *= 100;
  120. $data['fz_cash'] *= 100;
  121. $data['hf_cash'] *= 100;
  122. }
  123. }
  124. private function huafu_act($info)
  125. {
  126. $merchant = Dever::db('pay/yspay_merchant')->one($info['merchant_id']);
  127. if ($merchant) {
  128. $config = Dever::db('pay/yspay')->one(array('account_id' => $merchant['account_id']));
  129. if ($config) {
  130. return Dever::load('pay/yspay/multi')->huafu($config, $merchant, $info);
  131. }
  132. }
  133. return false;
  134. }
  135. private function fenzhang_act($info)
  136. {
  137. $merchant = Dever::db('pay/yspay_merchant')->one($info['merchant_id']);
  138. if ($merchant) {
  139. $config = Dever::db('pay/yspay')->one(array('account_id' => $merchant['account_id']));
  140. if ($config) {
  141. return Dever::load('pay/yspay/multi')->fenzhang($config, $merchant, $info);
  142. }
  143. }
  144. return false;
  145. }
  146. private function tixian_act($info)
  147. {
  148. $merchant = Dever::db('pay/yspay_merchant')->one($info['merchant_id']);
  149. if ($merchant) {
  150. $config = Dever::db('pay/yspay')->one(array('account_id' => $merchant['account_id']));
  151. if ($config) {
  152. return Dever::load('pay/yspay/account')->act($config, $merchant, $info);
  153. }
  154. }
  155. return false;
  156. }
  157. # 定时获取总额
  158. # 测试数据
  159. public function test_add_api()
  160. {
  161. $info['mid'] = 'test';
  162. $info['merOrderId'] = 'test123';
  163. $info['amount'] = 10000;
  164. $this->add('test123', array(), $info);
  165. return 'ok';
  166. }
  167. # 将测试数据改成待入账
  168. public function test_edit_api()
  169. {
  170. $data = Dever::db('pay/yspay_cash')->select(array('order_num' => 'test123', 'status' => 1));
  171. if ($data) {
  172. foreach ($data as $k => $v) {
  173. $this->up($v['id'], 2);
  174. }
  175. }
  176. return 'ok';
  177. }
  178. # 资金划付
  179. public function huafu_commit_api()
  180. {
  181. $where = array('status' => 2);
  182. $account_id = Dever::input('account_id');
  183. if ($account_id) {
  184. $where['account_id'] = $account_id;
  185. }
  186. $merchant_id = Dever::input('merchant_id');
  187. if ($merchant_id) {
  188. $where['merchant_id'] = $merchant_id;
  189. }
  190. $data = Dever::db('pay/yspay_cash')->select($where);
  191. if ($data) {
  192. foreach ($data as $k => $v) {
  193. $this->up($v['id'], 3);
  194. }
  195. }
  196. return 'ok';
  197. }
  198. # 随机获取平台商户
  199. public function getMid($account_id)
  200. {
  201. $merchant = Dever::db('pay/yspay_merchant')->select(array('type' => 2, 'account_id' => $account_id, 'status' => 1));
  202. if ($merchant) {
  203. $key = array_rand($merchant);
  204. if (isset($merchant[$key])) {
  205. return $merchant[$key];
  206. }
  207. }
  208. return false;
  209. }
  210. public function tixian($id, $name, $data)
  211. {
  212. $merchant_id = Dever::param('merchant_id', $data);
  213. $cash = Dever::param('cash', $data);
  214. $merchant = Dever::db('pay/yspay_merchant')->one($merchant_id);
  215. if ($merchant) {
  216. $cash = $cash*100;
  217. $yue = $merchant['hf_cash'] - $merchant['hf_tx_cash'] - 10000;
  218. if ($yue < $cash) {
  219. Dever::alert('余额不足');
  220. }
  221. $info['merchant_id'] = $merchant_id;
  222. $info['hf_cash'] = $cash;
  223. $info['order_num'] = Dever::order('TX');
  224. $status = $this->huafu_act($info);
  225. if ($status == 1) {
  226. Dever::db('pay/yspay_merchant')->updateTxCash(array('where_id' => $merchant_id, 'hf_tx_cash' => $cash));
  227. }
  228. }
  229. return false;
  230. }
  231. }