diff --git a/README.md b/README.md index 0f427b6..f5e4149 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,40 @@ npm install bold-api-sdk ### Payment Link +## Get + +```js +import { paymentLink } from 'bold-api-sdk'; + +const response = await paymentLink.get('bold-identity-key', 'LNK_EX95SG8SZT'); + +console.log(response); +``` + +#### Response + +```json +{ + "id": "LNK_EX95SG8SZT", + "total": 0, + "status": "ACTIVE", + "expiration_date": 1736984191779000000, + "description": null, + "api_version": 1, + "subtotal": 0, + "tip_amount": 0, + "taxes": [], + "creation_date": 1736982392918743600, + "payment_method": null, + "transaction_id": null, + "amount_type": "OPEN", + "is_sandbox": true, + "callback_url": null +} +``` + +## Create + ```js import { paymentLink } from 'bold-api-sdk'; diff --git a/src/app.d.ts b/src/app.d.ts index 426fab7..72cb6ab 100644 --- a/src/app.d.ts +++ b/src/app.d.ts @@ -1,6 +1,8 @@ declare module 'bold-api-sdk' { + export type AmountType = 'OPEN' | 'CLOSE'; + export interface Order { - amountType?: 'OPEN' | 'CLOSE'; + amountType?: AmountType; description?: string; payerEmail?: string; amount?: number; @@ -37,9 +39,34 @@ declare module 'bold-api-sdk' { errors: BoldError[]; } + export interface Tax { + type: string; + base: number; + value: number; + } + + export interface GetLinkResponse { + id: string; + total: number; + status: 'ACTIVE' | 'PROCESSING' | 'PAID' | 'EXPIRED'; + expiration_date: number; + description: string | null; + api_version: number; + subtotal: number; + tip_amount: number; + taxes: Tax[]; + creation_date: number; + payment_method: string | null; + transaction_id: string | null; + amount_type: AmountType; + is_sandbox: boolean; + callback_url: string | null; + } + export type BoldApiKey = string | undefined | null; export const paymentLink: { + get: (apiKey: BoldApiKey, id: string) => Promise; create: ( apiKey: BoldApiKey, order: Order, diff --git a/src/domain/payment_link.js b/src/domain/payment_link.js index e144ff4..d6a285b 100644 --- a/src/domain/payment_link.js +++ b/src/domain/payment_link.js @@ -1,6 +1,7 @@ const { paymentLinkService } = require('../services'); const paymentLink = { + get: async (apiKey, id) => paymentLinkService.get(apiKey, id), create: async (apiKey, order) => paymentLinkService.create(apiKey, order), }; diff --git a/src/services/payment_link.js b/src/services/payment_link.js index d0ac768..a73959b 100644 --- a/src/services/payment_link.js +++ b/src/services/payment_link.js @@ -1,5 +1,13 @@ const { httpClient } = require('../shared'); +const URL = '/online/link/v1'; + +exports.get = async (apiKey, id) => + httpClient.fetch({ + endpoint: `${URL}/${id}`, + apiKey, + }); + exports.create = async ( apiKey, { @@ -51,7 +59,7 @@ exports.create = async ( : {}; return httpClient.fetch({ - endpoint: '/online/link/v1', + endpoint: URL, apiKey, options: { method: 'POST', diff --git a/tests/domain/payment_link.test.js b/tests/domain/payment_link.test.js index 4fb3097..555bbfc 100644 --- a/tests/domain/payment_link.test.js +++ b/tests/domain/payment_link.test.js @@ -2,6 +2,26 @@ const paymentLink = require('../../src/domain/payment_link'); describe('paymentLink', () => { const PAYMENT_LINK = 'bold.co/payment'; + const apiKey = process.env.BOLD_API_KEY || 'bold-api-key'; + + describe('get', () => { + it('should return a payment link', async () => { + const { payload } = await paymentLink.create(); + const { payment_link: id } = payload; + + const { status } = await paymentLink.get(apiKey, id); + + expect(status).toBe('ACTIVE'); + }); + + it('should return an error if payment link does not exist', async () => { + const invalidId = 'non-existent-id'; + const { errors } = await paymentLink.get(apiKey, invalidId); + + expect(errors).toHaveLength(1); + expect(errors[0].errors).toBe(`Link ${invalidId} not valid`); + }); + }); describe('create', () => { const amountType = 'CLOSE'; @@ -11,7 +31,6 @@ describe('paymentLink', () => { const callbackUrl = 'https://example.org/return'; const expirationMinutes = 30; const currency = 'COP'; - const apiKey = process.env.BOLD_API_KEY || 'bold-api-key'; beforeEach(() => { process.env.BOLD_API_URL = 'https://integrations.api.bold.co';