| xqd
@@ -36,6 +36,9 @@ use app\wap\model\topic\TestPaperObtain;
|
|
|
use app\wap\model\material\DataDownloadBuy;
|
|
|
use app\wap\model\merchant\MerchantFlowingWater;
|
|
|
|
|
|
+use app\admin\model\study\Plan;
|
|
|
+use app\admin\model\study\PlanBuy;
|
|
|
+
|
|
|
|
|
|
/**订单表
|
|
|
* Class StoreOrder
|
| xqd
@@ -1482,4 +1485,225 @@ class StoreOrder extends ModelBasic
|
|
|
$data['special_id'] = $data['special']['id'];
|
|
|
return $data;
|
|
|
}
|
|
|
+
|
|
|
+ /***学习计划支付相关代码********************************************************************/
|
|
|
+ /**
|
|
|
+ * 创建订单专题订单
|
|
|
+ * @param $special
|
|
|
+ * @param $pinkId
|
|
|
+ * @param $pay_type
|
|
|
+ * @param $uid
|
|
|
+ * @param $payType
|
|
|
+ * @param int $link_pay_uid
|
|
|
+ * @param int $total_num
|
|
|
+ * @return bool|object
|
|
|
+ */
|
|
|
+ public static function createPlanOrder($special, $pinkId, $pay_type, $uid, $payType, $link_pay_uid = 0, $total_num = 1){
|
|
|
+ if (!array_key_exists($payType, self::$payType)) return self::setErrorInfo('选择支付方式有误!');
|
|
|
+ $userInfo = User::getUserData($uid);
|
|
|
+ if (!$userInfo) return self::setErrorInfo('用户不存在!');
|
|
|
+ $total_price = 0;
|
|
|
+ $combination_id = 0;
|
|
|
+ //自己买
|
|
|
+ $total_price = $special->sales;
|
|
|
+ //会员暂时去掉
|
|
|
+ // if (isset($userInfo['level']) && $userInfo['level'] > 0 && $special->member_pay_type == 1 && $special->special > 0) {
|
|
|
+ // $total_price = $special->member_money;
|
|
|
+ // }
|
|
|
+
|
|
|
+ $res = PlanBuy::PayPlan($special->id, $uid);
|
|
|
+ if ($res) return self::setErrorInfo('您已获得该学习计划,无需再次购买!');
|
|
|
+
|
|
|
+ $orderInfo = [
|
|
|
+ 'uid' => $uid,
|
|
|
+ 'mer_id' => 0,
|
|
|
+ 'order_id' => self::getNewOrderId(),
|
|
|
+ 'cart_id' => $special->id,
|
|
|
+ 'total_num' => $total_num,
|
|
|
+ 'total_price' => $total_price,
|
|
|
+ 'pay_price' => $total_price,
|
|
|
+ 'pay_type' => $payType,
|
|
|
+ 'combination_id' => $combination_id,
|
|
|
+ 'is_gift' => $pay_type == 1 ? 1 : 0,
|
|
|
+ 'pink_time' => 0,
|
|
|
+ 'paid' => 0,
|
|
|
+ 'pink_id' => $pinkId,
|
|
|
+ 'unique' => md5(time() . '' . $uid . $special->id),
|
|
|
+ 'cost' => $total_price,
|
|
|
+ 'link_pay_uid' => $userInfo['spread_uid'] ? 0 : $link_pay_uid,
|
|
|
+ 'spread_uid' => $userInfo['spread_uid'] ? $userInfo['spread_uid'] : 0,
|
|
|
+ 'is_del' => 0,
|
|
|
+ ];
|
|
|
+ $order = self::set($orderInfo);
|
|
|
+ if (!$order) return self::setErrorInfo('订单生成失败!');
|
|
|
+ StoreOrderStatus::status($order['id'], 'cache_key_create_order', '订单生成');
|
|
|
+ return $order;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 学习计划微信支付 为 0元时
|
|
|
+ * @param $order_id
|
|
|
+ * @param $uid
|
|
|
+ * @return bool
|
|
|
+ */
|
|
|
+ public static function jsPayPlanPrice($order_id, $uid)
|
|
|
+ {
|
|
|
+ $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::getUserData($uid);
|
|
|
+ self::beginTrans();
|
|
|
+ $res1 = UserBill::expend('购买学习计划', $uid, 'now_money', 'pay_product', $orderInfo['pay_price'], $orderInfo['id'], $userInfo['now_money'], '支付' . floatval($orderInfo['pay_price']) . '元购买学习计划');
|
|
|
+ $res2 = self::payPlanSuccess($order_id);
|
|
|
+ $res = $res1 && $res2;
|
|
|
+ self::checkTrans($res);
|
|
|
+ return $res;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * //TODO 支付成功后
|
|
|
+ * @param $orderId
|
|
|
+ * @param $notify
|
|
|
+ * @return bool
|
|
|
+ */
|
|
|
+ public static function payPlanSuccess($orderId)
|
|
|
+ {
|
|
|
+ $order = self::where('order_id', $orderId)->where('type', 0)->find();
|
|
|
+ if (!$order) return false;
|
|
|
+ $resPink = true;
|
|
|
+ $res2 = true;
|
|
|
+ $res3 = true;
|
|
|
+ User::bcInc($order['uid'], 'pay_count', 1, 'uid');
|
|
|
+ $res1 = self::where('order_id', $orderId)->where('type', 0)->update(['paid' => 1, 'pay_time' => time()]);
|
|
|
+ if ($res1 && $order['pay_type'] != 'yue') {
|
|
|
+ $res2 = UserBill::expend('购买学习计划', $order['uid'], $order['pay_type'], 'pay_product', $order['pay_price'], $order['id'], 0, '支付' . floatval($order['pay_price']) . '元购买学习计划');
|
|
|
+ }
|
|
|
+ if ($res1) {
|
|
|
+ $res3 = MerchantFlowingWater::setMerchantFlowingWater($order, 6);
|
|
|
+ }
|
|
|
+ if ($order['combination_id'] && $res1 && !$order['refund_status']) {
|
|
|
+ //$resPink = StorePink::createPink($order);//创建拼团
|
|
|
+ } else {
|
|
|
+ if (!$order['is_gift']) {
|
|
|
+ //如果是专栏,记录专栏下所有专题购买。
|
|
|
+ PlanBuy::setAllBuyplan($orderId, $order['uid'], $order['cart_id']);
|
|
|
+
|
|
|
+ //DataDownloadBuy::setDataDownload($orderId, $order['uid'], $order['cart_id'], 0);
|
|
|
+ try {
|
|
|
+ //专题返佣
|
|
|
+ User::backOrderBrokerage($order);
|
|
|
+ } catch (\Throwable $e) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ StoreOrderStatus::status($order->id, 'pay_success', '用户付款成功');
|
|
|
+ $site_url = SystemConfigService::get('site_url');
|
|
|
+ try {
|
|
|
+ $wechat_notification_message = SystemConfigService::get('wechat_notification_message');
|
|
|
+ if ($wechat_notification_message == 1) {
|
|
|
+ WechatTemplateService::sendTemplate(WechatUser::where('uid', $order['uid'])->value('openid'), WechatTemplateService::ORDER_PAY_SUCCESS, [
|
|
|
+ 'first' => '亲,您购买的专题已支付成功',
|
|
|
+ 'keyword1' => $orderId,
|
|
|
+ 'keyword2' => $order['pay_price'],
|
|
|
+ 'remark' => '点击查看订单详情'
|
|
|
+ ], $site_url . Url::build('wap/studyplan/index'));
|
|
|
+ WechatTemplateService::sendAdminNoticeTemplate([
|
|
|
+ 'first' => "亲,您有一个新的课程订单",
|
|
|
+ 'keyword1' => $orderId,
|
|
|
+ 'keyword2' => $order['pay_price'],
|
|
|
+ 'remark' => '请及时查看'
|
|
|
+ ]);
|
|
|
+ } else {
|
|
|
+ $data['character_string1']['value'] = $orderId;
|
|
|
+ $data['amount3']['value'] = $order['pay_price'];
|
|
|
+ $data['time2']['value'] = date('Y-m-d H:i:s', time());
|
|
|
+ $data['thing6']['value'] = '您购买的专题已支付成功!';
|
|
|
+ RoutineTemplate::sendOrderSuccess($data, $order['uid'], $site_url . Url::build('wap/studyplan/index'));
|
|
|
+ $dataAdmin['character_string1']['value'] = $orderId;
|
|
|
+ $dataAdmin['amount3']['value'] = $order['pay_price'];
|
|
|
+ $dataAdmin['time2']['value'] = date('Y-m-d H:i:s', time());
|
|
|
+ $dataAdmin['thing6']['value'] = '您有一个新的课程订单!';
|
|
|
+ RoutineTemplate::sendAdminNoticeTemplate($dataAdmin);
|
|
|
+ }
|
|
|
+ } catch (\Throwable $e) {
|
|
|
+ }
|
|
|
+ $res = $res1 && $resPink && $res2 && $res3;
|
|
|
+ return false !== $res;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 学习计划微信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 h5PlanPay($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('该支付无需支付!');
|
|
|
+ $site_name = SystemConfigService::get('site_name');
|
|
|
+ if (!$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
|
|
|
+ return WechatService::paymentPrepare(null, $orderInfo['order_id'], $orderInfo['pay_price'], 'plan', self::getSubstrUTf8($site_name . '-专题购买', 30), '', 'MWEB');
|
|
|
+ }
|
|
|
+
|
|
|
+ /**学习计划微信支付
|
|
|
+ * @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 jsPlanPay($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('该支付无需支付!');
|
|
|
+ $site_name = SystemConfigService::get('site_name');
|
|
|
+ $openid = WechatUser::uidToOpenid($orderInfo['uid']);
|
|
|
+ return WechatService::jsPay($openid, $orderInfo['order_id'], $orderInfo['pay_price'], 'plan', self::getSubstrUTf8($site_name, 30));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**专题余额支付
|
|
|
+ * @param $order_id
|
|
|
+ * @param $uid
|
|
|
+ * @return bool
|
|
|
+ * @throws \think\db\exception\DataNotFoundException
|
|
|
+ * @throws \think\db\exception\ModelNotFoundException
|
|
|
+ * @throws \think\exception\DbException
|
|
|
+ */
|
|
|
+ public static function yuePlanPay($order_id, $uid)
|
|
|
+ {
|
|
|
+ $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('该订单已支付!');
|
|
|
+ if ($orderInfo['pay_type'] != 'yue') return self::setErrorInfo('该订单不能使用余额支付!');
|
|
|
+ $userInfo = User::getUserData($uid);
|
|
|
+ if ($userInfo['now_money'] < $orderInfo['pay_price']) return self::setErrorInfo('余额不足' . 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'], bcsub($userInfo['now_money'], $orderInfo['pay_price'], 2), '余额支付' . floatval($orderInfo['pay_price']) . '元购买专题');
|
|
|
+ $res3 = self::payPlanSuccess($order_id);
|
|
|
+ try {
|
|
|
+ HookService::listen('yue_pay_product', $userInfo, $orderInfo, false, PaymentBehavior::class);
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ self::rollbackTrans();
|
|
|
+ return self::setErrorInfo($e->getMessage());
|
|
|
+ }
|
|
|
+ $res = $res1 && $res2 && $res3;
|
|
|
+ self::checkTrans($res);
|
|
|
+ return $res;
|
|
|
+ }
|
|
|
+
|
|
|
}
|