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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@commercetools/connect-payments-sdk

v1.2.0

Published

Payment SDK for commercetools payment connectors

Downloads

3,800

Readme

@commercetools/connect-payments-sdk

The foundation library for building commercetools payment connectors. It handles all the boilerplate that every connector needs: commercetools API clients, session verification, JWT authentication, request context propagation, custom type management, and retry logic on concurrent modifications so connector authors can focus on PSP-specific business logic.

Installation

npm install @commercetools/connect-payments-sdk

Quick start

Call setupPaymentSDK once at application startup. It wires together all services and authentication hooks and returns them as a single object.

import {
  setupPaymentSDK,
  Logger,
  RequestContextData,
} from '@commercetools/connect-payments-sdk';
import { getRequestContext, updateRequestContext } from './context';
import { config } from './config';

// Adapt your application logger to the SDK Logger interface
class AppLogger implements Logger {
  debug = (obj: object, msg: string) => console.debug(msg, obj);
  info  = (obj: object, msg: string) => console.info(msg, obj);
  warn  = (obj: object, msg: string) => console.warn(msg, obj);
  error = (obj: object, msg: string) => console.error(msg, obj);
}

export const paymentSDK = setupPaymentSDK({
  apiUrl:       config.apiUrl,
  authUrl:      config.authUrl,
  clientId:     config.clientId,
  clientSecret: config.clientSecret,
  projectKey:   config.projectKey,
  sessionUrl:   config.sessionUrl,
  checkoutUrl:  config.checkoutUrl,
  jwksUrl:      config.jwksUrl,
  jwtIssuer:    config.jwtIssuer,

  // Return the current request context (called on every incoming request)
  getContextFn: (): RequestContextData => {
    const { correlationId, requestId, authentication } = getRequestContext();
    return {
      correlationId: correlationId || '',
      requestId:     requestId || '',
      authentication,
    };
  },

  // Merge updated fields back into the request context store
  updateContextFn: (context: Partial<RequestContextData>) => {
    updateRequestContext({
      ...(context.correlationId  ? { correlationId:  context.correlationId  } : {}),
      ...(context.requestId      ? { requestId:      context.requestId      } : {}),
      ...(context.authentication ? { authentication: context.authentication } : {}),
    });
  },

  logger: new AppLogger(),
});

Configuration

| Option | Description | |---|---| | apiUrl | commercetools REST API URL (e.g. https://api.europe-west1.gcp.commercetools.com) | | authUrl | commercetools auth URL (e.g. https://auth.europe-west1.gcp.commercetools.com) | | clientId | commercetools API client ID with manage_payments, view_sessions and introspect_oauth_tokens scopes | | clientSecret | commercetools API client secret | | projectKey | commercetools project key | | sessionUrl | commercetools Session API URL (e.g. https://session.europe-west1.gcp.commercetools.com) | | checkoutUrl | commercetools Checkout API URL (e.g. https://checkout.europe-west1.gcp.commercetools.com) | | jwksUrl | JWKS endpoint used to verify incoming JWTs (e.g. https://mc-api.europe-west1.gcp.commercetools.com/.well-known/jwks.json) | | jwtIssuer | Expected iss claim in JWTs issued by commercetools (e.g. https://mc-api.europe-west1.gcp.commercetools.com) | | getContextFn | Function that returns the current RequestContextData from your request-scoped store | | updateContextFn | Function that merges partial data into your request-scoped store (used by the SDK to propagate correlationId etc.) | | logger | Optional. Custom logger implementing the Logger interface. Defaults to the built-in commercetools structured logger. |

What setupPaymentSDK returns

const {
  // commercetools API services
  ctCartService,           // get carts, calculate payment amounts, add payments
  ctPaymentService,        // create/update commercetools payments and transactions
  ctOrderService,          // find orders
  ctPaymentMethodService,  // manage stored payment methods (tokenization)
  ctCustomTypeService,     // create/update commercetools custom types (used in post-deploy)
  ctAuthorizationService,  // obtain OAuth2 tokens for outbound calls
  ctRecurringPaymentJobService,

  // Raw commercetools API client (escape hatch for unsupported operations)
  ctAPI,

  // Request context provider
  contextProvider,

  // Fastify hooks — register these on your routes
  sessionHeaderAuthHookFn,      // verifies the commercetools session ID from the `x-session-id` header
  sessionQueryParamAuthHookFn,  // verifies the commercetools session ID from a query parameter
  jwtAuthHookFn,                // verifies a commercetools-issued JWT
  oauth2AuthHookFn,             // verifies a connector OAuth2 token
  authorityAuthorizationHookFn, // checks the caller has the required authority
} = paymentSDK;

Custom types

The SDK ships predefined commercetools custom type drafts for storing payment method details. Register them during connector post-deploy:

import {
  GenerateCardDetailsCustomFieldsDraft,
  GenerateSepaDetailsCustomFieldsDraft,
  GenerateInterfaceInteractionCustomFieldsDraft,
} from '@commercetools/connect-payments-sdk';

// In post-deploy:
await paymentSDK.ctCustomTypeService.createOrUpdatePredefinedPaymentMethodTypes();
await paymentSDK.ctCustomTypeService.createOrUpdatePredefinedInterfaceInteractionType();

// When processing a notification for a card payment:
const customFields = GenerateCardDetailsCustomFieldsDraft({
  brand: 'Visa',
  lastFour: '4242',
  expiryMonth: 12,
  expiryYear: 2027,
});

For connector-specific custom types, use createOrUpdate directly:

await paymentSDK.ctCustomTypeService.createOrUpdate(myConnectorTypeDraft);