92 lines
2.8 KiB
JavaScript
92 lines
2.8 KiB
JavaScript
import {
|
|
assertVipPaymentMethod,
|
|
normalizeVipCapability,
|
|
normalizeVipOrders,
|
|
normalizeVipPackages,
|
|
normalizeVipPaymentOrder,
|
|
normalizeVipPaymentStatus
|
|
} from './vip-contract.js'
|
|
import {
|
|
createRequestError,
|
|
requestStrict
|
|
} from './request-client.js'
|
|
|
|
const assertVipId = (value, field) => {
|
|
const id = value === undefined || value === null ? '' : String(value)
|
|
if (!/^[1-9]\d*$/.test(id)) {
|
|
throw createRequestError(`${field}无效`, 'VIP_REQUEST_INVALID')
|
|
}
|
|
return id
|
|
}
|
|
|
|
export const vipApi = {
|
|
async getVipCapability(requestOptions = {}) {
|
|
const capability = await requestStrict({
|
|
url: '/genealogy/app/vip/capability',
|
|
method: 'GET'
|
|
}, {
|
|
requestController: requestOptions.requestController ?? null
|
|
})
|
|
return normalizeVipCapability(capability)
|
|
},
|
|
|
|
async getVipPackages(requestOptions = {}) {
|
|
const packages = await requestStrict({
|
|
url: '/genealogy/app/vip/packages',
|
|
method: 'GET'
|
|
}, {
|
|
requestController: requestOptions.requestController ?? null
|
|
})
|
|
return normalizeVipPackages(packages)
|
|
},
|
|
|
|
async getVipOrders(requestOptions = {}) {
|
|
const orders = await requestStrict({
|
|
url: '/genealogy/app/vip/orders',
|
|
method: 'GET'
|
|
}, {
|
|
requestController: requestOptions.requestController ?? null
|
|
})
|
|
return normalizeVipOrders(orders)
|
|
},
|
|
|
|
async createVipOrder(packageId, genealogyId = '', paymentMethod, requestId, requestOptions = {}) {
|
|
if (typeof requestId !== 'string' || !requestId.trim() || requestId.length > 64) {
|
|
throw createRequestError('VIP 下单请求号无效', 'VIP_REQUEST_INVALID')
|
|
}
|
|
const order = await requestStrict({
|
|
url: '/genealogy/app/vip/orders',
|
|
method: 'POST',
|
|
data: {
|
|
packageId: assertVipId(packageId, 'VIP 套餐标识'),
|
|
...(genealogyId ? { genealogyId: assertVipId(genealogyId, '家谱标识') } : {}),
|
|
paymentMethod: assertVipPaymentMethod(paymentMethod),
|
|
requestId: requestId.trim()
|
|
}
|
|
}, {
|
|
requestController: requestOptions.requestController ?? null
|
|
})
|
|
return normalizeVipPaymentOrder(order, paymentMethod)
|
|
},
|
|
|
|
async getVipPaymentStatus(transactionId, requestOptions = {}) {
|
|
const status = await requestStrict({
|
|
url: `/genealogy/app/vip/orders/${assertVipId(transactionId, '支付流水标识')}/payment`,
|
|
method: 'GET'
|
|
}, {
|
|
requestController: requestOptions.requestController ?? null
|
|
})
|
|
return normalizeVipPaymentStatus(status)
|
|
},
|
|
|
|
async closeVipPayment(transactionId, requestOptions = {}) {
|
|
const status = await requestStrict({
|
|
url: `/genealogy/app/vip/orders/${assertVipId(transactionId, '支付流水标识')}/close`,
|
|
method: 'POST'
|
|
}, {
|
|
requestController: requestOptions.requestController ?? null
|
|
})
|
|
return normalizeVipPaymentStatus(status)
|
|
}
|
|
}
|