Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

James Rush
PLUS
James Rush
Courses Plus Student 56 Points

Does anyone know how to integrate stripe payments with the user signup page from the PHP User Authentication course?

I don't quite know where to even begin with this. I am trying to integrate Stripe payments with the user registration page from the PHP User Authentication course. Basically, I am trying to find a way to use Stripe, with a form on the page, to charge a user at signup and based on their selection allow them a different user level on the site, which would have to be set in the database. If anyone knows at all how this could work it would be very helpful. Thank you.

4 Answers

Kevin Korte
Kevin Korte
28,149 Points

Hey James, Have you started anything? The scope of this question is huge, so it's hard to be specific, but we can certainly help tweak some things if you get stuck.

Stripe has amazing documents. They are a little daunting at first, but they are very through and detail. I'd suggest starting to read through their docs soon, they walk you through the process step by step. They API documentation is awesome as well. And if you use their new elements feature, you can put a CC input they they built, which includes validation, and style it to your site.

In short though, user goes through the sign up process, their CC number gets set straight to stripe, which stripe authenticates and returns to you a token. You attach a new hidden input to your form, setting the hidden inputs value to be the token value returned. Then finish submitting your information to stripe, such as the plan or subscription the customer just bought, and the CC token, which stripe will now charge. If all of that works okay, than you can keep some local records of the client, their info, etc, however NEVER EVER save their credit card number. You can get yourself into all kinds of trouble (legally) by doing so. That's why stripe passes back a card token, which you shouldn't need to save either.

Stripe also sends webhooks back to a URL you configured, which is a great way to keep your local DB up to date with what is happening on stripe. Just always think of Stripe as the master, and your local db as the slave. The only real reason to save anything from stripe (which is a good idea) is so that you can show your clients what they subscribed to, their cost, etc without having to hit stripe up with a request every single time.

Hopefully that gets you pointed in the right direction

James Rush
James Rush
Courses Plus Student 56 Points

Hello Kevin,

I have a form set up on the registration page but it is only for Stripe, do you know how can I catch the user data and submit it to the database.db (if possible only when it has reached the charge.php page)?

Also if you know how I can add the Role id selected in on the form as well to the database.db it would be very helpful. If it changes anything for the role id help, I have added some extra roles for different memberships as the PHP User Authentication course only had an admin and one other role so there are now multiple roles.

Thank you for the help, Kevin.

Kevin Korte
Kevin Korte
28,149 Points

Well, I'm a little bit dusty on specific PHP code, do you have anything you've written yet?

I can tell you, as I have very familiar with stripe though in a node js environment, that your order of operations is going to be.

  1. Submit credit card details to stripe. Stripe to validate, and will return a token if successfully validated, or an error. If you receive a token, no charge has been made yet.
  2. Append token to registration form in hidden input. Call stripe again, this time creating a new customer, and either subscribing a customer to a subscription (monthly recurring payments), or a one-time charge, in either event set the source of the subscription or charge to the token received in step one. In this step you'll be tying a token and a charge or subscription to a customer object, so stripe can automatically charge on a recurring subscription basis, or will charge the customer the one time fee. If this is successful, stripe replies with a customer object, that will include details about the customer, their subscription/charge, etc. If this fails, stripe will return an error
  3. If success, take the information returned from stripe, and parse it into a way to save to your local database. This is where the PHP courses on TH should come in handy to teach you to connect your app to your database. You can store as little, or as much as you need in your local database. The Stripe API docs will tell you exactly what you can expect to receive.

There is this to really help you get started: https://github.com/stripe/stripe-php

You can certainly add roles, and tie roles to subscription levels if you want. That doesn't really apply to stripe as much. Stripe is just focused on charging the right person, the right amount, at the right time. Roles, permissions, and user accounts on your app are really just left to you, if I'm reading your question correctly.

Let me know if that helps.

Kevin Korte
Kevin Korte
28,149 Points

Hey James, I'll try to look at this a little more later, but not sure how much further I can invest, as this is a big task, but I'll try.

In the mean time have you seen this: https://teamtreehouse.com/library/php-the-stripe-api

James Rush
PLUS
James Rush
Courses Plus Student 56 Points

Thank you, Kevin, I did have a look at the Treehouse Stripe course. However, like I said there doesn't seem to be any tutorials or helpful information in general anywhere on merging Stripe with an existing PHP Auth setup that has a .db file instead of an external database. I seem to have ended up with a set of pieces, I have my site with all the designs setup, then the PHP Auth setup on a different site and the Stripe checkout set up with the Auth site.

Mixing the Auth and Stripe together is the area where I am just completely lost and there doesn't seem to be anything that can help and I've been through a lot of tutorials, blogs, courses and GitHub with no luck finding a way to do accomplish it and finish everything I've got so far.

As I said any help would be great as I am at a complete roadblock. Thank you Kevin.

Kevin Korte
Kevin Korte
28,149 Points

Sweet! Glad to hear that