| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\plugins\adopt\models\client;
- use app\models\AccountLog;
- use app\models\common\CommonOrder;
- use app\models\Goods;
- use app\models\OrderUnion;
- use app\models\User;
- use app\modules\client\models\OrderComplete;
- use app\plugins\adopt\models\AdoptCostOrder;
- use app\plugins\adopt\models\AdoptOrderInfo;
- use app\modules\client\models\v1\ShareMoneyForm;
- use app\plugins\scanCodePay\models\Order;
- use app\plugins\scanCodePay\models\OrderDetail;
- use app\utils\Alipay\Alipay;
- // use app\utils\CloudPrint;
- use app\utils\Alipay\AlipayProfit;
- use app\utils\CloudPrint;
- use app\utils\Delivery\Delivery;
- use app\utils\OrderNo;
- use app\utils\Wechat\WechatNewPay;
- use app\utils\Wechat\WechatPay;
- use ByteDance\ByteDance;
- use EasyWeChat\MiniProgram\Application;
- use yii\base\BaseObject;
- use yii\base\Model;
- /**
- * @property User $user
- * @property Order $order
- */
- class CostOrderPayDataForm extends Model
- {
- 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;
- /** @var Application $wechat */
- private $wechat;
- private $wechatPay;
- private $order;
- const PAY_FROM_APP = 'app';
- const PAY_FROM_MINI = 'mini';
- const PAY_FROM_H5 = 'h5';
- public function rules()
- {
- return [
- [['pay_type', '_from'], 'required'],
- [['_from'], 'in', 'range' => ['app', 'mini']],
- [['pay_type'], 'in', 'range' => [1, 2, 3, 4]], // 'WECHAT_PAY', 'HUODAO_PAY', 'BALANCE_PAY' 'ALIPAY',
- [['form_id', 'order_id_list', '_from'], 'string'],
- ['is_combine', 'default', 'value' => 0],
- [['parent_user_id', 'condition', 'order_id', 'is_combine'], 'integer'],
- ];
- }
- 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);
- if ($this->order_id_list) {
- $order_id_list = json_decode($this->order_id_list, true);
- if (is_array($order_id_list) && count($order_id_list) == 1) {
- $this->order_id = $order_id_list[0];
- $this->order_id_list = '';
- }
- }
- $t = \Yii::$app->db->beginTransaction();
- $this->order = AdoptCostOrder::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->trade_status == Order::ORDER_FLOW_CANCEL) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '订单已取消',
- ];
- }
- // 存储parent_id
- CommonOrder::saveParentId($this->parent_user_id);
- $goods_names = '';
- if ($this->order->order_type == 0){
- $goods_names = "代采订单";
- }
- if ($this->order->order_type == 1){
- $goods_names = "保管订单";
- }
- if (!$this->order->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '订单保存失败'
- ];
- }
- // 余额抵扣支付
- $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 = \app\models\Order::IS_COMBINE_PAY;
- $this->order->save();
- }
- } else {
- $this->order->combine_money = 0;
- $this->order->is_combine_pay = \app\models\Order::NOT_COMBINE_PAY;
- $this->order->save();
- }
- if ($this->pay_type == 1 || $this->pay_type == 4 || $this->pay_type == 5) {
- 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' => '支付失败'
- ];
- }
- // // 支付完成后,相关操作
- $form = new OrderComplete();
- $form->order_id = $this->order->id;
- $form->order_type = 0;
- $form->store_id = get_store_id();
- $form->adoptOrderCostNotify();
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '0元支付'
- ];
- }
- if ($this->pay_type == 1) {
- if ($this->_from == self::PAY_FROM_APP) {
- if (is_profit_sharing()) {
- $result = WechatNewPay::orderUnify($this->order,OrderNo::ORDER_MALL, $goods_names, 0, true, $balance_price);
- } else {
- $result = WechatPay::orderUnify($this->order,OrderNo::ORDER_MALL, $goods_names, 0, true, $balance_price);
- }
- } else if ($this->_from == self::PAY_FROM_H5) {
- if (is_profit_sharing()) {
- $result = WechatNewPay::orderUnify($this->order,OrderNo::ORDER_MALL, $goods_names, 0, false, $balance_price, true);
- } else {
- $result = WechatPay::orderUnify($this->order,OrderNo::ORDER_MALL, $goods_names, 0, false, $balance_price, true);
- }
- } else {
- if (is_profit_sharing()) {
- $result = WechatNewPay::orderUnify($this->order,OrderNo::ORDER_MALL, $goods_names, 0, false, $balance_price);
- } else {
- $result = WechatPay::orderUnify($this->order,OrderNo::ORDER_MALL, $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 (\Yii::$app->params['paySwitch'] == 1) {
- $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 == 5) {
- $result = ByteDance::pay($this->order, '', $goods_names, 0 , $balance_price);
- if (isset($result['code']) && $result['code'] == 1) {
- return $result;
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $result['data'],
- ];
- }
- }
- //货到付款和余额支付数据处理
- if ($this->pay_type == 2 || $this->pay_type == 3) {
- $order = $this->order;
- // 中间页选择货到付款,需要存入支付类型
- if ($this->pay_type == 2) {
- $order->is_pay = 0;
- $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' => '支付失败',
- 'res' => $res
- ];
- }
- $order->is_pay = 1;
- $order->pay_type = 3;
- if ($order->order_type == 1) {
- $order->trade_status = AdoptCostOrder::ORDER_FLOW_NO_SEND;
- } else{
- $order->trade_status = AdoptCostOrder::ORDER_FLOW_CONFIRM;
- }
- $order->pay_time = time();
- if ($order->user_id != get_user_id()) {
- $order->pay_user_id = get_user_id();
- }
- if ($order->order_type == 0) {
- $adopt_order = AdoptOrderInfo::findOne(['order_id' => $order->order_id, 'store_id' => $order->store_id]);
- if ($adopt_order->pick_method == AdoptOrderInfo::ORDER_FLOW_NO_PICK) {
- $adopt_order->pick_method = AdoptOrderInfo::ORDER_FLOW_OTHER_PICK;
- $adopt_order->pick_treatment_method = AdoptOrderInfo::ORDER_FLOW_PICK_STORAGE;
- $adopt_order->pick_treatment_method_time = time();
- $adopt_order->pick_time = time();
- if (!$adopt_order->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '支付失败'
- ];
- }
- }
- }
- if (!$order->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '支付失败'
- ];
- }
- }
- // 同城配送订单下单
- // if ($order->is_delivery == 1) {
- // $detail_goods = \app\models\OrderDetail::find()->where(['order_id' => $order->id])->select('goods_name as name, pic, num, total_price as price')->asArray()->all();
- // Delivery::addOrder($detail_goods, $order->order_no);
- // }
- // 支付完成后,相关操作
- $form = new OrderComplete();
- $form->order_id = $order->id;
- $form->order_type = 0;
- $form->store_id = get_store_id();
- $form->adoptOrderCostNotify();
- // CloudPrint::doPrint($this->order->id, 0, $this->order->store_id);
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '支付成功',
- 'data' => ''
- ];
- }
- }
- }
|