# pagarme-bifrost-js

> PagarMe Bifrost WebSocket JS Class. An easy implementation of PagarMe Bifrost WebSocket Service.

Latest version **0.2.5** (published 2019-03-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install pagarme-bifrost-js
pnpm add pagarme-bifrost-js
yarn add pagarme-bifrost-js
bun add pagarme-bifrost-js
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.5 |
| Published | 2019-03-20 |
| First published | 2019-03-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 192.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Heitor Ramon Ribeiro |
| Maintainers | bloodf |
| Keywords | pagarme, websocket, tef |

## Links

- npm: https://www.npmjs.com/package/pagarme-bifrost-js
- Repository: https://github.com/bloodf/pagarme-bifrost-js
- Homepage: https://github.com/bloodf/pagarme-bifrost-js#readme
- Issues: https://github.com/bloodf/pagarme-bifrost-js/issues
- npm.io page: https://npm.io/package/pagarme-bifrost-js

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 0.2.5 (latest) — 2019-03-20
- 0.2.4 — 2019-03-20
- 0.2.3 — 2019-03-20
- 0.2.2 — 2019-03-20
- 0.2.1 — 2019-03-20
- 0.2.0 — 2019-03-20
- 0.1.9 — 2019-03-20
- 0.1.8 — 2019-03-20
- 0.1.7 — 2019-03-18
- 0.1.6 — 2019-03-18
- 0.1.5 — 2019-03-18
- 0.1.4 — 2019-03-15
- 0.1.3 — 2019-03-15

## README

# PagarME Bifrost.JS
Uma forma mais fácil de implementar o websocket da Pagar.me em seu sistema.

## Como Usar
### NPM / Yarn
```sh
npm install pagarme-bifrost-js --save

# For Yarn, use the command below.
yarn add pagarme-bifrost-js
```

### CDN

```html
<!-- For UNPKG use the code below. -->
<script src="https://unpkg.com/pagarme-bifrost-js@latest/dist/index.umd.js"></script>

<!-- For JSDelivr use the code below. -->
<script src="https://cdn.jsdelivr.net/npm/pagarme-bifrost-js@latest/dist/index.umd.js"></script>

<script>
  console.log(PagarMeBifrost);
</script>
```

## API
### Classe
Iniciando a classe
```JS
import PagarMeBifrost from 'pagarme-bifrost-js';

const Bifrost = new PagarMeBifrost({
  contextId: 'ABC123',
  encryptionKey: 'ENCKEY',
});
```

#### Construtor
##### Argumentos
|Propiedade|Tipo|Default|
|--|--|--|
| debug | boolean | Ativa o modo de Debug |
| host | string | Endereço de conexão do WebSocket |
| contextId | string | ID do contexto de conexão |
| baudRate | number | Taxa de comunicação |
| encryptionKey | string | Chave de criptografia Pagar.ME |
| pinPadMaxCharLine | number | Quantidade máxima de caracteres por linha do PinPad |
| pinPadMaxChar | number | Quantidade máxima de caracteres na tela do PinPad |
| pinPanDisplayLines | number | Número de linhas disponíveis no PinPad |

### initialize
Inicializa o WebSocket. Caso esteja tudo Ok, será retornado `true`, senão um `Error`

```JS
  Bifrost.initialize();
```

### terminate
Finaliza o WebSocket. Caso esteja tudo Ok, será retornado `true`, senão um `Error`

```JS
  Bifrost.terminate();
```

### status
Retorna o status do WebSocket. Caso esteja tudo Ok, será retornado um objeto de status `BifrostServiceStatus`, senão um `Error`

```JS
  /**
   * @typedef {object} BifrostServiceStatus
   * @property {boolean} connected - Is device connected
   * @property {string} contextId - Device Context
   * @property {string} connectedDeviceId - Connected Device Id
   */
  Bifrost.status();
```

### showMessage
Exibe uma mensagem ou array de mensagens no display do Pinpad.

```JS
  Bifrost.showMessage('MSG' || ['MSG']);
```

### payment
Inicializa o processo de pagamento no WebSocket, você deve passar dois parâmetros. O primeiro é o valor (float) e o segundo o metodo de pagamento ('credit'|'debit'|1|2). Caso esteja tudo Ok, será retornado `PinPadProcessedCardReturn`, senão um `Error`

```JS
  /**
   * @typedef {object} PinPadProcessedCardReturn
   * @property {string} card_hash
   * @property {string} card_holder_name
   * @property {number} error_code
   * @property {boolean} is_online_pin
   * @property {number} payment_method
   * @property {number} status
   */
  const amount = 10; // Float
  const method = 'credit'; // 'credit'|'debit'|1|2
  Bifrost.payment(amount, method);
```

### finish
Finaliza o processo de pagamento no WebSocket, após o pagamento iniciado e o backEnd processado ele, você deve passar os códigos devolvidos pelo backend para o serviço de WebSocket. Caso esteja tudo Ok, será retornado a resposta do serviço `Object`, senão um `Error`

```JS
  Bifrost.finish({
    code: '', // Código devolvido pelo servidor
    emvData: '', // Código devolvido pelo servidor
    messages: [''], // Array de mensagens para serem mostradas no PinPad
  });
```

## Exemplo
```JS
import PagarMeBifrost from 'pagarme-bifrost-js';

const Bifrost = new PagarMeBifrost({
  contextId: 'ABC123',
  encryptionKey: 'ENCKEY',
});
// Inicializando o serviço
Bifrost.initialize()
.then((status) =>{
  if(status){
    Bifrost.showMessage('Msg no PINPAD');
  }
})
.catch(() => {
  Bifrost.terminate();
});

// Fazendo um pagamento via crédito
Bifrost.payment(10.00, 'credit')
.then((response) => {
  /* Após enviar para o backend a resposta (response)
  * você vai receber os dados para a finalização 
  * */
  Bifrost.finish({
    code: '', // Código devolvido pelo servidor
    emvData: '', // Código devolvido pelo servidor
    messages: [''], // Array de mensagens para serem mostradas no PinPad
  });
});

```

---
_Source: https://npm.io/package/pagarme-bifrost-js · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
