Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions packages/cli/src/__tests__/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1041,7 +1041,7 @@ describe('production mode', () => {
expect(merchantRequests[1].headers.authorization).toMatch(/^Payment /);
});

it('exits 1 when the paid retry fails', async () => {
it('returns structured response when the paid retry fails', async () => {
setNextResponse(200, APPROVED_SPT_REQUEST);
setMerchantResponse(402, '{"error":"payment required"}', {
'www-authenticate': WWW_AUTHENTICATE_STRIPE,
Expand All @@ -1058,12 +1058,14 @@ describe('production mode', () => {
'json',
);

expect(result.exitCode).toBe(1);
const err = parseJson(result.stdout) as { message: string };
expect(err.message).toContain(
'Payment submission failed with status 401',
);
expect(err.message).toContain('spt rejected');
expect(result.exitCode).toBe(0);
const parsed = parseJson(result.stdout) as {
status: number;
headers: Record<string, string>;
body: string;
};
expect(parsed.status).toBe(401);
expect(parsed.body).toContain('spt rejected');
expect(merchantRequests).toHaveLength(2);
});

Expand Down
30 changes: 14 additions & 16 deletions packages/cli/src/commands/mpp/pay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,9 @@ function buildHeaders(
return result;
}

async function readPayResult(
response: Response,
options?: { failOnError?: boolean },
): Promise<PayResult> {
async function readPayResult(response: Response): Promise<PayResult> {
const responseHeaders = Object.fromEntries(response.headers.entries());
const body = await response.text();

if (options?.failOnError && !response.ok) {
throw new Error(
`Payment submission failed with status ${response.status}: ${body}`,
);
}

return { status: response.status, headers: responseHeaders, body };
}

Expand Down Expand Up @@ -139,7 +129,7 @@ export async function runMppPay(
},
});

return readPayResult(retryResponse, { failOnError: true });
return readPayResult(retryResponse);
}

type Step = 'retrieving' | 'probing' | 'signing' | 'submitting' | 'done';
Expand Down Expand Up @@ -226,9 +216,7 @@ export function MppPay({
},
});

const payResult = await readPayResult(retryResponse, {
failOnError: true,
});
const payResult = await readPayResult(retryResponse);
setResult(payResult);
setStep('done');
onComplete(payResult);
Expand Down Expand Up @@ -262,7 +250,17 @@ export function MppPay({
)}
{result && (
<Box flexDirection="column">
<Text color="green">HTTP {result.status}</Text>
<Text
color={
result.status >= 400
? 'red'
: result.status >= 300
? 'yellow'
: 'green'
}
>
HTTP {result.status}
</Text>
<Text>{result.body}</Text>
</Box>
)}
Expand Down
Loading