🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

@sfpy/node-sdk

Package Overview
Dependencies
Maintainers
0
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@sfpy/node-sdk

Official NodeJS SDK for Safepay API

3.0.2
latest
Source
npm
Version published
Weekly downloads
60
130.77%
Maintainers
0
Weekly downloads
 
Created
Source

Safepay NodeJS SDK

Official nodejs library for Safepay API.

Installation

With Yarn

yarn add @sfpy/node-sdk

With NPM

npm install @sfpy/node-sdk

Usage

Import and create a Safepay client by passing your config;

import { Safepay } from '@sfpy/node-sdk'

const safepay = new Safepay({
  environment: 'sandbox',
  apiKey: 'sec_asd12-2342s-1231s',
  v1Secret: 'bar',
  webhookSecret: 'foo'
})

You can now create payments and checkout links.

Payments

Create payment

ParameterTypeRequired
amountnumberYes
currencyPKR, USDYes
const { token } = await safepay.payments.create({
  amount: 10000,
  currency: 'PKR'
})

// Pass `token` to create checkout link

Checkout

ParameterTypeDescriptionRequired
tokenstringToken from safepay.payments.createYes
orderIdstringYour internal invoice / order idYes
cancelUrlstringUrl to redirect to if user cancels the flowYes
redirectUrlstringUrl to redirect to if user completes the flowYes
sourcestringOptional, defaults to customNo
webhooksbooleanOptional, defaults to falseNo
const url = safepay.checkout.create({
  token,
  orderId: 'T800',
  cancelUrl: 'http://example.com/cancel',
  redirectUrl: 'http://example.com/success',
  source: 'custom',
  webhooks: true
})

// redirect user to `url`

Verification

Signature

ParameterTypeDescriptionRequired
requestobjectThe req object from your serverYes
const valid = safepay.verify.signature(request)

// mark the invoice as paid if valid
// show an error if invalid

Webhook

ParameterTypeDescriptionRequired
requestobjectThe req object from your serverYes
const valid = await safepay.verify.webhook(request)

// mark the invoice as paid if valid
// show an error if invalid

Subscriptions

ParameterTypeDescriptionRequired
planIdstringThe plan id corresposnding to the plan you created on Safepay's merchant dashboardYes
referencestringUniquely identify subscription with your internal identifier e.g. order_idYes
cancelUrlstringUrl to redirect to if user cancels the flowYes
redirectUrlstringUrl to redirect to if user completes the flowYes
safepay.checkout
  .createSubscription({
    cancelUrl: 'https://www.google.com/',
    redirectUrl: 'https://www.google.com/',
    planId: 'plan_33e626b3-d92e-40b3-a379-4f89d61f8c83',
    reference: 'cc3f2475-4fb1-4d80-99f8-3a582a496fa6'
  })
  .then(url => {
    console.log(url)
  })
  .catch(error => {
    console.error(error)
  })

// redirect user to `url`
ParameterTypeDescriptionRequired
planIdstringThe plan id corresposnding to the plan you created on Safepay's merchant dashboardYes
referencestringUniquely identify subscription with your internal identifier e.g. order_idYes
cancelUrlstringUrl to redirect to if user cancels the flowYes
redirectUrlstringUrl to redirect to if user completes the flowYes
authTokenstringThe generated authentication tokenYes
// To create subscription url

const generateUrl = (token: string) => {
  return safepay.checkout.createSubscriptionWithToken({
    cancelUrl: 'https://www.google.com/',
    redirectUrl: 'https://www.google.com/',
    planId: 'plan_33e626b3-d92e-40b3-a379-4f89d61f8c83',
    reference: 'cc3f2475-4fb1-4d80-99f8-3a582a496fa6',
    authToken: token
  })
}

// To generate authentication token

safepay.authorization
  .create()
  .then(token => {
    generateUrl(token)
    // redirect user to `url`
  })
  .catch(error => {
    console.log(error)
  })

Cancel Subscription

ParameterTypeDescriptionRequired
subscriptionIdstringThe id for subscription you wish to cancelYes
safepay.subscription
  .cancel('sub_c94f2ffa-78cf-4de5-80c0-f57e3d1ce746')
  .then(response => {
    console.log(response)
  })
  .catch(error => {
    console.log(error.response.data.error)
  })

Pause Subscription

ParameterTypeDescriptionRequired
subscriptionIdstringThe id for subscription you wish to cancelYes
behaviorstring'KEEP_AS_READY' or 'MARK_UNCOLLECTIBLE' or 'MARK_VOID'Yes

Opt for marking the payment transaction as 'KEEP_AS_READY' during the pause, to retain payment readiness during subscription pause and automatically adjust the billing cycle for uninterrupted service when the payment ends

Opt for marking the payment transaction as 'MARK_UNCOLLECTIBLE' during the pause, indicating no collection attempts during this period. Ideal for mainting clear billing records

Opt for marking the payment transaction as 'MARK_VOID' during the pause, rendering it null and preventing any accidental payment processing. A safeguard for for accurate financial records

safepay.subscription
  .pause({
    behavior: SubscriptionPauseBehavior.MarkUncollectible,
    subscriptionId: 'sub_c94f2ffa-78cf-4de5-80c0-f57e3d1ce746'
  })
  .then(response => {
    console.log(response)
  })
  .catch(error => {
    console.log(error.response.data.error)
  })

Resume Subscription

ParameterTypeDescriptionRequired
subscriptionIdstringThe id for subscription you wish to cancelYes
safepay.subscription
  .resume('sub_c94f2ffa-78cf-4de5-80c0-f57e3d1ce746')
  .then(response => {
    console.log(response)
  })
  .catch(error => {
    console.log(error.response.data.error)
  })

Keywords

safepay

FAQs

Package last updated on 11 Jul 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts