earnbox

The Earnbox Node library provides convenient access to the Earnbox API.

Usage no npm install needed!

<script type="module">
  import earnbox from 'https://cdn.skypack.dev/earnbox';
</script>

README

Earnbox Node.js Library

The Earnbox Node library provides convenient access to the Earnbox API.

Installation

Install the package with:

npm install earnbox --save

Usage

The package needs to be configured with your API and secret key which are available in your Earnbox Dashboard.

const earnbox = require("earnbox")

earnbox.config({
  api: "YOUR-API-KEY",
  secret: "YOUR-SECRET-KEY"
})

To give a cashback:

var amount = 1000 // The amount your customer spent as an integer number of cents (or pennies, satoshis etc..)
var customerNumber = 263 // The Earnbox customer number

earnbox.giveCashback(amount, customerNumber, function(success) {
  // returns true if cashback was given successfully (callback is optional)
})

In order to redeem a cashback you need to setup a webhook URL. This can be done inside your Earnbox Dashboard. Once you've setup your webhook URL, create a new route:

app.post("/my-webhook-url", function(req, res) {

  // Check if the webhook request has valid headers
  var validHeaders = earnbox.verifyHeaders(req)
  if (!validHeaders) return res.end()

  // Get the transaction from the request body
  var transaction = req.body

  // Verify the transaction
  earnbox.verifyTransaction(transaction, function(err, verified) {
    if (err) return // HMAC verification failed, abort..

    // Transaction verified successfully
    console.log("TX verified:", transaction)

    // Update your customer checkout session
    //..

  })

  // Let Earnbox know you've received the webhook
  res.sendStatus(200)
})

Now that you're all setup, let's try to redeem a cashback.

In your browser navigate to: https://earnbox.co/redeem?business=YOUR-API-KEY. Change YOUR-API-KEY with your API key.

Now scan the QR code with the Earnbox app (or click on the button if you're on a mobile phone). You will now be asked how much you would like to pay. Enter an amount and click on PAY.

Earnbox will now process the transaction and send it to your webhook URL.

More coming soon..