微信JSSDK对接,代下单支付

This commit is contained in:
2026-08-02 11:49:35 +08:00
parent 50ba6e9c56
commit 09e732bfa3
32 changed files with 1223 additions and 218 deletions
@@ -553,6 +553,55 @@ export class WechatApiProvider implements IWechatProvider {
};
}
async createNativePrepay(params: {
orderNo: string;
description: string;
amountFen: number;
notifyUrl: string;
}) {
if (!this.isPayEnabled()) {
throw new InternalServerErrorException(
'微信支付未配置:请关闭 MOCK_PAY 并配置 WX_MCH_ID、WX_MCH_SERIAL_NO、WX_MCH_PRIVATE_KEY、WX_API_V3_KEY',
);
}
const notifyUrl = params.notifyUrl || this.notifyUrl;
if (!notifyUrl) {
throw new InternalServerErrorException('请配置 WX_PAY_NOTIFY_URL');
}
const payAppId = this.appId || this.miniAppId;
if (!payAppId) {
throw new InternalServerErrorException('微信支付未配置:请设置 WX_APP_ID');
}
const body = {
appid: payAppId,
mchid: this.mchId,
description: params.description,
out_trade_no: params.orderNo,
notify_url: notifyUrl,
amount: { total: params.amountFen, currency: 'CNY' },
};
const path = '/v3/pay/transactions/native';
const payload = JSON.stringify(body);
const auth = this.signPayRequest('POST', path, payload);
const res = await this.fetchPayJson<{ code_url?: string }>(
`https://api.mch.weixin.qq.com${path}`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
Authorization: auth,
},
body: payload,
},
);
if (!res.code_url) {
throw new InternalServerErrorException('微信 Native 下单失败');
}
this.logger.log(`NATIVE prepay ok mchid=${this.mchId} orderNo=${params.orderNo}`);
return { codeUrl: res.code_url };
}
async parsePayNotification(
headers: Record<string, string | string[] | undefined>,
rawBody: string,
@@ -51,6 +51,10 @@ export class WechatDisabledProvider implements IWechatProvider {
return this.disabled();
}
createNativePrepay() {
return this.disabled();
}
parsePayNotification() {
return this.disabled();
}
@@ -132,6 +132,14 @@ export interface IWechatProvider {
platform?: 'h5' | 'mini';
}): Promise<WechatJsapiPrepayParams>;
/** 创建 Native 扫码支付 code_url */
createNativePrepay(params: {
orderNo: string;
description: string;
amountFen: number;
notifyUrl: string;
}): Promise<{ codeUrl: string }>;
/** 解析并验签支付回调通知 */
parsePayNotification(
headers: Record<string, string | string[] | undefined>,
@@ -80,6 +80,10 @@ export class WechatMockProvider implements IWechatProvider {
throw new NotImplementedException('FEATURE_DISABLED');
}
createNativePrepay(): never {
throw new NotImplementedException('FEATURE_DISABLED');
}
parsePayNotification(): never {
throw new NotImplementedException('FEATURE_DISABLED');
}
@@ -80,6 +80,15 @@ export class WechatRouterProvider implements IWechatProvider {
return this.resolve().createJsapiPrepay(params);
}
createNativePrepay(params: {
orderNo: string;
description: string;
amountFen: number;
notifyUrl: string;
}) {
return this.resolve().createNativePrepay(params);
}
parsePayNotification(
headers: Record<string, string | string[] | undefined>,
rawBody: string,