zm
2021-04-09 93652ca875fc904a25a7f214adc548745573950a
commit | author | age
90c639 1 <?php
Z 2
3 /**
4  * link: http://www.zjhejiang.com/
5  * copyright: Copyright (c) 2020 浙江禾匠信息科技有限公司
6  * author: xay
7  */
8
9 namespace app\plugins\exchange\forms\exchange\basic;
10
11 use app\core\payment\PaymentOrder;
12 use app\events\OrderEvent;
13 use app\forms\api\order\OrderGoodsAttr;
14 use app\forms\api\order\OrderPayNotify;
15 use app\models\GoodsAttr;
16 use app\models\Order;
17 use app\models\OrderDetail;
18 use app\plugins\exchange\forms\common\CommonOrder;
19 use app\plugins\exchange\models\ExchangeRecordOrder;
20
21 class Goods extends BaseAbstract implements Base
22 {
23     public $has_imitate;
24
25     public function exchange(&$message, &$reward)
26     {
27         try {
28             $origin = $this->extra_info['origin'] ?? false;
29             if ($origin && $origin === 'admin') {
30                 $this->has_imitate = $this->extra_info['has_imitate'] ?? false;
31                 //Create 订单
32                 $order = $this->createOrder();
33                 //Create 详情
34                 $this->createOrderDetail($order);
35                 //Create 插件订单
36                 $this->createExchangeOrder($order);
37                 //handle 订单创建
38                 $this->handleCreateEvent($order);
3abc2a 39                 //handle 自动完成,提货客服后台提货时,不自动完成订单,需要和用户自助兑换提货的订单状态一样
Z 40                 //(new CommonOrder())->autoSend($order);
90c639 41                 //create PaymentOrder 支付完成
Z 42                 $this->paymentOrder($order);
43                 return true;
44             }
45             //支付
46             if ($origin && $origin === 'alipay') {
47                 return true;
48             }
49             //兑换
50             return false;
51         } catch (\Exception $e) {
52             $message = $e->getMessage();
53             return false;
54         }
55     }
56
57     public function paymentOrder($order)
58     {
59         $payOrder = new PaymentOrder([
60             'title' => '兑换中心后台',
61             'amount' => floatval($order->total_pay_price),
62             'orderNo' => $order->order_no,
63             'notifyClass' => OrderPayNotify::class,
64             'supportPayTypes' => [\app\core\payment\Payment::PAY_TYPE_BALANCE],
65         ]);
66         $copy = new CopyPayment();
67         $id = $copy->createOrder($payOrder, $this->user);
68         return $copy->payBuyBalance($id);
69     }
70
71     public function createOrder(): Order
72     {
73         $order = new Order();
74         $order->name = $this->extra_info['name'] ?? $this->user->nickname;
75         $order->mobile = $this->extra_info['mobile'] ?? $this->user->mobile;
8f1ce6 76         $order->address = $this->extra_info['address'] ?? '';
90c639 77         $order->mall_id = $this->user->mall_id;
Z 78         $order->user_id = $this->user->id;
79         $order->order_no = Order::getOrderNo('');
80         $order->total_price = 0;
81         $order->total_pay_price = 0;
82         $order->express_original_price = 0;
83         $order->express_price = 0;
84         $order->total_goods_price = 0;
85         $order->total_goods_original_price = 0;
86
87         $order->member_discount_price = 0;
88         $order->use_user_coupon_id = 0;
89         $order->coupon_discount_price = 0;
90         $order->use_integral_num = 0;
91         $order->integral_deduction_price = 0;
2fd658 92         $order->remark = '';
93652c 93         $sremark = $this->extra_info['card_platform'].' 卡号:'.$this->codeModel->card_no.' 密码:'.$this->codeModel->code;
Z 94         $order->seller_remark = $sremark;
90c639 95         $order->order_form = \Yii::$app->serializer->encode([]);
Z 96         $order->words = '';
97
98         $order->is_pay = 1;
99         $order->pay_type = 3;// 支付方式:1.在线支付 2.货到付款 3.余额支付
8f1ce6 100         //$order->send_type = 3; //配送方式:0--快递配送 1--到店自提 2--同城配送 3--自动
Z 101         $order->send_type = 0; //配送方式:0--快递配送 1--到店自提 2--同城配送 3--自动
90c639 102
3abc2a 103         $order->is_sale = 0;
Z 104         //$order->auto_sales_time = mysql_timestamp();
105         $order->is_confirm = 0;
106         //$order->confirm_time = mysql_timestamp();
107         $order->is_send = 0;
108         //$order->send_time = mysql_timestamp();
90c639 109
Z 110         $order->support_pay_types = \Yii::$app->serializer->encode([]);
111
112         $order->sign = 'exchange';
113         $order->token = \Yii::$app->security->generateRandomString();
114         $order->status = 1;
115         $order->platform = $this->user->userInfo->platform;
116         if (!$order->save()) {
117             throw new \Exception('order ERROR');
118         }
119         return $order;
120     }
121
122     public function createOrderDetail($order)
123     {
124         $attr_id = $this->config['attr_id'];
125         $goods = \app\plugins\exchange\models\Goods::findOne($this->config['goods_id']);
126
127         $orderDetail = new OrderDetail();
128         $orderDetail->order_id = $order->id;
129         $orderDetail->goods_id = $goods->id;
130         $orderDetail->num = intval($this->config['goods_num']);
131         $orderDetail->unit_price = 0;
132         $orderDetail->total_original_price = 0;
133         $orderDetail->total_price = 0;
134         $orderDetail->member_discount_price = 0;
135         $orderDetail->sign = 'exchange';
136
137         $attrModel = GoodsAttr::findOne($attr_id);
138         $attrGroups = \Yii::$app->serializer->decode($goods->attr_groups);
139         $orderGoodsAttr = new OrderGoodsAttr();
140         $orderGoodsAttr->goods = $goods;
141         $orderGoodsAttr->goodsAttr = $attrModel;
142         //$orderGoodsAttr->goodsAttr = $goods->attr[0];
143         $orderGoodsAttr->integral_price = 0;
144         $orderGoodsAttr->use_integral = 0;
145         //sub Stork
146
147         (new GoodsAttr())->updateStock($orderDetail->num, 'sub', $orderGoodsAttr->id);
148
149         $goodsInfo = [
150             'attr_list' => (new \app\models\Goods())->signToAttr($attrModel['sign_id'], $attrGroups),
151             'goods_attr' => $orderGoodsAttr,
152             'extra_info' => [
153                 'code_id' => $this->codeModel->code,
154                 'config' => $this->config,
155             ]
156         ];
157         $orderDetail->goods_info = $orderDetail->encodeGoodsInfo($goodsInfo);
158         if (!$orderDetail->save()) {
159             throw new \Exception('orderDetail ERROR');
160         }
161         return $orderDetail;
162     }
163
164     public function createExchangeOrder($order)
165     {
166         $exchangeOrder = new ExchangeRecordOrder();
167         $exchangeOrder->mall_id = $this->user->mall_id;
168         $exchangeOrder->user_id = $this->user->id;
169         $exchangeOrder->order_id = $order->id;
170         $exchangeOrder->code_id = $this->codeModel->id;
171         $exchangeOrder->token = $this->config['token'];
172         if (!$exchangeOrder->save()) {
173             throw new \Exception('exchangeOrder ERROR');
174         }
175     }
176
177     public function handleCreateEvent($order)
178     {
179         $user = $this->user;
180         $sender = new class ($user) {
181             public $user;
182
183             public function __construct($user)
184             {
185                 $this->user = $user;
186             }
187         };
188         $event = new OrderEvent();
189         $event->order = $order;
190         $event->sender = $sender;
191         \Yii::$app->trigger(Order::EVENT_CREATED, $event);
192     }
193 }