setName('ProfitSharingResult')->setDescription('自动分账'); $this->addArgument("order_id"); } protected function execute(Input $input, Output $output) { $order_id = $input->getArgument("order_id"); if ($order_id) { $where[] = ['order_id','=', $order_id]; } else { //查询已完成的订单,并且未分账的订单 $where[] = ['order_status','=', 30]; $where[] = ['pay_status','=', 20]; $where[] = ['is_refund','=', 10]; $where[] = ['is_delete','=', 0]; $where[] = ['profitsharing_status','=', 1]; $where[] = ['merchant_id','>', 0]; $where[] = ['pay_method','=', PaymentMethodEnum::WECHAT]; } $orders = Order::where($where) ->field('order_id,total_price,order_price,pay_price,pay_method,cost_price,merchant_id,store_id,order_status,pay_status,delivery_status,receipt_status,delivery_type,delivery_time,create_time,out_order_no,trade_id,commission_ratio') ->select(); // var_dump($orders->toArray()); // exit(); if ($orders->isEmpty()) { echo "没有已完成的订单要分账".PHP_EOL; return false; } //微信支付订单抽佣给平台、余额支付抽佣给平台,把订单金额记录到商户账上 foreach ($orders as $order) { try { //商户微信支付配置 $payment = Payment::where('store_id', $order->store_id)->where('merchant_id', $order->merchant_id)->where('method',PaymentMethodEnum::WECHAT)->where('is_enable', 1)->find(); if (!$payment) { echo $order['store_id']."微信支付方式没有配置".PHP_EOL; continue; } $payment_template = PaymentTemplate::where('template_id', $payment->template_id)->where('is_delete', 0)->find(); if (!$payment_template) { echo $order['store_id'].$payment->template_id."微信支付模版没有配置".PHP_EOL; continue; } $wechat_config = $payment_template['config'] ?? []; if (!$wechat_config) { echo $order['store_id'].$payment->template_id."微信支付模版没有配置11".PHP_EOL; continue; } $wechat_config = $wechat_config['wechat']['normal'] ?? []; //小程序配置 $mini = Setting::where('store_id', $order->store_id)->find(); if (!$mini) { echo $order['store_id']."小程序配置没有".PHP_EOL; continue; } $mini_config = $mini['values'] ?? []; if (!$mini_config) { echo $order['store_id'].$payment->template_id."微信支付模版没有配置11".PHP_EOL; continue; } //分账 //支付信息初始化 $PaymentModel = new Payment; $templateInfo = $PaymentModel->getPaymentInfo(PaymentMethodEnum::WECHAT, ClientEnum::MP_WEIXIN, $order->store_id, $order->merchant_id); $options = $templateInfo['template']['config'][PaymentMethodEnum::WECHAT]; $payment = PaymentFacade::store(PaymentMethodEnum::WECHAT)->setOptions($options, ClientEnum::MP_WEIXIN); // var_dump($payment); // exit(); $payment_trade = PaymentTrade::where('trade_id', $order->trade_id)->field('trade_no')->find(); if (!$payment_trade) { echo $order['store_id']."没有交易流水号".PHP_EOL; continue; } $transaction_id = $payment_trade->trade_no; $sharing = $payment->profitsharingQuery($order->out_order_no, $transaction_id); var_dump($sharing); if ($sharing['state'] == "FINISHED") { //更新 $ret = Order::where('order_id',$order->order_id)->update(['profitsharing_status' => 2, 'profitsharing_time' => time()]); echo "微信支付分账结果".PHP_EOL; var_dump($ret); } } catch (\Exception $e) { echo $e->getMessage(); continue; } } } }