退款申请返回\n"; $payResp = $request->rawBody(); $wechat = new \WePay\Refund($this->getWxConfig()); if (empty($payResp)) return $wechat->getNotifySuccessReply(); $data = $wechat->getNotify($payResp); if (empty($data)) return $wechat->getNotifySuccessReply(); $orderSn = $data['result']['out_refund_no']; } catch (\Throwable $throwable) { return error($throwable->getMessage()); } } /** * 订单支付 */ #[Route(path: "wx",methods: "post")] public function notifyWx(Request $request) { try { echo getDateFull()."===>支付返回\n"; $payResp = $request->rawBody(); $data = $this->payReturn($payResp); $order = (new SaasOrder)->where("order_sn",$data['attach'])->with(['shop' => function($query){ $query->field('shop_id,shop_name,shop_address'); }])->findOrEmpty(); if ($order->isEmpty()) return $this->getNotifySuccessReply(); if ($order['status'] <> 0) return $this->getNotifySuccessReply(); // 已支付或者是其他状态 $order->status = 1; $order->pay_at = getDateFull(); $order->transaction_id = $data['transaction_id']??''; $order->notify_status = 1; $order->pay_type = 1; $order->save(); $shop = (new SaasShop)->where("shop_id",$order['shop_id'])->with(['wx' => function($query){ $query->field("shop_id,openid,is_msg"); }])->findOrEmpty(); if ($shop->isEmpty()) return $this->getNotifySuccessReply(); $shop->balance = Db::raw("balance+".$order['money']); $shop->total_balance = Db::raw("total_balance+".$order['money']); $shop->save(); (new SaasShopLog)->insertGetId([ "shop_id" => $order['shop_id'], "money" => $order['money'], "balance" => $shop->balance, "remark" => "新订单【{$order['order_sn']}】" ]); events("create-order",['shop' => $order['shop_id'],'openid' => $order['openid'],"order" => $order['order_sn']]); // 推送消息-公众号 if (!empty($shop['wx'])) { $obj = \We::WeChatTemplate([ "appid" => sConf("wechat.appid"), "appsecret" => sConf("wechat.secret"), "token" => sConf("wechat.token"), "encodingaeskey" => sConf("wechat.aeskey") ]); foreach ($shop['wx'] as $val) { if ($val['is_msg'] == 1) { $obj->send([ "touser" => $val['openid'], "template_id" => "D20ZEWmUNmXMHLwiTOzaEateX5NvM9zoCbp2YwbIHsI", "data" => [ "thing20" => ["value" => $val['shop_name']], "character_string1" => ["value" => $order['order_sn']], "amount16" => ["value" => format_money($order['money'] / 100)], "time2" => ["value" => date('Y-m-d H:i')] ], "url" => "https://inmei.yunenv.cn/weixin/order/detail?id=" . $order['id'] ]); } } } return $this->getNotifySuccessReply(); } catch (\Throwable $throwable) { return error($throwable->getMessage()); } } /** * 充值并支付 */ #[Route(path: "recharge",methods: "post")] public function notifyPayRecharge(Request $request) { try { echo getDateFull()."===>充值并支付支付返回\n"; $payResp = $request->rawBody(); $data = $this->payReturn($payResp); $orderBuy = (new SaasUserBuy)->where("order_sn",$data['attach'])->with(['orders'])->findOrEmpty(); if ($orderBuy->isEmpty()) return $this->getNotifySuccessReply(); if ($orderBuy['status'] <> 0) return $this->getNotifySuccessReply(); // 已支付或者是其他状态 $orderMoney = 0; $logData[0] = [ "openid" => $orderBuy['openid'], "card_no" => $orderBuy['card_no'], "shop_id" => $orderBuy['shop_id'], "money" => $orderBuy['total_money'], "order_sn" => $orderBuy['order_sn'], "type" => 2, "remark" => "充值", "balance" => $orderBuy['total_money'] ]; if (!empty($orderBuy['orders'])) { $orderMoney = $orderBuy['orders']['money']; $logData[1] = [ "openid" => $orderBuy['openid'], "card_no" => $orderBuy['card_no'], "shop_id" => $orderBuy['shop_id'], "order_sn" => $orderBuy['order_sn'], "money" => $orderMoney, "type" => 1, "remark" => "订单付款", "balance" => $orderBuy['total_money'] - $orderMoney ]; }; $orderBuy->status = 1; $orderBuy->pay_at = getDateFull(); $orderBuy->transaction_id = $data['transaction_id']??''; $orderBuy->save(); $shop = (new SaasShop)->where("shop_id",$orderBuy['shop_id'])->with(['wx' => function($query){ $query->field("shop_id,openid,is_msg"); }])->findOrEmpty(); if ($shop->isEmpty()) return $this->getNotifySuccessReply(); // 开通vip账户 $card = (new SaasUser)->where("card_no",$orderBuy['card_no'])->findOrEmpty(); $balanceMoney = $orderBuy['total_money'] - $orderMoney; if ($card->isEmpty()) { $card->insertGetId([ "openid" => $orderBuy['openid'], "shop_id" => $orderBuy['shop_id'], "card_no" => $orderBuy['card_no'], "balance" => $balanceMoney, // 余额 "total_balance" => $orderBuy['money'], // 累计充值 "total_consume" => $orderMoney // 累计消费 ]); } else { $card->total_consume = Db::raw("total_consume+{$orderMoney}"); $card->total_balance = Db::raw("total_balance+{$orderBuy['money']}"); $card->balance = Db::raw("balance+{$balanceMoney}"); $card->save(); } if (!empty($orderBuy['orders'])) { $order = (new SaasOrder)->where("order_sn",$orderBuy['order_sn'])->findOrEmpty(); if ($order->isEmpty()) return $this->getNotifySuccessReply(); if ($order['status'] <> 0) return $this->getNotifySuccessReply(); // 已支付或者是其他状态 $order->status = 1; $order->pay_at = getDateFull(); $order->transaction_id = $data['transaction_id']??''; $order->notify_status = 1; $order->pay_type = 2; $order->save(); events("create-order",['shop' => $order['shop_id'],'openid' => $order['openid'],"order" => $order['order_sn']]); } $shop->balance = Db::raw("balance+".$orderBuy['money']); $shop->total_balance = Db::raw("total_balance+".$orderBuy['money']); $shop->save(); (new SaasShopLog)->insertGetId([ "shop_id" => $orderBuy['shop_id'], "money" => $orderBuy['money'], "balance" => $shop->balance, "type" => 1, "remark" => "会员卡充值", "status" => 1 ]); // 推送消息-公众号 if (!empty($shop['wx'])) { $obj = \We::WeChatTemplate([ "appid" => sConf("wechat.appid"), "appsecret" => sConf("wechat.secret"), "token" => sConf("wechat.token"), "encodingaeskey" => sConf("wechat.aeskey") ]); foreach ($shop['wx'] as $val) { if ($val['is_msg'] == 1) { $obj->send([ "touser" => $val['openid'], "template_id" => "v1TVHflG9h5BPRLb2hH10r8oOV5OFMKD2cmqIw1nH-w", "data" => [ "thing1" => ["value" => '微信用户'.$orderBuy['openid']], "thing6" => ["value" => $val['shop_name']], "amount3" => ["value" => format_money($orderBuy['money'] / 100)], "character_string9" => ["value" => $orderBuy['order_sn']], "time4" => ["time5" => date('Y-m-d H:i')] ] ]); } } } (new SaasUserLog)->insertAll($logData); return $this->getNotifySuccessReply(); } catch (\Throwable $throwable) { return error($throwable->getMessage()); } } /** * 会员卡充值 */ #[Route(path: "recharges",methods: "post")] public function notifyDataRecharge(Request $request) { try { echo getDateFull()."===>会员卡支付返回\n"; $payResp = $request->rawBody(); $data = $this->payReturn($payResp); } catch (\Throwable $throwable) { return error($throwable->getMessage()); } } protected function payReturn($payResp) { $wechat = new \WeChat\Pay($this->getWxConfig()); if (empty($payResp)) return $this->getNotifySuccessReply(); $data = Tools::xml2arr($payResp); if (empty($data)) return $this->getNotifySuccessReply(); return $data; } /** * 获取微信支付通知成功回复 XML * @return string */ protected function getNotifySuccessReply(): string { return Tools::arr2xml(['return_code' => 'SUCCESS', 'return_msg' => 'OK']); } /** * 小程序配置 * @return array */ protected function getWxConfig(): array { return [ 'token' => 'test', 'appid' => sConf("wechat.mini_appid"), 'appsecret' => sConf("wechat.mini_secret"), 'encodingaeskey' => 'BJIUzE0gqlWy0GxfPp4J1oPTBmOrNDIGPNav1YFH5Z5', // 配置商户支付参数(可选,在使用支付功能时需要) 'mch_id' => sConf("wechat.mch_id"), 'mch_key' => sConf("wechat.mch_key") ]; } }