提现银行卡异步返回\n"; $data = $this->jhfReturn($request->all(),"withdraw.succeeded"); if (empty($data)) return error("提现失败"); if ($data['status'] == "succeeded") // 成功了 { $day = date("Y-m-d",strtotime("-1 day")); $todayMoney = (new SystemUserMoney)->where("member_id",$data['member_id'])->where("day",$day)->select(); if ($todayMoney->count() > 0) { $state = (new SystemUserMoney)->where("member_id",$data['member_id'])->where("day",$day)->update(['status' => 1,"withdraw_id" => $data['withdraw_id']]); } } return success("ok"); } catch (\Throwable $throwable) { return error($throwable->getMessage()); } } /** * 退款回调 * @param Request $request */ #[Route(path: "refund",methods: "post")] public function notifyRefund(Request $request) { try { $data = $this->jhfReturn($request->all(),"refund.succeeded"); if (empty($data)) return error("支付失败"); // $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 = explode("-",$data['attach']); $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'] <> 4) return $this->getNotifySuccessReply(); $order->status = 6; $order->refund_at = getDateFull(); $order->save(); (new SaasOrderDetail)->where("order_sn",$order['order_sn'])->update(['status' => 6]); $orderMoney = (($order['money'] > $order['discount']) ? $order['discount'] : $order['money']); // 商家账户额度退款 $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 = $shop['balance'] - $orderMoney; $shop->total_balance = $shop['total_balance'] - $orderMoney; $shop->save(); (new SaasShopLog)->insertGetId([ "shop_id" => $order['shop_id'], "money" => $orderMoney, "balance" => $shop->balance, "remark" => "订单退款【{$order['order_sn']}】", "type" => 2 ]);// 推送消息-公众号 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" => "WboxN-32rdHORBQHzaVvQb9kPN7rLaXAPS_BL2yfb5w", "data" => [ "amount3" => ["value" => format_money($order['money']/100)], "character_string1" => ["value" => $order['old_order']], "time4" => ["value" => date('Y-m-d H:i')] ] ]); } } } } catch (\Throwable $throwable) { return error($throwable->getMessage()); } } /** * 订单支付 */ #[Route(path: "wx",methods: "post")] public function notifyWx(Request $request) { try { $data = $this->jhfReturn($request->all()); if (empty($data)) return error("支付失败"); // $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 "success"; if ($order['status'] <> 0) return "success"; // 已支付或者是其他状态 $order->status = 1; $order->pay_at = getDateFull(); $order->transaction_id = $data['transaction_id']??''; $order->payment_id = $data['payment_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 "success"; $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'],'print' => $order['print_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 "success"; } catch (\Throwable $throwable) { return "success"; } } /** * 充值并支付 */ #[Route(path: "recharge",methods: "post")] public function notifyPayRecharge(Request $request) { try { echo getDateFull()."===>充值并支付支付返回\n"; $data = $this->jhfReturn($request->all()); if (empty($data)) return error("支付失败"); $orderBuy = (new SaasUserBuy)->where("order_sn",$data['attach'])->with(['orders'])->findOrEmpty(); if ($orderBuy->isEmpty()) return "success"; if ($orderBuy['status'] <> 0) return "success"; // 已支付或者是其他状态 $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->payment_id = $data['payment_id']??''; $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 "success"; // 开通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 "success"; if ($order['status'] <> 0) return "success"; // 已支付或者是其他状态 $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'],'print' => $order['print_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 "success"; } catch (\Throwable $throwable) { return error($throwable->getMessage()); } } /** * 会员卡充值 */ #[Route(path: "recharges",methods: "post")] public function notifyDataRecharge(Request $request) { try { echo getDateFull()."===>会员卡支付返回\n"; $data = $this->jhfReturn($request->all()); if (empty($data)) return error("支付失败"); } catch (\Throwable $throwable) { return error($throwable->getMessage()); } } /** * 第三方支付返回 * @param array $respData * @return array */ protected function jhfReturn(array $respData = [],string $return = "payment.succeeded"): array { if ($respData['type'] <> $return) return []; $resCipher = Utils::aes_decrypt($respData['resCipher'], sConf("wechat.jhf_aeskey")); $data = json_decode($resCipher,true); if ($return == "withdraw.succeeded") { print_r($data); } $data['attach'] = $data['order_no']; $data['transaction_id'] = $data['out_trans_id']??''; $data['payment_id'] = $data['payment_id']??''; $data['withdraw_id'] = $data['withdraw_id']??''; $data['member_id'] = $data['member_id']??''; $data['status'] = $data['status']??''; return $data; } 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 json(['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") ]; } }