Cash.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php namespace Pay\Yspay;
  2. use Dever;
  3. class Cash
  4. {
  5. # 入账:待确认
  6. public function add($mid, $amount, $order_num, $source_order_num, $fenzhang = 0)
  7. {
  8. $merchant = Dever::db('pay/yspay_merchant')->one(array('mid' => $mid));
  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 == 3) {
  47. # 开始划付
  48. $state = $this->huafu_act($info);
  49. if ($state != 'ok') {
  50. return false;
  51. }
  52. $update['rdate'] = time();
  53. } elseif ($status == 4) {
  54. # 开始提现
  55. $state = $this->tixian_act($info);
  56. if ($state != 'ok') {
  57. return false;
  58. }
  59. $update['tdate'] = time();
  60. } elseif ($status == 2 && $amount && $amount > 0 && $info['ycash'] != $amount) {
  61. # 待入账 可以修改金额
  62. $this->getCash($amount, $merchant, $update, $fenzhang);
  63. }
  64. $state = Dever::db('pay/yspay_cash')->update($update);
  65. if ($state) {
  66. if ($status == 3) {
  67. $total = Dever::db('pay/yspay_cash')->getTotal(array('status' => 3, 'merchant_id' => $info['merchant_id']));
  68. if ($total) {
  69. 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']));
  70. }
  71. }
  72. return $state;
  73. }
  74. }
  75. }
  76. return false;
  77. }
  78. private function getCash($amount, $merchant, &$data, $fenzhang = 0)
  79. {
  80. $yspay = Dever::db('pay/yspay')->one(array('account_id' => $merchant['account_id']));
  81. if ($yspay) {
  82. $cash_per = 0;
  83. if ($merchant['type'] == 1 && $yspay['type'] == 2) {
  84. $cash_per = $merchant['cash_per'];
  85. if (!$cash_per) {
  86. if ($yspay && $yspay['cash_per']) {
  87. $cash_per = $yspay['cash_per'];
  88. }
  89. }
  90. if (!$cash_per || $cash_per <= 0) {
  91. $cash_per = 0;
  92. } else {
  93. $cash_per = $cash_per/100;
  94. }
  95. }
  96. $per = $yspay['per']/100;
  97. $cash_jy_per = $yspay['cash_jy_per']/100;
  98. $data['ycash'] = $amount/100;
  99. $data['yl_cash'] = round($data['ycash'] * $per, 2);
  100. $data['pt_cash'] = round($data['ycash'] * $cash_jy_per, 2);
  101. $data['cash'] = round($data['ycash'] - $data['yl_cash'] - $data['pt_cash'], 2);
  102. if ($yspay['cash_type'] == 1) {
  103. $fz_cash = $data['ycash'];
  104. } else {
  105. $fz_cash = $data['cash'];
  106. }
  107. //$data['fz_cash'] = round($fz_cash*$cash_per, 2);
  108. $data['fz_cash'] = 0;
  109. if ($fenzhang && $fenzhang > 0) {
  110. $data['fz_cash'] += $fenzhang;
  111. }
  112. $data['hf_cash'] = round($data['cash'] - $data['fz_cash'], 2);
  113. $data['ycash'] *= 100;
  114. $data['yl_cash'] *= 100;
  115. $data['pt_cash'] *= 100;
  116. $data['cash'] *= 100;
  117. $data['fz_cash'] *= 100;
  118. $data['hf_cash'] *= 100;
  119. }
  120. }
  121. private function huafu_act($info)
  122. {
  123. $merchant = Dever::db('pay/yspay_merchant')->one($info['merchant_id']);
  124. if ($merchant) {
  125. $config = Dever::db('pay/yspay')->one(array('account_id' => $merchant['account_id']));
  126. if ($config) {
  127. return Dever::load('pay/yspay/multi')->act($config, $merchant, $info);
  128. }
  129. }
  130. return false;
  131. }
  132. private function tixian_act($info)
  133. {
  134. $merchant = Dever::db('pay/yspay_merchant')->one($info['merchant_id']);
  135. if ($merchant) {
  136. $config = Dever::db('pay/yspay')->one(array('account_id' => $merchant['account_id']));
  137. if ($config) {
  138. return Dever::load('pay/yspay/account')->act($config, $merchant, $info);
  139. }
  140. }
  141. return false;
  142. }
  143. # 定时获取总额
  144. # 测试数据
  145. public function test_add_api()
  146. {
  147. $info['mid'] = 'test';
  148. $info['merOrderId'] = 'test123';
  149. $info['amount'] = 10000;
  150. $this->add('test123', array(), $info);
  151. return 'ok';
  152. }
  153. # 将测试数据改成待入账
  154. public function test_edit_api()
  155. {
  156. $data = Dever::db('pay/yspay_cash')->select(array('order_num' => 'test123', 'status' => 1));
  157. if ($data) {
  158. foreach ($data as $k => $v) {
  159. $this->up($v['id'], 2);
  160. }
  161. }
  162. return 'ok';
  163. }
  164. # 资金划付
  165. public function huafu_commit_api()
  166. {
  167. $where = array('status' => 2);
  168. $account_id = Dever::input('account_id');
  169. if ($account_id) {
  170. $where['account_id'] = $account_id;
  171. }
  172. $merchant_id = Dever::input('merchant_id');
  173. if ($merchant_id) {
  174. $where['merchant_id'] = $merchant_id;
  175. }
  176. $data = Dever::db('pay/yspay_cash')->select($where);
  177. if ($data) {
  178. foreach ($data as $k => $v) {
  179. $this->up($v['id'], 3);
  180. }
  181. }
  182. return 'ok';
  183. }
  184. # 随机获取平台商户
  185. public function getMid($account_id)
  186. {
  187. $merchant = Dever::db('pay/yspay_merchant')->select(array('type' => 2, 'account_id' => $account_id, 'status' => 1));
  188. if ($merchant) {
  189. $key = array_rand($merchant);
  190. if (isset($merchant[$key])) {
  191. return $merchant[$key]['mid'];
  192. }
  193. }
  194. return false;
  195. }
  196. }