2aa9d73325
集中保留原版权声明,移除文件头检查和全局测试忽略规则。验证:全量单测、受影响用例复测及静态检查通过。
118 lines
3.7 KiB
JavaScript
118 lines
3.7 KiB
JavaScript
export const PAYPAL_RETURN_STATES = {
|
|
success: { type: 'success', message: 'PayPal 充值成功' },
|
|
pending: { type: 'info', message: 'PayPal 支付处理中,请稍后查看充值记录' },
|
|
error: { type: 'error', message: 'PayPal 支付确认失败,请稍后重试' },
|
|
cancelled: { type: 'info', message: '已取消 PayPal 支付' },
|
|
};
|
|
|
|
export function parsePayPalQuote(payload) {
|
|
const data = payload?.data?.data ?? payload?.data ?? payload;
|
|
if (!data || data.currency !== 'USD') {
|
|
throw new Error('Invalid PayPal quote currency');
|
|
}
|
|
const payAmount = Number(data.pay_amount);
|
|
const basePayAmount = Number(data.base_pay_amount);
|
|
const serviceFee = Number(data.service_fee);
|
|
const cnyPayAmount = Number(data.cny_pay_amount);
|
|
const exchangeRate = Number(data.exchange_rate);
|
|
const quoteExpiresAt = Number(data.quote_expires_at);
|
|
if (
|
|
!Number.isFinite(payAmount) ||
|
|
payAmount < 0.01 ||
|
|
!Number.isFinite(basePayAmount) ||
|
|
basePayAmount < 0.01 ||
|
|
!Number.isFinite(serviceFee) ||
|
|
serviceFee < 0 ||
|
|
Math.abs(basePayAmount + serviceFee - payAmount) > 0.001 ||
|
|
!Number.isFinite(cnyPayAmount) ||
|
|
!Number.isFinite(exchangeRate) ||
|
|
exchangeRate <= 0 ||
|
|
typeof data.quote_token !== 'string' ||
|
|
data.quote_token.length === 0 ||
|
|
!Number.isFinite(quoteExpiresAt)
|
|
) {
|
|
throw new Error('Invalid PayPal quote amount');
|
|
}
|
|
return {
|
|
currency: 'USD',
|
|
basePayAmount,
|
|
serviceFee,
|
|
payAmount,
|
|
feePayer: data.fee_payer === 'user' ? 'user' : 'platform',
|
|
feeRateBps: Number(data.fee_rate_bps) || 0,
|
|
feePolicyVersion: Number(data.fee_policy_version) || 0,
|
|
cnyPayAmount,
|
|
exchangeRate,
|
|
quota: Number(data.quota) || 0,
|
|
marketExchangeRate: Number(data.market_exchange_rate) || exchangeRate,
|
|
exchangeRateSource: data.exchange_rate_source || 'manual',
|
|
exchangeRateQuotedAt: Number(data.exchange_rate_quoted_at) || 0,
|
|
quoteExpiresAt,
|
|
quoteToken: data.quote_token,
|
|
};
|
|
}
|
|
|
|
export function isPayPalQuoteExpired(quote, now = Date.now()) {
|
|
return !quote?.quoteExpiresAt || quote.quoteExpiresAt * 1000 <= now;
|
|
}
|
|
|
|
export function getPayPalQuoteRemainingSeconds(quote, now = Date.now()) {
|
|
if (!quote?.quoteExpiresAt) return 0;
|
|
return Math.max(0, Math.ceil(quote.quoteExpiresAt - now / 1000));
|
|
}
|
|
|
|
export function formatPayPalUSD(amount) {
|
|
const value = Number(amount);
|
|
return `$${Number.isFinite(value) ? value.toFixed(2) : '0.00'} USD`;
|
|
}
|
|
|
|
export function getPayPalReturnState(search) {
|
|
const status = new URLSearchParams(search).get('paypal');
|
|
return status && PAYPAL_RETURN_STATES[status]
|
|
? { status, ...PAYPAL_RETURN_STATES[status] }
|
|
: null;
|
|
}
|
|
|
|
export function removePayPalReturnState(url) {
|
|
const next = new URL(url, 'https://local.invalid');
|
|
next.searchParams.delete('paypal');
|
|
return `${next.pathname}${next.search}${next.hash}`;
|
|
}
|
|
|
|
export function isPayPalAvailable(enabled, methods) {
|
|
return Boolean(
|
|
enabled && methods?.some((method) => method.type === 'paypal'),
|
|
);
|
|
}
|
|
|
|
export function mergePayPalFeePolicyFormValues(currentValues, policy) {
|
|
return {
|
|
...(currentValues || {}),
|
|
PayPalUserPaysFee: Boolean(policy?.user_pays_fee),
|
|
PayPalFeeRatePercent: Number(policy?.rate_bps || 0) / 100,
|
|
};
|
|
}
|
|
|
|
export async function requestPayPalOrder(
|
|
api,
|
|
endpoint,
|
|
body,
|
|
config,
|
|
maxAttempts = 3,
|
|
) {
|
|
let response;
|
|
for (let attempt = 0; attempt < maxAttempts; attempt += 1) {
|
|
response = await api.post(endpoint, body, config);
|
|
if (
|
|
response.status !== 202 ||
|
|
response.data?.message !== 'PAYPAL_ORDER_CREATING'
|
|
) {
|
|
return response;
|
|
}
|
|
if (attempt + 1 < maxAttempts) {
|
|
await new Promise((resolve) => setTimeout(resolve, 800));
|
|
}
|
|
}
|
|
return response;
|
|
}
|