# billdeskJS
A tool to integrate billdesk payment gateway in JS
## Usage
After installation simply require the module in your file like this.
```
const billdesk = require('billdeskjs');
```

Now, you need to define some environment variables like 

```
MID = <ur merchant id>;
SEC_ID = <ur security id>;
BILL_URL = <billdesk_url>;
CONF_BILL_URL = <url for scheduled checks>;
CHECKSUM_KEY = <ur checksum>;
RETURN_URL = <ur return url>;
```

All the above mention variables (except for return url) will be provided by billdesk to you on request.

Now,to use it in ur project,simply make a new object of the class you required by doing


```
const client = new billdesk(MID, SEC_ID, CHECKSUM_KEY, RETURN_URL);
```
This needs to be done at the beginning of your script<br>
Now, use it by to generate message for in the format that billdesk requires and then send a post request to the BILL_URL from a html form<br>

```
var msg = client.get_message(uniqueTransactionID,amount,aditionalInfo1,aditionalInfo2,aditionalInfo3,aditionalInfo4)
```

You will receive a response at your return url which needs to be parsed.(Take a look at the structure of retrun url <a href="https://gist.github.com/FerociousCentaur/8f1b7a4de9f0122e5766766554a9aeb1">here</a>)<br>
The response received will be a array containing several fields.Pass the 'msg' parameter like this

```
var response = receivedResponse.msg; //assuming u stored the response in receivedResponse
var parsedResponse = client.responseMsg(response);
```

Now parsedResponse will have this structure

```
{
  MID: 'ur mid',
  OrderID: 'ur unique transaction id(passed at the time of message generation)',
  TaxnNo: 'taxation number for the transaction(generated by billdesk)',
  AMNT: 'transaction amount',
  TStat: 'transaction status',
  DnT: 'date and time (ex 09-03-2021 00:44:45)',
  TMode: 'transaction mode (ex Netbanking)'
}
```

<b>Note</b> All the data types are string in the above array.

Now TStat update your database on the basis of TStat.Take a look at the billdesk official documentation or this  <a href="https://gist.github.com/FerociousCentaur/8f1b7a4de9f0122e5766766554a9aeb1">gist</a> for more info about Status Codes.

Now, some transaction may not be processed instantly and will have a Transaction Status code as given <a href="https://gist.github.com/FerociousCentaur/8f1b7a4de9f0122e5766766554a9aeb1">here</a>. They will require you to send requests after every 1 hour until billdesk gives you some different resposne. Now, the structure of this query message will somewhat different than that of message obtained by `client.get_message(...)` .This format of message can be obtained by 

```
schedule_msg = client.get_schedule_msg(<order_id>);
```
order_id is the unique id <b>YOU</b> generated for each transaction.<br><br>
You will send this message to `CONF_BILL_URL` and the message you will recieve again needs to be parsed. To do so 

```
schedule_resp = client.schedule_resp(<response recieved>);
```

The structure of `schedule_resp` will be 

```
{
  MID: 'ur mid',
  TaxnNo: 'taxation number for the transaction(generated by billdesk)',
  OrderID: 'ur unique transaction id(passed at the time of message generation)',
  TStat: 'transaction status',
  RfndStat : 'refund status',
  AMNT: 'transaction amount'
}
```

The database should be updated on the basis of combination of status code and refund status which can be found in billdesk official documnetation or <a href="https://gist.github.com/FerociousCentaur/8f1b7a4de9f0122e5766766554a9aeb1">here</a>.
