Cash.php 6.8 KB

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