Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stripe.redirectToCheckout does not exist on type stripe #69

Closed
donemmx opened this issue Sep 25, 2020 · 7 comments
Closed

stripe.redirectToCheckout does not exist on type stripe #69

donemmx opened this issue Sep 25, 2020 · 7 comments

Comments

@donemmx
Copy link

donemmx commented Sep 25, 2020

please how do i fix this issue.
import { AngularFirestore } from '@angular/fire/firestore';
import { ServicesClass } from './../services/service';
import { Component, OnInit } from '@angular/core';
import Stripe from 'stripe';

@component({
selector: 'app-plans',
templateUrl: './plans.component.html',
styleUrls: ['./plans.component.css'],
})
export class PlansComponent implements OnInit {
plans;
constructor(private afs: AngularFirestore, public service: ServicesClass) {
this.afs
.collection('products')
.ref.where('active', '==', true)
.get()
.then(function (querySnapshot) {
querySnapshot.forEach(async function (doc) {
console.log(doc.id, ' => ', doc.data());
// const priceSnap = await doc.ref.collection('prices').get();
// priceSnap.docs.forEach((doc) => {
// console.log(doc.id, ' => ', doc.data());
// });
});
});
}

async subscribe() {
const docRef = await this.afs
.collection('users')
.doc(this.service.userData.uid)
.collection('checkout_sessions')
.add({
price: 'price_1GqIC8HYgolSBA35zoTTN2Zl',
success_url: window.location.origin,
cancel_url: window.location.origin,
});
// Wait for the CheckoutSession to get attached by the extension
docRef.onSnapshot((snap) => {
const { sessionId } = snap.data();
if (sessionId) {
// We have a session, let's redirect to Checkout
// Init Stripe

    // const stripe = Stripe('pk_test_*****************');

    const stripe = new Stripe(
      'pk_********************',
      {
        apiVersion: '2020-08-27',
        typescript: true,
      }
    );
     stripe.redirectToCheckout({ sessionId });
  }
});

}

ngOnInit(): void {}
}

@donemmx
Copy link
Author

donemmx commented Sep 25, 2020

Its really of a big concern because i really need it to redirect me to stripe checkout from my angular app

@thorsten-stripe
Copy link
Contributor

@donemmx it looks like you're importing the stripe node library rather than Stripe.js. You will want to do this instead: https://github.com/stripe/stripe-js#installation

import {loadStripe} from '@stripe/stripe-js';

const stripe = await loadStripe('pk_test_TYooMQauvdEDq54NiTphI7jx');
stripe.redirectToCheckout({ sessionId });

@donemmx
Copy link
Author

donemmx commented Sep 25, 2020

please how where do i add const stripe = await loadStripe('pk_test_TYooMQauvdEDq54NiTphI7jx'); this line that won't make it throw errors.
i am quite new to this.

@donemmx
Copy link
Author

donemmx commented Sep 25, 2020

import { AngularFirestore } from '@angular/fire/firestore';
import { ServicesClass } from './../services/service';
import { Component, OnInit } from '@angular/core';
import {loadStripe} from '@stripe/stripe-js';

@component({
selector: 'app-plans',
templateUrl: './plans.component.html',
styleUrls: ['./plans.component.css'],
})
export class PlansComponent implements OnInit {
plans;
constructor(private afs: AngularFirestore, public service: ServicesClass) {
this.afs
.collection('products')
.ref.where('active', '==', true)
.get()
.then(function (querySnapshot) {
querySnapshot.forEach(async function (doc) {
console.log(doc.id, ' => ', doc.data());
const priceSnap = await doc.ref.collection('prices').get();
priceSnap.docs.forEach((doc) => {
console.log(doc.id, ' => ', doc.data());
});
});
});
}

async subscribe() {
const docRef = await this.afs
.collection('users')
.doc(this.service.userData.uid)
.collection('checkout_sessions')
.add({
price: 'price_1GqIC8HYgolSBA35zoTTN2Zl',
success_url: window.location.origin,
cancel_url: window.location.origin,
});
// Wait for the CheckoutSession to get attached by the extension
docRef.onSnapshot((snap) => {
const { sessionId } = snap.data();
if (sessionId) {
// We have a session, let's redirect to Checkout
// Init Stripe
// const stripe = Stripe('pk_test_*****************');
// );
const stripe = await loadStripe('pk_test_TYooMQauvdEDq54NiTphI7jx');
stripe.redirectToCheckout({ sessionId });
}
});
}

ngOnInit(): void {}
}

@thorsten-stripe
Copy link
Contributor

You need to make the onSnapshot callback function async so that you can call await within it:

  // Wait for the CheckoutSession to get attached by the extension
  docRef.onSnapshot(async (snap) => {
    const { sessionId } = snap.data();
    if (sessionId) {
      // We have a session, let's redirect to Checkout
      // Init Stripe
      const stripe = await loadStripe("pk_test_*****************");
      stripe.redirectToCheckout({ sessionId });
    }
  });

@donemmx
Copy link
Author

donemmx commented Sep 25, 2020

thank you so much it works. no more errors...hurray... awesome

@thorsten-stripe
Copy link
Contributor

Glad to hear that 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants