* @day: 2018/01/05 */ namespace app\models\user; use crmeb\basic\BaseModel; use crmeb\services\AlipayService; use crmeb\services\MiniProgramService; use crmeb\services\SystemConfigService; use crmeb\services\WechatService; use crmeb\traits\ModelTrait; /** * TODO 用户充值 * Class UserRecharge * @package app\models\user */ class UserRecharge extends BaseModel { /** * 数据表主键 * @var string */ protected $pk = 'id'; /** * 模型名称 * @var string */ protected $name = 'user_recharge'; use ModelTrait; protected $insert = ['add_time']; protected function setAddTimeAttr() { return time(); } /** * 创建充值订单 * @param $uid * @param $price * @param string $recharge_type * @param int $paid * @return UserRecharge|bool|\think\Model */ public static function addRecharge($uid, $price, $recharge_type = 'weixin', $give_price = 0, $paid = 0, $get_money_type = 'now_money', $get_money_num = 0) { $order_id = self::getNewOrderId($uid); if (!$order_id) return self::setErrorInfo('订单生成失败!'); $add_time = time(); $return_money = bcmul(sys_config('recharge_return_money', 0), $price, 8); $return_money_type = sys_config('recharge_return_money_type', ''); $return_day = sys_config('recharge_return_day', 1); 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')); } /** * 生成充值订单号 * @param int $uid * @return bool|string */ public static function getNewOrderId($uid = 0) { if (!$uid) return false; $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(); return 'wx' . date('YmdHis', time()) . (10000 + $count + $uid); } /** * 充值js支付 * @param $orderInfo * @return array|string * @throws \Exception */ public static function jsPay($orderInfo) { return MiniProgramService::jsPay(WechatUser::uidToOpenid($orderInfo['uid']), $orderInfo['order_id'], $orderInfo['price'], 'user_recharge', '用户充值'); } public static function aliPay($orderInfo) { if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!'); if ($orderInfo['paid']) exception('支付已支付!'); if ($orderInfo['price'] <= 0) exception('该支付无需支付!'); $site_name = sys_config('site_name'); if (!$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称'); $alipay = SystemConfigService::more(['alipay_app_id', 'alipay_pub_key', 'alipay_private_key', 'alipay_key']); $notifyUrl = sys_config('site_url') . '/api/alipay/notify'; $aliPay = new AlipayService(); $aliPay->setAppid($alipay['alipay_app_id']); $aliPay->setNotifyUrl($notifyUrl); $aliPay->setRsaPrivateKey($alipay['alipay_private_key']); $aliPay->setTotalFee($orderInfo['price']); $aliPay->setOutTradeNo($orderInfo['order_id']); $aliPay->setOrderName('用户充值 - ' . $site_name); $aliPay->setPassbackParams(['attach' => 'user_recharge']); $orderStr = $aliPay->getOrderStr(); return $orderStr; } public static function appPay($orderInfo) { if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!'); if ($orderInfo['paid']) exception('支付已支付!'); if ($orderInfo['price'] <= 0) exception('该支付无需支付!'); $site_name = sys_config('site_name'); $wechat = SystemConfigService::more(['weixin_open_appid', 'weixin_open_appsecret']); $payment = SystemConfigService::more(['pay_weixin_mchid', 'pay_weixin_client_cert', 'pay_weixin_client_key', 'pay_weixin_key', 'pay_weixin_open']); if (!$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称'); $config = array( 'appid' => $wechat['weixin_open_appid'], //填写高级调用功能的app id 'appsecret' => $wechat['weixin_open_appsecret'], //填写高级调用功能的app secret 'mchid' => $payment['pay_weixin_mchid'], //商户id 'key' => $payment['pay_weixin_key'], //填写你设定的key 'sslcert_path' => $payment['pay_weixin_client_cert'], 'sslkey_path' => $payment['pay_weixin_client_key'], 'transfer_rsa_public_path' => '', //企业转账到银行卡rsa公钥证书文件路径 ); $wechatpay = new \JiaLeo\Payment\Wechatpay\AppPay($config); $pay_data = [ 'body' => '用户充值 - ' . $site_name, //内容 'attach' => 'user_recharge', //商家数据包 'out_trade_no' => $orderInfo['order_id'], //商户订单号 'total_fee' => bcmul($orderInfo['price'], 100, 0), //支付价格(单位:分) 'notify_url' => sys_config('site_url') . '/api/wechat/notify', //后台回调地址 ]; $url = $wechatpay->handle($pay_data); return $url; } /** * 微信H5支付 * @param $orderInfo * @return mixed */ public static function wxH5Pay($orderInfo) { return WechatService::paymentPrepare(null, $orderInfo['order_id'], $orderInfo['price'], 'user_recharge', '用户充值', '', 'MWEB'); } /** * 公众号支付 * @param $orderInfo * @return array|string * @throws \Exception */ public static function wxPay($orderInfo) { return WechatService::jsPay(WechatUser::uidToOpenid($orderInfo['uid'], 'openid'), $orderInfo['order_id'], $orderInfo['price'], 'user_recharge', '用户充值'); } /** * //TODO用户充值成功后 * @param $orderId */ public static function rechargeSuccess($orderId) { $order = self::where('order_id', $orderId)->where('paid', 0)->find(); if (!$order) return false; $user = User::getUserInfo($order['uid']); self::beginTrans(); $price = bcadd($order['price'], $order['give_price'], 2); $res1 = self::where('order_id', $order['order_id'])->update(['paid' => 1, 'pay_time' => time()]); if ($order['get_money_type'] == 'now_money') { $mark = '成功充值余额' . floatval($order['price']) . '元' . ($order['give_price'] ? ',赠送' . $order['give_price'] . '元' : ''); $res2 = UserBill::income('用户余额充值', $order['uid'], 'now_money', 'recharge', $order['price'], $order['id'], $user['now_money'], $mark); $res3 = User::edit(['now_money' => bcadd($user['now_money'], $price, 2)], $order['uid'], 'uid'); $res = $res1 && $res2 && $res3; } else { $mark = '成功充值' . get_money_name($order['get_money_type']) . floatval($order['get_money_num']); $res2 = UserMoney::incomeMoney($order['uid'], $order['get_money_type'], $order['get_money_num'], 'recharge_money', '用户代币充值', $mark, $orderId); $res = $res1 && $res2; } self::checkTrans($res); event('RechargeSuccess', [$order]); return $res; } /** * 导入佣金到余额 * @param $uid 用户uid * @param $price 导入金额 * @return bool * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public static function importNowMoney($uid, $price) { $user = User::getUserInfo($uid); self::beginTrans(); try { $broken_time = intval(sys_config('extract_time')); $search_time = time() - 86400 * $broken_time; //返佣 + $brokerage_commission = UserBill::where(['uid' => $uid, 'category' => 'now_money', 'type' => 'brokerage']) ->where('add_time', '>', $search_time) ->where('pm', 1) ->sum('number'); //退款退的佣金 - $refund_commission = UserBill::where(['uid' => $uid, 'category' => 'now_money', 'type' => 'brokerage']) ->where('add_time', '>', $search_time) ->where('pm', 0) ->sum('number'); $broken_commission = bcsub($brokerage_commission, $refund_commission, 2); if ($broken_commission < 0) $broken_commission = 0; $commissionCount = bcsub($user['brokerage_price'], $broken_commission, 2); if ($price > $commissionCount) return self::setErrorInfo('转入金额不能大于可提现佣金!'); $res1 = User::bcInc($uid, 'now_money', $price, 'uid'); $res3 = User::bcDec($uid, 'brokerage_price', $price, 'uid'); $res2 = UserBill::expend('用户佣金转入余额', $uid, 'now_money', 'recharge', $price, 0, $user['now_money'], '成功转入余额' . floatval($price) . '元'); $extractInfo = [ 'uid' => $uid, 'real_name' => $user['nickname'], 'extract_type' => 'balance', 'extract_price' => $price, 'balance' => bcsub($user['brokerage_price'], $price, 2), 'add_time' => time(), 'status' => 1 ]; $res4 = UserExtract::create($extractInfo); $res = $res2 && $res1 && $res3; self::checkTrans($res); if ($res) { event('ImportNowMoney', [$uid, $price]); } return $res; } catch (\Exception $e) { self::rollbackTrans(); return self::setErrorInfo($e->getMessage()); } } public static function sendReturn() { $uids = self::where('paid', 1)->group('uid')->field('uid,count(id)')->select(); try { $res = true; foreach ($uids as $v) { if (UserRechargeReturnLog::where('uid', $v['uid'])->whereTime('add_time', 'today')->find()) { continue; } $send = []; $orders = self::where('paid', 1)->where('uid', $v['uid'])->select(); foreach ($orders as $vv) { 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']))) { continue; } if (isset($send[$vv['return_money_type']])) { $send[$vv['return_money_type']] = bcadd($send[$vv['return_money_type']], bcdiv($vv['return_money'], $vv['return_day'], 8), 8); } else { $send[$vv['return_money_type']] = bcdiv($vv['return_money'], $vv['return_day'], 8); } $res = $res && self::where('id', $vv['id'])->inc('returned_day', 1)->update(); } // var_dump($send); foreach ($send as $kk => $vv) { if ($kk == 'now_money' || $kk == 'integral' || $kk == 'brokerage_price') { $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, '充值每日返还'); $res = $res && User::where('uid', $v['uid'])->inc($kk, (float)$vv)->update(); } else { // var_dump(1111); // var_dump($vv); $res = $res && UserMoney::incomeMoney($v['uid'], $kk, $vv, 'recharge_return', '充值返还', '充值每日返还'); } } $res = $res && UserRechargeReturnLog::create(['uid' => $v['uid'], 'add_time' => time()]); } return $res; } catch (\Exception $e) { return self::setErrorInfo($e->getMessage()); } } }