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 🙏

© 2024 – Pkg Stats / Ryan Hefner

stripe-goose

v1.5.5

Published

A node package for handling stripe payments with a user id saved in a mongoose document

Downloads

27

Readme

Stripe-Goose

This is a package for handling stripe actions using a customerId value in a mongoose document's field.

Instantiating the Object

All the methods are attached to one exported class.

const stripegoose = new Stripegoose(stripeId,User)

The constructor calls for a stripe secret key as its first argument, and a mongoose collection for its second argument. This Mongoose collection's schema must include "stripeId" as a field (and it should be of type "string").

Methods

Note: Most the following methods require a 'docId.' By this, I just mean the id of the mongoDB document that you are attaching the stripe customer to. This document needs to be part of the same collection that was passed in the constructor. For the examples below, we are using the id of a particular user document that is in our User collection.

The following methods are available:

createCustomer(docId)

const userId = "6343333d7d02232f0f469664"
stripegoose.createCustomer(userId)

This function will use the stripe API to create a customerId, which then will be saved in the "stripeId" field of the document.

addCard(docId,cardInfo)

    const userId = "6343333d7d02232f0f469664"
    const card =   {
        number: '5555555555554444',
        exp_month: 10,
        exp_year: 2023,
        cvc: '314',
    }
    await stripegoose.addCard(userId,card)

This will add the card as a payment method on stripe. This payment method will not be saved to the database.

addPaymentMethodWithId(userId,paymentMethodId)

    const paymentMethodId = "pm_934nmvf893nv89jn3vf"
    const userId = "6343333d7d02232f0f469664"
    stripegoose.addPaymentMethodWithId(userId,paymentMethodId)

This method is useful if the client is creating the payment method on the frontend. After creating it, they can post to the server the paymentMethodId so you can add it to their customer profile on stripe.

getUsersCards(docId)

    const userId = "6343333d7d02232f0f469664"
    const cards = await stripegoose.getUsersCards(userId)

This will retrieve all the user's cards using the Stripe API.

getUsersPaymentMethodIds(docId)

This operates like the above method, but just returns an array of the paymentMethodIds.

makePayment(docId,paymentMethodId,amount,currency)

const userId = "6343333d7d02232f0f469664"
const response = await stripegoose.makePayment(userId,"pm_39493ndf839njv90nj3f",9995,"usd")

Payment method ids can be fetched with stripegoose.getUsersCards(docId) The amount is given in cents. The code for various currencies can be found in stripe's documentation.

removeCard(paymentMethodId)

stripegoose.removePaymentMethod("pm_1Lr8RgIL4JNHFQKVySUDxcja")

Note that you do not need the doc's id to remove a payment method.

updateMetaData(paymentMethodId,metadata)

const paymentMethodId = "pm_934nmvf893nv89jn3vf"
const metadata = {tag:"Business Card"}
stripegoose.updateMetaData(paymentMethodId,metadata)

This is useful if you want to store information about the card that is not a built in field for the Stripe API. This means that you do not have to create a separate model for storing these values in your database.

radioPaymentMetaData(userId,metaDataTag,paymentMethodId)

    const userId = "6343333d7d02232f0f469664"
    const metaDataTag = "isDefault"
    const paymentMethodId = "pm_934nmvf893nv89jn3vf"
    stripegoose.radioPaymentMetaData(userId,metaDataTag,paymentMethodId)

The above example sets isDefault to true for the given payment method and sets isDefault to false for all other payment methods for that customer.

Further Development

I created this library to help with a specific e-commerce site I was interning at. It includes at this point only the functionality that that specific project required. If there are additional methods that would be helpful for your project, please reach out to me at [email protected] for suggestions.