All files / lib/resources paymentLinks.js

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100                                                                                                                                                                                                       
/**
 * @name PaymentLinks
 * @description This module exposes functions
 *              related to the `/payment_links` path.
 *
 * @module paymentLinks
 **/
 
import { cond, has, T, curry } from 'ramda'
 
import routes from '../routes'
import request from '../request'
 
const findOne = curry((opts, body) =>
  request.get(opts, routes.paymentLinks.details(body.id), {})
)
 
const findAll = curry((opts, pagination) =>
  request.get(opts, routes.paymentLinks.base, pagination || {})
)

/**
 * `GET /payment_links`
 * Makes a request to /payment_links or to /payment_links/:id
 *
 * @param {Object} opts An options params which
 *                      is usually already bound
 *                      by `connect` functions.
 *
 * @param {Object} body The payload for the request.
 * {@link https://docs.pagar.me/v2017-08-28/reference#retornando-links-de-pagamento|API Reference for this payload}
 * @param {Number} [body.id] The paymentLink ID. If not sent a
 *                           paymentLink list will be returned instead.
 * @param {Number} [body.count] Pagination option for paymentLink list.
 *                              Number of paymentLink in a page
 * @param {Number} [body.page] Pagination option for paymentLink list.
 *                             The page index.
*/
const find = (opts, body) =>
  cond([
    [has('id'), findOne(opts)],
    [T, findAll(opts)],
  ])(body)
 
/**
 * `GET /payment_links`
 * Makes a request to /payment_links to get all paymentLinks.
 *
 * @param {Object} opts An options params which
 *                      is usually already bound
 *                      by `connect` functions.
 *
 * @param {Number} [body.count] Pagination option for paymentLink list.
 *                              Number of paymentLink in a page
 * @param {Number} [body.page] Pagination option for paymentLink list.
 *                             The page index.
*/
const all = (opts, body) =>
  findAll(opts, body)
 
/**
 * `POST /payment_links`
 * Creates a paymentLink from the given payload.
 *
 * @param {Object} opts An options params which
 *                      is usually already bound
 *                      by `connect` functions.
 *
 * @param {Object} body The payload for the request.
 * {@link https://docs.pagar.me/v2017-08-28/reference#criando-um-link-de-pagamento-1|API Reference for this payload}
 *
 * @returns {Promise} Resolves to the result of
 *                    the request or to an error.
 */
const create = (opts, body) =>
  request.post(opts, routes.paymentLinks.base, body)
 
/**
 * `POST /payment_links/:id/cancel`
 * Cancels a paymentLink from the given id.
 *
 * @param {Object} opts An options params which
 *                      is usually already bound
 *                      by `connect` functions.
 *
 * @param {Number} body.id The paymentLink ID.
 *
 * @returns {Promise} Resolves to the result of
 *                    the request or to an error.
 */
const cancel = (opts, body) =>
  request.post(opts, routes.paymentLinks.cancel(body.id), body)
 
export default {
  find,
  all,
  create,
  cancel,
}