All files / lib/resources transactions.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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228                            1x       1x                                         1x                                     1x                                 1x                               1x                                     1x                                       1x                                 1x                                     1x                                     1x                                       1x                                            
/**
 * @name Transactions
 * @description This module exposes functions
 *              related to the `/transactions` path.
 *
 * @module transactions
 **/
 
import { cond, has, T, curry, pick } from 'ramda'
 
import routes from '../routes'
import request from '../request'
 
 
const findOne = curry((opts, body) =>
  request.get(opts, routes.transactions.details(body.id), {})
)
 
const findAll = curry((opts, pagination) =>
  request.get(opts, routes.transactions.base, pagination || {})
)
 
/**
 * `GET /transactions`
 * Makes a request to /transactions or to /transactions/: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/reference#retornando-transações|API Reference for this payload}
 * @param {Number} [body.id] The transaction ID. If not sent a
 *                           transaction list will be returned instead.
 * @param {Number} [body.count] Pagination option for transaction list.
 *                              Number of transaction in a page
 * @param {Number} [body.page] Pagination option for transaction list.
 *                             The page index.
*/
const find = (opts, body) =>
  cond([
    [has('id'), findOne(opts)],
    [T, findAll(opts)],
  ])(body)
 
/**
 * `GET /transactions`
 * Makes a request to /transactions to get all transactions.
 *
 * @param {Object} opts An options params which
 *                      is usually already bound
 *                      by `connect` functions.
 *
 * @param {Number} [body.count] Pagination option for recipient list.
 *                              Number of recipient in a page
 * @param {Number} [body.page] Pagination option for recipient list.
 *                             The page index.
*/
const all = (opts, body) =>
  findAll(opts, body)
 
/**
 * `POST /transactions`
 * Creates a transaction 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/reference#criar-transação|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.transactions.base, body)
 
/**
 * `POST /transactions/:id/capture`
 * Captures a transaction from the given id.
 *
 * @param {Object} opts An options params which
 *                      is usually already bound
 *                      by `connect` functions.
 *
 * @param {Number} body.id The transaction ID.
 *
 * @returns {Promise} Resolves to the result of
 *                    the request or to an error.
 */
const capture = (opts, body) =>
  request.post(opts, routes.transactions.capture(body.id), body)
 
/**
 * `POST /transactions/:id/refund`
 * Refunds a transaction from the given 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/reference#estorno-de-transação|API Reference for this payload}
 *
 * @param {Number} body.id The transaction ID.
 *
 * @returns {Promise} Resolves to the result of
 *                    the request or to an error.
 */
const refund = (opts, body) =>
  request.post(opts, routes.transactions.refund(body.id), body)
 
/**
 * `POST /transactions/:id/collect_payment`
 * Sends a payment link to a customer
 *
 * @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/reference#notificando-cliente-sobre-boleto-a-ser-pago|API Reference for this payload}
 * @param {Number} body.id - The transaction id
 * @param {String} body.email - User email to send the
 *                              payment request
 *
 * @returns {Promise} Resolves to the result of
 *                    the request or to an error.
 */
const collectPayment = (opts, body) =>
  request.post(opts, routes.transactions.collectPayment(body.id), body)
 
/**
 * `GET /transactions/card_hash_key`
 * Create a card hash key
 *
 * @param {Object} opts - An options params which
 *                        is usually already bound
 *                        by `connect` functions.
 *
 * @param {Object} body The payload for the request.
 * {@link https://dash.readme.io/project/pagarme/v1/refs/gerando-card_hash-manualmente-1|API Reference for this payload}
 *
 * @returns {Promise} - Resolves to the result of
 *                      the request or to an error.
 */
const cardHashKey = opts =>
  request.get(opts, routes.transactions.cardHashKey, {})
 
/**
 * `PUT /transactions/:id`
 * Updates a transaction 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.
 *
 * @param {Number} body.id The transaction ID
 * @param {Number} body.status The transaction status
 * @returns {Promise} A promise that resolves to
 *                    the newly created transactions's
 *                    data or to an error.
 **/
const update = (opts, body) =>
  request.put(opts, routes.transactions.details(body.id), body)
 
/**
 * `GET /transactions/calculate_installments_amount`
 * Calculates the value of each purchase's installments
 *
 * @param {Object} opts An options params which
 *                      is usually already bound
 *                      by `connect` functions.
 *
 * @param {Number} body.interest_rate - The interest rate's value.
 * @param {Number} body.amount - The value of the purchase.
 * @param {Number} [body.max_installments] - The max number of installments.
 * @param {Number} [body.free_installments] - The number of installments without interest.
 *
 * @returns {Promise} Resolves to the result of
 *                    the request or to an error.
 */
const calculateInstallmentsAmount = (opts, body) =>
  request.get(opts, routes.transactions.calculateInstallmentsAmount, body)
 
/**
 * `POST /transactions/:id/reprocess`
 * Reprocess a transaction from the given id.
 *
 * @param {Object} opts An options params which
 *                      is usually already bound
 *                      by `connect` functions.
 *
 * @param {Number} body.id The transaction ID.
 * @param {Boolean} body.capture Should capture
 *                               the transaction.
 * @param {Boolean} body.analyze Should analyze
 *                               the transaction.
 *
 * @returns {Promise} Resolves to the result of
 *                    the request or to an error.
 */
const reprocess = (opts, body) => {
  const payload = pick(['capture', 'analyze'], body)
 
  return request.post(
    opts,
    routes.transactions.reprocess(body.id),
    payload
  )
}
 
export default {
  find,
  all,
  capture,
  create,
  refund,
  collectPayment,
  cardHashKey,
  update,
  calculateInstallmentsAmount,
  reprocess,
}