252 lines
7.2 KiB
TypeScript
252 lines
7.2 KiB
TypeScript
import { randomUUID } from 'node:crypto';
|
|
import { seedPartnerOrder } from './helpers/b2b-partner-order';
|
|
import { secondActor } from './helpers/commerce';
|
|
import { identityApp, type IdentityApp } from './helpers/identity-app';
|
|
|
|
describe('B2B partner partial payments', () => {
|
|
let ctx: IdentityApp;
|
|
const auth = { type: 'bearer' as const };
|
|
const payment = (
|
|
amount: string,
|
|
reference: string = randomUUID(),
|
|
idempotencyKey = randomUUID(),
|
|
) => ({
|
|
amount,
|
|
reference,
|
|
idempotencyKey,
|
|
method: 'BANK_TRANSFER',
|
|
receivedOn: '2026-10-03',
|
|
});
|
|
|
|
beforeAll(async () => {
|
|
ctx = await identityApp();
|
|
}, 60000);
|
|
afterAll(async () => {
|
|
await ctx?.close();
|
|
});
|
|
beforeEach(async () => {
|
|
await ctx.clearLimits();
|
|
});
|
|
|
|
it('records partial receipts, returns exact balances, and rejects duplicate or excessive payments', async () => {
|
|
const { order } = await seedPartnerOrder(ctx);
|
|
const url = `/api/v1/b2b/partner-orders/${order.id}/payments`;
|
|
const input = payment('100.25', ' bank/123 ');
|
|
const first = await ctx
|
|
.api()
|
|
.post(url)
|
|
.auth(ctx.token, auth)
|
|
.send(input)
|
|
.expect(201);
|
|
expect(first.body).toMatchObject({
|
|
amount: '100.25',
|
|
currency: 'INR',
|
|
reference: 'BANK/123',
|
|
});
|
|
expect(
|
|
(await ctx.api().post(url).auth(ctx.token, auth).send(input).expect(201))
|
|
.body.id,
|
|
).toBe(first.body.id);
|
|
expect(
|
|
(
|
|
await ctx
|
|
.api()
|
|
.post(url)
|
|
.auth(ctx.token, auth)
|
|
.send({ ...input, amount: '101.25' })
|
|
.expect(409)
|
|
).body.code,
|
|
).toBe('B2B_PAYMENT_REPLAY_CONFLICT');
|
|
expect(
|
|
(
|
|
await ctx
|
|
.api()
|
|
.post(url)
|
|
.auth(ctx.token, auth)
|
|
.send(payment('1.00', 'BANK/123'))
|
|
.expect(409)
|
|
).body.code,
|
|
).toBe('B2B_PAYMENT_REFERENCE_USED');
|
|
await ctx
|
|
.api()
|
|
.post(url)
|
|
.auth(ctx.token, auth)
|
|
.send(payment('200.00'))
|
|
.expect(201);
|
|
expect(
|
|
(
|
|
await ctx
|
|
.api()
|
|
.post(url)
|
|
.auth(ctx.token, auth)
|
|
.send(payment('3002.76'))
|
|
.expect(409)
|
|
).body.code,
|
|
).toBe('B2B_PAYMENT_AMOUNT_LIMIT');
|
|
const listed = await ctx
|
|
.api()
|
|
.get(`${url}?limit=1&offset=1`)
|
|
.auth(ctx.token, auth)
|
|
.expect(200);
|
|
expect(listed.body).toMatchObject({
|
|
orderId: order.id,
|
|
currency: 'INR',
|
|
merchandiseTotal: '3303.00',
|
|
paidTotal: '300.25',
|
|
outstandingTotal: '3002.75',
|
|
});
|
|
expect(listed.body.payments).toHaveLength(1);
|
|
expect(
|
|
(
|
|
await ctx
|
|
.api()
|
|
.get(`${url}/${first.body.id}`)
|
|
.auth(ctx.token, auth)
|
|
.expect(200)
|
|
).body.amount,
|
|
).toBe('100.25');
|
|
expect(
|
|
await ctx.db.auditEvent.count({
|
|
where: { action: 'b2b_payment.recorded', targetId: first.body.id },
|
|
}),
|
|
).toBe(1);
|
|
});
|
|
|
|
it('blocks cancellation until active receipts are reversed, preserving a retryable audit trail', async () => {
|
|
const { order } = await seedPartnerOrder(ctx);
|
|
const url = `/api/v1/b2b/partner-orders/${order.id}/payments`;
|
|
const input = payment('100.25');
|
|
const recorded = await ctx
|
|
.api()
|
|
.post(url)
|
|
.auth(ctx.token, auth)
|
|
.send(input)
|
|
.expect(201);
|
|
const cancelUrl = `/api/v1/b2b/partner-orders/${order.id}/cancel`;
|
|
expect(
|
|
(
|
|
await ctx
|
|
.api()
|
|
.post(cancelUrl)
|
|
.auth(ctx.token, auth)
|
|
.send({
|
|
reason: 'Partner withdrew order',
|
|
idempotencyKey: randomUUID(),
|
|
})
|
|
.expect(409)
|
|
).body.code,
|
|
).toBe('B2B_PAYMENT_CANCEL_BLOCKED');
|
|
const reverseUrl = `${url}/${recorded.body.id}/reverse`;
|
|
const reversal = {
|
|
reason: 'Bank entry was recorded in error',
|
|
idempotencyKey: randomUUID(),
|
|
};
|
|
const reversed = await ctx
|
|
.api()
|
|
.post(reverseUrl)
|
|
.auth(ctx.token, auth)
|
|
.send(reversal)
|
|
.expect(201);
|
|
expect(reversed.body.reversal.reason).toBe(reversal.reason);
|
|
expect(
|
|
(
|
|
await ctx
|
|
.api()
|
|
.post(reverseUrl)
|
|
.auth(ctx.token, auth)
|
|
.send(reversal)
|
|
.expect(201)
|
|
).body.reversal.paymentId,
|
|
).toBe(recorded.body.id);
|
|
expect(
|
|
(
|
|
await ctx
|
|
.api()
|
|
.post(reverseUrl)
|
|
.auth(ctx.token, auth)
|
|
.send({ ...reversal, reason: 'Different reason' })
|
|
.expect(409)
|
|
).body.code,
|
|
).toBe('B2B_PAYMENT_REVERSE_REPLAY_CONFLICT');
|
|
expect(
|
|
(
|
|
await ctx
|
|
.api()
|
|
.post(reverseUrl)
|
|
.auth(ctx.token, auth)
|
|
.send({ ...reversal, idempotencyKey: randomUUID() })
|
|
.expect(409)
|
|
).body.code,
|
|
).toBe('B2B_PAYMENT_ALREADY_REVERSED');
|
|
expect(
|
|
(await ctx.api().get(url).auth(ctx.token, auth).expect(200)).body,
|
|
).toMatchObject({ paidTotal: '0.00', outstandingTotal: '3303.00' });
|
|
await ctx
|
|
.api()
|
|
.post(cancelUrl)
|
|
.auth(ctx.token, auth)
|
|
.send({ reason: 'Partner withdrew order', idempotencyKey: randomUUID() })
|
|
.expect(201);
|
|
expect(
|
|
(
|
|
await ctx
|
|
.api()
|
|
.post(url)
|
|
.auth(ctx.token, auth)
|
|
.send(payment('1.00'))
|
|
.expect(409)
|
|
).body.code,
|
|
).toBe('B2B_PAYMENT_ORDER_CANCELLED');
|
|
expect(
|
|
await ctx.db.auditEvent.count({
|
|
where: { action: 'b2b_payment.reversed', targetId: recorded.body.id },
|
|
}),
|
|
).toBe(1);
|
|
});
|
|
|
|
it('requires scoped read and manage permissions', async () => {
|
|
const { order } = await seedPartnerOrder(ctx);
|
|
const url = `/api/v1/b2b/partner-orders/${order.id}/payments`;
|
|
const outsider = await secondActor(ctx, false, [
|
|
'b2b.payment.read',
|
|
'b2b.payment.manage',
|
|
]);
|
|
expect(
|
|
(await ctx.api().get(url).auth(outsider.token, auth).expect(404)).body
|
|
.code,
|
|
).toBe('B2B_ORDER_NOT_FOUND');
|
|
const noAccess = await secondActor(ctx);
|
|
await ctx.api().get(url).auth(noAccess.token, auth).expect(403);
|
|
await ctx
|
|
.api()
|
|
.post(url)
|
|
.auth(noAccess.token, auth)
|
|
.send(payment('1.00'))
|
|
.expect(403);
|
|
const readOnly = await secondActor(ctx, true, ['b2b.payment.read']);
|
|
await ctx.api().get(url).auth(readOnly.token, auth).expect(200);
|
|
await ctx
|
|
.api()
|
|
.post(url)
|
|
.auth(readOnly.token, auth)
|
|
.send(payment('1.00'))
|
|
.expect(403);
|
|
});
|
|
|
|
it('serializes competing receipts so only one can use the remaining balance', async () => {
|
|
const { order } = await seedPartnerOrder(ctx);
|
|
const url = `/api/v1/b2b/partner-orders/${order.id}/payments`;
|
|
const results = await Promise.all([
|
|
ctx.api().post(url).auth(ctx.token, auth).send(payment('2000.00')),
|
|
ctx.api().post(url).auth(ctx.token, auth).send(payment('2000.00')),
|
|
]);
|
|
expect(results.map((result) => result.status).sort()).toEqual([201, 409]);
|
|
expect(results.find((result) => result.status === 409)?.body.code).toBe(
|
|
'B2B_PAYMENT_AMOUNT_LIMIT',
|
|
);
|
|
expect(
|
|
(await ctx.api().get(url).auth(ctx.token, auth).expect(200)).body,
|
|
).toMatchObject({ paidTotal: '2000.00', outstandingTotal: '1303.00' });
|
|
});
|
|
});
|