123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Bear
- * Date: 2019/8/12
- * Time: 下午7:14
- */
- namespace app\main\service;
- use app\common\library\Ip;
- use app\common\library\Redis;
- use app\common\model\Goods;
- use app\main\constants\AdminConstants;
- use app\main\constants\ErrorCodeConstants;
- use app\main\constants\OrderContents;
- use app\main\constants\PayConstants;
- use app\main\InternalInterface\PayInterface;
- use app\main\model\object\OrderObject;
- use app\main\model\object\ReturnObject;
- use app\main\model\object\UserObject;
- use app\main\model\object\WxPayObject;
- use app\main\model\object\WxPayParamsObject;
- use fast\Random;
- abstract class BasePayService extends BaseService implements PayInterface
- {
- /**
- * @var OrderObject
- */
- public $oOrder;
- /**
- * @var WxPayObject
- */
- public $oWxPay;
- /**
- * @var WxPayParamsObject
- */
- public $oWxPayParams;
- /**
- * @var UserObject
- */
- public $oUser;
- /**
- * @var Goods
- */
- public $mGoods;
- /**
- * @param $payment_method
- * @return $this
- */
- public function init($payment_method)
- {
- $pay = OfficialAccountsService::instance()
- ->getWxpayModel()
- ->where('payment_method', $payment_method)
- ->where('status', PayConstants::WXPAY_STATUS_ON)
- ->find();
- if (!$pay) {
- throw new \Exception('支付方式不存在,请换一种支付方式');
- }
- $this->oWxPay = (new WxPayObject())->bind($pay->toArray());
- $this->oOrder = new OrderObject();
- return $this;
- }
- /**
- * 设置用户属性
- * @param UserObject $user
- * @return $this
- */
- public function setUser(UserObject $user)
- {
- $this->oUser = $user;
- return $this;
- }
- /**
- * 获取用户属性
- * @return UserObject
- */
- public function getUser()
- {
- return $this->oUser;
- }
- /**
- * 设置主动参数
- * @param $wxPayParams
- * @return $this
- */
- public function setParams($wxPayParams)
- {
- $this->oWxPayParams = (new WxPayParamsObject())->bind($wxPayParams);
- return $this;
- }
- /**
- * 检查活动是否有效
- * @param null $activity_id
- * @return ReturnObject
- */
- public function checkActivity($activity_id = null)
- {
- if (!$activity_id) {
- $activity_id = $this->oWxPayParams->activityId;
- }
- if (!$activity_id) {
- return $this->getReturn();
- }
- $activity = ActivityService::instance()->getActivityModel()->where('id', $activity_id)->find();
- if (!$activity) {
- return $this->setCode(ErrorCodeConstants::PARAMS_ERROR_INVALID)->setMsg('活动不存在')->getReturn();
- }
- if ($activity['starttime'] > time() || $activity['endtime'] < time()) {
- return $this->setCode(ErrorCodeConstants::PARAMS_ERROR_INVALID)->setMsg('活动未进行')->getReturn();
- }
- return $this->getReturn();
- }
- /**
- * @return \app\main\model\object\ReturnObject|array
- */
- public function saveOrder()
- {
- try {
- $goods_id = $this->oWxPayParams->id ?? 0;
- if (!$this->mGoods = GoodsService::instance()->getGoodsModel()->getGoodsInfoById($goods_id)) {
- throw new \Exception('商品不存在');
- }
- $checkActivity = $this->checkActivity();
- if ($checkActivity->code != ErrorCodeConstants::SUCCESS) {
- return $checkActivity;
- }
- //初始化订单信息
- $this->initOrderInfo();
- $cacheKey = OrderService::instance()->getOrderCacheKey($this->oOrder)->data;
- if ($cache = Redis::instance()->get($cacheKey)) {
- LogService::debug('调用缓存:' . $cacheKey . ':' . $cache);
- return $this->setData(json_decode($cache, true))->getReturn();
- }
- //订单扣量&订单转移
- list($admin_id, $group_id) = $this->initOrderDeductionOrTransfer();
- $this->oOrder->admin_id = $admin_id;
- //订单赏金处理
- $this->initOrderReward();
- //三方平台订单创建
- $pay_json = $this->createOrder();
- //系统创建订单
- if (!$ordersId = OrderService::instance()->getOrderModel()->insertGetId($this->oOrder->toArray())) {
- throw new \Exception("订单创建失败");
- }
- if ($pay_json->code == ErrorCodeConstants::SUCCESS) {
- $pay_json->data['order_id'] = $this->oOrder->out_trade_no;
- Redis::instance()->setex($cacheKey, PayConstants::getPayTimeoutSeconds(), json_encode($pay_json->data));
- }
- //统计相关信息,扣量不统计
- if (!$this->oOrder->deduct) {
- $channel_id = WebUserService::instance()->getUserChannelId()->data;
- $business = $this->oWxPayParams->businessLine ?? PayConstants::BUSINESS_APP;
- OrderCollectService::instance()->addCreateOrderCollect($this->oOrder->user_id, $admin_id, (bool)$this->oOrder->day, $channel_id, $group_id, $business, (bool)$this->oOrder->activity_id);
- }
- return $pay_json;
- } catch (\Exception $e) {
- return $this->setCode(ErrorCodeConstants::EXCEPTION)->setMsg($e->getMessage())->getReturn();
- }
- }
- /**
- * 订单扣量与转移
- */
- protected function initOrderDeductionOrTransfer()
- {
- $admin_id = WebUserService::instance()->getUserAdminId()->data;
- $group_id = model('AuthGroupAccess')->getGroupId($admin_id);
- $channel_id = $this->oUser->channel_id;
- $is_activity = (bool)$this->oWxPayParams->activityId;
- try {
- if ($channel_id) {//订单KL
- if ($adminInfo = AdminKlService::instance()->orderKL($admin_id, $channel_id, $this->oOrder->toArray(), $is_activity)->data) {
- $this->oOrder->deduct = OrderContents::ORDER_DEDUCT_YES;
- $this->oOrder->admin_id = $adminInfo['admin_id'];
- $this->oOrder->resource_id = $admin_id;
- }
- }
- if ($group_id == AdminConstants::ADMIN_GROUP_ID_AGENT && empty($this->oOrder->deduct)) { //订单ZY
- if ($adminInfo = AdminKlService::instance()->orderTransfer($admin_id, $channel_id, $this->oOrder->toArray(), $is_activity)) {
- $this->oOrder->benefit = $adminInfo['benefit'];
- $this->oOrder->money_benefit = round($this->mGoods['money'] * $adminInfo['benefit'], 2);
- $this->oOrder->admin_id = $channel_id;
- $this->oOrder->resource_id = $admin_id;
- $admin_id = $channel_id;
- $group_id = AdminConstants::ADMIN_GROUP_ID_CHANNEL;
- }
- }
- } catch (\Exception $e) {
- LogService::error('订单KL失败, Error:' . $e->getMessage());
- }
- return [$admin_id, $group_id];
- }
- /**
- * 订单的赏金处理
- */
- final protected function initOrderReward()
- {
- $admin_id = WebUserService::instance()->getUserAdminId()->data;
- $adminExtend = AdminService::instance()->getAdminExtendModel()->getInfo($this->oOrder->admin_id);
- $is_activity = (bool)$this->oWxPayParams->activityId;
- $group_id = model('AuthGroupAccess')->getGroupId($admin_id);
- //不是活动订单,如果订单没有扣量也没有转移,判断订单是否有赏金
- if (
- !$is_activity &&
- empty($this->oOrder->deduct) &&
- empty($this->oOrder->resource_id) &&
- $group_id == 4 &&
- !empty($adminExtend['invite_id'])
- ) {
- $invite_admin = model('AdminExtend')->where(['admin_id' => $adminExtend['invite_id']])->find();
- $admin_agent = model('Admin')->where('id', $adminExtend['invite_id'])->find();
- if ($invite_admin && $admin_agent['status'] == 'normal') {
- $this->oOrder->reward_money = round($this->mGoods['money'] * $invite_admin['reward_benefit'], 2);
- $this->oOrder->reward_admin_id = $adminExtend['invite_id'];
- $this->oOrder->reward_benefit = $invite_admin['reward_benefit'];
- }
- }
- }
- /**
- * 初始化订单信息
- */
- protected function initOrderInfo()
- {
- $this->oOrder->admin_id = WebUserService::instance()->getUserAdminId()->data;
- $adminExtend = AdminService::instance()->getAdminExtendModel()->getInfo($this->oOrder->admin_id);
- $benefit = $adminExtend['benefit'];
- if($this->oWxPay->business_line == PayConstants::BUSINESS_APP){
- $benefit = $adminExtend['benefit_app'];
- }
- //初始化订单信息
- $this->oOrder->user_id = $this->oUser->id;
- $this->oOrder->benefit = $benefit;
- $this->oOrder->money = $this->mGoods['money'];
- $this->oOrder->money_benefit = round($this->mGoods['money'] * $benefit, 2);
- $this->oOrder->category = $this->mGoods['category_id'];
- $this->oOrder->ip = Ip::ip();
- $this->oOrder->referral_id = $this->oUser->referral_id;
- $this->oOrder->referral_id_permanent = $this->oUser->referral_id_permanent;
- $this->oOrder->book_id = $this->oWxPayParams->bookId;
- $this->oOrder->chapter_id = $this->oWxPayParams->chapterId;
- $this->oOrder->goods_id = $this->mGoods['id'];
- $this->oOrder->kandian = $this->mGoods['kandian'];
- $this->oOrder->free_kandian = $this->mGoods['free_kandian'];
- $this->oOrder->day = $this->mGoods['day'];
- $this->oOrder->type = $this->mGoods['type'];
- $this->oOrder->state = OrderContents::ORDER_STATE_TO_PAY;
- $this->oOrder->activity_id = $this->oWxPayParams->activityId;
- $this->oOrder->wxpay_id = $this->oWxPay->id;
- $this->oOrder->payment_method = $this->oWxPay->payment_method;
- $this->oOrder->createtime = time();
- $this->oOrder->updatetime = time();
- $this->oOrder->business_line = $this->oWxPay->business_line;
- $this->oOrder->ext = $this->oUser->ext;
- //需要三方支付补充信息
- $this->oOrder->payid = '';
- $this->oOrder->pay_json = '';
- $this->oOrder->out_trade_no = '';
- $this->oOrder->pdorderid = '';
- }
- /**
- * 获取订单编号
- * @param $user_id
- * @return \app\main\model\object\ReturnObject
- */
- protected function getOutTradeNo($user_id)
- {
- $number = date('YmdHis') . '_' . $user_id . '_' . Random::build('alnum', 4);
- return $this->setData($number)->getReturn();
- }
- /**
- * 获取跳转地址
- * @return \app\main\model\object\ReturnObject
- */
- public function getRedirectUrl()
- {
- $url = $_SERVER['HTTP_REFERER'] ?? '';
- if ($url) {
- $referral = parse_url($url);
- $url = $referral['scheme'] . '://' . $referral['host'] . $referral['path'] . '?status=return';
- }
- return $this->setData($url)->getReturn();
- }
- /**
- * 调用第三方服务创建订单
- * @return ReturnObject
- */
- abstract function createOrder();
- }
|