@stripe/scripts
TypeScript icon, indicating that this package has built-in type declarations

0.0.2 • Public • Published

Stripe Scripts SDK

Development setup

Prerequisites

  • Node.js (v18.14.0 or higher)
  • npm (v9.3.1 or higher)

Installation

npm install @stripe/scripts

Authoring scripts

Here is an example script:

import type {
  DiscountCalculationFunction,
  DiscountableItem,
  DiscountResult,
} from '@stripe/scripts/discount_calculation';

import type {PositiveMonetaryAmount, Percent} from '@stripe/scripts';

/**
 * Configuration for the discount calculator function
 */
export type DiscountCalculatorConfiguration = {
  max_discount_amount: PositiveMonetaryAmount;
  discount_percent: Percent;
};

/**
 * Gives a percentage off discount upto a maximum discount amount
 *
 * @param {DiscountCalculatorConfiguration} config - The configuration containing max discount amount and discount percent
 * @param {DiscountableItem} item - The items to apply discounts to
 * @returns {DiscountResult} - The discounts applied to the items
 */
const percentOffUptoMaxDiscount: DiscountCalculationFunction<
  DiscountCalculatorConfiguration
> = (
  config: DiscountCalculatorConfiguration,
  item: DiscountableItem,
): DiscountResult => {
  const {max_discount_amount, discount_percent} = config;
  let discountAmount = 0;

  if (
    item.gross_amount.currency.toLowerCase().trim() ===
    max_discount_amount.currency.toLowerCase().trim()
  ) {
    const discountAmountValue =
      (item.gross_amount.amount * discount_percent) / 100;
    discountAmount = Math.min(discountAmountValue, max_discount_amount.amount);
  }

  return {
    discount: {
      amount: {
        amount: discountAmount,
        currency: item.gross_amount.currency,
      },
    },
  };
};

export default percentOffUptoMaxDiscount;

You can explore the examples package for more example scripts as well as explore how you can set up your custom scripts package.

See the SDK Documentation for more information on how to use the SDK.

Packaging

Instructions for how to package and upload your scripts are coming soon!

Readme

Keywords

Package Sidebar

Install

npm i @stripe/scripts

Version

0.0.2

License

MIT

Unpacked Size

67.2 kB

Total Files

24

Last publish

Collaborators

  • nazli-stripe
  • stripejs
  • jordand-stripe
  • arashn-stripe
  • maxliu-stripe
  • hideokamoto-stripe
  • jackieosborn-stripe
  • billfinn-stripe
  • lins-stripe
  • stripe-bindings
  • jil-stripe
  • tomas-stripe
  • graceg-stripe
  • bibek-stripe
  • rado-stripe
  • yuluomeng
  • cyuen-stripe
  • fkuo-stripe
  • tomasf-stripe
  • chaves-stripe
  • porter-stripe
  • lemuel-stripe
  • ngrubb-stripe
  • cchalstrom-stripe
  • charliecruzan-stripe
  • mattpatt-stripe
  • ericfrank-stripe
  • tomer-stripe
  • mindy-stripe
  • tennhard-stripe
  • jackokerman-stripe
  • ianjabour-stripe
  • stegs-stripe
  • jeremyg-stripe
  • ebarrenechea-stripe
  • stevekaliski-stripe
  • matebek-stripe
  • henryx-stripe
  • kovalev-stripe
  • aaronhernandez-stripe
  • aywang-stripe
  • timbennett-stripe
  • davidme-stripe
  • kentwilliams-stripe
  • svenugopal-stripe
  • alexander-stripe
  • toluo-stripe
  • tillh-stripe
  • wooj-stripe
  • carlosmuvi
  • samer.alabi
  • joyceqin-stripe
  • jaynewstrom-stripe
  • gbirch-stripe
  • cttsai
  • luisv-stripe
  • lamflam-stripe