| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\client\models\v1;
- use app\models\Level;
- use app\models\LevelOrder;
- use app\models\Order;
- use app\models\ReOrder;
- use app\models\SaasUser;
- use app\models\UserShareMoney;
- use app\modules\client\models\ApiModel;
- use app\models\AccountLog;
- use app\models\common\CommonOrder;
- use app\models\User;
- use app\modules\client\models\OrderComplete;
- use app\utils\Alipay\Alipay;
- use app\utils\Alipay\AlipayProfit;
- use app\utils\CloudPrint;
- use app\utils\OrderNo;
- use app\utils\Wechat\WechatNewPay;
- use app\utils\Wechat\WechatPay;
- use yii\base\BaseObject;
- use yii\base\Model;
- use yii\helpers\Json;
- use app\utils\Delivery\Delivery;
- use app\models\Store;
- /**
- * @property User $user
- * @property LevelOrder $order
- */
- class LevelPayDataForm extends ApiModel
- {
- public $store_id;
- public $order_id;
- public $order_id_list;
- public $pay_type;
- public $user;
- public $form_id;
- public $parent_user_id;
- public $condition;
- public $_from;
- public $is_combine;
- public $session_key;
- /** @var Wechat $wechat */
- private $wechat;
- private $wechatPay;
- private $order;
- const PAY_FROM_APP = 'app';
- const PAY_FROM_OFFICIAL = 'official';
- const PAY_FROM_H5 = 'h5';
- const PAY_FROM_MINI = 'mini';
- public function rules()
- {
- return [
- [['pay_type', '_from'], 'required'],
- [['_from'], 'in', 'range' => ['app', 'mini', 'official', self::PAY_FROM_H5]],
- [['pay_type'], 'in', 'range' => [1, 2, 3, 4, 5, Order::PAY_TYPE_WX_B2B]], // 'WECHAT_PAY', 'HUODAO_PAY', 'BALANCE_PAY' 'ALIPAY',
- [['_from'], 'string'],
- ['is_combine', 'default', 'value' => 0],
- [[ 'order_id', 'is_combine'], 'integer'],
- [['session_key'], 'safe']
- ];
- }
- public function search()
- {
- $this->wechatPay = \Yii::$app->controller->wechatPay;
- if (!$this->validate()) {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0],
- ];
- }
- $this->user->money = doubleval($this->user->money);
- $t = \Yii::$app->db->beginTransaction();
- $this->order = LevelOrder::findOne([
- 'store_id' => $this->store_id,
- 'id' => $this->order_id,
- ]);
- if (!$this->order) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '订单不存在',
- ];
- }
- if ($this->order->is_delete == 1 || $this->order->is_pay == 1) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '订单已取消或已支付',
- ];
- }
- $goods_names = '会员购买';
- // 余额抵扣支付
- $balance_price = 0;
- if ($this->is_combine) {
- if ($this->user->money >= $this->order->pay_price) {
- $this->pay_type = 3;
- } else {
- $balance_price = $this->user->money;
- $this->order->combine_money = $this->user->money;
- $this->order->is_combine_pay = 1;
- $this->order->save();
- }
- } else {
- $this->order->combine_money = 0;
- $this->order->is_combine_pay = 0;
- $this->order->save();
- }
- // 供应链系统下单时是否使用平台商户号,1使用,0未使用
- if (\Yii::$app->prod_is_dandianpu() && !Store::hasIncoming($this->store_id)) {
- $this->order->is_use_platform_mch = 1;
- $this->order->save();
- }
- if ($this->pay_type == 1 || $this->pay_type == 4) {
- if ($this->order->pay_price == 0) {
- $this->order->is_pay = 1;
- $this->order->pay_type = 1;
- if ($this->pay_type == 4) {
- $this->order->pay_type = 4;
- }
- $this->order->pay_time = time();
- if (!$this->order->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '支付失败'
- ];
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '0元支付'
- ];
- }
- if ($this->pay_type == 1) {
- // if ($this->_from == self::PAY_FROM_APP) {
- // if (is_profit_pay()) {
- // $result = WechatNewPay::orderUnify($this->order, OrderNo::ORDER_RECHARGE, $goods_names, 0, true, $balance_price);
- // } else {
- // $result = WechatPay::orderUnify($this->order, OrderNo::ORDER_RECHARGE, $goods_names, 0, true, $balance_price);
- // }
- // } else {
- // if (is_profit_pay()) {
- // $result = WechatNewPay::orderUnify($this->order, OrderNo::ORDER_RECHARGE, $goods_names, 0, false, $balance_price);
- // } else {
- // $result = WechatPay::orderUnify($this->order, OrderNo::ORDER_RECHARGE, $goods_names, 0, false, $balance_price);
- // }
- // }
- if ($this->_from == self::PAY_FROM_APP) {
- $self_mini = intval(\app\models\Option::get('self_mini', get_store_id(), 'store', 0)['value']);
- if (\Yii::$app->prod_is_dandianpu() && !$self_mini) {
- $result = WechatPay::orderUnify($this->order,OrderNo::ORDER_LEVEL, $goods_names, 0, true, $balance_price);
- } else {
- if (is_profit_pay()) {
- $result = WechatNewPay::orderUnify($this->order,OrderNo::ORDER_LEVEL, $goods_names, 0, true, $balance_price);
- } else {
- $result = WechatPay::orderUnify($this->order,OrderNo::ORDER_LEVEL, $goods_names, 0, true, $balance_price);
- }
- }
- } else if ($this->_from == self::PAY_FROM_H5) {
- if (\Yii::$app->prod_is_dandianpu() && !Store::hasIncoming($this->order->store_id) && self_mini() === false) {
- $result = WechatPay::orderUnify($this->order,OrderNo::ORDER_LEVEL, $goods_names, 0, false, $balance_price, true);
- } else {
- if (is_profit_pay()) {
- $result = WechatNewPay::orderUnify($this->order,OrderNo::ORDER_LEVEL, $goods_names, 0, false, $balance_price, true);
- } else {
- $result = WechatPay::orderUnify($this->order,OrderNo::ORDER_LEVEL, $goods_names, 0, false, $balance_price, true);
- }
- }
- } else if ($this->_from == self::PAY_FROM_OFFICIAL) {
- $self_mini = intval(\app\models\Option::get('self_mini', get_store_id(), 'store', 0)['value']);
- if (\Yii::$app->prod_is_dandianpu() && !Store::hasIncoming($this->order->store_id) && !$self_mini) {
- $result = WechatPay::orderUnify($this->order,OrderNo::ORDER_LEVEL, $goods_names, 0, false, $balance_price, false, true);
- } else {
- if (is_profit_pay()) {
- $result = WechatNewPay::orderUnify($this->order,OrderNo::ORDER_LEVEL, $goods_names, 0, false, $balance_price, false, true);
- } else {
- $result = WechatPay::orderUnify($this->order,OrderNo::ORDER_LEVEL, $goods_names, 0, false, $balance_price, false, true);
- }
- }
- } else {
- if (\Yii::$app->prod_is_dandianpu() && !Store::hasIncoming($this->order->store_id) && self_mini() === false) {
- $result = WechatPay::orderUnify($this->order,OrderNo::ORDER_LEVEL, $goods_names, 0, false, $balance_price);
- } else {
- if (is_profit_pay() && Store::hasIncoming($this->order->store_id)) {
- $result = WechatNewPay::orderUnify($this->order,OrderNo::ORDER_LEVEL, $goods_names, 0, false, $balance_price);
- } else {
- $result = WechatPay::orderUnify($this->order,OrderNo::ORDER_LEVEL, $goods_names, 0, false, $balance_price);
- }
- }
- }
- if (isset($result['code']) && $result['code'] == 1) {
- return $result;
- }
- CloudPrint::doPrint($this->order->id, 0, $this->order->store_id);
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => (object)$result['data'],
- 'res' => $result['res'],
- 'body' => $goods_names
- ];
- }
- if ($this->pay_type == 4) {
- if (is_profit_pay('ali')) {
- $result = AlipayProfit::mini($this->order, $goods_names, $this->user, '', '', false, 0, $balance_price);
- } else {
- $result = Alipay::mini($this->order, $goods_names, $this->user, '', '', 0, $balance_price);
- }
- if (isset($result['code']) && $result['code'] == 1) {
- return $result;
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $result['data']['trade_no'],
- 'body' => $goods_names
- ];
- }
- }
- if ($this->pay_type == Order::PAY_TYPE_WX_B2B) {
- return \app\modules\admin\models\UserAuditForm::pay($this->session_key, $this->order,OrderNo::ORDER_RECHARGE, $goods_names);
- }
- //货到付款和余额支付数据处理
- if ($this->pay_type == 2 || $this->pay_type == 3) {
- $order = $this->order;
- // 中间页选择货到付款,需要存入支付类型
- if ($this->pay_type == 2) {
- $order->pay_type = 2;
- $order->save();
- }
- //余额支付 用户余额变动
- if ($this->pay_type == 3) {
- $user = User::findOne(['id' => $order->user_id]);
- if ($user->money < $order->pay_price) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '支付失败,余额不足',
- ];
- }
- // $user->money -= floatval($order->pay_price);
- $res = AccountLog::saveLog($user->id, floatval($order->pay_price), AccountLog::TYPE_BALANCE, AccountLog::LOG_TYPE_EXPEND, 1, $order->id, "购买会员余额支付,订单号为:{$order->order_no}。");
- if (!$res) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '支付失败'
- ];
- }
- $order->is_pay = 1;
- $order->pay_type = 3;
- $order->pay_time = time();
- if (!$order->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '支付失败'
- ];
- } else {
- //会员升级
- $user = User::findOne($order->user_id);
- $user->level = $order->after_level;
- $user->save();
- $level = Level::findOne(['level' => $user->level, 'store_id' => $order->store_id, 'is_delete' => 0]);
- if ($level) {
- $saas_user = SaasUser::findOne(['mobile' => $user->binding, 'is_delete' => 0]);
- $user_name = $user->nickname;
- if ($saas_user) {
- $user_name = $saas_user->name;
- }
- if ($user->old_parent_id) {
- $parent = User::findOne($user->old_parent_id);//上级
- //检测一级是否存在且佣金是否设置
- if ($parent && $level->firstProfit) {
- //发放一级佣金
- //...
- $parent->total_price += $level->firstProfit;
- $parent->price += $level->firstProfit;
- $parent->save();
- UserShareMoney::set($level->firstProfit, $user->old_parent_id, $order->id, 0, 1, $order->store_id, UserShareMoney::LEVEL_ORDER_TYPE, '用户' . $user_name . '购买会员发放一级佣金');
- if ($parent->old_parent_id) {
- $parent_1 = User::findOne($parent->old_parent_id);//上级
- //检测二级是否存在且佣金是否设置
- if ($parent_1 && $level->secondProfit) {
- //发放二级佣金
- $parent_1->total_price += $level->secondProfit;
- $parent_1->price += $level->secondProfit;
- $parent_1->save();
- UserShareMoney::set($level->secondProfit, $parent->old_parent_id, $order->id, 0, 2, $order->store_id, UserShareMoney::LEVEL_ORDER_TYPE, '用户' . $user_name . '购买会员发放二级佣金');
- //检测三级是否存在且佣金是否设置
- $parent_2 = User::findOne($parent_1->old_parent_id);//上级
- if ($parent_2 && $level->thirdProfit) {
- //发放三级佣金
- $parent_2->total_price += $level->thirdProfit;
- $parent_2->price += $level->thirdProfit;
- $parent_2->save();
- UserShareMoney::set($level->thirdProfit, $parent_1->old_parent_id, $order->id, 0, 3, $order->store_id, UserShareMoney::LEVEL_ORDER_TYPE, '用户' . $user_name . '购买会员发放三级佣金');
- }
- }
- }
- }
- }
- }
- }
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '下单成功',
- 'data' => ''
- ];
- }
- }
- }
|