UserRecharge.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\wap\model\user;
  12. use app\wap\model\user\WechatUser;
  13. use basic\ModelBasic;
  14. use service\SystemConfigService;
  15. use service\WechatService;
  16. use think\Log;
  17. use traits\ModelTrait;
  18. use app\wap\model\user\User;
  19. use service\HookService;
  20. class UserRecharge extends ModelBasic
  21. {
  22. use ModelTrait;
  23. protected $insert = ['add_time'];
  24. protected function setAddTimeAttr()
  25. {
  26. return time();
  27. }
  28. public static function addRecharge($uid,$price,$recharge_type = 'weixin',$paid = 0)
  29. {
  30. $order_id = self::getNewOrderId();
  31. $rate = SystemConfigService::get('gold_rate');
  32. return self::set(compact('order_id','uid','price','recharge_type','paid','rate'));
  33. }
  34. public static function getNewOrderId()
  35. {
  36. $count = (int) self::where('add_time',['>=',strtotime(date("Y-m-d"))],['<',strtotime(date("Y-m-d",strtotime('+1 day')))])->count();
  37. return 'wx1'.date('YmdHis',time()).(10000+$count+1);
  38. }
  39. public static function jsRechargePay($orderId, $field = 'order_id')
  40. {
  41. if (is_string($orderId))
  42. $orderInfo = self::where($field, $orderId)->find();
  43. else
  44. $orderInfo = $orderId;
  45. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  46. if ($orderInfo['paid']) exception('支付已支付!');
  47. if ($orderInfo['price'] <= 0) exception('该订单无需支付!');
  48. $openid = WechatUser::uidToOpenid($orderInfo['uid']);
  49. return WechatService::jsPay($openid, $orderInfo['order_id'], $orderInfo['price'], 'recharge', SystemConfigService::get('site_name'));
  50. }
  51. public static function yuePay($order_id, $user_info)
  52. {
  53. $uid = $user_info['uid'];
  54. $orderInfo = self::where('uid', $uid)->where('order_id', $order_id)->find();
  55. if (!$orderInfo) return self::setErrorInfo('订单不存在!');
  56. if ($orderInfo['paid']) return self::setErrorInfo('该订单已支付!');
  57. if ($orderInfo['recharge_type'] != 'yue') return self::setErrorInfo('该订单不能使用余额支付!');
  58. $userInfo = User::getUserInfo($uid);
  59. if ($userInfo['now_money'] < $orderInfo['price'])
  60. return self::setErrorInfo('余额不足' . floatval($orderInfo['price']));
  61. self::beginTrans();
  62. $res1 = self::where('order_id',$order_id)->update(['paid'=>1,'pay_time'=>time()]);
  63. $goldNum = money_rate_num((int)$orderInfo['price'], 'gold');
  64. $goldName = SystemConfigService::get("gold_name");
  65. $res2 = false !== UserBill::income('用户充值'.$goldName,$orderInfo['uid'],'gold_num','recharge',$goldNum,0,$user_info['gold_num'],'用户充值'.$orderInfo['price'].'元人民币获得'.$goldNum.'个'.$goldName);
  66. $res3 = User::bcInc($orderInfo['uid'],'gold_num',$goldNum,'uid');
  67. $res4 = User::bcDec($orderInfo['uid'],'now_money',$orderInfo['price'],'uid');
  68. $res5 = UserBill::expend('充值金币', $uid, 'now_money', 'recharge', $orderInfo['price'], $orderInfo['id'], $userInfo['now_money'], '余额支付' . floatval($orderInfo['price']) . '元充值'.$goldName);
  69. try {
  70. $res = $res1 && $res2 && $res3 && $res4 && ($res5 ? true : false);
  71. self::checkTrans($res);
  72. return $res;
  73. } catch (\Exception $e) {
  74. self::rollbackTrans();
  75. return self::setErrorInfo($e->getMessage());
  76. }
  77. }
  78. /**
  79. * //TODO用户微信充值成功后
  80. * @param $orderId
  81. */
  82. public static function rechargeSuccess($orderId)
  83. {
  84. $order = self::where('order_id',$orderId)->where('paid',0)->find();
  85. if(!$order) return false;
  86. $user = User::getUserInfo($order['uid']);
  87. self::beginTrans();
  88. $res1 = self::where('order_id',$order['order_id'])->update(['paid'=>1,'pay_time'=>time()]);
  89. $goldNum = money_rate_num((int)$order['price'], 'gold');
  90. $goldName = SystemConfigService::get("gold_name");
  91. $res2 = UserBill::income('用户充值'.$goldName,$order['uid'],'gold_num','recharge',$goldNum,0,$user['gold_num'],'用户充值'.$order['price'].'元人民币获得'.$goldNum.'个'.$goldName);
  92. $res3 = User::bcInc($order['uid'],'gold_num',$goldNum,'uid');
  93. $res = $res1 && $res2 && $res3 ;
  94. self::checkTrans($res);
  95. return $res;
  96. }
  97. }