where('id', Auth::id())->value('openid'); $pay = EasyWeChat::pay(); $merchant = $pay->getMerchant(); // $app = EasyWeChat::officialAccount(); // $client = $app->getClient(); $config = EasyWeChat::pay()->getConfig(); $app = new Application($config); $response = $app->getClient()->postJson("https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi", [ "mchid" => (string)$merchant->getMerchantId(), // <---- 请修改为您的商户号 "out_trade_no" => uniqid('native') . rand(1, 1000), // "native12177525012012070352333'.rand(1,1000).'", "appid" => $app->getConfig()->get('app_id'), // <---- 请修改为服务号的 appid "description" => "Image形象店-深圳腾大-QQ公仔", "notify_url" => env('BACKEND_URL') . env('WECHAT_PAYMENT_NOTIFY_URL'), "amount" => [ "total" => 1, "currency" => "CNY" ], "payer" => [ "openid" => $openid // <---- 请修改为服务号下单用户的 openid ] ]); $app->getValidator()->validate($response->toPsrResponse()); // 验证通过 // dd($response->toArray(false)); $prepayId = $response['prepay_id']; $sign = [ 'appId' => $config['app_id'], // 公众号 appid 'timeStamp' => (string)time(), // 时间戳 'nonceStr' => uniqid(), // 随机字符串 'package' => 'prepay_id=' . $prepayId, // prepay_id 'signType' => 'RSA', // 签名类型,默认为 MD5 ]; // 对参数进行排序并拼接成字符串 ksort($sign); $stringA = urldecode(http_build_query($sign)); $privateKey =file_get_contents( $config['private_key']); // 拼接 key // 使用 OpenSSL 生成签名 openssl_sign($stringA, $signature, $privateKey, OPENSSL_ALGO_SHA256); $sign['paySign'] = base64_encode($signature); // 生成签名 return $sign; } catch (\Exception $e) { return response()->json(['error' => $e->getMessage()], 500); } // $app = EasyWeChat::pay(); // // $server = $app->getServer(); //// $server->prepend() // $server->handlePaid(function (Message $message, \Closure $next) use ($app) { // try { // $app->getValidator()->validate($app->getRequest()); // // 验证通过,业务处理 // } catch (Exception $e) { // // 验证失败 // } // return $next($message); // }); // return $server->serve(); } /** * @throws Throwable */ public function notify(): false|\Psr\Http\Message\ResponseInterface { try { $pay = EasyWeChat::pay(); $server = $pay->getServer(); $server->handlePaid(function (Message $message, \Closure $next) { // $message->out_trade_no 获取商户订单号 // $message->payer['openid'] 获取支付者 openid // 🚨🚨🚨 注意:推送信息不一定靠谱哈,请务必验证 // 建议是拿订单号调用微信支付查询接口,以查询到的订单状态为准 Log::log('success', 'payNotify', (array)$message); return $next($message); }); // 默认返回 ['code' => 'SUCCESS', 'message' => '成功'] return $server->serve(); } catch (Exception $e) { Log::log('error', $e->getMessage()); return false; } } }