Cash.php 7.3 KB

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