feat: 支付商户
This commit is contained in:
108
src/utils/business/paymentMerchantPool.ts
Normal file
108
src/utils/business/paymentMerchantPool.ts
Normal file
@@ -0,0 +1,108 @@
|
||||
import type {
|
||||
PaymentCredentialEntry,
|
||||
PaymentCredentials,
|
||||
PaymentMerchant,
|
||||
PaymentMerchantMethod,
|
||||
PaymentMerchantPoolPayload,
|
||||
PaymentPoolStrategy
|
||||
} from '@/types/api/paymentMerchantPools'
|
||||
|
||||
export const PAYMENT_NO_AVAILABLE_MERCHANT_MESSAGE = '暂无可用商户'
|
||||
export const PAYMENT_FAILED_REISSUE_MESSAGE = '支付失败,请重新发起支付'
|
||||
|
||||
export interface PaymentPoolFormModel {
|
||||
name: string
|
||||
payment_method: PaymentMerchantMethod
|
||||
member_ids: number[]
|
||||
enabled: boolean
|
||||
strategy: PaymentPoolStrategy
|
||||
statistic_cycle: PaymentMerchantPoolPayload['statistic_cycle']
|
||||
threshold_amount_yuan: number | undefined
|
||||
threshold_count: number | undefined
|
||||
time_period_started_at: string
|
||||
time_period_unit: PaymentMerchantPoolPayload['time_period_unit']
|
||||
time_period_value: number | undefined
|
||||
remark: string
|
||||
}
|
||||
|
||||
export const isPlatformUserType = (userType?: number | string | null): boolean =>
|
||||
[1, 2].includes(Number(userType))
|
||||
|
||||
export const credentialEntriesToObject = (
|
||||
entries: PaymentCredentialEntry[]
|
||||
): PaymentCredentials => {
|
||||
return entries.reduce<PaymentCredentials>((credentials, entry) => {
|
||||
const key = entry.key.trim()
|
||||
if (!key || !entry.value) return credentials
|
||||
credentials[key] = entry.value
|
||||
return credentials
|
||||
}, {})
|
||||
}
|
||||
|
||||
export const hasCredentialEntries = (entries: PaymentCredentialEntry[]): boolean =>
|
||||
entries.some((entry) => entry.key.trim() && entry.value)
|
||||
|
||||
export const sortMerchantsByMemberIds = (
|
||||
merchants: PaymentMerchant[],
|
||||
memberIds: number[]
|
||||
): PaymentMerchant[] => {
|
||||
const merchantMap = new Map(merchants.map((merchant) => [Number(merchant.id), merchant]))
|
||||
return memberIds.map((id) => merchantMap.get(Number(id))).filter(Boolean) as PaymentMerchant[]
|
||||
}
|
||||
|
||||
export const yuanToFen = (value?: number | null): number | undefined => {
|
||||
if (value === undefined || value === null || Number.isNaN(Number(value))) return undefined
|
||||
return Math.round(Number(value) * 100)
|
||||
}
|
||||
|
||||
export const buildPaymentPoolPayload = (form: PaymentPoolFormModel): PaymentMerchantPoolPayload => {
|
||||
const payload: PaymentMerchantPoolPayload = {
|
||||
name: form.name.trim(),
|
||||
payment_method: form.payment_method,
|
||||
member_ids: [...form.member_ids],
|
||||
enabled: form.enabled,
|
||||
strategy: form.strategy,
|
||||
remark: form.remark.trim()
|
||||
}
|
||||
|
||||
if (form.strategy === 'amount') {
|
||||
payload.statistic_cycle = form.statistic_cycle
|
||||
payload.threshold_amount = yuanToFen(form.threshold_amount_yuan)
|
||||
} else if (form.strategy === 'count') {
|
||||
payload.statistic_cycle = form.statistic_cycle
|
||||
payload.threshold_count = form.threshold_count
|
||||
} else {
|
||||
payload.time_period_started_at = form.time_period_started_at
|
||||
payload.time_period_unit = form.time_period_unit
|
||||
payload.time_period_value = form.time_period_value
|
||||
}
|
||||
|
||||
return payload
|
||||
}
|
||||
|
||||
export const resolvePaymentFailureMessage = (
|
||||
errorCode: string | number | undefined | null,
|
||||
noAvailableMerchantCode: string | number | undefined | null
|
||||
): string => {
|
||||
if (
|
||||
noAvailableMerchantCode !== undefined &&
|
||||
noAvailableMerchantCode !== null &&
|
||||
errorCode !== undefined &&
|
||||
errorCode !== null &&
|
||||
String(errorCode) === String(noAvailableMerchantCode)
|
||||
) {
|
||||
return PAYMENT_NO_AVAILABLE_MERCHANT_MESSAGE
|
||||
}
|
||||
|
||||
return PAYMENT_FAILED_REISSUE_MESSAGE
|
||||
}
|
||||
|
||||
export const isNoAvailableMerchantError = (
|
||||
errorCode: string | number | undefined | null,
|
||||
noAvailableMerchantCode: string | number | undefined | null
|
||||
): boolean =>
|
||||
noAvailableMerchantCode !== undefined &&
|
||||
noAvailableMerchantCode !== null &&
|
||||
errorCode !== undefined &&
|
||||
errorCode !== null &&
|
||||
String(errorCode) === String(noAvailableMerchantCode)
|
||||
Reference in New Issue
Block a user