import test from 'node:test'; import assert from 'node:assert/strict'; import { formatPayPalUSD, getPayPalReturnState, getPayPalQuoteRemainingSeconds, isPayPalQuoteExpired, isPayPalAvailable, mergePayPalFeePolicyFormValues, parsePayPalQuote, removePayPalReturnState, requestPayPalOrder, } from './paypalPayment.js'; test('merges fee policy without clearing saved PayPal settings', () => { assert.deepEqual( mergePayPalFeePolicyFormValues( { PayPalClientID: 'client-id', PayPalWebhookID: 'webhook-id', PayPalSandbox: true, PayPalFXMode: 'realtime', }, { user_pays_fee: true, rate_bps: 440 }, ), { PayPalClientID: 'client-id', PayPalWebhookID: 'webhook-id', PayPalSandbox: true, PayPalFXMode: 'realtime', PayPalUserPaysFee: true, PayPalFeeRatePercent: 4.4, }, ); }); test('parses a PayPal USD quote', () => { assert.deepEqual( parsePayPalQuote({ data: { data: { currency: 'USD', base_pay_amount: '95.60', service_fee: '4.40', pay_amount: '100.00', fee_payer: 'user', fee_rate_bps: 440, fee_policy_version: 3, cny_pay_amount: '730.00', exchange_rate: '7.3', quota: 50000000, market_exchange_rate: '7.28', exchange_rate_source: 'open_exchange_rates', exchange_rate_quoted_at: 1790236800, quote_expires_at: 1790237400, quote_token: 'v2.payload.signature', }, }, }), { currency: 'USD', basePayAmount: 95.6, serviceFee: 4.4, payAmount: 100, feePayer: 'user', feeRateBps: 440, feePolicyVersion: 3, cnyPayAmount: 730, exchangeRate: 7.3, quota: 50000000, marketExchangeRate: 7.28, exchangeRateSource: 'open_exchange_rates', exchangeRateQuotedAt: 1790236800, quoteExpiresAt: 1790237400, quoteToken: 'v2.payload.signature', }, ); }); test('rejects malformed or non-USD quotes', () => { assert.throws(() => parsePayPalQuote({ currency: 'CNY' })); assert.throws(() => parsePayPalQuote({ currency: 'USD', pay_amount: 'NaN', cny_pay_amount: '10', exchange_rate: '7.3', }), ); }); test('formats USD and handles return state once', () => { assert.equal(formatPayPalUSD('12.3'), '$12.30 USD'); assert.deepEqual(getPayPalReturnState('?foo=1&paypal=cancelled'), { status: 'cancelled', type: 'info', message: '已取消 PayPal 支付', }); assert.equal( removePayPalReturnState('/recharge?foo=1&paypal=success#top'), '/recharge?foo=1#top', ); }); test('PayPal is selectable only when enabled and present', () => { assert.equal(isPayPalAvailable(true, [{ type: 'paypal' }]), true); assert.equal(isPayPalAvailable(false, [{ type: 'paypal' }]), false); assert.equal(isPayPalAvailable(true, [{ type: 'stripe' }]), false); }); test('calculates quote expiry without going negative', () => { const quote = { quoteExpiresAt: 1010 }; assert.equal(getPayPalQuoteRemainingSeconds(quote, 1000_000), 10); assert.equal(isPayPalQuoteExpired(quote, 1009_000), false); assert.equal(isPayPalQuoteExpired(quote, 1010_000), true); assert.equal(getPayPalQuoteRemainingSeconds(quote, 1020_000), 0); }); test('retries an in-progress PayPal create with the same quote token', async () => { const bodies = []; const api = { post: async (_endpoint, body) => { bodies.push(body); if (bodies.length < 3) { return { status: 202, data: { success: true, message: 'PAYPAL_ORDER_CREATING' }, }; } return { status: 200, data: { success: true, data: {} } }; }, }; const body = { amount_option_id: 1, quote_token: 'signed-token' }; const response = await requestPayPalOrder(api, '/paypal', body, undefined, 3); assert.equal(response.status, 200); assert.equal(bodies.length, 3); assert.equal( bodies.every((item) => item.quote_token === 'signed-token'), true, ); });