# EMV\*

{% hint style="warning" %}
Sample code uses ES6 language features such as arrow functions and promises. For compatibility with IE11, code written with these features must be either transpiled using tools like Babel or refactored accordingly using callbacks.
{% endhint %}

## Introduction

This container supports functionality for EMV "chip and PIN" bank cards, including:

* VISA, MasterCard, Amex, CB and UK Post Office Account contact cards
* PayWave (VISA) and PayPass (MasterCard) contactless cards

## Interface

```
export interface AbstractEmv {
    readApplicationData(callback?: (error: T1CLibException, data: PaymentReadApplicationDataResponse) => void): Promise<PaymentReadApplicationDataResponse>;
    readData(callback?: (error: T1CLibException, data: PaymentReadDataResponse) => void): Promise<PaymentReadDataResponse>;

    allCerts(aid: string, filters: string[] | Options, callback?: (error: T1CLibException, data: PaymentAllCertsResponse | TokenAllCertsExtendedResponse) => void): Promise<PaymentAllCertsResponse | TokenAllCertsExtendedResponse>;
    issuerPublicCertificate(aid: string, callback?: (error: T1CLibException, data: PaymentCertificateResponse) => void): Promise<PaymentCertificateResponse>;
    iccPublicCertificate(aid: string, callback?: (error: T1CLibException, data: PaymentCertificateResponse) => void): Promise<PaymentCertificateResponse>;

    allCertsExtended(aid: string, filters: string[] | Options, callback?: (error: T1CLibException, data: TokenAllCertsExtendedResponse) => void): Promise<TokenAllCertsExtendedResponse>;
    issuerPublicCertificateExtended(aid: string, callback?: (error: T1CLibException, data: TokenCertificateExtendedResponse) => void): Promise<TokenCertificateExtendedResponse>;
    iccPublicCertificateExtended(aid: string, callback?: (error: T1CLibException, data: TokenCertificateExtendedResponse) => void): Promise<TokenCertificateExtendedResponse>;

    verifyPin(body: PaymentVerifyPinData, callback?: (error: T1CLibException, data: PaymentVerifyPinResponse) => void): Promise<PaymentVerifyPinResponse>;
}
```

## Get EMV container object

Initialise a Trust1Connector:

```javascript
T1CSdk.initialize(config).then(res => {

}, err => {
    console.error(error)
});
```

Get the EMV service:

```javascript
var emv = client.emv(reader_id);
```

Call a function for the EMV container:

```javascript
function callback(err,data) {
    if(err){console.log("Error:",JSON.stringify(err, null, '  '));}
    else {console.log(JSON.stringify(data, null, '  '));}
}
emv.readData(callback)
```

### Obtain the Reader-ID

