checkLogin(); $openid = request('openid',''); $orderId = request('order_id',''); $snowflakeId = request('snowflake_id',''); $orderInfo = Orders::query()->where('id',$orderId)->get(); if (empty($orderInfo)) return json_send(['code'=>'error','msg'=>'订单不存在','data'=>['id'=>null]]); if ($orderInfo['custom_uid'] != $uid) return json_send(['code'=>'error','msg'=>'订单已支付或取消','data'=>['id'=>null]]); if ($orderInfo['status'] != 1) return json_send(['code'=>'error','msg'=>'订单已支付或取消','data'=>['id'=>null]]); $payment = new Payment(); return $payment->pay(['out_trade_no' => $orderInfo['snowflake_id'],'openid' => $openid,'description' => '开邻智教','total_price' => $orderInfo['pay_total']]); } /** * 小程序微信支付回调 /api/wechat_pay/notify * * */ public function notify() { $post_data = request(); Log::log('notify_wechat_pay', 'post_data:' . $post_data); //获取headers参数 $headers = request()->header(); Log::log('notify_wechat_pay', '微信支付回调返回headers参数:' . json_encode($headers)); $inWechatpaySignature = $headers['wechatpay-signature']; $inWechatpayTimestamp = $headers['wechatpay-timestamp']; $inWechatpaySerial = $headers['wechatpay-serial']; $inWechatpayNonce = $headers['wechatpay-nonce']; $inBody = $post_data; $apiv3Key = Config('wechat.APIV3');// 在商户平台上设置的APIv3密钥 // 根据通知的平台证书序列号,查询本地平台证书文件, $platformCertificateFilePath = Config('wechat.platformCertificate'); $platformPublicKeyInstance = Rsa::from($platformCertificateFilePath, Rsa::KEY_TYPE_PUBLIC); // 检查通知时间偏移量,允许5分钟之内的偏移 $timeOffsetStatus = 300 >= abs(Formatter::timestamp() - (int)$inWechatpayTimestamp); Log::log('notify_wechat_pay', '时间偏移量:' . $timeOffsetStatus); $verifiedStatus = Rsa::verify( // 构造验签名串 Formatter::joinedByLineFeed($inWechatpayTimestamp, $inWechatpayNonce, $inBody), $inWechatpaySignature, $platformPublicKeyInstance ); Log::log('notify_wechat_pay', '验签:' . $verifiedStatus); $orderService = new OrderService(); if ($timeOffsetStatus && $verifiedStatus) { // 转换通知的JSON文本消息为PHP Array数组 $inBodyArray = (array)json_decode($inBody, true); // 使用PHP7的数据解构语法,从Array中解构并赋值变量 ['resource' => [ 'ciphertext' => $ciphertext, 'nonce' => $nonce, 'associated_data' => $aad ]] = $inBodyArray; // 加密文本消息解密 $inBodyResource = AesGcm::decrypt($ciphertext, $apiv3Key, $nonce, $aad); // 把解密后的文本转换为PHP Array数组 $inBodyResourceArray = (array)json_decode($inBodyResource, true); Log::log('notify_wechat_pay', '打印解密后的结果:' . json_encode($inBodyResourceArray)); Log::log('notify_wechat_pay', '参数:' . $inBodyResourceArray['trade_state'] . '订单号' . $inBodyResourceArray['out_trade_no'] . '微信支付号' . $inBodyResourceArray['transaction_id']); if ($inBodyResourceArray['trade_state'] == "SUCCESS") { Log::log('notify_wechat_pay', '通知订单'); $res = $orderService->payCallback($inBodyResourceArray['out_trade_no'], $inBodyResourceArray['transaction_id']); Log::log('notify_wechat_pay', '通知返回' . json_encode($res)); } } } }