在线支付
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Exceptions\RepositoryException;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* 微信小程序服务(wx.login 登录凭证校验 code2session)
|
||||
*
|
||||
* 在线支付前置:用小程序 wx.login() 返回的 code 换取付款人 openid,
|
||||
* 作为旺铺统一下单的 open_id(服务商模式下的微信用户标识)。
|
||||
*/
|
||||
class WechatMiniService
|
||||
{
|
||||
/**
|
||||
* wx.login 的 code 换 openid
|
||||
*
|
||||
* @return string 用户 openid
|
||||
* @throws RepositoryException 未配置小程序或凭证校验失败
|
||||
*/
|
||||
public function code2session(string $code): string
|
||||
{
|
||||
$appid = trim((string) config('services.wechat.mini.appid'));
|
||||
$secret = trim((string) config('services.wechat.mini.secret'));
|
||||
if ($appid === '' || $secret === '') {
|
||||
throw new RepositoryException('微信小程序未配置 AppID/Secret,请联系管理员');
|
||||
}
|
||||
|
||||
$result = Http::timeout(10)->get('https://api.weixin.qq.com/sns/jscode2session', [
|
||||
'appid' => $appid,
|
||||
'secret' => $secret,
|
||||
'js_code' => $code,
|
||||
'grant_type' => 'authorization_code',
|
||||
])->json();
|
||||
|
||||
$openid = is_array($result) ? (string) ($result['openid'] ?? '') : '';
|
||||
if ($openid === '') {
|
||||
Log::warning('微信 code2session 失败', ['response' => $result]);
|
||||
throw new RepositoryException('微信登录凭证校验失败:' . ($result['errmsg'] ?? '请重新进入小程序'));
|
||||
}
|
||||
|
||||
return $openid;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user