Initial Commit
This commit is contained in:
111
packages/money/payment/payment-provider-paypal/tests/fixtures/paypal-recorder.ts
vendored
Normal file
111
packages/money/payment/payment-provider-paypal/tests/fixtures/paypal-recorder.ts
vendored
Normal file
@@ -0,0 +1,111 @@
|
||||
import type { FetchLike } from "@suaveplan/payment-providers";
|
||||
import { PayPalProvider } from "../../src/paypal/paypal.js";
|
||||
|
||||
export interface Recorded {
|
||||
readonly method: string;
|
||||
readonly url: string;
|
||||
readonly headers: Record<string, string>;
|
||||
readonly body: string | undefined;
|
||||
}
|
||||
|
||||
export interface Recorder {
|
||||
readonly fetch: FetchLike;
|
||||
readonly calls: Recorded[];
|
||||
}
|
||||
|
||||
export function recorder(
|
||||
handler: (recorded: Recorded) => Response | Promise<Response>
|
||||
): Recorder {
|
||||
const calls: Recorded[] = [];
|
||||
const fetchImpl: FetchLike = async (url, init) => {
|
||||
const headers: Record<string, string> = {};
|
||||
const raw = init?.headers as Record<string, string> | undefined;
|
||||
if (raw !== undefined) {
|
||||
for (const [k, v] of Object.entries(raw)) {
|
||||
headers[k] = v;
|
||||
}
|
||||
}
|
||||
const body = init?.body;
|
||||
const bodyStr =
|
||||
typeof body === "string"
|
||||
? body
|
||||
: body === undefined
|
||||
? undefined
|
||||
: String(body);
|
||||
const recorded: Recorded = {
|
||||
method: init?.method ?? "GET",
|
||||
url,
|
||||
headers,
|
||||
body: bodyStr,
|
||||
};
|
||||
calls.push(recorded);
|
||||
return handler(recorded);
|
||||
};
|
||||
return { fetch: fetchImpl, calls };
|
||||
}
|
||||
|
||||
export function jsonResponse(data: unknown, status = 200): Response {
|
||||
return new Response(JSON.stringify(data), {
|
||||
status,
|
||||
headers: { "content-type": "application/json" },
|
||||
});
|
||||
}
|
||||
|
||||
export const TOKEN_RESPONSE = {
|
||||
access_token: "tok_abc",
|
||||
expires_in: 3600,
|
||||
};
|
||||
export const TOKEN_RESPONSE_REFRESH = {
|
||||
access_token: "tok_xyz",
|
||||
expires_in: 3600,
|
||||
};
|
||||
|
||||
export function baseOrder(): Record<string, unknown> {
|
||||
return {
|
||||
id: "ORDER_123",
|
||||
status: "CREATED",
|
||||
intent: "CAPTURE",
|
||||
purchase_units: [
|
||||
{
|
||||
amount: { currency_code: "USD", value: "50.00" },
|
||||
description: "Test order",
|
||||
payments: { captures: [], refunds: [] },
|
||||
},
|
||||
],
|
||||
create_time: "2026-01-01T00:00:00Z",
|
||||
update_time: "2026-01-01T00:00:00Z",
|
||||
};
|
||||
}
|
||||
|
||||
export function baseRefund(): Record<string, unknown> {
|
||||
return {
|
||||
id: "REFUND_123",
|
||||
amount: { value: "20.00", currency_code: "USD" },
|
||||
status: "COMPLETED",
|
||||
create_time: "2026-01-02T00:00:00Z",
|
||||
};
|
||||
}
|
||||
|
||||
export interface MakeArgs {
|
||||
readonly fetch: FetchLike;
|
||||
readonly now?: () => number;
|
||||
readonly base64Encode?: (input: string) => string;
|
||||
readonly baseUrl?: string;
|
||||
readonly sandbox?: boolean;
|
||||
}
|
||||
|
||||
export function make(args: MakeArgs): PayPalProvider {
|
||||
return new PayPalProvider({
|
||||
clientId: "client_x",
|
||||
clientSecret: "secret_y",
|
||||
fetch: args.fetch,
|
||||
...(args.now !== undefined ? { now: args.now } : {}),
|
||||
...(args.base64Encode !== undefined
|
||||
? { base64Encode: args.base64Encode }
|
||||
: {}),
|
||||
...(args.baseUrl !== undefined ? { baseUrl: args.baseUrl } : {}),
|
||||
...(args.sandbox !== undefined ? { sandbox: args.sandbox } : {}),
|
||||
});
|
||||
}
|
||||
|
||||
export const PROD = "https://api-m.paypal.com";
|
||||
Reference in New Issue
Block a user