3.0.1 • Published 7 months ago

@sfpy/node-sdk v3.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

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

Create checkout link

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

Create subscription link

ParameterTypeDescriptionRequired
planIdstringThe plan id corresposnding to the plan you created on Safepay's merchant dashboardYes
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'
  })
  .then(url => {
    console.log(url)
  })
  .catch(error => {
    console.error(error)
  })

// redirect user to `url`

Create authorization token first and then generate subscription link

ParameterTypeDescriptionRequired
planIdstringThe plan id corresposnding to the plan you created on Safepay's merchant dashboardYes
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',
    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)
  })
3.0.1

7 months ago

3.0.0

7 months ago

2.0.2

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago