Recharge.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. namespace app\common\model;
  3. use \think\Db;
  4. /**
  5. * 订单支付模型
  6. */
  7. class Recharge
  8. {
  9. /**
  10. * 支付回调
  11. */
  12. //pc端充值金币
  13. public function rechargepaysucc($orderId,$args){
  14. $order = Db::name('pay_order')->where(array('pay_no'=>$orderId,'status'=>2))->find();
  15. if( $order )
  16. {
  17. $remark = $order['payment_class'] == 'alipay' ? '支付宝' : '微信';
  18. Db::startTrans();
  19. //更新订单状态
  20. $rs = Db::name('pay_order')->where(['id'=>$order['id']])->update(['status'=>1]);
  21. if(!$rs){
  22. Db::rollback();
  23. filePut('[wallet][paySucc] pay_order update fail'.$orderId);
  24. abort(500,'订单更新失败');
  25. }
  26. //先充值
  27. $result = model('Wallet')->lockChangeAccountRemain($args['user_id'],0,'gold',$args['gold'],10, $remark . '金币充值:'.$orderId,'pay_order',$order['id']);
  28. if($result['status']===false)
  29. {
  30. Db::rollback();
  31. filePut('[wallet][paySucc]网银充值入账更新余额失败 recharge money fail'.$orderId.$result['msg']);
  32. abort(500,'网银充值入账 model wallet recharge money fail');
  33. }
  34. Db::commit();
  35. return true;
  36. }else{
  37. filePut('[wallet][paySucc]在线支付订单有误 '.$orderId);
  38. abort(500,'model wallet fail');
  39. return false;
  40. }
  41. }
  42. //app充值金币
  43. public function goldpaysucc($orderId,$args,$type=0){
  44. $order = Db::name('pay_order')->where(array('pay_no'=>$orderId,'status'=>2))->find();
  45. if( $order )
  46. {
  47. Db::startTrans();
  48. //更新订单状态
  49. $rs = Db::name('pay_order')->where(['id'=>$order['id']])->update(['status'=>1]);
  50. if(!$rs){
  51. Db::rollback();
  52. filePut('[wallet][paySucc] pay_order update fail'.$orderId);
  53. abort(500,'订单更新失败');
  54. }
  55. $remark = $order['payment_class'] == 'alipay' ? '支付宝' : '微信';
  56. //先充值
  57. $result = model('Wallet')->lockChangeAccountRemain($args['user_id'],0,'gold',$args['gold'],10, $remark . '金币充值:'.$orderId,'pay_order',$order['id'],1);
  58. if($result['status']===false)
  59. {
  60. Db::rollback();
  61. filePut('[wallet][paySucc]网银充值入账更新余额失败 recharge money fail'.$orderId.$result['msg']);
  62. abort(500,'网银充值入账 model wallet recharge money fail');
  63. }
  64. //若有上级,给上级返利
  65. if ($args['intro_uid']) {
  66. //获取返利比率
  67. if ($type == 1){
  68. $intro_recharge_rebate_rate = 10;
  69. }else{
  70. $agent_info = Db::name('user')->where(['id' => $args['intro_uid']])->field('is_agent,h_intro_recharge_rebate_rate')->find();
  71. $intro_recharge_rebate_rate = ($agent_info['is_agent'] == 1) ? $agent_info['h_intro_recharge_rebate_rate'] : (int)config('site.intro_recharge_rebate_rate');
  72. }
  73. if ($intro_recharge_rebate_rate > 0 && $intro_recharge_rebate_rate <= 100) {
  74. //上级获得金额数量
  75. $intro_uid_money = number_format($args['money'] * $intro_recharge_rebate_rate / 100, 2, '.', '');
  76. if ($intro_uid_money > 0) {
  77. $intro_result = model('wallet')->lockChangeAccountRemain($args['intro_uid'],$args['user_id'],'money',$intro_uid_money,65,'邀请人充值奖励:'.$orderId,'pay_order',$order['id']);
  78. if($intro_result['status']===false)
  79. {
  80. Db::rollback();
  81. filePut('[wallet][paySucc]上级返利更新余额失败 recharge money fail'.$orderId.$intro_result['msg']);
  82. abort(500,'上级返利 model wallet recharge money fail');
  83. }
  84. }
  85. }
  86. }
  87. //首充
  88. $first_map = [
  89. 'uid' => $args['user_id'],
  90. // 'pg_id' => $args['pg_id'],
  91. ];
  92. $first_check = Db::name('user_paygold_log')->where($first_map)->find();
  93. if(empty($first_check)){
  94. //首充日志
  95. $first_map['createtime'] = time();
  96. $first_log_id = Db::name('user_paygold_log')->insertGetId($first_map);
  97. if(!$first_log_id){
  98. Db::rollback();
  99. abort(500,'首充赠送失败');
  100. }
  101. //送金币
  102. if(isset($args['first_gold']) && $args['first_gold'] > 0){
  103. $result = model('Wallet')->lockChangeAccountRemain($args['user_id'],0,'gold',$args['first_gold'],14, $remark . '金币充值:'.$orderId,'pay_order',$order['id']);
  104. if($result['status']===false)
  105. {
  106. Db::rollback();
  107. filePut('[wallet][paySucc]网银充值入账更新余额失败 recharge money fail'.$orderId.$result['msg']);
  108. abort(500,'网银充值入账 model wallet recharge money fail');
  109. }
  110. }
  111. //送vip
  112. if(isset($args['first_vipdays']) && $args['first_vipdays'] > 0){
  113. $user_info = Db::name('user_wallet')->where('user_id',$args['user_id'])->lock(true)->find();
  114. if($user_info['vip_endtime'] < time()){
  115. //过期了
  116. $vip_endtime = time() + (intval($args['first_vipdays']) * 86400);
  117. }else{
  118. //追加vip
  119. $vip_endtime = $user_info['vip_endtime'] + (intval($args['first_vipdays']) * 86400);
  120. }
  121. $result = Db::name('user_wallet')->where('user_id',$args['user_id'])->update(['vip_endtime'=>$vip_endtime]);
  122. if($result === false)
  123. {
  124. Db::rollback();
  125. filePut('[wallet][paySucc]网银充值入账更新vip时间失败 recharge money fail'.$orderId);
  126. abort(500,'网银充值入账 model wallet recharge money fail');
  127. }
  128. }
  129. }
  130. //vip充值额外送金币
  131. if(isset($args['vip_gold']) && $args['vip_gold'] > 0){
  132. $result = model('Wallet')->lockChangeAccountRemain($args['user_id'],0,'gold',$args['vip_gold'],101, $remark . '金币充值:'.$orderId,'pay_order',$order['id']);
  133. if($result['status']===false)
  134. {
  135. Db::rollback();
  136. filePut('[wallet][paySucc]网银充值入账更新余额失败 recharge money fail'.$orderId.$result['msg']);
  137. abort(500,'网银充值入账 model wallet recharge money fail');
  138. }
  139. }
  140. Db::commit();
  141. return true;
  142. }else{
  143. filePut('[wallet][paySucc]在线支付订单有误 '.$orderId);
  144. abort(500,'model wallet fail');
  145. return false;
  146. }
  147. }
  148. //app充值vip
  149. public function vippaysucc($orderId,$args){
  150. $order = Db::name('pay_order')->where(array('pay_no'=>$orderId,'status'=>2))->find();
  151. if( $order )
  152. {
  153. Db::startTrans();
  154. //更新订单状态
  155. $rs = Db::name('pay_order')->where(['id'=>$order['id']])->update(['status'=>1]);
  156. if(!$rs){
  157. Db::rollback();
  158. filePut('[wallet][paySucc] pay_order update fail'.$orderId);
  159. abort(500,'订单更新失败');
  160. }
  161. //先充值
  162. $user_info = Db::name('user_wallet')->where('user_id',$args['user_id'])->lock(true)->find();
  163. if($user_info['vip_endtime'] < time()){
  164. //过期了
  165. $vip_endtime = time() + (intval($args['days']) * 86400);
  166. }else{
  167. //追加vip
  168. $vip_endtime = $user_info['vip_endtime'] + (intval($args['days']) * 86400);
  169. }
  170. $update_data = [
  171. 'vip_endtime'=>$vip_endtime,
  172. 'vip_level' =>$args['vip_level'], //修改等级,同级不影响,向上立刻改,向下不允许
  173. 'pay_money' => bcadd($user_info['pay_money'], $args['money'], 2) //pay_money第2处修改,$args['money']是vip配置表的人民币价格
  174. ];
  175. $result = Db::name('user_wallet')->where('user_id',$args['user_id'])->update($update_data);
  176. if($result === false)
  177. {
  178. Db::rollback();
  179. filePut('[wallet][paySucc]网银充值入账更新vip时间失败 recharge money fail'.$orderId);
  180. abort(500,'网银充值入账 model wallet recharge money fail');
  181. }
  182. if ($args['gold_num'] > 0) {
  183. //赠送金币
  184. $wallet_rs = model('wallet')->lockChangeAccountRemain($args['user_id'], 0,'gold', $args['gold_num'], 17, '开通会员赠送金币');
  185. if ($wallet_rs['status'] === false) {
  186. Db::rollback();
  187. filePut('[wallet][paySucc] gold update fail' . $orderId);
  188. abort(500, '赠送金币更新失败');
  189. }
  190. }
  191. //tag任务赠送金币
  192. //开通VIP 50金币
  193. // $task_rs = \app\common\model\TaskLog::tofinish($args['user_id'],9);
  194. // if($task_rs === false){
  195. // Db::rollback();
  196. // $this->error('完成任务赠送奖励失败');
  197. // }
  198. Db::commit();
  199. return true;
  200. }else{
  201. filePut('[wallet][paySucc]在线支付订单有误 '.$orderId);
  202. abort(500,'model wallet fail');
  203. return false;
  204. }
  205. }
  206. }