Cash.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php namespace Pay\Yspay;
  2. use Dever;
  3. class Cash
  4. {
  5. # 入账:待确认
  6. public function add($mid, $amount, $order_num, $source_order_num)
  7. {
  8. $merchant = Dever::db('pay/yspay_merchant')->one(array('mid' => $mid));
  9. if ($merchant) {
  10. $data = array();
  11. $data['merchant_id'] = $merchant['id'];
  12. $data['order_num'] = $order_num;
  13. $data['source_order_num'] = $source_order_num;
  14. $info = Dever::db('pay/yspay_cash')->find($data);
  15. if ($amount && $amount > 0) {
  16. $this->getCash($amount, $merchant, $data);
  17. }
  18. $data['status'] = 1;
  19. if ($info) {
  20. $data['where_id'] = $id = $info['id'];
  21. Dever::db('pay/yspay_cash')->update($data);
  22. } else {
  23. $id = Dever::db('pay/yspay_cash')->insert($data);
  24. }
  25. return $id;
  26. }
  27. return false;
  28. }
  29. # 修改状态
  30. public function up($id, $status = 2, $amount = false)
  31. {
  32. if (is_numeric($id)) {
  33. $where['id'] = $id;
  34. } else {
  35. $where['source_order_num'] = $id;
  36. }
  37. $info = Dever::db('pay/yspay_cash')->find($where);
  38. if ($info) {
  39. $merchant = Dever::db('pay/yspay_merchant')->one($info['merchant_id']);
  40. if ($merchant) {
  41. $update = array('status' => $status, 'where_id' => $info['id']);
  42. if ($status == 3) {
  43. # 开始划付
  44. $state = $this->huafu_act($info);
  45. if ($state != 'ok') {
  46. return false;
  47. }
  48. $update['rdate'] = time();
  49. } elseif ($status == 4) {
  50. # 开始提现
  51. $state = $this->tixian_act($info);
  52. if ($state != 'ok') {
  53. return false;
  54. }
  55. $update['tdate'] = time();
  56. } elseif ($status == 2 && $amount && $amount > 0 && $info['ycash'] != $amount) {
  57. # 待入账 可以修改金额
  58. $this->getCash($amount, $merchant, $update);
  59. }
  60. $state = Dever::db('pay/yspay_cash')->update($update);
  61. if ($state) {
  62. if ($status == 3) {
  63. $total = Dever::db('pay/yspay_cash')->getTotal(array('status' => 3, 'merchant_id' => $info['merchant_id']));
  64. if ($total) {
  65. 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']));
  66. }
  67. }
  68. return $state;
  69. }
  70. }
  71. }
  72. return false;
  73. }
  74. private function getCash($amount, $merchant, &$data)
  75. {
  76. $yspay = Dever::db('pay/yspay')->one(array('account_id' => $merchant['account_id']));
  77. if ($yspay) {
  78. $cash_per = 0;
  79. if ($merchant['type'] == 1 && $yspay['type'] == 2) {
  80. $cash_per = $merchant['cash_per'];
  81. if (!$cash_per) {
  82. if ($yspay && $yspay['cash_per']) {
  83. $cash_per = $yspay['cash_per'];
  84. }
  85. }
  86. if (!$cash_per || $cash_per <= 0) {
  87. $cash_per = 0;
  88. } else {
  89. $cash_per = $cash_per/100;
  90. }
  91. }
  92. $per = $yspay['per']/100;
  93. $cash_jy_per = $yspay['cash_jy_per']/100;
  94. $data['ycash'] = $amount;
  95. $data['yl_cash'] = round($data['ycash'] * $per, 2);
  96. $data['pt_cash'] = round($data['ycash'] * $cash_jy_per, 2);
  97. $data['cash'] = round($data['ycash'] - $data['yl_cash'] - $data['pt_cash'], 2);
  98. if ($yspay['cash_type'] == 1) {
  99. $fz_cash = $data['ycash'];
  100. } else {
  101. $fz_cash = $data['cash'];
  102. }
  103. $data['fz_cash'] = round($fz_cash*$cash_per, 2);
  104. $data['hf_cash'] = round($data['cash'] - $data['fz_cash'], 2);
  105. }
  106. }
  107. private function huafu_act($info)
  108. {
  109. $merchant = Dever::db('pay/yspay_merchant')->one($info['merchant_id']);
  110. if ($merchant) {
  111. $config = Dever::db('pay/yspay')->one(array('account_id' => $merchant['account_id']));
  112. if ($config) {
  113. return Dever::load('pay/yspay/multi')->act($config, $merchant, $info);
  114. }
  115. }
  116. return false;
  117. }
  118. private function tixian_act($info)
  119. {
  120. $merchant = Dever::db('pay/yspay_merchant')->one($info['merchant_id']);
  121. if ($merchant) {
  122. $config = Dever::db('pay/yspay')->one(array('account_id' => $merchant['account_id']));
  123. if ($config) {
  124. return Dever::load('pay/yspay/account')->act($config, $merchant, $info);
  125. }
  126. }
  127. return false;
  128. }
  129. # 定时获取总额
  130. # 测试数据
  131. public function test_add_api()
  132. {
  133. $info['mid'] = 'test';
  134. $info['merOrderId'] = 'test123';
  135. $info['amount'] = 10000;
  136. $this->add('test123', array(), $info);
  137. return 'ok';
  138. }
  139. # 将测试数据改成待入账
  140. public function test_edit_api()
  141. {
  142. $data = Dever::db('pay/yspay_cash')->select(array('order_num' => 'test123', 'status' => 1));
  143. if ($data) {
  144. foreach ($data as $k => $v) {
  145. $this->up($v['id'], 2);
  146. }
  147. }
  148. return 'ok';
  149. }
  150. # 资金划付
  151. public function huafu_commit_api()
  152. {
  153. $data = Dever::db('pay/yspay_cash')->select(array('status' => 2));
  154. if ($data) {
  155. foreach ($data as $k => $v) {
  156. $this->up($v['id'], 3);
  157. }
  158. }
  159. return 'ok';
  160. }
  161. # 随机获取平台商户
  162. public function getMid($account_id)
  163. {
  164. $merchant = Dever::db('pay/yspay_merchant')->select(array('type' => 2, 'account_id' => $account_id, 'status' => 1));
  165. if ($merchant) {
  166. $key = array_rand($merchant);
  167. if (isset($merchant[$key])) {
  168. return $merchant[$key]['mid'];
  169. }
  170. }
  171. return false;
  172. }
  173. }