npm package: @placetopay/pinpad-sdk
Description: This API provides a PinPad Javascrypt SDK to conect with PinPad service of Placetopay.
Author: Meiyer Jaimes
Latest version: 2.0.6

# PinPad

A PIN pad or PIN entry device (PED) is an electronic device used in a debit, credit or smart card-based transaction 
to accept and encrypt the cardholder's personal identification number (PIN).

This API provides a **PinPad SDK** service with PinBlock generation encrypted 
according to ISO 9564. This specification describes four formats to generate
a PinBlock. You can get more information abouts these formats here at

- [ISO 9564 (Wikipedia)](https://en.wikipedia.org/wiki/ISO_9564).
- [PIN-BLOCKS (EFTLAB)](https://www.eftlab.com/knowledge-base/261-complete-list-of-pin-blocks-in-payments/).

## :pushpin: Installation

Use the following command to install this library.

```bash
 npm i @placetopay/pinpad-sdk
```

## :books: Usage

### Initialize the PinPad JavaScript SDK  

For the PinPad will be successfully loaded, you must initialize the sdk as shown below:

```javascript
import PinPad from '@placetopay/pinpad-sdk'

let pad = new PinPad({
   format: 0, // may be 0,1,2 or 3
   locale: document.documentElement.lang, // language to display
   mask: true   // may be true, false or any string (i.e '*', '-', ...)
})
```

### Connection with PINPAD backend

If you plan to use the standard backend from JavaScript SDK to get numbers positions and generate
pinblocks, let's set the base endpoint URL and  your API TOKEN in `PinPadClient` class.

```javascript
const client = new PinPadClient(url, token)
```

You can perform a request to create a transaction and obtain the transaction id and positions as follows:

```javascript
const response = client.createTransaction()
    .then(res => {
        return res.data
    }).catch(err => {
        return err
    })
```

You will get a response like the following

```json
{
    "status": {
        "status": 1000,
        "message": "The token has been successfully validated. PinPad SDK will be loaded"
    },
    "data": {
        "positions": "7296183540",
        "transactionId": "c89cb967-dcd3-467e-aebc-0299b1acb707"
    }
}
```

### Render PinPad

You can use the `render` method of the `PinPad` class to render the pinpad. You only need pass it
the html id and positions (obtained from PINPAD back)

```javascript
pad.render('div_id', positions)
```

### Send PIN to encrypt

To send the PIN to encrypt, the PinPad JavaScript SDK provides a funtion `sendPin(transactionId, data)`. Return `Pinblock`.

send PIN as shown below:

```javascript
const data = {
    transactionId,  // transaction id
    format, // format to apply (required)
    pin,    // positions digits from user (required)
    pan,    // card number (required if format is 0 or 3)
}

client.generatePinBlock(data)
    .then(res => {
        return res.data
    }).catch(err => {
        return err
    })
```

You'll get a response like the following

```json
{
    "status": {
        "status": 1000,
        "message": "The PinBlock has been successfully generated."
    },
        "data": {
        "pinblock": "A810A95D0562214A"
    }
}
```