微信小程序支付功能打通

This commit is contained in:
2026-07-13 00:07:17 +08:00
parent f2cdc0222d
commit 450dc44d0e
14 changed files with 73 additions and 28 deletions
@@ -50,7 +50,7 @@ export class WechatApiProvider implements IWechatProvider {
const cfg = loadAppConfig();
return (
!cfg.mockPay &&
!!this.appId &&
!!(this.miniAppId || this.appId) &&
!!this.mchId &&
!!this.mchSerialNo &&
!!this.mchPrivateKey &&
@@ -288,6 +288,7 @@ export class WechatApiProvider implements IWechatProvider {
amountFen: number;
openId: string;
notifyUrl: string;
platform?: 'h5' | 'mini';
}) {
if (!this.isPayEnabled()) {
throw new InternalServerErrorException(
@@ -298,8 +299,17 @@ export class WechatApiProvider implements IWechatProvider {
if (!notifyUrl) {
throw new InternalServerErrorException('请配置 WX_PAY_NOTIFY_URL');
}
const platform = params.platform ?? 'h5';
const payAppId = platform === 'mini' ? this.miniAppId || this.appId : this.appId || this.miniAppId;
if (!payAppId) {
throw new InternalServerErrorException(
platform === 'mini'
? '小程序支付未配置:请设置 WX_MINI_APP_ID'
: '微信支付未配置:请设置 WX_APP_ID',
);
}
const body = {
appid: this.miniAppId || this.appId,
appid: payAppId,
mchid: this.mchId,
description: params.description,
out_trade_no: params.orderNo,
@@ -329,13 +339,13 @@ export class WechatApiProvider implements IWechatProvider {
const timeStamp = String(Math.floor(Date.now() / 1000));
const nonceStr = randomUUID().replace(/-/g, '');
const packageStr = `prepay_id=${res.prepay_id}`;
const message = `${this.appId}\n${timeStamp}\n${nonceStr}\n${packageStr}\n`;
const message = `${payAppId}\n${timeStamp}\n${nonceStr}\n${packageStr}\n`;
const sign = createSign('RSA-SHA256');
sign.update(message);
sign.end();
const paySign = sign.sign(this.mchPrivateKey, 'base64');
return {
appId: this.appId,
appId: payAppId,
timeStamp,
nonceStr,
package: packageStr,
@@ -73,6 +73,7 @@ export interface IWechatProvider {
amountFen: number;
openId: string;
notifyUrl: string;
platform?: 'h5' | 'mini';
}): Promise<WechatJsapiPrepayParams>;
/** 解析并验签支付回调通知 */
@@ -75,6 +75,7 @@ export class WechatRouterProvider implements IWechatProvider {
amountFen: number;
openId: string;
notifyUrl: string;
platform?: 'h5' | 'mini';
}) {
return this.resolve().createJsapiPrepay(params);
}