oauth->userFromCode($code); // \debug_log($result->toArray()); $app = WechatMini::getWechatConfig(get_store_id(), 0, WechatMini::TYPE_OFFICIAL); if (!$app) { return ''; } $app->server->push(function ($message) { //取消关注事件 if ($message['MsgType'] == 'event') { if ($message['Event'] == 'subscribe') { if (\Yii::$app->prod_is_dandianpu()) { $auto_reply = Option::get('one_store_wechat_official_auto_reply', 0, 'saas', '')['value']; } else { $wechat_config = WechatConfig::findOne(['store_id' => get_store_id(), 'type' => WechatConfig::TYPE_CONFIG_MP, 'is_delete' => 0]); $wechat_config_ext = json_decode($wechat_config->ext, true); $auto_reply = $wechat_config_ext['auto_reply']; } } elseif ($message['Event'] === 'CLICK') { //点击事件 后台设置自定义菜单按钮点击事件 if (strpos($message['EventKey'], 'news_') !== false) { $id = str_replace('news_', '', $message['EventKey']); debug_log($id,'wechat_msg.log'); $menu_config = WechatMenuConfig::findOne($id); debug_log($menu_config->value,'wechat_msg.log'); if ($menu_config->value) { $value = json_decode($menu_config->value, true); $title = $value['title']; $pic = $value['pic']; $link = $value['link']; $desc = $value['desc']; $items = [ new NewsItem([ 'title' => $title, 'description' => $desc, 'url' => $link, 'image' => $pic, // ... ]), ]; return new News($items); } } } return $auto_reply ?: '你好!'; } return '你好!'; }); $response = $app->server->serve(); return $response->send(); } public function actionVideoShopOrderCallback() { try { $request = \Yii::$app->req; if (\RUN_MODE == 'wokerman') { $text = $request->rawBody(); } else { $text = file_get_contents("php://input"); } $getParam = get_params(); $postParam = post_params(); // $getParam = [ // "mini_id" => "117", // "signature" => "ecb8e998c045af55dbb914a6c952be857ee96e49", // "timestamp" => "1700636495", // "nonce" => "330111248", // "openid" => "oWGTC5HEQVaRCcgsG-Nqngx5g2xY", // "encrypt_type" => "aes", // "msg_signature" => "0d9b2134abb3475603e477de29347c69a9121960", // ]; // $postParam = [ // "ToUserName" => "gh_da9fa8ea5e45", // "Encrypt" => "AdJGDYz430yBDIFb+MqdavxuhOAEaCfyEEn5dm4AnLvTbBS6lZpstq4bn/8KJaGAj+QVr7ZoeunMzB8pJMw5hdKMi970G7cuiYKVd+NjxfBCE3iJ+RvfRWQOjVNhuPm59JyDFEQ1XhB4kKZYIhGFqdhP8KGgSpar7rnWBe9VKtYP1rLf3Vi31gfd/Y9x5wQLNA7VXmEDsal+Ie5WU8rw89jKQAhBee+m3PwCPUqFP+XnKdOE9KooXB84bHe4BxYSmI0lWZkmuOgWe3krvGQj1UJBk6qxQw4kZsrKkq6elkFO4suWBxm3SfmycppNr6nayeu6UxcGf/DS9AmXZWxMU9yWZpeNfEqN2YeYfcNt8akruY+U5apZW3qmu1QOTUC/" // ]; debug_log([ 'get' => $getParam, 'post' => $postParam, ], 'wechat-call-back.log'); $mini_id = $getParam['mini_id']; $store_mini = StoreMini::findOne($mini_id); $store_id = 0; if ($store_mini) { $store_id = $store_mini->store_id; } $group = "video_shop_config_" . $mini_id; $token = Option::get('token', $store_id, $group)['value'];//'Tianxin100'; $encodingAesKey = Option::get('encodingAesKey', $store_id, $group)['value'];//'yu2dLuOA76EjN5nVSqQE4LOQSNT8MBx72SaJry5X4gJ' $signature = $getParam['signature']; $tmpArr = array($token, $getParam['timestamp'], $getParam['nonce']); sort($tmpArr, SORT_STRING); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if ($tmpStr == $signature) { if (empty($postParam)) { return $getParam['echostr']; } } else { return false; } $format = ""; $format_xml = sprintf($format, $postParam['Encrypt']); $wx = new \WXBizMsgCrypt($token, $encodingAesKey, ''); $errCode = $wx->decryptMsg($getParam['msg_signature'], $getParam['timestamp'], $getParam['nonce'], $format_xml, $msg); if ($errCode === 0) { $order_msg = json_decode($msg, true); debug_log($order_msg, __CLASS__); if (isset($order_msg['Event'])) { $order_info = $order_msg['order_info']; $out_order_id = $order_info['order_id']; $time = $order_msg['CreateTime']; $order_id_list = VideoShopOrderExt::find()->where(['out_order_id' => $out_order_id, 'is_delete' => 0]) ->select('store_id, mini_id, out_order_id')->asArray()->all(); if(!$order_id_list){ $order_id_list = [[ 'store_id' => $store_id, 'mini_id' => $mini_id, 'out_order_id' => (string)$out_order_id, ]]; } $params = []; if (in_array($order_msg['Event'], [ 'channels_ec_order_ext_info_update',//订单其他信息更新 'channels_ec_order_settle',//订单结算成功 'channels_ec_order_new',//订单下单 'channels_ec_order_pay',//订单下单 'channels_ec_order_cancel',//订单取消 'channels_ec_order_deliver',//订单发货 'channels_ec_order_confirm'//订单收货 ])) { $orderForm = new VideoShopOrderForm(); foreach ($order_id_list as $item) { $base_ = new WechatMini(); $miniProgram = $base_::getWechatConfig($item['store_id'], $item['mini_id'], 1); if ($miniProgram) { $orderForm->miniProgram = $miniProgram; $orderForm->store_id = $item['store_id']; $orderForm->mini_id = $item['mini_id']; $result = $orderForm->syncOrderInfo([$item['out_order_id']]); debug_log(['小店订单回调' => $result],'syncOrder.log'); } } } // $order_msg = [ // "ToUserName" => "gh_da9fa8ea5e45", // "FromUserName" => "oWGTC5HEQVaRCcgsG-Nqngx5g2xY", // "CreateTime" => 1700635660, // "MsgType" => "event", // "Event" => "product_spu_audit", // "ProductSpuAudit" => [ // "product_id" => 10000077907861, // "status" => 3, // "reason" => "aaadd123", // ] // ]; if (in_array($order_msg['Event'], [ 'product_spu_audit',//商品审核 'product_spu_listing',//商品上下架 'product_spu_update'//商品数据更新 ])) { $key = camelize($order_msg['Event']); $product_id = $order_msg[$key] ? $order_msg[$key]['product_id'] : 0; $reason = $order_msg[$key] ? $order_msg[$key]['reason'] : ''; if($product_id){ \app\models\VideoShopGoodsExt::updateAll(['wx_status_reason' => (string)$reason], ['product_id' => (string)$product_id, 'store_id' => $store_id, 'mini_id' => $mini_id]); } if ($order_msg['Event'] === "product_spu_update") { if (isset($order_msg[$key]['status']) && isset($product_id)) { if (intval($order_msg[$key]['status']) === 2) { $videoShopGoodsExt = \app\models\VideoShopGoodsExt::findOne(['product_id' => (string)$product_id, 'store_id' => $store_id, 'mini_id' => $mini_id]); if ($videoShopGoodsExt) { $goods_id = $videoShopGoodsExt->goods_id; \app\models\Goods::deleteAll(['id' => $goods_id]); \app\models\GoodsPic::deleteAll(['goods_id' => $goods_id]); \app\models\VideoShopGoodsExt::deleteAll(['id' => $videoShopGoodsExt->id]); } } } } $form = new VideoShopGoodsForm(); $base_ = new WechatMini(); $miniProgram = $base_::getWechatConfig($store_id, $mini_id, 1); $form->miniProgram = $miniProgram; $form->store_id = $store_id; $form->mini_id = $base_::$mini_id; $res = $form->syncGoodsInfo($product_id); debug_log(['小店订单回调' => $res],'syncGoods.log'); } // if ($order_msg['Event'] === ) { // $params = [ // 'is_pay' => 1, // 'pay_time' => $order_info['pay_time'], // 'trade_status' => Order::ORDER_FLOW_NO_SEND // ]; // } // // // if ($order_msg['Event'] === ) { // $params = [ // 'trade_status' => Order::ORDER_FLOW_CANCEL, // ]; // } // // // if ($order_msg['Event'] === ) { // $params = [ // 'trade_status' => Order::ORDER_FLOW_SEND, // 'send_time' => $time // ]; // } // // // if ($order_msg['Event'] === 'channels_ec_order_confirm') { // $params = [ // 'trade_status' => Order::ORDER_FLOW_CONFIRM, // 'confirm_time' => $time // ]; // } // // // // Order::updateAll($params, ['id' => $order_id_list]); } } debug_log([ 'errCode' => $errCode, 'msg' => $msg ], 'wechat-call-back.log'); } catch (\Exception $e) { debug_log([ 'code' => 1, 'get' => get_params(), 'errCode' => $e->getMessage() ], 'wechat-call-back.log'); } } public function actionDeliveryCallBack() { try { $params = post_params(); $order_no = $params['store_order_id']; $deliveryInfo = DeliveryInfo::findOne(['order_no' => $order_no]); if ($deliveryInfo) { $deliveryInfo->wx_order_status = $params['order_status']; } if (!$deliveryInfo->save()) { throw new \Exception(json_encode($deliveryInfo->errors, JSON_UNESCAPED_UNICODE)); } $order_ = Order::findOne(['order_no' => $order_no, 'is_delete' => 0]); if (in_array($params['order_status'], [30000, 40000, 50000])) { $order_->trade_status = Order::ORDER_FLOW_SEND; } if (intval($params['order_status']) === 70000) { $order_->trade_status = Order::ORDER_FLOW_CONFIRM; } if (!$order_->save()) { throw new \Exception(json_encode($order_->errors, JSON_UNESCAPED_UNICODE)); } //取消订单 if (in_array($params['order_status'], [20000, 20001, 60000])) { $new_delivery = new WechatNewDelivery(); $result = $new_delivery->handleCancel(['order_no' => $order_no]); debug_log($result, 'delivery_.log'); } return $this->asJson([ "return_code" => 0, "return_msg" => "OK" ]); } catch (\Exception $e) { debug_log([ 'line' => $e->getLine(), 'file' => $e->getFile(), 'message' => $e->getMessage() ], 'delivery_.log'); } } //微信转账回调 (先不回) public function actionWechatTransferBatchesNotify() { try { debug_log(['Wechatpay-Serial' => get_header('Wechatpay-Serial'), 'get_header' => get_header(), 'get_params' => get_params(), 'post_params' => post_params()], 'wechatTransferBatchesNotify.log'); $store_id = get_params('store_id'); if ($store_id > 0) { $store = Store::findOne($store_id); $wechat_cash = Option::get('wechat_cash', $store_id, 'store')['value']; if($store->is_platform_transfers == 1){ $wechat_cash = Option::get('store_wechat_cash', $store_id, 'store')['value']; } $wechat_cash = json_decode($wechat_cash, true); $params = post_params(); $res = (new \app\utils\WechatMerchant\WxV3())->decryptToString( $params['resource']['associated_data'], $params['resource']['nonce'], $params['resource']['ciphertext'], $wechat_cash['mch_api_key'] ); if (!$res) { return $this->asJson([ 'code' => 'FAIL' ]); } debug_log(['decryptToString' => $res], 'wechatTransferBatchesNotify.log'); $res = json_decode($res, true); $cash = Cash::findOne(['order_no' => $res['out_bill_no']]); if ($cash->status == Cash::STATUS_REFUSE) { return $this->asJson([ 'code' => 'FAIL' ]); } if (intval($cash->wx_cash_type) == Cash::WX_CASH_TYPE_NEW) { if (!in_array($cash->wx_cash_state, ['SUCCESS', 'FAIL', 'CANCELLED'])) { $cash->wx_cash_state = $res['state']; if (in_array($res['state'], ['CANCELLED', 'FAIL'])) { $cash->wx_cash_error = $res['fail_reason']; $cash->status = Cash::STATUS_REFUSE; $user = User::findOne(['id' => $cash->user_id]); cash::cashRefuse($cash->id); } if ($cash->save()) { debug_log([ 'type' => '提现驳回成功', 'result' => $res ], 'wechatTransferBatchesNotify.log'); if (isset($user)) { NoticeSend::CashFail($cash->user_id, $user->binding, $cash->price, '提现被驳回', '提现被驳回', $store_id); } } else { debug_log([ 'type' => '提现驳回失败', 'error' => json_encode($cash->errors, JSON_UNESCAPED_UNICODE) ], 'wechat_cash_status.log'); } } else { debug_log([ 'type' => '提现驳回失败 状态已经更改', 'error' => json_encode($cash->errors, JSON_UNESCAPED_UNICODE) ], 'wechat_cash_status.log'); } } } else { $platform_mch_api_key = Option::get('platform_mch_api_key', 0, 'saas')['value']; $params = post_params(); $res = (new \app\utils\WechatMerchant\WxV3())->decryptToString( $params['resource']['associated_data'], $params['resource']['nonce'], $params['resource']['ciphertext'], $platform_mch_api_key ); if (!$res) { return $this->asJson([ 'code' => 'FAIL' ]); } debug_log(['decryptToString' => $res], 'wechatStoreTransferBatchesNotify.log'); $res = json_decode($res, true); $storeCash = StoreCash::findOne(['order_no' => $res['out_bill_no']]); if ($storeCash->status == Cash::STATUS_REFUSE) { return $this->asJson([ 'code' => 'FAIL' ]); } if (intval($storeCash->wx_cash_type) == Cash::WX_CASH_TYPE_NEW) { if (!in_array($storeCash->wx_cash_state, ['SUCCESS', 'FAIL', 'CANCELLED'])) { $storeCash->wx_cash_state = $res['state']; if (in_array($res['state'], ['CANCELLED', 'FAIL'])) { $storeCash->status = Cash::STATUS_REFUSE; $store = Store::findOne($storeCash->store_id); $price = bcadd($storeCash->price, $storeCash->service_charge, 2); $subMoney = \app\models\Store::addMoney($store, $price, '商城提现打款失败'); debug_log([ 'type' => '商城提现驳回结果', 'result' => $subMoney ], 'wechatStoreTransferBatchesNotify.log'); } if ($storeCash->save()) { debug_log([ 'type' => '提现驳回成功', 'result' => $res ], 'wechatStoreTransferBatchesNotify.log'); } else { debug_log([ 'type' => '提现驳回失败', 'error' => json_encode($storeCash->errors, JSON_UNESCAPED_UNICODE) ], 'wechatStoreTransferBatchesNotify.log'); } } else { debug_log([ 'type' => '提现驳回失败 状态已经更改', 'error' => json_encode($storeCash->errors, JSON_UNESCAPED_UNICODE) ], 'wechatStoreTransferBatchesNotify.log'); } } } return $this->asJson([ 'code' => 'SUCCESS' ]); } catch (\Exception $e) { debug_log(['message' => $e->getMessage(), 'line' => $e->getLine(), 'file' => $e->getFile()], 'wechatTransferBatchesNotify.log'); return $this->asJson([ 'code' => 'SUCCESS' ]); } } }