New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@karhoo/demand-bloc-payment

Package Overview
Dependencies
Maintainers
5
Versions
112
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@karhoo/demand-bloc-payment

BLoC to work with Karhoo Payment API

  • 20.1.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
118
increased by2850%
Maintainers
5
Weekly downloads
 
Created
Source
Karhoo logo

@karhoo/demand-bloc-payment

BLoC to work with Karhoo Payment API


License

Installation

npm i @karhoo/demand-bloc-payment

Warnings

This library uses Promise. For old browsers, e.g. IE11 you must bring your own polyfill. You can use js-core@3 to polyfill Promise

Usage

import { getApi } from '@karhoo/demand-api'
import { PaymentBloc, BraintreeProvider } from '@karhoo/demand-bloc-payment'

const organisationId = '1a12345d-e111-1da1-111f-a1111e1e11f1'
const currencyCode = 'GBP'
const amount = 10

const payer = {
  id: 'id',
  email: 'email@of.user',
  first_name: 'firstName',
  last_name: 'lastName',
}

const braintreeOptions = {
  organisationId,
  currencyCode,
}

const providers = {
  Braintree: new BraintreeProvider(paymentService, braintreeOptions),
}

const options = {
  paymentCardsEnabled: true,
}

const block = await PaymentBloc.create({
  providers,
  paymentService,
  options,
  cardsInfo,
})

await bloc.initPayment()

if (bloc.validatePaymentDetails()) {
  const result = await bloc.verifyCardWithThreeDSecure(amount)

  console.log(result)
}

await bloc.dispose()

For payment without 3d secure verification:

if (bloc.validatePaymentDetails()) {
  const result = await bloc.getPaymentNonce()

  console.log(result)
}

Cards support:

const cardsInfo = {
  cards: [],
  payer: undefined,
  setPaymentCards(cards, payer) {
    this.cards = cards
    this.payer = payer
  },
  getSelectedPaymentCard() {
    return this.cards[0]
  },
  clear() {
    this.cards = []
    this.payer = undefined
  },
}

const bloc = new PaymentBloc(provider, { paymentCardsEnabled: true }, cardsInfo)

await bloc.initPayment()

Save card:

const result = await bloc.savePaymentCard(payer)

console.log(result)

All bloc methods, except for dispose, should be called only after initialization is complete otherwise it may lead to unexpected behavior. If dispose is called before initialization is complete initialization will be cancelled and errors.operationCancelled will be thrown.

Providers

Braintree

import { HostedFieldsTokenizePayload, ThreeDSecureVerifyPayload } from 'braintree-web'
import { getApi } from '@karhoo/demand-api'
import { BraintreeProvider, BraintreeProviderOptions, braintreeDefaultValues, braintreeErrors } from '@karhoo/demand-bloc-payment'

const organisationId = '1a12345d-e111-1da1-111f-a1111e1e11f1'
const currencyCode = 'GBP'

const options: BraintreeProviderOptions = {
  organisationId,
  currencyCode
}

const provider = new BraintreeProvider(getApi().paymentService, options)

`options` should have `organisationId` and `currencyCode`. Other fields are not required

if hostedFields is not provided following defaults will be used:

{
  hostedFieldsConfig: braintreeDefaultValues.defaultHostedFieldsConfig
  hostedFieldsStyles: braintreeDefaultValues.defaultHostedFieldsStyles
}

if invalidFieldClass is not provided braintreeDefaultValues.defaultInvalidFieldClass will be used. if withThreeDSecure is not provided braintreeDefaultValues.default3DSecureStatus will be used.

Initialize provider:

await provider.initialize()

In case of failure either braintree specific error or braintreeErrors.authorizationToken will be thrown

Validate payment form:

const isValid: boolean = provider.validatePaymentForm()

Tokenize hosted fields:

const tokenizeResponse: HostedFieldsTokenizePayload = await provider.tokenizeHostedFields()

Verify:

const amount = 10

const result: ThreeDSecureVerifyPayload = await provider.verifyCard(amount, tokenizeResponse.nonce)

Save card:

await provider.saveCard('nonce')

Get saved cards:

const data = await provider.getSavedCards()

Dispose:

await provider.dispose()

All provider methods, except for dispose, should be called only after initialization is complete otherwise it may lead to unexpected behavior. If dispose is called before initialization is complete initialization will be cancelled and errors.operationCancelled will be thrown.

Keywords

FAQs

Package last updated on 17 Jan 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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc