UserRecharge.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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, $get_money_type = 'now_money', $get_money_num = 0)
  46. {
  47. $order_id = self::getNewOrderId($uid);
  48. if (!$order_id) return self::setErrorInfo('订单生成失败!');
  49. $add_time = time();
  50. $return_money = bcmul(sys_config('recharge_return_money', 0), $price, 8);
  51. $return_money_type = sys_config('recharge_return_money_type', '');
  52. $return_day = sys_config('recharge_return_day', 1);
  53. return self::create(compact('return_money', 'return_money_type', 'return_day', 'order_id', 'uid', 'price', 'recharge_type', 'paid', 'add_time', 'give_price', 'get_money_num', 'get_money_type'));
  54. }
  55. /**
  56. * 生成充值订单号
  57. * @param int $uid
  58. * @return bool|string
  59. */
  60. public static function getNewOrderId($uid = 0)
  61. {
  62. if (!$uid) return false;
  63. $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();
  64. return 'wx' . date('YmdHis', time()) . (10000 + $count + $uid);
  65. }
  66. /**
  67. * 充值js支付
  68. * @param $orderInfo
  69. * @return array|string
  70. * @throws \Exception
  71. */
  72. public static function jsPay($orderInfo)
  73. {
  74. return MiniProgramService::jsPay(WechatUser::uidToOpenid($orderInfo['uid']), $orderInfo['order_id'], $orderInfo['price'], 'user_recharge', '用户充值');
  75. }
  76. public static function aliPay($orderInfo)
  77. {
  78. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  79. if ($orderInfo['paid']) exception('支付已支付!');
  80. if ($orderInfo['price'] <= 0) exception('该支付无需支付!');
  81. $site_name = sys_config('site_name');
  82. if (!$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  83. $alipay = SystemConfigService::more(['alipay_app_id', 'alipay_pub_key', 'alipay_private_key', 'alipay_key']);
  84. $notifyUrl = sys_config('site_url') . '/api/alipay/notify';
  85. $aliPay = new AlipayService();
  86. $aliPay->setAppid($alipay['alipay_app_id']);
  87. $aliPay->setNotifyUrl($notifyUrl);
  88. $aliPay->setRsaPrivateKey($alipay['alipay_private_key']);
  89. $aliPay->setTotalFee($orderInfo['price']);
  90. $aliPay->setOutTradeNo($orderInfo['order_id']);
  91. $aliPay->setOrderName('用户充值 - ' . $site_name);
  92. $aliPay->setPassbackParams(['attach' => 'user_recharge']);
  93. $orderStr = $aliPay->getOrderStr();
  94. return $orderStr;
  95. }
  96. public static function appPay($orderInfo)
  97. {
  98. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  99. if ($orderInfo['paid']) exception('支付已支付!');
  100. if ($orderInfo['price'] <= 0) exception('该支付无需支付!');
  101. $site_name = sys_config('site_name');
  102. $wechat = SystemConfigService::more(['weixin_open_appid', 'weixin_open_appsecret']);
  103. $payment = SystemConfigService::more(['pay_weixin_mchid', 'pay_weixin_client_cert', 'pay_weixin_client_key', 'pay_weixin_key', 'pay_weixin_open']);
  104. if (!$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  105. $config = array(
  106. 'appid' => $wechat['weixin_open_appid'], //填写高级调用功能的app id
  107. 'appsecret' => $wechat['weixin_open_appsecret'], //填写高级调用功能的app secret
  108. 'mchid' => $payment['pay_weixin_mchid'], //商户id
  109. 'key' => $payment['pay_weixin_key'], //填写你设定的key
  110. 'sslcert_path' => $payment['pay_weixin_client_cert'],
  111. 'sslkey_path' => $payment['pay_weixin_client_key'],
  112. 'transfer_rsa_public_path' => '', //企业转账到银行卡rsa公钥证书文件路径
  113. );
  114. $wechatpay = new \JiaLeo\Payment\Wechatpay\AppPay($config);
  115. $pay_data = [
  116. 'body' => '用户充值 - ' . $site_name, //内容
  117. 'attach' => 'user_recharge', //商家数据包
  118. 'out_trade_no' => $orderInfo['order_id'], //商户订单号
  119. 'total_fee' => bcmul($orderInfo['price'], 100, 0), //支付价格(单位:分)
  120. 'notify_url' => sys_config('site_url') . '/api/wechat/notify', //后台回调地址
  121. ];
  122. $url = $wechatpay->handle($pay_data);
  123. return $url;
  124. }
  125. /**
  126. * 微信H5支付
  127. * @param $orderInfo
  128. * @return mixed
  129. */
  130. public static function wxH5Pay($orderInfo)
  131. {
  132. return WechatService::paymentPrepare(null, $orderInfo['order_id'], $orderInfo['price'], 'user_recharge', '用户充值', '', 'MWEB');
  133. }
  134. /**
  135. * 公众号支付
  136. * @param $orderInfo
  137. * @return array|string
  138. * @throws \Exception
  139. */
  140. public static function wxPay($orderInfo)
  141. {
  142. return WechatService::jsPay(WechatUser::uidToOpenid($orderInfo['uid'], 'openid'), $orderInfo['order_id'], $orderInfo['price'], 'user_recharge', '用户充值');
  143. }
  144. /**
  145. * //TODO用户充值成功后
  146. * @param $orderId
  147. */
  148. public static function rechargeSuccess($orderId)
  149. {
  150. $order = self::where('order_id', $orderId)->where('paid', 0)->find();
  151. if (!$order) return false;
  152. $user = User::getUserInfo($order['uid']);
  153. self::beginTrans();
  154. $price = bcadd($order['price'], $order['give_price'], 2);
  155. $res1 = self::where('order_id', $order['order_id'])->update(['paid' => 1, 'pay_time' => time()]);
  156. if ($order['get_money_type'] == 'now_money') {
  157. $mark = '成功充值余额' . floatval($order['price']) . '元' . ($order['give_price'] ? ',赠送' . $order['give_price'] . '元' : '');
  158. $res2 = UserBill::income('用户余额充值', $order['uid'], 'now_money', 'recharge', $order['price'], $order['id'], $user['now_money'], $mark);
  159. $res3 = User::edit(['now_money' => bcadd($user['now_money'], $price, 2)], $order['uid'], 'uid');
  160. $res = $res1 && $res2 && $res3;
  161. } else {
  162. $mark = '成功充值' . get_money_name($order['get_money_type']) . floatval($order['get_money_num']);
  163. $res2 = UserMoney::incomeMoney($order['uid'], $order['get_money_type'], $order['get_money_num'], 'recharge_money', '用户代币充值', $mark, $orderId);
  164. $res = $res1 && $res2;
  165. }
  166. self::checkTrans($res);
  167. event('RechargeSuccess', [$order]);
  168. return $res;
  169. }
  170. /**
  171. * 导入佣金到余额
  172. * @param $uid 用户uid
  173. * @param $price 导入金额
  174. * @return bool
  175. * @throws \think\Exception
  176. * @throws \think\db\exception\DataNotFoundException
  177. * @throws \think\db\exception\ModelNotFoundException
  178. * @throws \think\exception\DbException
  179. */
  180. public static function importNowMoney($uid, $price)
  181. {
  182. $user = User::getUserInfo($uid);
  183. self::beginTrans();
  184. try {
  185. $broken_time = intval(sys_config('extract_time'));
  186. $search_time = time() - 86400 * $broken_time;
  187. //返佣 +
  188. $brokerage_commission = UserBill::where(['uid' => $uid, 'category' => 'now_money', 'type' => 'brokerage'])
  189. ->where('add_time', '>', $search_time)
  190. ->where('pm', 1)
  191. ->sum('number');
  192. //退款退的佣金 -
  193. $refund_commission = UserBill::where(['uid' => $uid, 'category' => 'now_money', 'type' => 'brokerage'])
  194. ->where('add_time', '>', $search_time)
  195. ->where('pm', 0)
  196. ->sum('number');
  197. $broken_commission = bcsub($brokerage_commission, $refund_commission, 2);
  198. if ($broken_commission < 0)
  199. $broken_commission = 0;
  200. $commissionCount = bcsub($user['brokerage_price'], $broken_commission, 2);
  201. if ($price > $commissionCount) return self::setErrorInfo('转入金额不能大于可提现佣金!');
  202. $res1 = User::bcInc($uid, 'now_money', $price, 'uid');
  203. $res3 = User::bcDec($uid, 'brokerage_price', $price, 'uid');
  204. $res2 = UserBill::expend('用户佣金转入余额', $uid, 'now_money', 'recharge', $price, 0, $user['now_money'], '成功转入余额' . floatval($price) . '元');
  205. $extractInfo = [
  206. 'uid' => $uid,
  207. 'real_name' => $user['nickname'],
  208. 'extract_type' => 'balance',
  209. 'extract_price' => $price,
  210. 'balance' => bcsub($user['brokerage_price'], $price, 2),
  211. 'add_time' => time(),
  212. 'status' => 1
  213. ];
  214. $res4 = UserExtract::create($extractInfo);
  215. $res = $res2 && $res1 && $res3;
  216. self::checkTrans($res);
  217. if ($res) {
  218. event('ImportNowMoney', [$uid, $price]);
  219. }
  220. return $res;
  221. } catch (\Exception $e) {
  222. self::rollbackTrans();
  223. return self::setErrorInfo($e->getMessage());
  224. }
  225. }
  226. public static function sendReturn()
  227. {
  228. $uids = self::where('paid', 1)->group('uid')->field('uid,count(id)')->select();
  229. try {
  230. $res = true;
  231. foreach ($uids as $v) {
  232. if (UserRechargeReturnLog::where('uid', $v['uid'])->whereTime('add_time', 'today')->find()) {
  233. continue;
  234. }
  235. $send = [];
  236. $orders = self::where('paid', 1)->where('uid', $v['uid'])->select();
  237. foreach ($orders as $vv) {
  238. if ($vv['return_money'] <= 0 || $vv['return_day'] <= 0 || $vv['returned_day'] >= $vv['return_day'] || !in_array($vv['return_money_type'], array_merge(array_keys(init_money_type()), ['now_money', 'integral', 'brokerage_price']))) {
  239. continue;
  240. }
  241. if (isset($send[$vv['return_money_type']])) {
  242. $send[$vv['return_money_type']] = bcadd($send[$vv['return_money_type']], bcdiv($vv['return_money'], $vv['return_day'], 8), 8);
  243. } else {
  244. $send[$vv['return_money_type']] = bcdiv($vv['return_money'], $vv['return_day'], 8);
  245. }
  246. $res = $res && self::where('id', $vv['id'])->inc('returned_day', 1)->update();
  247. }
  248. // var_dump($send);
  249. foreach ($send as $kk => $vv) {
  250. if ($kk == 'now_money' || $kk == 'integral' || $kk == 'brokerage_price') {
  251. $res = $res && UserBill::income('充值返还', $v['uid'], $kk == 'integral' ? 'integral' : 'now_money', $kk == 'brokerage_price' ? 'brokerage' : 'recharge_return', $vv, 0, User::where('uid', $v['uid'])->value($kk) + $vv, '充值每日返还');
  252. $res = $res && User::where('uid', $v['uid'])->inc($kk, (float)$vv)->update();
  253. } else {
  254. // var_dump(1111);
  255. // var_dump($vv);
  256. $res = $res && UserMoney::incomeMoney($v['uid'], $kk, $vv, 'recharge_return', '充值返还', '充值每日返还');
  257. }
  258. }
  259. $res = $res && UserRechargeReturnLog::create(['uid' => $v['uid'], 'add_time' => time()]);
  260. }
  261. return $res;
  262. } catch (\Exception $e) {
  263. return self::setErrorInfo($e->getMessage());
  264. }
  265. }
  266. }