'微信支付', 'yue' => '余额支付', 'alipay' => '支付宝支付']; /** * 生成订单唯一id * @param $uid 用户uid * @return string */ public static function getNewOrderId() { list($msec, $sec) = explode(' ', microtime()); $msectime = number_format((floatval($msec) + floatval($sec)) * 1000, 0, '', ''); $orderId = 'lv' . $msectime . mt_rand(10000, 99999); while (self::be(['order_id' => $orderId])) $orderId = 'lv' . $msectime . mt_rand(10000, 99999); return $orderId; } /** * 创建订单 * @param $uid * @param $level_id * @param $payType * @param $mark * @return Level|bool|\think\Model */ public static function createLevelOrder($uid, $level_id, $payType, $spread_uid = 0, $buy_num = 1) { self::beginTrans(); try { //Log::write('会员1:111'.$payType, 'error'); if (!array_key_exists($payType, self::$payType)) return self::setErrorInfo('选择支付方式有误!', true); $userInfo = User::getUserInfo($uid); if (!$userInfo) return self::setErrorInfo('用户不存在!', true); $levelInfo = SystemUserLevel::get(['id' => $level_id, 'is_del' => 0]); if (!$levelInfo) return self::setErrorInfo('等级不存在', true); list($where['uid'], $where['level_id'], $where['pay_type'], $where['paid']) = [$uid, $level_id, $payType, 0]; $info = self::where($where)->find(); if ($info) { return $info; } $orderInfo = [ 'uid' => $uid, 'order_id' => self::getNewOrderId(), 'level_id' => $level_id, 'paid' => 0, 'is_del' => 0, 'pay_price' => $levelInfo->money, 'pay_type' => $payType, 'body' => '开通会员-' . $levelInfo->name, 'addtime' => time(), 'spread_uid' => $spread_uid, 'buy_num' => $buy_num ]; $order = self::create($orderInfo); if (!$order) { Log::write('会员1:订单生成失败', 'error'); return self::setErrorInfo('订单生成失败!', true); } self::commitTrans(); return $order; } catch (\PDOException $e) { Log::write('会员2:' . $e->getMessage(), 'error'); self::rollbackTrans(); return self::setErrorInfo('生成订单时SQL执行错误错误原因:' . $e->getMessage()); } catch (\Exception $e) { Log::write('会员3:' . $e->getMessage(), 'error'); self::rollbackTrans(); return self::setErrorInfo('生成订单时系统错误错误原因:' . $e->getMessage()); } } public static function paySuccess($orderId, $paytype = 'weixin') { $order = self::where('order_id', $orderId)->find()->toArray(); $user = User::where('uid', $order['uid'])->find(); if (!$user) return false; $res1 = self::where('order_id', $orderId)->update(['paid' => 1, 'pay_type' => $paytype, 'paytime' => time()]);//订单改为支付 $res2 = UserLevel::setUserLevel($order['uid'], $order['level_id']); $res = $res1 && $res2; User::setSpreadLevel($order['spread_uid'], $order['uid']); // WechatUser::buyMemberGiveFreeCoupon($user['uid']); //购买会员送免配券 return false !== $res; } /** * TODO 小程序JS支付 * @param $orderId * @param string $field * @return array|string * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public static function jsPay($orderId, $field = 'order_id') { if (is_string($orderId)) $orderInfo = self::where($field, $orderId)->find(); else $orderInfo = $orderId; if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!'); if ($orderInfo['paid']) exception('支付已支付!'); if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!'); $openid = WechatUser::getOpenId($orderInfo['uid']); $bodyContent = "会员升级-" . SystemUserLevel::where(['id' => $orderInfo['level_id']])->value('name'); $site_name = sys_config('site_name'); if (!$bodyContent && !$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称'); return MiniProgramService::jsPay($openid, $orderInfo['order_id'], $orderInfo['buy_num'] * 100, 'user_level', self::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30)); } /** * 微信公众号JS支付 * @param $orderId * @param string $field * @return array|string * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public static function wxPay($orderId, $field = 'order_id') { if (is_string($orderId)) $orderInfo = self::where($field, $orderId)->find(); else $orderInfo = $orderId; if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!'); if ($orderInfo['paid']) exception('支付已支付!'); if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!'); $openid = WechatUser::uidToOpenid($orderInfo['uid'], 'openid'); $bodyContent = "会员升级-" . SystemUserLevel::where(['id' => $orderInfo['level_id']])->value('name'); $site_name = sys_config('site_name'); if (!$bodyContent && !$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称'); return WechatService::jsPay($openid, $orderInfo['order_id'], $orderInfo['pay_price'], 'user_level', self::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30)); } /** * 微信h5支付 * @param $orderId * @param string $field * @return array|string * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public static function h5Pay($orderId, $field = 'order_id') { if (is_string($orderId)) $orderInfo = self::where($field, $orderId)->find(); else $orderInfo = $orderId; if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!'); if ($orderInfo['paid']) exception('支付已支付!'); if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!'); $bodyContent = "会员升级-" . SystemUserLevel::where(['id' => $orderInfo['level_id']])->value('name'); $site_name = sys_config('site_name'); if (!$bodyContent && !$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称'); return WechatService::paymentPrepare(null, $orderInfo['order_id'], $orderInfo['pay_price'], 'user_level', self::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30), '', 'MWEB'); } /** * 余额支付 * @param $order_id * @param $uid * @param string $formId * @return bool * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public static function yuePay($order_id, $uid, $formId = '') { $orderInfo = self::where('uid', $uid)->where('order_id', $order_id)->find(); if (!$orderInfo) return self::setErrorInfo('订单不存在!'); if ($orderInfo['paid']) return self::setErrorInfo('该订单已支付!'); $userInfo = User::getUserInfo($uid); if ($userInfo['now_money'] < $orderInfo['pay_price']) return self::setErrorInfo(['status' => 'pay_deficiency', 'msg' => '余额不足' . floatval($orderInfo['pay_price'])]); self::beginTrans(); $res1 = false !== User::bcDec($uid, 'now_money', $orderInfo['pay_price'], 'uid'); $res2 = UserBill::expend('会员升级', $uid, 'now_money', 'pay_product', $orderInfo['pay_price'], $orderInfo['id'], $userInfo['now_money'], '余额支付' . floatval($orderInfo['pay_price']) . '元升级' . SystemUserLevel::where(['id' => $orderInfo['level_id']])->value('name')); $res3 = self::paySuccess($order_id, 'yue');//余额支付成功 try { PaymentRepositories::yuePayProduct($userInfo, $orderInfo); } catch (\Exception $e) { self::rollbackTrans(); return self::setErrorInfo($e->getMessage()); } $res = $res1 && $res2 && $res3; self::checkTrans($res); return $res; } public static function jsPayPrice($order_id, $uid, $formId = '') { $orderInfo = self::where('uid', $uid)->where('order_id', $order_id)->where('is_del', 0)->find(); if (!$orderInfo) return self::setErrorInfo('订单不存在!'); if ($orderInfo['paid']) return self::setErrorInfo('该订单已支付!'); $userInfo = User::getUserInfo($uid); self::beginTrans(); $res1 = UserBill::expend('会员升级', $uid, 'now_money', 'pay_product', $orderInfo['pay_price'], $orderInfo['id'], $userInfo['now_money'], '微信支付' . floatval($orderInfo['pay_price']) . '元升级' . SystemUserLevel::where(['id' => $orderInfo['level_id']])->value('name')); $res2 = self::paySuccess($order_id, 'weixin', $formId);//微信支付为0时 $res = $res1 && $res2; self::checkTrans($res); return $res; } }