Skip to content

Commit

Permalink
v1.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
pariola committed May 26, 2018
1 parent b0a0523 commit e29dcda
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
21 changes: 20 additions & 1 deletion README.md
Expand Up @@ -62,6 +62,8 @@ paystack.transactions.list({ perPage: 20 }).then(function(error, body) {

### Extras

#### I

A function to add Paystack's fee to your charging fee

```js
Expand All @@ -83,6 +85,23 @@ percentage: ...,
console.log(helper.addFeesTo(5000));
```

#### II

A Paystack Events helper (Express Middleware)
```js
const paystack = require("paystack-api")("secret_key");
const events = paystack.Events;

events.on("event_name", data => {
// Act
console.log("hola!");
});

// Hooks with Express
app.post("/my/webhook/url", events.middleware);
```


### Resources

* customer
Expand Down Expand Up @@ -191,4 +210,4 @@ console.log(helper.addFeesTo(5000));

### TODO

* [ ] Create a Paystack Events `express` middleware
* [X] Create a Paystack Events `express` middleware
4 changes: 4 additions & 0 deletions index.js
Expand Up @@ -5,6 +5,7 @@ Paystack API wrapper

const request = require("request-promise");
const endpoint = "https://api.paystack.co";
const Events = require("./resources/events");

function Paystack(key) {
if (!(this instanceof Paystack)) {
Expand All @@ -14,6 +15,9 @@ function Paystack(key) {
this.endpoint = endpoint;
this.key = key;
this.import();

// Setup Events
this.Events = new Events(this.key);
}

const resources = {
Expand Down
3 changes: 2 additions & 1 deletion package.json
@@ -1,9 +1,10 @@
{
"name": "paystack-api",
"version": "1.0.2",
"version": "1.0.3",
"main": "index.js",
"license": "MIT",
"dependencies": {
"mitt": "^1.1.3",
"request": "^2.85.0",
"request-promise": "^4.2.2"
},
Expand Down
28 changes: 28 additions & 0 deletions resources/events.js
@@ -0,0 +1,28 @@
const crypto = require("crypto");
const mitt = require("mitt");

const emitter = mitt();

function Events(key) {
this.key = key;
}

Events.prototype.middleware = function(req, res) {
res.sendStatus(200);
// Validate request
var hash = crypto
.createHmac("sha512", this.key)
.update(JSON.stringify(req.body))
.digest("hex");
if (hash == req.headers["x-paystack-signature"]) {
// Retrieve the request's body
var event = req.body;
emitter.emit(event.event, event.data);
}
};

// Extend emitter
Events.prototype.on = emitter.on;
Events.prototype.off = emitter.off;

module.exports = Events;

0 comments on commit e29dcda

Please sign in to comment.