4.35.1 • Published 6 days ago

pagarme v4.35.1

Weekly downloads
5,953
License
MIT
Repository
-
Last release
6 days ago

Pagar.me JavaScript

Quality Gate Status Code Smells Coverage Maintainability Rating Security Rating

A JavaScript library to interface with Pagar.me's API, it works in the browser and with Node.js.

The documentation can be found in our JSDocs

Description

This library covers all your needs for integrating with Pagar.me, providing:

  • A clean Promise-based interface for all endpoints in Pagarme's API
  • A fast way to generate card hashes
  • Postback signature validation
  • Documents validations (CPF, CNPJ, and others)

How to use

First, install it:

yarn add pagarme

Or using npm:

npm install pagarme

Pagar.me's JavaScript library can be used in three ways:

Node.js

Import like usual:

import pagarme from 'pagarme'

also works using require:

const pagarme = require('pagarme')

Browser (CommonJS)

Import the browser build:

import pagarme from 'pagarme/browser'

also works using require:

const pagarme = require('pagarme/browser')

Browser (Global Variable)

You can also use the latest release from our CDN and import the build directly in your HTML:

<script src="https://assets.pagar.me/pagarme-js/4.11/pagarme.min.js" />

The library will be available as the global variable pagarme.

API Docs

This library provides a promise based interface for all functions. Before you can use the library, you need to provide authentication details which will be used through API calls.

For a detailed documentation, see our JSDocs.

Client API

All of Pagar.me's REST API endpoints are covered in the client object. Every function call issued to client will return a Promise which represents and manages the result's lifecycle.

Using connect

When you call connect, a Promise which resolves to a client or an error will be returned. If an authentication error happens, you can catch the error with the Promise interface:

import pagarme from 'pagarme'

pagarme.client.connect({ email: 'user@email.com', password: '123456' })
  .then(client => client.transactions.all())
  .then(transactions => console.log(transactions))
  .catch(error => console.error(error))

As the entire library is based on promises, you can also use ES6 generators with every call to make code more procedural:

import pagarme from 'pagarme'

let client

try {
  client = yield pagarme.client.connect({
    email: 'user@email.com',
    password: '123456'
  })
} catch (err) {
  console.log('Authentication error')
}

try {
  const transactions = yield client.transactions.all()
  console.log(transactions)
} catch (err) {
  console.log('Error fetching transactions')
}

The downside of this approach is that you need to handle errors using try/catch.

Pagar.me authorizes clients in various fashions. This library handles all available authentication strategies:

Using API key:

import pagarme from 'pagarme'

pagarme.client.connect({ api_key: 'ak_test_y7jk294ynbzf93' })
  .then(client => client.transactions.all())
  .then(transactions => console.log(transactions))

:warning: Never use API keys in the browser, you should use encryption keys instead.

Using encryption key:

import pagarme from 'pagarme'

const card = {
  card_number: '4111111111111111',
  card_holder_name: 'abc',
  card_expiration_date: '1225',
  card_cvv: '123',
}

pagarme.client.connect({ encryption_key: 'ek_test_y7jk294ynbzf93' })
  .then(client => client.security.encrypt(card))
  .then(card_hash => console.log(card_hash))

Using email and password:

import pagarme from 'pagarme'

pagarme.client.connect({ email: 'user@email.com', password: '123456' })
  .then(client => client.transactions.all())
  .then(transactions => console.log(transactions))

Building

To build the library, use npm start.

  • Node.js build is produced inside the dist directory.
  • Browser build is produced inside the browser directory.

Testing

To run the library tests, use npm test.

To run tests, you need to export API_KEY environment variable with your API key. When submitting a PR, Travis will already have it exported.

Contributing

Community contributions are essential for keeping this library great. We simply can't access the huge number of platforms and myriad configurations for running it, so if you find any problems, feel free to open an issue.

Be sure to provide at least the following information on the issue:

  • Environment (e.g. Node 7, Chrome 57)
  • Operating System (e.g. iOS 10)
  • Library version (e.g. 3.0.0)

We provide source maps to ease debugging. Use them whenever possible when providing stack traces as it will make digging onto the issue easier.

License

The MIT License (MIT)
Copyright (c) 2017 Pagar.me Pagamentos S/A
4.35.1

6 days ago

4.35.0

8 days ago

4.34.1

3 months ago

4.34.0

3 months ago

4.33.1

8 months ago

4.33.0

8 months ago

4.32.0

12 months ago

4.31.0

1 year ago

4.30.0

1 year ago

4.28.0

2 years ago

4.27.0

2 years ago

4.29.0

1 year ago

4.26.0

2 years ago

4.25.0

2 years ago

4.24.0

2 years ago

4.23.1

2 years ago

4.23.0

2 years ago

4.21.1

2 years ago

4.21.2

2 years ago

4.21.3

2 years ago

4.21.0

2 years ago

4.22.0

2 years ago

4.20.2

2 years ago

4.20.3

2 years ago

4.20.1

2 years ago

4.20.0

3 years ago

4.19.6

3 years ago

4.19.5

3 years ago

5.0.0

3 years ago

4.19.4

3 years ago

4.19.3

3 years ago

4.19.2

3 years ago

4.19.1

3 years ago

4.19.0

3 years ago

4.18.0

3 years ago

4.17.0

3 years ago

4.16.1

3 years ago

4.16.0

3 years ago

4.15.3

3 years ago

4.15.1

3 years ago

4.15.0

3 years ago

4.14.0

3 years ago

4.13.0

4 years ago

4.12.0

4 years ago

4.11.0

4 years ago

4.10.0

4 years ago

4.9.0

4 years ago

4.8.0

4 years ago

4.7.1

4 years ago

4.7.0

4 years ago

4.6.0

4 years ago

4.5.3

4 years ago

4.5.2

4 years ago

4.5.1

5 years ago

4.5.0

5 years ago

4.4.1

5 years ago

4.4.0

5 years ago

4.3.0

5 years ago

4.2.1

5 years ago

4.2.0

5 years ago

4.1.1

5 years ago

3.9.2

5 years ago

2.2.3

5 years ago

2.2.2

5 years ago

4.1.0

5 years ago

4.0.0

5 years ago

3.9.1

5 years ago

3.9.0

5 years ago

3.8.0

6 years ago

3.7.3

6 years ago

3.7.2

6 years ago

3.7.1

6 years ago

3.7.0

6 years ago

3.5.0

6 years ago

3.4.0

6 years ago

3.3.0

6 years ago

3.2.0

6 years ago

3.1.3

7 years ago

3.1.2

7 years ago

3.1.1

7 years ago

3.1.0

7 years ago

3.0.2

7 years ago

3.0.1

7 years ago

3.0.0

7 years ago

1.0.9

8 years ago

1.0.8

8 years ago

1.0.7

8 years ago

1.0.6

8 years ago

1.0.5

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago