Idris.AH
Idris.AH

Reputation: 420

Importing https://js.stripe.com/v3/ for use in google cloud functions

I am developing an Angular7 application using Stripe and Google Cloud Functions. I am handling payments via Stripe.

In this instance, I am attempting to handle subscription payments. In order to do this, I require the Stripe library so I can call the APIs. According to the Stripe documentation, the library should be imported like so:

<script src="https://js.stripe.com/v3/"></script>
const stripe = Stripe('pk_test_.......');

This is not an issue in my actual app as I just place the script tag in my index.html and the variable stripe in my component. However, because I want to call these Stripe functions from my cloud functions, there is no index.html to import the library.

How can I import this so I can call the createPaymentMethod() function? Keep in mind that my cloud functions are written in Typescript too. See below:

export const createSubscription = functions.https.onCall(async (data, context) => {
    //Verify call is being made from user from app
    const uid = context.auth && context.auth.uid;
    const cardElement = data.cardElement

    if(!uid) return "error";

    const stripe = Stripe('sk_test_....');

    //Create payment method for customer
    const paymentMethod = await stripe_pub.createPaymentMethod('card', cardElementObj.card, 
    {
        billing_details: {
        name: userName,
        email: firebaseUserEmail
        },
    });

In this case, I just get the error:

Property 'paymentMethods' does not exist on type 'Stripe'

I am actually able to import the actual stripe APIs themselves, using the installed library of https://github.com/stripe/stripe-node.

var stripe = require('stripe')('sk_test_...');

const paymentMethod = await stripe.paymentMethods.create('card',cardElementObj.card);

However, the subtle difference here is that, although both methods achieve the same outcome, they both take different parameters. I am using Stripe elements, which means I have a card element, which is useful in the first example, but, not the second, where you require the actual card number, expiry, etc. Hence why I want to import it using the first method.

Essentially, the question is, how can I import a JS file e.g. into my Cloud Functions to be used?

Can anyone help me understand how to import this library for use in my cloud functions? Or maybe I could download the JS file and import it that way? Any help is appreciated!

Thanks!

Upvotes: 1

Views: 918

Answers (1)

Max
Max

Reputation: 1523

You could use the Library from https://www.npmjs.com/package/stripe-angular.

Then you don't have to import the Module by yourself.

If you like to import the Module by yourself please follow the answered post in this question: In Angular 5, how can I import NPM modules with no @types

Upvotes: 1

Related Questions