| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- <?php
- namespace app\controller\api;
- use app\extra\basic\Base;
- use app\model\saas\SaasOrder;
- use app\model\saas\SaasShop;
- use app\model\saas\SaasShopLog;
- use app\model\saas\SaasUser;
- use app\model\saas\SaasUserBuy;
- use app\model\saas\SaasUserLog;
- use LinFly\Annotation\Route\Controller;
- use LinFly\Annotation\Route\Route;
- use support\Request;
- use support\Response;
- use think\facade\Db;
- use WeChat\Contracts\Tools;
- #[Controller(prefix: "/notify")]
- class Notify extends Base
- {
- /**
- * 退款回调
- * @param Request $request
- */
- #[Route(path: "refund",methods: "post")]
- public function notifyRefund(Request $request)
- {
- try {
- echo getDateFull() . "===>退款申请返回\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'],
- "type" => 2,
- "remark" => "充值"
- ];
- if (!empty($orderBuy['orders']))
- {
- $orderMoney = $orderBuy['orders']['money'];
- $logData[1] = [
- "openid" => $orderBuy['openid'],
- "card_no" => $orderBuy['card_no'],
- "shop_id" => $orderBuy['shop_id'],
- "money" => $orderMoney,
- "type" => 1,
- "remark" => "订单付款"
- ];
- };
- $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 = 1;
- $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")
- ];
- }
- }
|