Socket
Book a DemoInstallSign in
Socket

midtrans-snap

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

midtrans-snap

(Un)official Midtrans Snap Client wrapper

1.0.1
latest
Source
npmnpm
Version published
Weekly downloads
5
-44.44%
Maintainers
1
Weekly downloads
 
Created
Source

midtrans-snap

(Un)official Midtrans Snap Client (SnapJS)

npm npm

Features

  • Bundler friendly, support ESM and CJS
  • Strongly typed, written with Typescript
  • Promisify callback, easy to use with async-await

Installation

# Using NPM
npm install --save midtrans-snap

# Using Yarn
yarn add midtrans-snap

Usage

import { initSnap, useSnap } from 'midtrans-snap'

// You need run this once
initSnap('YOUR_CLIENT_KEY', 'sandbox'/* or 'production' */)

// Later, you can call useSnap() anywhere
const snap   = useSnap()
const result = await snap.pay('SNAP_PAY_TOKEN')

Nuxt 3

Using this in Nuxt 3 is quite simple, just create the plugin plugins/snap.client.ts.

export default defineNuxtPlugin(() => {
  initSnap('YOUR_CLIENT_KEY', 'sandbox')
})

Promisify callback

On original one, SnapJS has 4 callbacks: onSuccess, onPending, onError, onClose. This library united this into single Promise, so you can await it.

  • onSuccess and onPending will resolve the Promise
  • onClose and onError will reject the Promise
import { useSnap, isCancel } from 'midtrans-snap'

try {
  const snap   = useSnap()
  const result = await snap.pay('SNAP_PAY_TOKEN')

  if (result.transaction_status !== 'pending')
    console.log('Payment Sucess')
} catch (error) {
  if (isCancel(error)) {
    console.log('Customer closed the popup without finishing the payment')
  } else {
    console.log('Payment error')
  }
}

Contribution

  • Clone this repository
  • Play Caramelldansen in background (very important)
  • Run deps using yarn install
  • Write your additional feature
  • Don't forget to write the test
  • Open PR

License

This project publish under MIT LIcense, see LICENSE for more details.

Keywords

midtrans

FAQs

Package last updated on 07 Nov 2023

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

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.