The constructor for the EMV expect as the parameter to be a valid reader-ID. A reader-ID can be obtained from the exposed core functionality, for more information see [Core Services](https://t1t.gitbook.io/t1c-js-guide-v3/payment/broken-reference).\
Core services responds with available card-readers, available card in a card-reader, etc.\
For example:\
In order to get all connected card-readers, with available cards:

```javascript
var coreService = client.core();
core.readersCardAvailable(callback);
```

This function call returns:

We notice that a card object is available in the response in the context of a detected reader.\
The reader in the example above is `VASCO DIGIPASS 870`, has pin-pad capabilities, and there is a card detected with given ATR and some descriptions.\
An ATR (**A**nswer **T**o **R**eset) identifies the type of a smart-card.\
The reader, has a unique ID, `reader_id`; this `reader_id` must be used in order to request functionalities for the EMV card.\
This must be done upon instantiation of the EMV container:

```javascript
var emv = client.emv(reader_id);
```

All methods for `emv` will use the selected reader - identified by the `reader_id`.

## Reading data

### Applications

List the supported applications on the EMV card

```javascript
client.emv(reader_id).readData(callback);
```

An example callback:

```javascript
function callback(err,data) {
    if(err){
        console.log("Error:",JSON.stringify(err, null, '  '));
    }
    else {
        console.log(JSON.stringify(data, null, '  '));
    }
}
```

Response:

```javascript
{
  "data": [
      {
        "aid": "A0000000048002", 
        "name": "MAESTRO", 
        "priority": 1
      },{
        "aid": "A0000000048008", 
        "name": "MASTERCARD",
        "priority": 1
      }
    ],
  "success": true
}
```

### Application data

The application data contains information of the holder of the card, the validity, the primary account number, ...

```javascript
client.emv(reader_id).readApplicationData(callback);
```

An example callback:

```javascript
function callback(err,data) {
    if(err){
        console.log("Error:",JSON.stringify(err, null, '  '));
    }
    else {
        console.log(JSON.stringify(data, null, '  '));
    }
}
```

Response:

```javascript
{
 "country": "BE",
 "countryCode": "0056",
 "effectiveDate": "190201",
 "expirationDate": "241231",
 "language": "nlen",
 "name": "",
 "pan": "670...001"
}
```

## Extended certificates

You can also fetch the extended versions of the certificates via the functions

```
allCertsExtended(parseCerts?: boolean, filters?: string[] | Options, callback?: (error: T1CLibException, data: TokenAllCertsExtendedResponse) => void): Promise<TokenAllCertsExtendedResponse>;
iccPublicCertificateExtended(parseCerts?: boolean, callback?: (error: T1CLibException, data: TokenCertificateExtendedResponse) => void): Promise<TokenCertificateExtendedResponse>;
issuerPublicCertificateExtended(parseCerts?: boolean, callback?: (error: T1CLibException, data: TokenCertificateExtendedResponse) => void): Promise<TokenCertificateExtendedResponse>;
```

this has the capabilities to return multiple certificates if the token has multiple of this type.

for a single certificate the response looks like:

```json
{
    "success" : true
    "data" : {
        "certificates": [{
            "certificate"?: string,            
            "exponent"?: string,
            "remainder"?: string,
            "parsedCertificate"?: Certificate
        }]
    }
}
```

the `allCertsExtended` returns the following, with the contents of the certificates as the one you can see above;

```json
{
    "success" : true
    "data" : {
        "issuerPublicCertificate": {
            "certificates": [...]
        },
        "iccPublicCertificate": {
            "certificates": [...]
        }
   }
}
```

## Issuer Public Key Certificate

On some applications there is an issuer public key certificate present. The aid parameter indicates which application you want to use, this can be fetched using the applications endpoint.

```javascript
// Application ID can be retrieved with the Applications endpoint
var aid = "..."

client.emv(reader_id).issuerPublicCertificate(aid, callback);
```

An example callback:

```javascript
function callback(err,data) {
    if(err){
        console.log("Error:",JSON.stringify(err, null, '  '));
    }
    else {
        console.log(JSON.stringify(data, null, '  '));
    }
}
```

Response:

```javascript
{
  "data": {
    "data": "base64 encoded data", 
    "exponent": "base64 encoded data", 
    "remainder": "base64 encoded data"
  }, 
  "success": true
}
```

## ICC Public Key Certificate

On some applications there is an icc public key certificate present. The aid parameter indicates which application you want to use, this can be fetched using the applications endpoint.

```javascript
// Application ID can be retrieved with the Applications endpoint
var aid = "..."

client.emv(reader_id).iccPublicCertificate(aid, callback);
```

An example callback:

```javascript
function callback(err,data) {
    if(err){
        console.log("Error:",JSON.stringify(err, null, '  '));
    }
    else {
        console.log(JSON.stringify(data, null, '  '));
    }
}
```

Response:

```javascript
{
 "certificate": "dxL8JnkxHneX36tdQCzz...HC3Wpt/qppk008q9OMDgVp0F7NJjCC2mXg3b/qD7c09WFXUwZ+XdkmIefhoZT/kme4QEoD49+ppQiqSeCaRjn6N0OetcjleWkPej8vE0QG4mLlG/edWTVbfMGbED9Kbjf4ooauNnq+iAVwgHedsDpdDWJLhV8sDSLQgZ1B3fMQuyZIaD79+X+H9fbhmJg+j7Lr638srglWM9VlthaWjRbFH2HzNEiQ9sOE20lmj6WM6zdYas9+Z4hcwZqWbeiTeIJDwDc6w==",
 "exponent": "AQAB",
 "remainder": ""
}
```

## Verify PIN

### Verify PIN without pin-pad

When the web or native application is responsible for showing the password input, the following request is used to verify a card holder PIN:

```javascript
var data = {
    "pin": "...."
}
client.emv().verifyPin(data, callback);
```

Response:

```javascript
{
 "verified": true
}
```

### Verify PIN with pin-pad

When the pin entry is done on the pin-pad, the following request is used to verify a given PIN:

```javascript
var data = {}
client.emv().verifyPin(data, callback);
```

Response:

```javascript
{
 "verified": true
}
```

### Verify PIN - retries left

After an unsuccessful PIN verification, the error code indicates the number of retries left. For example, when executing:

```javascript
  $("#buttonValidate").on('click', function () {
      var _body={};
      _body.pin = $("#psw").val(); //only when no pin-pad available
      var emv = connector.emv(reader_id);
      emv.verifyPin(_body, validationCallback);
  });
```

The following error message will be returned when PIN is wrong:

```javascript
{
  "code": 301,
  "description": "Wrong pin, 2 tries remaining",
  "success": false
}
```

After a second wrong PIN verification:

```javascript
{
  "code": 301,
  "description": "Wrong pin, 1 try remaining",
  "success": false
}
```

Note that, when the user has at least one retry left, entering a correct PIN resets the `PIN retry status`.

| Code  | Description                                            |
| ----- | ------------------------------------------------------ |
| `301` | Warning: the user can try twice more to verify his PIN |
| `301` | Warning: the user has only 1 retry left                |
| `301` | Error: the PIN is blocked                              |

## Error Handling

### Error Object

The functions specified are asynchronous and always need a callback function.\
The callback function will reply with a data object in case of success, or with an error object in case of an error. An example callback:

```javascript
function callback(err,data) {
    if(err){
        console.log("Error:",JSON.stringify(err, null, '  '));
    }
    else {
        console.log(JSON.stringify(data, null, '  '));
    }
}
```

The error object returned:

```javascript
{
  success: false,
  description: "some error description",
  code: "some error code"
}
```

For the error codes and description, see [Status codes](https://t1t.gitbook.io/t1c-js-guide-v3/payment/broken-reference).
