You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

112 lines
3.3 KiB

<?php
namespace App\Controller;
use App\Constants\LogLabel;
use App\Model\OrderMain;
use EasyWeChat\Factory;
use Hyperf\DbConnection\Db;
use Hyperf\Guzzle\CoroutineHandler;
use Exception;
class NotifyController extends BaseController
{
public function wxminiOnline()
{
$config = config('wxpay');
$app = Factory::payment($config);
$app['guzzle_handler'] = CoroutineHandler::class;
// 通知回调,进行业务处理
$response = $app->handlePaidNotify(function ($message, $fail) use ($app) {
Db::beginTransaction();
try {
$this->log->event(
LogLabel::PAY_NOTIFY_WXMINI,
$message
);
// 查询订单
$orderMain = OrderMain::query()
->where(['global_order_id' => $message['out_trade_no'], 'type' => OrderMain::ORDER_TYPE_ONLINE, 'state' => OrderMain::ORDER_STATE_UNPAY])
->where('time', '>=', date('Y-m-d H:i:s', (time()-900)))
->first();
if (empty($orderMain)) {
// 去查一下微信订单
$wxOrder = $app->order->queryByOutTradeNumber($orderMain->global_order_id);
$this->log->event(
LogLabel::PAY_NOTIFY_WXMINI,
$wxOrder
);
// return true;
}
// 修改订单、子订单状态
$currentTime = time();
$orderMain->state = OrderMain::ORDER_STATE_UNTAKE;
$orderMain->time_pay = $currentTime;
$orderMain->pay_time = date('Y-m-d H:i:s', $currentTime);
$orderMain->save();
// 更新销量、商品库存,新增月销数据
// 喇叭通知,兼容旧音响,MQTT+IOT
// 公众号模板消息
// 打印订单
Db::commit();
return true;
} catch (Exception $e) {
Db::rollBack();
}
});
$response->send();
}
public function wxminiOffline()
{
$config = config('wxpay');
$app = Factory::payment($config);
$app['guzzle_handler'] = CoroutineHandler::class;
// 通知回调,进行业务处理
$response = $app->handlePaidNotify(function ($message, $fail) use ($app) {
$this->log->event(
LogLabel::PAY_NOTIFY_WXMINI,
$message
);
// 查询订单
$orderMain = OrderMain::query()
->where(['global_order_id' => $message['out_trade_no'], 'type' => OrderMain::ORDER_TYPE_OFFLINE, 'state' => OrderMain::ORDER_STATE_UNPAY])
->where('time', '>=', date('Y-m-d H:i:s', (time()-900)))
->first();
if (empty($orderMain)) {
// 去查一下微信订单
$wxOrder = $app->order->queryByOutTradeNumber($orderMain->global_order_id);
$this->log->event(
LogLabel::PAY_NOTIFY_WXMINI,
$wxOrder
);
// return true;
}
});
$response->send();
}
}