| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- <?php
- // +----------------------------------------------------------------------
- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- // +----------------------------------------------------------------------
- // | Author: CRMEB Team <admin@crmeb.com>
- // +----------------------------------------------------------------------
- namespace app\services\order;
- use app\adminapi\controller\v1\marketing\StoreCouponUser;
- use app\dao\order\StoreOrderDao;
- use app\model\user\User;
- use app\services\activity\coupon\StoreCouponIssueServices;
- use app\services\activity\lottery\LuckLotteryServices;
- use app\services\activity\combination\StorePinkServices;
- use app\services\BaseServices;
- use app\services\pay\PayServices;
- use app\services\user\UserBrokerageServices;
- use app\services\user\UserServices;
- use crmeb\exceptions\ApiException;
- /**
- * Class StoreOrderSuccessServices
- * @package app\services\order
- * @method getOne(array $where, ?string $field = '*', array $with = []) 获取去一条数据
- */
- class StoreOrderSuccessServices extends BaseServices
- {
- /**
- *
- * StoreOrderSuccessServices constructor.
- * @param StoreOrderDao $dao
- */
- public function __construct(StoreOrderDao $dao)
- {
- $this->dao = $dao;
- }
- /**
- * 0元支付
- * @param array $orderInfo
- * @param int $uid
- * @return bool
- * @throws \think\Exception
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function zeroYuanPayment(array $orderInfo, int $uid, string $payType = PayServices::YUE_PAY)
- {
- if ($orderInfo['paid']) {
- throw new ApiException(410265);
- }
- return $this->paySuccess($orderInfo, $payType);//余额支付成功
- }
- /**
- * 支付成功
- * @param array $orderInfo
- * @param string $paytype
- * @param array $other
- * @return bool
- * @throws \Psr\SimpleCache\InvalidArgumentException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function paySuccess(array $orderInfo, string $paytype = PayServices::WEIXIN_PAY, array $other = [])
- {
- $updata = ['paid' => 1, 'pay_type' => $paytype, 'pay_time' => time()];
- $orderInfo['pay_time'] = $updata['pay_time'];
- $orderInfo['pay_type'] = $paytype;
- if ($other && isset($other['trade_no'])) {
- $updata['trade_no'] = $other['trade_no'];
- }
- /** @var StoreOrderCartInfoServices $orderInfoServices */
- $orderInfoServices = app()->make(StoreOrderCartInfoServices::class);
- $orderInfo['storeName'] = $orderInfoServices->getCarIdByProductTitle((int)$orderInfo['id']);
- $res1 = $this->dao->update($orderInfo['id'], $updata);
- $resPink = true;
- if ($orderInfo['combination_id'] && $res1 && !$orderInfo['refund_status']) {
- /** @var StorePinkServices $pinkServices */
- $pinkServices = app()->make(StorePinkServices::class);
- /** @var StoreOrderServices $orderServices */
- $orderServices = app()->make(StoreOrderServices::class);
- $resPink = $pinkServices->createPink($orderServices->tidyOrder($orderInfo, true));//创建拼团
- }
- //缓存抽奖次数 除过线下支付
- if (isset($orderInfo['pay_type']) && $orderInfo['pay_type'] != 'offline') {
- /** @var LuckLotteryServices $luckLotteryServices */
- $luckLotteryServices = app()->make(LuckLotteryServices::class);
- $luckLotteryServices->setCacheLotteryNum((int)$orderInfo['uid'], 'order');
- }
- $orderInfo['send_name'] = $orderInfo['real_name'];
- //订单支付成功后置事件
- event('OrderPaySuccessListener', [$orderInfo]);
- //用户推送消息事件
- event('NoticeListener', [$orderInfo, 'order_pay_success']);
- //支付成功给客服发送消息
- event('NoticeListener', [$orderInfo, 'admin_pay_success_code']);
- // 推送订单
- event('OutPushListener', ['order_pay_push', ['order_id' => (int)$orderInfo['id']]]);
- // 小程序订单管理 (自提商品)
- if ($orderInfo['shipping_type'] == 2) {
- event('OrderShipping', ['product', $orderInfo, 4, '', '']);
- }
- $res = $res1 && $resPink;
- if ($orderInfo['order_type']==1){ //礼包商品订单赠送复购商品优惠卷
- $grant_id = sys_config('repeat_voucher',0);
- $grant_num = sys_config('repeat_voucher_num',0);
- if ($grant_id>0 && $grant_num>0){
- for ($i=0;$i<$grant_num;$i++){
- self::grantUser($grant_id,$orderInfo['uid']);
- }
- }
- }
- $spread_id = User::where('uid',$orderInfo['uid'])->value('spread_uid');
- if ($spread_id>0){
- self::spreadReward($spread_id,$orderInfo);
- }
- return false !== $res;
- }
- /**
- * 发放优惠券到指定个人
- * @return mixed
- */
- public function grantUser($id,$uid)
- {
- $data['id'] = $id;
- $data['uid'] = $uid;
- if (!$data['id']) return app('json')->fail(100100);
- /** @var StoreCouponIssueServices $issueService */
- $issueService = app()->make(StoreCouponIssueServices::class);
- $coupon = $issueService->get($data['id']);
- if (!$coupon) {
- return app('json')->fail(100026);
- } else {
- $coupon = $coupon->toArray();
- }
- $user = explode(',', $data['uid']);
- $issueService->setCoupon($coupon, $user);
- return true;
- }
- public function spreadReward($spread_uid, $orderInfo){
- try {
- // 获取上级的分销等级
- $agent_level = User::where('uid', $spread_uid)->value('agent_level');
- if (!$agent_level) {
- return false;
- }
- // 获取分销等级详细信息
- $agentLevelInfo = \app\model\agent\AgentLevel::where('id', $agent_level)->find();
- if (!$agentLevelInfo) {
- return false;
- }
- $agentLevelInfo = $agentLevelInfo->toArray();
- // 发放优惠券奖励
- if ($agentLevelInfo['free_issue'] > 0) {
- $this->grantUser($agentLevelInfo['free_issue'], $spread_uid);
- }
- // 发放佣金奖励
- if ($agentLevelInfo['spread_brokerage'] > 0) {
- // 计算佣金金额:订单价格 * spread_brokerage / 100
- $brokerageAmount = bcmul((string)$orderInfo['total_price'], (string)($agentLevelInfo['spread_brokerage'] / 100), 2);
- if ($brokerageAmount > 0) {
- /** @var UserServices $userServices */
- $userServices = app()->make(UserServices::class);
- /** @var UserBrokerageServices $userBrokerageServices */
- $userBrokerageServices = app()->make(UserBrokerageServices::class);
- $balance = $userServices->value(['uid' => $spread_uid], 'brokerage_price');
- $nickname = User::where('uid',$orderInfo['uid'])->value('nickname');
- $userBrokerageServices->income('get_brokerage', $spread_uid, [
- 'nickname' => $nickname,
- 'pay_price' => $orderInfo['total_price'],
- 'number' => $brokerageAmount,
- ], bcadd((string)$balance, (string)$brokerageAmount, 2), $orderInfo['id'] ?? 0);
- $userServices->bcInc($spread_uid, 'brokerage_price', $brokerageAmount, 'uid');
- }
- }
- return true;
- } catch (\Exception $e) {
- @file_put_contents("quanju.txt", json_encode($e->getMessage())."-spreadReward报错内容\r\n", 8);
- @file_put_contents("quanju.txt", json_encode($e->getLine())."-spreadReward报错位置\r\n", 8);
- @file_put_contents("quanju.txt", json_encode($e->getFile())."-spreadReward报错文件\r\n", 8);
- throw $e;
- }
- }
- }
|