npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

safepay

v0.0.2

Published

Official NodeJS SDK for Safepay API

Downloads

69

Readme

Safepay NodeJS SDK

Official nodejs library for Safepay API.

Read up here for getting started and understanding the payment flow with Safepay: https://safepay.helpscoutdocs.com/article/50-starting-with-safepay-step-by-step-guide

Installation

npm i safepay

Basic Usage

Instantiate the safepay instance with a config object

const config = {
  environment: "sandbox",
  sandbox: {
    baseUrl: "https://sandbox.api.getsafepay.com",
    apiKey: "sec_e5dd3b9f-48f4-46d7-a484-0a1502b41576",
    apiSecret: "20b883bd5453c419f15951579f963af4b4678425502da1311a14927018487bec"
  },
  production: {
    baseUrl: "https://api.getsafepay.com",
    apiKey: process.env.API_KEY,
    apiSecret: process.env.API_SECRET
  }
}

let sfpy = new Safepay(config)

Every resource method returns a promise.

instance.payments
  .create({
    key: "value"
  })
  .then(response => {
    // handle success
  })
  .catch(error => {
    // handle error
  });

If you want to use callbacks instead of promises, every resource method will accept a callback function as a last parameter. The callback functions will behave as Error First Callbacks

instance.payments.all(
  {
    from: '2016-08-01',
    to: '2016-08-20',
  },
  (error, response) => {
    if (error) {
      // handle error
    } else {
      // handle success
    }
  }
);

Supported Resources

  • Payments

  • Checkout


Initializing an order

Before you can redirect your customer to Safepay, you have to initialize an order. After you initialize the Safepay client, you can make the following request to create a payment

// initialize payment
sfpy.payments.create({
  amount: 1000,
  currency: "PKR",
}).then((response) => {
  return response.data
}).then((data) => {
  return sfpy.checkout.create({
    tracker: data.data.token,
    orderId: "1234",
    source: "custom",
    cancelUrl: "https://example.com/payment-cancelled",
    redirectUrl: "https://example.com/payment-complete"
  })
}).then((url) => {
  console.log(url)
  return 
}).catch((error) => {
  console.error(error)
})

Verifying the signature

When your user completes a payment, Safepay will make a POST request to your redirectUrl with the following data:

tracker,
token,
orderId,
ref,
sig

Before you mark the order as paid, you should verify the signature like so

// validate signature
function processPayment({ tracker, token, ref, sig, order_id }) {
  const valid = Safepay.validateWebhookSignature(tracker, sig, config.sandbox.apiSecret)
  if (!valid) {
    throw new Error("invalid payment signature. rejecting order...")
  }

  console.log("signature verified...")
  console.log("proceeding to mark order as paid")
}

After validating the signature, store the relevant Safepay payment IDs in your database alongside the order.

Development

npm install

Testing

npm test

Release

  1. Switch to master branch. Make sure you have the latest changes in the local master
  2. Update the CHANGELOG.md & bump the version in package.json
  3. Commit
  4. Tag the release & push to Github
  5. Create a release on GitHub using the website with more details about the release
  6. Publish to npm with npm publish command

Licence

MIT Licensed. See LICENSE.txt for more details