use EasyWeChat\Pay\Application;
use WebmanTech\Logger\Logger;
public function submit(Request $request)
{
$params = OrderSubmitRequest::validate($request->all());
$lock_name = 'order:submit:' . $request->uid;
$lock = Redis::setEx($lock_name, 3, 1);
if (!$lock) return fail('请勿重复提交');
$price = Config::cacheValue('fee_money');
$data['price'] = $price;
$data['order_sn'] = 'WA' . get_order_sn();
$data['uid'] = $request->uid;
$data['type'] = 1;
$res = Order::create($data);
if (!$res) return fail('创建订单失败');
$config = config('wechat.payment.default');
$order = [
"mchid" => $config['mch_id'],
"out_trade_no" => $data['order_sn'],
"appid" => $config['app_id'],
"description" => '微信支付测试',
"notify_url" => 'https://'.request()->host() . "/xxx/callback", // 支付成功后回调通知URL
"amount" => [
"total" => (int)bcmul($price,100),
"currency" => "CNY",
],
"payer" => [
"openid" => $request->user->openid,
],
];
try {
$app = new Application($config);
$res = $app->getClient()->postJson("v3/pay/transactions/jsapi", $order);
$res = $res->toArray();
} catch (\Throwable $e) {
Logger::info($e->getMessage());
return fail($e->getMessage());
}
if (!isset($res['prepay_id'])) {
Logger::info($res);
return fail('获取prepay_id失败');
}
$prepayId = $res['prepay_id'];
$utils = $app->getUtils();
$config = $utils->buildMiniAppConfig($prepayId, $config['app_id'], 'RSA');
Redis::del($lock_name);
return success($config);
}
openssl x509 -in apiclient_cert.pem -noout -serial