import {z} from 'zod' export type EventVersion = string; export type ResourceVersion = string; export type Resource = {[key: string]: unknown}; export type LinkDescription = {href: string, rel: string, method?: ('GET' | 'POST' | 'PUT' | 'DELETE' | 'HEAD' | 'CONNECT' | 'OPTIONS' | 'PATCH') | undefined}; export type LinkDescriptionList = Array; export type Event = {id?: string | undefined, create_time?: string | undefined, resource_type?: string | undefined, event_version?: EventVersion | undefined, event_type?: string | undefined, summary?: string | undefined, resource_version?: ResourceVersion | undefined, resource?: Resource | undefined, links?: LinkDescriptionList | undefined}; export type VerifyWebhookSignature = {auth_algo: string, cert_url: string, transmission_id: string, transmission_sig: string, transmission_time: string, webhook_id: string, webhook_event: Event}; export type CreateApiV1NotificationsVerifyWebhookSignatureArgs = {body: VerifyWebhookSignature}; export type CreateApiV1NotificationsVerifyWebhookSignatureOptions = {baseUrl?: string | undefined, Oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const verifyWebhookSignatureResponse = z.object({verification_status: z.enum(["SUCCESS", "FAILURE"])}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Verify webhook signature * * POST /v1/notifications/verify-webhook-signature */ export const createApiV1NotificationsVerifyWebhookSignature = async (args: CreateApiV1NotificationsVerifyWebhookSignatureArgs, options?: CreateApiV1NotificationsVerifyWebhookSignatureOptions) => { const {baseUrl = "https://api-m.paypal.com", fetch: fetchFn = fetch, Oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Oauth2 !== undefined) headers.set('authorization', `Bearer ${Oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/notifications/verify-webhook-signature${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(verifyWebhookSignatureResponse, await res.json()) };