微信公众号支付

This commit is contained in:
liu
2026-09-04 21:22:46 +08:00
parent 52e339cdd3
commit cf1d3a6229
13 changed files with 494 additions and 24 deletions
+30 -10
View File
@@ -32,11 +32,17 @@ class OnlinePaymentService
/** 在线支付渠道:微信官方支付 */
public const string CHANNEL_WECHAT = 'wechat';
/** 支付场景:微信小程序(wx.login 换 openidwx.requestPayment 调起) */
public const string SCENE_MINI = 'mini';
/** 支付场景:公众号 H5(网页授权换 openidWeixinJSBridge 调起) */
public const string SCENE_MP = 'mp';
public function __construct(
protected BillNumberService $billNumber,
protected WangpuPayService $wangpu,
protected WechatPayService $wxpay,
protected WechatMiniService $wechat,
protected WechatMpService $wechatMp,
) {}
/**
@@ -55,20 +61,29 @@ class OnlinePaymentService
* 发起在线支付
*
* @param array<int, int> $billIds 合并付款的账单ID
* @param string $code 小程序 wx.login() 返回的登录凭证
* @param string $code 小程序 wx.login() 登录凭证 / 公众号网页授权 code(按 scene 区分)
* @param string $scene 支付场景:mini=小程序(默认) / mp=公众号 H5
* @return array{0: PaymentModel, 1: array<string, mixed>} 支付单与渠道返回的调起支付参数
* @throws Throwable
*/
public function create(StoreModel $store, array $billIds, string $code, string $remark = ''): array
public function create(StoreModel $store, array $billIds, string $code, string $remark = '', string $scene = self::SCENE_MINI): array
{
$channel = $this->channel();
$payMethod = $channel === self::CHANNEL_WECHAT ? PaymentModel::METHOD_WECHAT : PaymentModel::METHOD_WANGPU;
// 换取付款人 openid 并绑定到门店(下次可直接复用)
$openid = $this->wechat->code2session($code);
if ((string) $store->openid !== $openid) {
$store->openid = $openid;
$store->save();
// 换取付款人 openid 并绑定到门店(小程序/公众号分场景绑定,两者 openid 维度不同不可混用)
if ($scene === self::SCENE_MP) {
$openid = $this->wechatMp->code2openid($code);
if ((string) $store->mp_openid !== $openid) {
$store->mp_openid = $openid;
$store->save();
}
} else {
$openid = $this->wechat->code2session($code);
if ((string) $store->openid !== $openid) {
$store->openid = $openid;
$store->save();
}
}
[$payment, $bills] = DB::transaction(function () use ($store, $billIds, $openid, $remark, $payMethod) {
@@ -122,12 +137,14 @@ class OnlinePaymentService
(string) $payment->amount,
$openid,
'账单合并付款-' . $payment->payment_no,
$scene === self::SCENE_MP ? $this->wechatMp->appid() : '',
)
: $this->wangpu->createOrder([
'mer_order_id' => $payment->payment_no,
'order_amt' => (string) $payment->amount,
'open_id' => $openid,
'sub_appid' => $this->subAppid(),
'sub_appid' => $this->subAppid($scene),
'payway_code' => $this->wangpu->paywayCode($scene),
'order_title' => '账单合并付款-' . $payment->payment_no,
'notifyurl' => $this->wangpu->notifyUrl(),
]);
@@ -391,10 +408,13 @@ class OnlinePaymentService
}
/**
* 下单微信子 appid(默认取小程序 appid
* 下单微信子 appid(默认取小程序 appid;公众号 H5 场景取公众号 appid
*/
protected function subAppid(): string
protected function subAppid(string $scene = self::SCENE_MINI): string
{
if ($scene === self::SCENE_MP) {
return $this->wechatMp->appid();
}
$subAppid = (string) site_config('wangpu.sub_appid', '');
if ($subAppid === '') {
$subAppid = (string) config('services.wangpu.sub_appid', '');