Some checks failed
Build and Deploy Front / deploy (push) Failing after 3m53s
混合支付支持 UseBalance;微信/支付宝配置改为问号提示;Vite 堆降至 4G 并关闭 gzip 体积统计,减轻生产同机 CI 压力。 Co-authored-by: Cursor <cursoragent@cursor.com>
286 lines
9.2 KiB
TypeScript
286 lines
9.2 KiB
TypeScript
import { axiosInstance } from '@/api/rbac'
|
|
import { assertBusinessOk, omsUrl, unwrapData } from '@/api/oms/common'
|
|
|
|
export type WeChatPayScene = 'native' | 'jsapi'
|
|
|
|
export interface WeChatJsapiPayParams {
|
|
appId: string
|
|
timeStamp: string
|
|
nonceStr: string
|
|
package: string
|
|
signType: string
|
|
paySign: string
|
|
}
|
|
|
|
export interface CreateWeChatPayResult {
|
|
outTradeNo: string
|
|
rechargeRecordId: number
|
|
channel: string
|
|
status: string
|
|
codeUrl?: string
|
|
expireTime?: string
|
|
jsapi?: WeChatJsapiPayParams
|
|
}
|
|
|
|
export interface WeChatPayStatusResult {
|
|
outTradeNo: string
|
|
status: string
|
|
statusCode: number
|
|
amount: number
|
|
transactionId?: string
|
|
paidTime?: string
|
|
rechargeRecordId: number
|
|
codeUrl?: string
|
|
}
|
|
|
|
function mapCreate(raw: any): CreateWeChatPayResult {
|
|
const js = raw?.jsapi ?? raw?.Jsapi
|
|
return {
|
|
outTradeNo: String(raw?.outTradeNo ?? raw?.OutTradeNo ?? ''),
|
|
rechargeRecordId: Number(raw?.rechargeRecordId ?? raw?.RechargeRecordId ?? 0),
|
|
channel: String(raw?.channel ?? raw?.Channel ?? ''),
|
|
status: String(raw?.status ?? raw?.Status ?? ''),
|
|
codeUrl: (raw?.codeUrl ?? raw?.CodeUrl) || undefined,
|
|
expireTime: (raw?.expireTime ?? raw?.ExpireTime) || undefined,
|
|
jsapi: js
|
|
? {
|
|
appId: String(js.appId ?? js.AppId ?? ''),
|
|
timeStamp: String(js.timeStamp ?? js.TimeStamp ?? ''),
|
|
nonceStr: String(js.nonceStr ?? js.NonceStr ?? ''),
|
|
package: String(js.package ?? js.Package ?? ''),
|
|
signType: String(js.signType ?? js.SignType ?? 'RSA'),
|
|
paySign: String(js.paySign ?? js.PaySign ?? ''),
|
|
}
|
|
: undefined,
|
|
}
|
|
}
|
|
|
|
function mapStatus(raw: any): WeChatPayStatusResult {
|
|
return {
|
|
outTradeNo: String(raw?.outTradeNo ?? raw?.OutTradeNo ?? ''),
|
|
status: String(raw?.status ?? raw?.Status ?? ''),
|
|
statusCode: Number(raw?.statusCode ?? raw?.StatusCode ?? 0),
|
|
amount: Number(raw?.amount ?? raw?.Amount ?? 0),
|
|
transactionId: (raw?.transactionId ?? raw?.TransactionId) || undefined,
|
|
paidTime: (raw?.paidTime ?? raw?.PaidTime) || undefined,
|
|
rechargeRecordId: Number(raw?.rechargeRecordId ?? raw?.RechargeRecordId ?? 0),
|
|
codeUrl: (raw?.codeUrl ?? raw?.CodeUrl) || undefined,
|
|
}
|
|
}
|
|
|
|
/** POST /recharge/pay/wechat/create */
|
|
export async function createWeChatPay(params: {
|
|
rechargeAmount: number
|
|
scene: WeChatPayScene
|
|
customerId?: number
|
|
oauthCode?: string
|
|
}) {
|
|
const headers: Record<string, string> = {}
|
|
if (params.oauthCode) {
|
|
headers['X-Wechat-OAuth-Code'] = params.oauthCode
|
|
}
|
|
const res = await axiosInstance.post(
|
|
omsUrl('/recharge/pay/wechat/create'),
|
|
{
|
|
rechargeAmount: params.rechargeAmount,
|
|
scene: params.scene,
|
|
customerId: params.customerId,
|
|
oauthCode: params.oauthCode,
|
|
},
|
|
{ headers },
|
|
)
|
|
return mapCreate(unwrapData(res.data) ?? res.data)
|
|
}
|
|
|
|
/** GET /recharge/pay/wechat/status */
|
|
export async function getWeChatPayStatus(outTradeNo: string) {
|
|
const res = await axiosInstance.get(omsUrl('/recharge/pay/wechat/status'), {
|
|
params: { outTradeNo },
|
|
})
|
|
return mapStatus(unwrapData(res.data) ?? res.data)
|
|
}
|
|
|
|
/** POST /recharge/pay/wechat/close */
|
|
export async function closeWeChatPay(outTradeNo: string) {
|
|
const res = await axiosInstance.post(omsUrl('/recharge/pay/wechat/close'), { outTradeNo })
|
|
assertBusinessOk(res.data)
|
|
}
|
|
|
|
export interface CreateOrderWeChatPayLine {
|
|
orderId: number
|
|
serviceNo?: string
|
|
payableAmount: number
|
|
balanceAmount: number
|
|
wechatAmount: number
|
|
couponInstanceId?: number
|
|
}
|
|
|
|
export interface CreateOrderWeChatPayResult {
|
|
payableTotal: number
|
|
balanceAmount: number
|
|
wechatAmount: number
|
|
customerBalance: number
|
|
paidImmediately: boolean
|
|
outTradeNo?: string
|
|
channel?: string
|
|
status?: string
|
|
codeUrl?: string
|
|
expireTime?: string
|
|
jsapi?: WeChatJsapiPayParams
|
|
lines: CreateOrderWeChatPayLine[]
|
|
}
|
|
|
|
/** POST /order/pay/wechat/create — 余额优先,差额微信 */
|
|
export async function createOrderWeChatPay(params: {
|
|
orderIds: number[]
|
|
couponByOrderId?: Record<number, number | null>
|
|
scene: WeChatPayScene
|
|
/** 是否使用账户余额抵扣,默认 true */
|
|
useBalance?: boolean
|
|
oauthCode?: string
|
|
}) {
|
|
const headers: Record<string, string> = {}
|
|
if (params.oauthCode) {
|
|
headers['X-Wechat-OAuth-Code'] = params.oauthCode
|
|
}
|
|
const res = await axiosInstance.post(
|
|
omsUrl('/order/pay/wechat/create'),
|
|
{
|
|
orderIds: params.orderIds,
|
|
couponByOrderId: params.couponByOrderId,
|
|
scene: params.scene,
|
|
useBalance: params.useBalance !== false,
|
|
oauthCode: params.oauthCode,
|
|
},
|
|
{ headers },
|
|
)
|
|
const raw = unwrapData(res.data) ?? res.data
|
|
const js = raw?.jsapi ?? raw?.Jsapi
|
|
const linesRaw = raw?.lines ?? raw?.Lines ?? []
|
|
return {
|
|
payableTotal: Number(raw?.payableTotal ?? raw?.PayableTotal ?? 0),
|
|
balanceAmount: Number(raw?.balanceAmount ?? raw?.BalanceAmount ?? 0),
|
|
wechatAmount: Number(raw?.wechatAmount ?? raw?.WechatAmount ?? 0),
|
|
customerBalance: Number(raw?.customerBalance ?? raw?.CustomerBalance ?? 0),
|
|
paidImmediately: Boolean(raw?.paidImmediately ?? raw?.PaidImmediately),
|
|
outTradeNo: (raw?.outTradeNo ?? raw?.OutTradeNo) || undefined,
|
|
channel: (raw?.channel ?? raw?.Channel) || undefined,
|
|
status: (raw?.status ?? raw?.Status) || undefined,
|
|
codeUrl: (raw?.codeUrl ?? raw?.CodeUrl) || undefined,
|
|
expireTime: (raw?.expireTime ?? raw?.ExpireTime) || undefined,
|
|
jsapi: js
|
|
? {
|
|
appId: String(js.appId ?? js.AppId ?? ''),
|
|
timeStamp: String(js.timeStamp ?? js.TimeStamp ?? ''),
|
|
nonceStr: String(js.nonceStr ?? js.NonceStr ?? ''),
|
|
package: String(js.package ?? js.Package ?? ''),
|
|
signType: String(js.signType ?? js.SignType ?? 'RSA'),
|
|
paySign: String(js.paySign ?? js.PaySign ?? ''),
|
|
}
|
|
: undefined,
|
|
lines: (Array.isArray(linesRaw) ? linesRaw : []).map((row: any) => ({
|
|
orderId: Number(row?.orderId ?? row?.OrderId ?? 0),
|
|
serviceNo: (row?.serviceNo ?? row?.ServiceNo) || undefined,
|
|
payableAmount: Number(row?.payableAmount ?? row?.PayableAmount ?? 0),
|
|
balanceAmount: Number(row?.balanceAmount ?? row?.BalanceAmount ?? 0),
|
|
wechatAmount: Number(row?.wechatAmount ?? row?.WechatAmount ?? 0),
|
|
couponInstanceId: row?.couponInstanceId ?? row?.CouponInstanceId ?? undefined,
|
|
})),
|
|
} as CreateOrderWeChatPayResult
|
|
}
|
|
|
|
export interface WeChatPayMerchantConfig {
|
|
configured: boolean
|
|
enabled: boolean
|
|
mchId: string
|
|
mchSerialNo: string
|
|
platformPublicKeyId: string
|
|
notifyUrl: string
|
|
hasApiV3Key: boolean
|
|
hasPrivateKey: boolean
|
|
hasPlatformPublicKey: boolean
|
|
appId?: string
|
|
}
|
|
|
|
export async function getWeChatPayMerchantConfig(): Promise<WeChatPayMerchantConfig> {
|
|
const res = await axiosInstance.get(omsUrl('/system/other-config/wechat-pay-merchant'))
|
|
const data = unwrapData(res.data) as any
|
|
return {
|
|
configured: Boolean(data?.configured ?? data?.Configured),
|
|
enabled: Boolean(data?.enabled ?? data?.Enabled),
|
|
mchId: String(data?.mchId ?? data?.MchId ?? ''),
|
|
mchSerialNo: String(data?.mchSerialNo ?? data?.MchSerialNo ?? ''),
|
|
platformPublicKeyId: String(data?.platformPublicKeyId ?? data?.PlatformPublicKeyId ?? ''),
|
|
notifyUrl: String(data?.notifyUrl ?? data?.NotifyUrl ?? ''),
|
|
hasApiV3Key: Boolean(data?.hasApiV3Key ?? data?.HasApiV3Key),
|
|
hasPrivateKey: Boolean(data?.hasPrivateKey ?? data?.HasPrivateKey),
|
|
hasPlatformPublicKey: Boolean(data?.hasPlatformPublicKey ?? data?.HasPlatformPublicKey),
|
|
appId: (data?.appId ?? data?.AppId) || undefined,
|
|
}
|
|
}
|
|
|
|
export async function saveWeChatPayMerchantConfig(payload: {
|
|
enabled: boolean
|
|
mchId: string
|
|
mchSerialNo: string
|
|
platformPublicKeyId: string
|
|
notifyUrl?: string
|
|
apiV3Key?: string
|
|
privateKeyPem?: string
|
|
platformPublicKeyPem?: string
|
|
}) {
|
|
const res = await axiosInstance.post(
|
|
omsUrl('/system/other-config/wechat-pay-merchant/save'),
|
|
payload,
|
|
)
|
|
assertBusinessOk(res.data)
|
|
return getWeChatPayMerchantConfig()
|
|
}
|
|
|
|
/** 微信内 JSAPI 调起 */
|
|
export function invokeWeChatJsapiPay(params: WeChatJsapiPayParams): Promise<'ok' | 'cancel' | 'fail'> {
|
|
return invokeWeixinBridge(params)
|
|
}
|
|
|
|
function invokeWeixinBridge(params: WeChatJsapiPayParams): Promise<'ok' | 'cancel' | 'fail'> {
|
|
return new Promise((resolve) => {
|
|
const run = () => {
|
|
const bridge = (window as any).WeixinJSBridge
|
|
if (!bridge?.invoke) {
|
|
resolve('fail')
|
|
return
|
|
}
|
|
bridge.invoke(
|
|
'getBrandWCPayRequest',
|
|
{
|
|
appId: params.appId,
|
|
timeStamp: params.timeStamp,
|
|
nonceStr: params.nonceStr,
|
|
package: params.package,
|
|
signType: params.signType || 'RSA',
|
|
paySign: params.paySign,
|
|
},
|
|
(res: { err_msg?: string }) => {
|
|
const msg = String(res?.err_msg || '')
|
|
if (msg.includes(':ok')) resolve('ok')
|
|
else if (msg.includes(':cancel')) resolve('cancel')
|
|
else resolve('fail')
|
|
},
|
|
)
|
|
}
|
|
|
|
if (typeof (window as any).WeixinJSBridge === 'undefined') {
|
|
document.addEventListener('WeixinJSBridgeReady', run, false)
|
|
setTimeout(() => {
|
|
if (typeof (window as any).WeixinJSBridge === 'undefined') resolve('fail')
|
|
}, 3000)
|
|
} else {
|
|
run()
|
|
}
|
|
})
|
|
}
|
|
|
|
export function isWeChatBrowser() {
|
|
return /MicroMessenger/i.test(navigator.userAgent || '')
|
|
}
|