UserRecharge.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2018/01/05
  6. */
  7. namespace app\models\user;
  8. use crmeb\basic\BaseModel;
  9. use crmeb\services\AlipayService;
  10. use crmeb\services\MiniProgramService;
  11. use crmeb\services\SystemConfigService;
  12. use crmeb\services\WechatService;
  13. use crmeb\traits\ModelTrait;
  14. /**
  15. * TODO 用户充值
  16. * Class UserRecharge
  17. * @package app\models\user
  18. */
  19. class UserRecharge extends BaseModel
  20. {
  21. /**
  22. * 数据表主键
  23. * @var string
  24. */
  25. protected $pk = 'id';
  26. /**
  27. * 模型名称
  28. * @var string
  29. */
  30. protected $name = 'user_recharge';
  31. use ModelTrait;
  32. protected $insert = ['add_time'];
  33. protected function setAddTimeAttr()
  34. {
  35. return time();
  36. }
  37. /**
  38. * 创建充值订单
  39. * @param $uid
  40. * @param $price
  41. * @param string $recharge_type
  42. * @param int $paid
  43. * @return UserRecharge|bool|\think\Model
  44. */
  45. public static function addRecharge($uid, $price, $recharge_type = 'weixin', $give_price = 0, $paid = 0)
  46. {
  47. $order_id = self::getNewOrderId($uid);
  48. if (!$order_id) return self::setErrorInfo('订单生成失败!');
  49. $add_time = time();
  50. return self::create(compact('order_id', 'uid', 'price', 'recharge_type', 'paid', 'add_time', 'give_price'));
  51. }
  52. /**
  53. * 生成充值订单号
  54. * @param int $uid
  55. * @return bool|string
  56. */
  57. public static function getNewOrderId($uid = 0)
  58. {
  59. if (!$uid) return false;
  60. $count = (int)self::where('uid', $uid)->where('add_time', '>=', strtotime(date("Y-m-d")))->where('add_time', '<', strtotime(date("Y-m-d", strtotime('+1 day'))))->count();
  61. return 'wx' . date('YmdHis', time()) . (10000 + $count + $uid);
  62. }
  63. /**
  64. * 充值js支付
  65. * @param $orderInfo
  66. * @return array|string
  67. * @throws \Exception
  68. */
  69. public static function jsPay($orderInfo)
  70. {
  71. return MiniProgramService::jsPay(WechatUser::uidToOpenid($orderInfo['uid']), $orderInfo['order_id'], $orderInfo['price'], 'user_recharge', '用户充值');
  72. }
  73. /**
  74. * 微信H5支付
  75. * @param $orderInfo
  76. * @return mixed
  77. */
  78. public static function wxH5Pay($orderInfo)
  79. {
  80. return WechatService::paymentPrepare(null, $orderInfo['order_id'], $orderInfo['price'], 'user_recharge', '用户充值', '', 'MWEB');
  81. }
  82. /**
  83. * 公众号支付
  84. * @param $orderInfo
  85. * @return array|string
  86. * @throws \Exception
  87. */
  88. public static function wxPay($orderInfo)
  89. {
  90. return WechatService::jsPay(WechatUser::uidToOpenid($orderInfo['uid'], 'openid'), $orderInfo['order_id'], $orderInfo['price'], 'user_recharge', '用户充值');
  91. }
  92. public static function aliPay($orderInfo)
  93. {
  94. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  95. if ($orderInfo['paid']) exception('支付已支付!');
  96. if ($orderInfo['price'] <= 0) exception('该支付无需支付!');
  97. $site_name = sys_config('site_name');
  98. if (!$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  99. $alipay = SystemConfigService::more(['alipay_app_id', 'alipay_pub_key', 'alipay_private_key', 'alipay_key']);
  100. $notifyUrl = sys_config('site_url') . '/api/alipay/notify';
  101. $aliPay = new AlipayService();
  102. $aliPay->setAppid($alipay['alipay_app_id']);
  103. $aliPay->setNotifyUrl($notifyUrl);
  104. $aliPay->setRsaPrivateKey($alipay['alipay_private_key']);
  105. $aliPay->setTotalFee($orderInfo['price']);
  106. $aliPay->setOutTradeNo($orderInfo['order_id']);
  107. $aliPay->setOrderName('用户充值 - ' . $site_name);
  108. $aliPay->setPassbackParams(['attach' => 'user_recharge']);
  109. $orderStr = $aliPay->getOrderStr();
  110. return $orderStr;
  111. }
  112. /**
  113. * //TODO用户充值成功后
  114. * @param $orderId
  115. */
  116. public static function rechargeSuccess($orderId)
  117. {
  118. $order = self::where('order_id', $orderId)->where('paid', 0)->find();
  119. if (!$order) return false;
  120. $user = User::getUserInfo($order['uid']);
  121. self::beginTrans();
  122. $price = bcadd($order['price'], $order['give_price'], 2);
  123. $res1 = self::where('order_id', $order['order_id'])->update(['paid' => 1, 'pay_time' => time()]);
  124. $mark = '成功充值余额' . floatval($order['price']) . '元' . ($order['give_price'] ? ',赠送' . $order['give_price'] . '元' : '');
  125. $res2 = UserBill::income('用户余额充值', $order['uid'], 'now_money', 'recharge', $order['price'], $order['id'], $user['now_money'], $mark);
  126. $res3 = User::edit(['now_money' => bcadd($user['now_money'], $price, 2)], $order['uid'], 'uid');
  127. $res = $res1 && $res2 && $res3;
  128. self::checkTrans($res);
  129. event('RechargeSuccess', [$order]);
  130. return $res;
  131. }
  132. /**
  133. * 导入佣金到余额
  134. * @param $uid 用户uid
  135. * @param $price 导入金额
  136. * @return bool
  137. * @throws \think\Exception
  138. * @throws \think\db\exception\DataNotFoundException
  139. * @throws \think\db\exception\ModelNotFoundException
  140. * @throws \think\exception\DbException
  141. */
  142. public static function importNowMoney($uid, $price)
  143. {
  144. $user = User::getUserInfo($uid);
  145. self::beginTrans();
  146. try {
  147. $broken_time = intval(sys_config('extract_time'));
  148. $search_time = time() - 86400 * $broken_time;
  149. //返佣 +
  150. $brokerage_commission = UserBill::where(['uid' => $uid, 'category' => 'now_money', 'type' => 'brokerage'])
  151. ->where('add_time', '>', $search_time)
  152. ->where('pm', 1)
  153. ->sum('number');
  154. //退款退的佣金 -
  155. $refund_commission = UserBill::where(['uid' => $uid, 'category' => 'now_money', 'type' => 'brokerage'])
  156. ->where('add_time', '>', $search_time)
  157. ->where('pm', 0)
  158. ->sum('number');
  159. $broken_commission = bcsub($brokerage_commission, $refund_commission, 2);
  160. if ($broken_commission < 0)
  161. $broken_commission = 0;
  162. $commissionCount = bcsub($user['brokerage_price'], $broken_commission, 2);
  163. if ($price > $commissionCount) return self::setErrorInfo('转入金额不能大于可提现佣金!');
  164. $res1 = User::bcInc($uid, 'now_money', $price, 'uid');
  165. $res3 = User::bcDec($uid, 'brokerage_price', $price, 'uid');
  166. $res2 = UserBill::expend('用户佣金转入余额', $uid, 'now_money', 'recharge', $price, 0, $user['now_money'], '成功转入余额' . floatval($price) . '元');
  167. $extractInfo = [
  168. 'uid' => $uid,
  169. 'real_name' => $user['nickname'],
  170. 'extract_type' => 'balance',
  171. 'extract_price' => $price,
  172. 'balance' => bcsub($user['brokerage_price'], $price, 2),
  173. 'add_time' => time(),
  174. 'status' => 1
  175. ];
  176. $res4 = UserExtract::create($extractInfo);
  177. $res = $res2 && $res1 && $res3;
  178. self::checkTrans($res);
  179. if ($res) {
  180. event('ImportNowMoney', [$uid, $price]);
  181. }
  182. return $res;
  183. } catch (\Exception $e) {
  184. self::rollbackTrans();
  185. return self::setErrorInfo($e->getMessage());
  186. }
  187. }
  188. }