['app', 'mini', 'h5', 'official']], [['pay_type'], 'in', 'range' => [1, 2, 3]], // 'WECHAT_PAY', 'ALIPAY', 'BALANCE_PAY', [['form_id', 'order_id_list', '_from'], 'string'], ['is_combine', 'default', 'value' => 0], [['parent_user_id', 'condition', 'order_id', 'is_combine'], 'integer'], ]; } public function search() { $orderType = OrderNo::ORDER_ALLIANCE_PURCHASE; $this->wechatPay = \Yii::$app->controller->wechatPay; if (!$this->validate()) { return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0], ]; } 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(); if ($this->order_id) { //单个订单付款 $this->order = PurchaseOrder::findOne([ 'id' => $this->order_id, 'is_delete' => 0, ]); //上门安装逻辑 $merchant = new MerchantForm(); $res = $merchant->isNeedInstall($this->order_id, 1); if ($res['code'] !== 0) { return $res; } if ($this->order->pay_price == 0) { $this->order->pay_time = time(); $this->order->is_pay = 1; $this->order->pay_type = 3; $this->order->trade_status = PurchaseOrder::ORDER_FLOW_NO_SEND; $this->order->save(); try{ // 支付完成后,相关操作 self::notify($this->order->id); } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => '支付失败 ' . $e->getMessage(), ]; } $t->commit(); return [ 'code' => 0, 'msg' => '支付成功' ]; } if (!$this->order) { $t->rollBack(); return [ 'code' => 1, 'msg' => '订单不存在', ]; } if ($this->order->is_delete == 1 || $this->order->trade_status == PurchaseOrder::ORDER_FLOW_CANCEL) { $t->rollBack(); return [ 'code' => 1, 'msg' => '订单已取消', ]; } if ($this->order->is_pay == 1 || $this->order->trade_status > -1) { $t->rollBack(); return [ 'code' => 1, 'msg' => '订单状态异常', ]; } $goods_names = ''; $goods_list = PurchaseOrderDetail::find()->where(['order_id' => $this->order->id, 'is_delete' => 0])->asArray()->all(); foreach ($goods_list as $goods) { $goods_names .= $goods['goods_name'] . ';'; } $goods_names = mb_substr($goods_names, 0, 32, 'utf-8'); $this->order->order_union_id = 0; 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 = PurchaseOrder::IS_COMBINE_PAY; // $this->order->save(); // } // } else { // $this->order->combine_money = 0; // $this->order->is_combine_pay = PurchaseOrder::NOT_COMBINE_PAY; // $this->order->save(); // } if ($this->pay_type == 1) { 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' => '支付失败' ]; } try{ // 支付完成后,相关操作 self::notify($this->order->id); } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => '支付失败 ' . $e->getMessage(), ]; } $t->commit(); return [ 'code' => 0, 'msg' => '0元支付' ]; } if ($this->pay_type == 1) { $balance_price = 0; // \Yii::error($this->_from); $result = WechatBusinessPay::orderUnify($this->order, $orderType, $goods_names, 0, false, 0); if (isset($result['code']) && $result['code'] == 1) { if ($this->_from == self::PAY_FROM_OFFICIAL) { $result['wechat_platform_open_id'] = get_user()->wechat_platform_open_id; $result['h5_auth_link'] = LoginForm::getAuthLink(); } 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 == 2) { if ($this->_from == self::PAY_FROM_MINI) { 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 ]; } else { if (is_profit_pay('ali')) { $result = AlipayProfit::app($this->order, $goods_names, $this->user, $balance_price); } else { $result = Alipay::app($this->order, $goods_names, $this->user, $balance_price); } if (isset($result['code']) && $result['code'] == 1) { $t->commit(); return $result; } $t->commit(); return [ 'code' => 0, 'msg' => 'success', 'data' => $result['data'], 'body' => $goods_names ]; } } } //货到付款和余额支付数据处理 if ($this->pay_type == 3) { $order = $this->order; //余额支付 用户余额变动 if ($this->pay_type == 3) { $order->is_pay = 1; $order->pay_type = 3; $order->trade_status = PurchaseOrder::ORDER_FLOW_NO_SEND; $order->pay_time = time(); if ($order->user_id != get_user_id()) { $order->pay_user_id = get_user_id(); } if (!$order->save()) { $t->rollBack(); return [ 'code' => 1, 'msg' => '支付失败' ]; } } try{ // 支付完成后,相关操作 $res = self::notify($order->id); if ($res['code'] !== 0) { throw new \Exception($res['msg']); } } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => '支付失败 ' . $e->getMessage(), ]; } // CloudPrint::doPrint($this->order->id, 0, $this->order->store_id); $t->commit(); return [ 'code' => 0, 'msg' => '支付成功', 'data' => '' ]; } } elseif ($this->order_id_list) { //多个订单合并付款 $order_id_list = json_decode($this->order_id_list, true); if (!$order_id_list) { $t->rollBack(); return [ 'code' => 1, 'msg' => '数据错误:订单格式不正确。', ]; } $order_list = []; $total_pay_price = 0; foreach ($order_id_list as $order_id) { $order = PurchaseOrder::findOne([ 'id' => $order_id, 'is_delete' => 0, ]); if (!$order) { $t->rollBack(); return [ 'code' => 1, 'msg' => '订单不存在', ]; } if ($order->is_pay == 1) { $t->rollBack(); return [ 'code' => 1, 'msg' => '存在已付款的订单,订单合并支付失败,请到我的订单重新支付。', ]; } $order_list[] = $order; $total_pay_price += doubleval($order->pay_price); // $this->setReturnData($order); } // 余额抵扣支付 $balance_price = 0; // if ($this->is_combine) { // if ($this->user->money >= $total_pay_price) { // $this->pay_type = 3; // } else { // $balance_price = $this->user->money; // $this->order->combine_money = $this->user->money; // $this->order->is_combine_pay = PurchaseOrder::IS_COMBINE_PAY; // $this->order->save(); // } // } else { // $this->order->combine_money = 0; // $this->order->is_combine_pay = PurchaseOrder::NOT_COMBINE_PAY; // $this->order->save(); // } // 微信支付 if ($this->pay_type == 1) { $result = WechatBusinessPay::orderUnify($order_list,OrderNo::ORDER_UNION, null, $total_pay_price, false, $balance_price); if (isset($result['code']) && $result['code'] == 1) { $t->rollBack(); 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' => $result['body'], ]; } if ($this->pay_type == 2) { if (is_profit_pay('ali')) { $result = AlipayProfit::mini($this->order, '', $this->user, '', OrderNo::ORDER_UNION, false, $total_pay_price, $balance_price); } else { $result = Alipay::mini($this->order, '', $this->user, '', OrderNo::ORDER_UNION, $total_pay_price, $balance_price); } if (isset($result['code']) && $result['code'] == 1) { return $result; } $t->commit(); return [ 'code' => 0, 'msg' => 'success', 'data' => $result['data']['trade_no'], ]; } //货到付款和余额支付数据处理 if ($this->pay_type == 3) { //余额支付 用户余额变动 if ($this->pay_type == 3) { foreach ($order_list as $order) { $order->is_pay = 1; $order->pay_type = 3; $order->trade_status = PurchaseOrder::ORDER_FLOW_NO_SEND; $order->pay_time = time(); if (!$order->save()) { $t->rollBack(); return [ 'code' => 1, 'msg' => '支付失败,数据错误' ]; } } } try{ $order_ids = []; foreach ($order_list as $order) { $order_ids[] = $order->id; } // 支付完成后,相关操作 self::notify(0, $order_ids); } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => '支付失败 ' . $e->getMessage(), 'e' => $e->getTrace(), ]; } // CloudPrint::doPrint($this->order->id, 0, $this->order->store_id); //查询订单信息 //var_dump($is_on); $t->commit(); return [ 'code' => 0, 'msg' => '订单提交成功' ]; } } } public static function notify($order_id, $order_ids = []){ // \Yii::error('-----------PurchaseOrder保存失败' . Json::encode($order->getErrors())); if(count($order_ids) <= 1){ $order_ids = []; } $order = PurchaseOrder::findOne($order_id ?: $order_ids); if ($order) { $item = \app\models\StoreCloud::findOne(['store_id' => $order->store_id, 'is_delete' => 0, 'is_enable' => 1]); } if (empty($item)) { $item = get_saas_purchase_store_cloud(); } $form = new MerchantForm(); $form->order_id = $order_id; $form->type = 1; $form->token_stroe_cloud_id = $item ? $item->id : 0; $res = $form->purchaseSetPurchaseOrder($order_ids); // self::agencyPrice($order_id); if ($res['code'] === 0) { if (!empty($res['order_data'])) { foreach ($res['order_data'] as $index => $order_datum) { if (!empty($order_datum)) { $purchase_order = PurchaseOrder::find()->where(['cloud_order_id' => $res['data'][$index]])->asArray()->one(); foreach ($order_datum as $value) { $purchase = new PurchaseOrder(); $purchase->attributes = $purchase_order; $purchase->user_id = $purchase_order['user_id']; $purchase->transaction_id = $purchase_order['transaction_id']; $purchase->saas_id = $purchase_order['saas_id']; $purchase->cloud_order_id = $value['id']; $purchase->cloud_order_no = $value['order_no']; $purchase->cloud_supplier_id = $value['supplier_id']; $purchase->express_price = $value['express_price']; // $purchase->total_price = $value['pay_price']; // $purchase->pay_price = ($value['pay_price'] + ($value['express_price'])); $purchase->total_price = $value['pay_price']; $purchase->pay_price = $value['pay_price']; $result = $purchase->save(); if (!$result) { return [ 'code' => 1, 'msg' => implode(';', array_values($purchase->firstErrors)) ]; } else { PurchaseOrderDetail::updateAll(['is_delete' => 1], ['order_id' => $purchase_order['id']]); foreach ($value['goods_list'] as $goods) { // $CloudGoodsBind = CloudGoodsBind::findOne(['cloud_goods_id' => $goods['id'], 'is_delete' => 0]); $attr = json_decode($goods['attr'], true); foreach ($attr['attr_list'] as &$attr_list_item) { $attr_list_item = array_merge($attr_list_item, ['attr_group_name' => '规格']); } $PurchaseOrderDetail = new PurchaseOrderDetail(); $PurchaseOrderDetail->order_id = $purchase->id; $PurchaseOrderDetail->goods_id = $goods['id']; $PurchaseOrderDetail->goods_name = $goods['name']; $PurchaseOrderDetail->num = $goods['num']; $PurchaseOrderDetail->total_price = $attr['wholesale_price']; $PurchaseOrderDetail->created_at = time(); $PurchaseOrderDetail->attr = json_encode($attr['attr_list']); $PurchaseOrderDetail->pic = $attr['pic']; $PurchaseOrderDetail->is_give = intval(($goods['give_goods_id'] > 0)); $PurchaseOrderDetail->save(); } //上门安装逻辑 $merchant = new MerchantForm(); $res = $merchant->isNeedInstall($purchase->id, 1, true); if ($res['code'] !== 0) { return $res; } $purchase_order_obj = PurchaseOrder::findOne($purchase_order['id']); $purchase_order_obj->is_delete = 1; $purchase_order_obj->save(); $cloud_order = array_merge(['pifa' => 1], $value ?: []); queue_push(new CreatedStbzOrderTranJob(['data' => $cloud_order])); } } } } return [ 'code' => 0, 'msg' => '成功' ]; } } else { return $res; } //单个订单胜天半子 if ($order) { $cloud_order_id = $order->cloud_order_id; $supplier_form = new SupplierForm(); $supplier_form->order_id = (string)$cloud_order_id; $res = $supplier_form->supplierOrderList($order->supplier_id); debug_log('supplier_order_list' . json_encode($res)); if ($res['code'] === 0) { $cloud_order = $res['data']['list'][0]; $cloud_order = array_merge(['pifa' => 1], $cloud_order ?: []); queue_push(new CreatedStbzOrderTranJob(['data' => $cloud_order])); } return $res; } else { return [ 'code' => 1, 'msg' => '订单信息错误' ]; } } // //采购商抽佣 // private static function agencyPrice($order_id){ // try { // $order = PurchaseOrder::find()->where(['id' => $order_id])->asArray()->one(); // $supplier = Supplier::findOne(['cloud_supplier_id' => $order['supplier_id']]); // $option = Option::get('agency_price_config', 0, 'saas'); // if ($option['value']) { // $option = json_decode($option['value'], true); // } // $province_id = $supplier->province_id; // $city_id = $supplier->city_id; // $district_id = $supplier->district_id; // $city_percent = $option['supplier_city_percent'] ?? 0; // $district_percent = $option['supplier_district_percent'] ?? 0; // $province_percent = $option['supplier_province_percent'] ?? 0; // $rate = 0; // // $admin_list = Admin::find()->where(['is_delete' => 0, 'province_id' => $province_id])->andWhere(['>', 'area_level', 0])->asArray()->all(); // $store_user = StoreCloud::findOne(['saas_user_id' => $order['saas_id']]); // foreach ($admin_list as $item) { // $price = 0; // $desc = ''; // if ((int)$item['area_level'] === 3) { // $price = $order['pay_price'] * ($province_percent / 100) * ($supplier->rate / 100); // $price = sprintf("%.2f", $price); // $desc = '商户'. $store_user->name .'订单'. $order['order_no'] .'采购订单-发放 '. $price .'元省代佣金'; // } // if ((int)$item['area_level'] === 2 && (int)$item['city_id'] === $city_id) { // $price = $order['pay_price'] * ($city_percent / 100) * ($supplier->rate / 100); // $price = sprintf("%.2f", $price); // $desc = '商户'. $store_user->name .'订单'. $order['order_no'] .'采购订单-发放 '. $price .'元省代佣金'; // } // if ((int)$item['area_level'] === 1 && (int)$item['city_id'] === (int)$city_id && (int)$item['district_id'] === (int)$district_id) { // $price = $order['pay_price'] * ($district_percent / 100) * ($supplier->rate / 100); // $price = sprintf("%.2f", $price); // $desc = '商户'. $store_user->name .'订单'. $order['order_no'] .'采购订单-发放 '. $price .'元省代佣金'; // } // if (!$price) { // continue; // } // //给用户增加佣金 // $SaasUser = SaasUser::find()->where(['id' => $item['saas_user_id']])->one(); // if ($SaasUser) { // // $SaasUser->share_profit = ($SaasUser->share_profit * 1) + ($price * 1); // $res = $SaasUser->save(); // //增加记录 // if ($res) { // $form = new StoreShareMoney(); // $form->user_id = $item['saas_user_id']; // $form->store_id = $order['store_id']; // $form->profit = $rate; // $form->total_price = $order['pay_price']; // $form->desc = $desc; // $form->order_id = $order_id; // $form->created_at = time(); // $form->type = 1; // $form->commission = $price; // $form->order_type = 1; // $form->cloud_type = 1; // $form->status = 1; // $form->is_send = 1; // $form->save(); // } // } // } // // // // } catch (\Exception $e) { // debug_log('抽佣' . $e->getMessage() . $e->getLine()); // \Yii::error($e->getMessage()); // } // } //返回数据 private function outPutData($code,$msg,$data=[]){ $putData = []; $putData['code'] = $code; $putData['msg'] = $msg; if($data) $putData['data'] = $data; if($msg == 'token不能为空'){ $putData['code'] = -1; } return $putData; } }