All files / lib/resources subscriptions.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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175                                                                                                                                                                                                                                                                                                                                                             
/**
 * @name Subscriptions
 * @description This module exposes functions
 *              related to the `/subscriptions` path.
 *
 * @module subscriptions
 **/
 
import { cond, has, T, curry, omit } from 'ramda'
 
import routes from '../routes'
import request from '../request'
 
const findOne = curry((opts, body) =>
  request.get(opts, routes.subscriptions.details(body.id), {})
)
 
const findAll = curry((opts, pagination) =>
  request.get(opts, routes.subscriptions.base, pagination || {})
)

/**
 * `GET /subscriptions`
 * Makes a request to /subscriptions or to /subscriptions/: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://pagarme.readme.io/v1/reference#retornando-uma-assinatura|API Reference for this payload}
 * @param {Number} [body.id] The subscription's ID. If not sent a
 *                           subscriptions list will be returned instead.
 * @param {Number} [body.count] Pagination option to get a list of subscriptions.
 *                              Number of subscriptions in a page
 * @param {Number} [body.page] Pagination option for a list of subscriptions.
 *                             The page index.
*/
const find = (opts, body) =>
  cond([
    [has('id'), findOne(opts)],
    [T, findAll(opts)],
  ])(body)
 
/**
 * `GET /subscriptions`
 * Makes a request to /subscriptions to get all subscriptions.
 *
 * @param {Object} opts An options params which
 *                      is usually already bound
 *                      by `connect` functions.
 *
 * @param {Number} [body.count] Pagination option to get a list of subscriptions.
 *                              Number of subscriptions in a page
 * @param {Number} [body.page] Pagination option for a list of subscriptions.
 *                             The page index.
*/
const all = (opts, body) =>
  findAll(opts, body)
 
/**
 * `POST /subscriptions`
 * Creates a subscription 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://pagarme.readme.io/v1/reference#criando-assinaturas|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.subscriptions.base, body)
 
/**
 * `PUT /subscriptions/:id`
 * Updates a subscription 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://pagarme.readme.io/v1/reference#atualizando-uma-assinatura|API Reference for this payload}
 * @param {Number} body.id The subscription's ID
 * @returns {Promise} A promise that resolves to
 *                    the newly created subscriptions's
 *                    data or to an error.
 */
const update = (opts, body) =>
  request.put(opts, routes.subscriptions.details(body.id), body)
 
/**
 * `POST /subscriptions/:id/cancel`
 * Cancels a subscription
 *
 * @param {Object} opts An options params which
 *                      is usually already bound
 *                      by `connect` functions.
 * @param {Object} body The payload for the request
 * {@link https://pagarme.readme.io/v1/reference#cancelando-uma-assinatura|API Reference for this payload}
 * @param {Number} body.id The subscription's ID
 *
 * @returns {Promise} Resolves to the result of
 *                    the request or to an error.
 */
const cancel = (opts, body) =>
  request.post(opts, routes.subscriptions.cancel(body.id), {})
 
/**
 * `POST /subscriptions/:id/transactions`
 * Creates a transaction for a subscription
 *
 * @param {Object} opts An options params which
 *                      is usually already bound
 *                      by `connect` functions.
 * @param {Object} body The payload for the request
 * @param {Number} body.id The subscription's ID
 *
 * @returns {Promise} Resolves to the result of
 *                    the request or to an error.
 */
const createTransaction = (opts, body) =>
  request.post(opts, routes.subscriptions.transactions(body.id), body)
 
/**
 * `GET /subscriptions/:id/transactions`
 * Gets transactions for a subscription
 *
 * @param {Object} opts An options params which
 *                      is usually already bound
 *                      by `connect` functions.
 * @param {Object} body The payload for the request
 * {@link https://pagarme.readme.io/v1/reference#transações-em-uma-assinatura|API Reference for this payload}
 * @param {Number} body.id The subscription's ID
 *
 * @returns {Promise} Resolves to the result of
 *                    the request or to an error.
 */
const findTransactions = (opts, body) =>
  request.get(opts, routes.subscriptions.transactions(body.id), omit(['id'], body))
 
/**
 * `POST /subscriptions/:id/settle_charge`
 * Skips the next x charges for a subscription
 *
 * @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/reference#pulando-cobranças|API Reference for this payload}
 * @param {Number} body.id The subscription's ID
 *
 * @returns {Promise} A promise that resolves to
 *                    the subscription whose id
 *                    was passed on the request
 *                    body or to an error.
 */
const settleCharge = (opts, body) =>
  request.post(opts, routes.subscriptions.settleCharge(body.id), body)
 
export default {
  all,
  find,
  findAll,
  create,
  update,
  cancel,
  createTransaction,
  findTransactions,
  settleCharge,
}