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

@sfpy/checkout-components

v1.0.1

Published

Safepay Checkout components, for integrating checkout products.

Downloads

2,720

Readme

Safepay Checkout and Button

A set of components allowing easy integration of Safepay Button and Safepay Checkout into your website, powered by zoid

Prerequisites

This integration sets up online payment options using this library, which presents the Safpepay Button to your buyers.

Complete the steps in Get started to get the following sandbox information from the Developer Dashboard

  • Email ID and password of a sandbox account, to login and verify a checkout purchase
  • Sandbox API key of your Safepay sandbox account

Technical flow

  1. You add the payment button to your webpage
  2. Your buyer clicks a button
  3. The button calls the Safepay Order API to set up a payment session
  4. The button launches the Safepay Checkout experience
  5. The buyer approves the payment
  6. The button calls the Safepay Order API to finalize the transaction
  7. You show a confirmation message to your buyer

Installation

You can install the Safepay Checkout Components library either through NPM or Yarn, or include the library inside a script tag in your index.html file. Both options are described below

A. Install through NPM or Yarn

In your project root you can run either one of the below commands (depending on what package manager you use)

NPM

npm i --save @sfpy/checkout-components

Yarn

yarn add @sfpy/checkout-components

B. Add a script tag

<html>
  <head>
    <!-- ..... -->
    <script src="https://unpkg.com/@sfpy/[email protected]/dist/sfpy-checkout.js"></script>
    <!-- ..... -->
  </head>
  <body>
    <!-- ..... -->
  </body>
</html>

You can choose whichever version suits your integration from the releases

Integration

To accept payments on your website, after you've successfuly installed the Safepay Checkout Components library, You'll have to add buttons to your website. We also have a fully working demo on the Quick Start Guide.

Support for all the popular frameworks like React, Angular and Vue comes out of the box but you can also use Vanilla javascript as showb below.

<!DOCTYPE html>
<html>
  <head>
    <title>Safepay checkout demo</title>
    <script
      type="text/javascript"
      src="https://unpkg.com/@sfpy/[email protected]/dist/sfpy-checkout.js"
    ></script>
  </head>
  <body>
    <!-- Set up a container element for the button -->
    <div id="safepay-button-container"></div>
    <script>
      safepay
        .Button({
          env: "sandbox",
          client: {
            sanbox: "sec_733defcf-835f-4cd1-99fd-b3e62dd83fe0",
          },
          style: {
            mode: "light", // dark or light
            size: "large", // small medium large
            variant: "primary", // primary dark
          },
          orderId: "12344", // Your custom order ID
          source: "website" // The source of the payment
          // Details to set up the transaction
          // when a payment button is clicked
          payment: {
            currency: "PKR",
            amount: 1000.5,
          },
          // Finalize the transaction after payer approval
          onPayment: function (data) {
            // At this point your customer has approved the payment
            // and you can show a success message or make an API request
            // to your servers to add the data.
            return fetch(`/api/orders/mark-paid`, {
              method: "post",
            });
          },
          onCancel: function () {
            console.log("cancelled");
          },
        })
        .render("#safepay-button-container");
    </script>
  </body>
</html>

Props

The Safepay Checkout Button expects the following props to be passed to it in order to your configure a purchase for your buyer

| Name | Type | Required | Description | | :---------- | :--------- | :------- | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | env | string | true | One of two possible values sandbox or production | | client | object | true | A map of env to your api key. Each value in the map must correspond to the api key for the correct environment | | style | object | true | A few options to configure the look and feel of the button | | orderId | string | false | An optional value in case you want to link your store's order ID with this payment | | source | string | false | An optional value in case you want to link a source with this payment to identify the channel | | payment | object | true | Amount and currency values to configure the payment when your customer clicks a button | | onPayment | function | true | A callback to notify you when your customer has completed the payment. This function is passed in the Transaction object through the payment attribute on the data object | | onCancel | function | true | A callback to notify you when your customer cancels the payment or abandons it. At this point you're free to take them back to the shopping cart page for instance |

React Integration

Since we're huge fans of React at Safepay, we've integrated this library into our products like Quick Links through the following React integration that ships out of the box with this library

import safepay from "@sfpy/checkout-components";
import React from "react";
import ReactDOM from "react-dom";

const SafepayButtonInstance = safepay.Button.driver("react", {
  React: React,
  ReactDOM: ReactDOM,
});

export default SafepayButtonInstance;

And then in another file you import SafepayButtonInstance and render is using familiar React and JSX.

import SafepayButtonInstance from './safepay-button-intance'

const MyCheckoutPage = ({ orderId }) => (
  <div>
    <p>Checkout for order id {orderId}</p>
    <SafepayButtonInstance
      env={'sandbox'}
      client={{
        'sandbox': 'sec_733defcf-835f-4cd1-99fd-b3e62dd83fe0'
      }}
      style={{
        mode: 'light',
        size: 'medium',
        variant: 'primary'
      }}
      orderId={"12344"},
      payment={{
        "currency": "PKR",
        "amount": 1000.50
      }}
      onPayment={(data) => {
        // At this point your customer has approved the payment
        // and you can show a success message or make an API request
        // to your servers to add the data.
        return fetch(`/api/orders/mark-paid`, {
          method: "post",
        })
      }}
      onCancel={() => {
        console.log('payment cancelled')
      }}
    />
  </div>
)

Local development

If you want to contribute or you're looking to run the demo included in this package, please read on.

  1. Clone this repository on your local machine
git clone https://github.com/getsafepay/safepay-checkout-components.git
  1. cd into the directory and install node_modules
~/safepay-checkout-components: yarn install
  1. To run the demo, run yarn run serve or npm run serve and follow the instructions printed on the terminal