UNPKG

yookassa-sdk-node

Version:
82 lines (67 loc) 2.19 kB
import createClient, { Client } from 'openapi-fetch'; import type { paths, components, operations } from './openapi'; export type Schemas = components['schemas']; export type CreatePaymentBody = components['schemas']['CreatePaymentRequest']; export type GetPaymentListQuery = operations['get-payment-list']['parameters']['query']; export type CreateInvoiceBody = components['schemas']['CreateInvoiceRequest']; export type WebhookEvent = components['schemas']['WebhookEvent']; interface SDKOptions { shopId: string; secretKey: string; /** * RequestInit extension object to pass as 2nd argument to fetch when supported (defaults to * undefined) */ requestInitExt?: Record<string, unknown>; } export class YooKassaSDK { constructor({ shopId, secretKey, requestInitExt }: SDKOptions) { this.client = createClient<paths>({ baseUrl: 'https://api.yookassa.ru/v3/', requestInitExt }); this.client.use({ onRequest({ request }) { const credentials = btoa(`${shopId}:${secretKey}`); request.headers.set('Authorization', 'Basic ' + credentials); }, }); } private readonly client: Client<paths>; createPayment({ body, idempotenceKey }: { body: CreatePaymentBody; idempotenceKey: string }) { return this.client.POST('/payments', { params: { header: { ['Idempotence-Key']: idempotenceKey, }, }, body, }); } getPaymentList({ query }: { query: GetPaymentListQuery }) { return this.client.GET('/payments', { params: { query }, }); } getPayment({ payment_id }: { payment_id: string }) { return this.client.GET('/payments/{payment_id}', { params: { path: { payment_id }, }, }); } createInvoice({ body, idempotenceKey }: { body: CreateInvoiceBody; idempotenceKey: string }) { return this.client.POST('/invoices', { params: { header: { ['Idempotence-Key']: idempotenceKey, }, }, body, }); } getInvoice({ invoice_id }: { invoice_id: string }) { return this.client.GET('/invoices/{invoice_id}', { params: { path: { invoice_id }, }, }); } }