Cash.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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/$this->num;
  100. }
  101. }
  102. $per = $yspay['per']/$this->num;
  103. $cash_jy_per = $yspay['cash_jy_per']/$this->num;
  104. $data['ycash'] = $amount/$this->num;
  105. $data['yl_cash'] = round($data['ycash'] * $per, 3);
  106. $data['pt_cash'] = round($data['ycash'] * $cash_jy_per, 3);
  107. $data['cash'] = round($data['ycash'] - $data['yl_cash'] - $data['pt_cash'], 3);
  108. if ($yspay['cash_type'] == 1) {
  109. $fz_cash = $data['ycash'];
  110. } else {
  111. $fz_cash = $data['cash'];
  112. }
  113. //$data['fz_cash'] = round($fz_cash*$cash_per, 3);
  114. $data['fz_cash'] = 0;
  115. if ($fenzhang && $fenzhang > 0) {
  116. $data['fz_cash'] += $fenzhang;
  117. }
  118. $data['hf_cash'] = round($data['cash'] - $data['fz_cash'], 3);
  119. $data['ycash'] *= $this->num;
  120. $data['yl_cash'] *= $this->num;
  121. $data['pt_cash'] *= $this->num;
  122. $data['cash'] *= $this->num;
  123. $data['fz_cash'] *= $this->num;
  124. $data['hf_cash'] *= $this->num;
  125. }
  126. }
  127. private function huafu_act($info)
  128. {
  129. $merchant = Dever::db('pay/yspay_merchant')->one($info['merchant_id']);
  130. if ($merchant) {
  131. $config = Dever::db('pay/yspay')->one(array('account_id' => $merchant['account_id']));
  132. if ($config) {
  133. return Dever::load('pay/yspay/multi')->huafu($config, $merchant, $info);
  134. }
  135. }
  136. return false;
  137. }
  138. private function fenzhang_act($info)
  139. {
  140. $merchant = Dever::db('pay/yspay_merchant')->one($info['merchant_id']);
  141. if ($merchant) {
  142. $config = Dever::db('pay/yspay')->one(array('account_id' => $merchant['account_id']));
  143. if ($config) {
  144. return Dever::load('pay/yspay/multi')->fenzhang($config, $merchant, $info);
  145. }
  146. }
  147. return false;
  148. }
  149. private function tixian_act($info)
  150. {
  151. $merchant = Dever::db('pay/yspay_merchant')->one($info['merchant_id']);
  152. if ($merchant) {
  153. $config = Dever::db('pay/yspay')->one(array('account_id' => $merchant['account_id']));
  154. if ($config) {
  155. return Dever::load('pay/yspay/account')->act($config, $merchant, $info);
  156. }
  157. }
  158. return false;
  159. }
  160. # 将测试数据改成待入账
  161. public function test_edit_api()
  162. {
  163. $data = Dever::db('pay/yspay_cash')->select(array('order_num' => 'test123', 'status' => 1));
  164. if ($data) {
  165. foreach ($data as $k => $v) {
  166. $this->up($v['id'], 2);
  167. }
  168. }
  169. return 'ok';
  170. }
  171. # 资金划付
  172. public function huafu_commit_api()
  173. {
  174. $where = array('status' => 2);
  175. $account_id = Dever::input('account_id');
  176. if ($account_id) {
  177. $where['account_id'] = $account_id;
  178. }
  179. $merchant_id = Dever::input('merchant_id');
  180. if ($merchant_id) {
  181. $where['merchant_id'] = $merchant_id;
  182. }
  183. $data = Dever::db('pay/yspay_cash')->select($where);
  184. if ($data) {
  185. foreach ($data as $k => $v) {
  186. $this->up($v['id'], 3);
  187. }
  188. }
  189. return 'ok';
  190. }
  191. # 随机获取平台商户
  192. public function getMid($account_id)
  193. {
  194. $merchant = Dever::db('pay/yspay_merchant')->select(array('type' => 2, 'account_id' => $account_id, 'status' => 1));
  195. if ($merchant) {
  196. $key = array_rand($merchant);
  197. if (isset($merchant[$key])) {
  198. return $merchant[$key];
  199. }
  200. }
  201. return false;
  202. }
  203. public function tixian($id, $name, $data)
  204. {
  205. $merchant_id = Dever::param('merchant_id', $data);
  206. $cash = Dever::param('cash', $data);
  207. $merchant = Dever::db('pay/yspay_merchant')->one($merchant_id);
  208. if ($merchant) {
  209. $cash = $cash*$this->num;
  210. $yue = $merchant['hf_cash'] - $merchant['hf_tx_cash'] - $this->num*100;
  211. if ($yue < $cash) {
  212. Dever::alert('余额不足');
  213. }
  214. $info['merchant_id'] = $merchant_id;
  215. $info['hf_cash'] = $cash;
  216. $info['order_num'] = Dever::order('TX');
  217. $status = $this->huafu_act($info);
  218. if ($status == 1) {
  219. Dever::db('pay/yspay_merchant')->updateTxCash(array('where_id' => $merchant_id, 'hf_tx_cash' => $cash));
  220. }
  221. }
  222. return false;
  223. }
  224. }