Python for Entrepreneurs Transcripts
Chapter: Making money (credit cards and businesses)
Lecture: Calling the Stripe API
0:00 OK, we have all the information we need, I guess that we probably want to send this email after this successful purchase,
0:09 so we've got this, we need this little print out, I don't think we need that anymore,
0:13 that was just a test that we were getting the user and the album passed through correctly,
0:17 we got our Stripe token correctly, now it's just down to doing this bit of code, let me just verify logged_in_user, album, price and token,
0:26 user, album, price and token. OK, so there is two steps to do, one is to save this to the database,
0:33 that is going to be easy, we are going to do that here, so let's just go ahead, we are going to create our purchase, it's AlbumPurchase,
0:41 we want to create one of these and let's see what it takes, it has an id, we don't have to do anything there, created is auto-set, these four though,
0:52 description, let's set description last, amount_paid, say like this purchase=..., purchase.amount_paid, is going to be amount_paid_usd,
1:05 purchase.user_id is going to be user.id, album_id is going to be album.id and that's like this,
1:18 finally, we'll just say purchase.description is going to be something like, we'll set that in the line above
1:27 and then we just say session.add(purchase), session.commit. Standard SQLAlchemy. So this records it for us.
1:36 The other thing we need to do is record it in Stripe. So let's go over here, and we'll say CreditCard..., I moved all the Stripe stuff over here,
1:48 we'll say complete_stripe_purchase and it's going to take the token, description and the price in US dollars, amount_paid...
1:57 So let's go and set this description, OK, so that's what that's going to be,
2:02 and then, I think in order to complete this purchase, we are also going to need the identity, so the email, but that may come with the token,
2:09 we're going to find out in the second. No, actually, we are not going to pass the email here,
2:14 because it was previously associated with the token in that checkout form, so it will just remember the email, so we are going to do this,
2:21 and then we are also going to say StoreService.__record_purchase() and it needs user.id, album.id, amount_paid_usd and the description.
2:34 So those two things we are going to do, I probably should add a little more validation and error handling,
2:40 but for this demo, we are just going to roll with this. OK, so final step is to implement that right there.
2:46 You can see we haven't gotten far. It turns out the Stripe API is quite simple,
2:52 we just need to use the Stripe package, so I am going to go up here and import it, and PyCharm will take care of it for us on a couple of levels,
3:00 so it is going to say first of all, install that package, that seemed to go well, next add it to the requirements, to the setup, OK,
3:10 and now this last one is hey you are not using it, well, that is what we are going to do now.
3:15 So the way it works is we have to set up, first compute the price, price in USD, price_in_cents (USD) or let me just leave like price_in_cents,
3:27 we are going to round this up, and we are going to take the price in US dollars
3:33 and we are going to round base 10 and of course convert that from dollars to cents, OK, so there is our price, we already have our description,
3:42 and the next thing we need to do is set the API key so we'll say stripe.api_key = CreditCardProcessor.__private key, like so,
3:56 not the public key, the private key, this is the one you don't share, and then all we have to do is create a charge, say stripe.Charge.create
4:07 and here we have to pass a few things, the amount, it's going to be equal to price_in_cents,
4:16 you don't want to get one one hundredth of what you expected, do you? No, currency='USD' source is going to be the stripe_token
4:32 and the description=desc. And then we can return the charge for whoever wants to inspect it, record details about it and so on.
4:44 OK, so let's just do a quick thing back here, just do a cleanup, and let me just print out the charge, so that we can see what happens here.
5:00 Now there is a lot of moving parts, but I think we got it right, let's give it a try,
5:04 so I want to come over here, to this album and I want to buy the album, I am going to put in my test card, remember you will get really good at this,
5:13 because you do this a lot, we can go to our service, it's going to finalize this with Stripe and we of course did something wrong here,
5:25 our stripe error invalid request looks like I need to convert that to an integer. I thought we were so close. OK, let's do this.
5:36 Try it again, Now I could try to do this again, let's see, is it going to rerun it or is it going to crash. Oh look at that, store success,
5:53 now we are missing the template so it doesn't feel so good, right, it didn't go "yes, you purchased just album, thank you,
5:59 here is your download link" or whatever, but, it went through, the charge went through. So here you can see all the information we got back.
6:10 This is what came back from Stripe. OK, here is the amount, amount refunded, etc, etc,
6:19 the name of the purchase, the id, this is the thing we can go back and look up in our Stripe dashboard, the exact name of the id
6:27 so maybe we want to put that on the purchase record, it would be a really good idea to do that, we are not doing it, and so on.
6:35 OK, so our Stripe purchase worked, we just need to write that template, so I'll do that and pause the video and you can check it our in a second.
6:44 Here we go, that's better, right, I just wrote the template and refreshed it and notice I also added the actual album that we purchased so you can say
6:52 thank you for purchasing such and such, you can have a download link, whatever you need to do to like get them their deliverable
6:59 that they are looking for when they purchased it. So over here, we already had in the "complete", we had the album id,
7:06 so I just took that onto the end and then just again, grabbed it and success, we also have a failed and it just says "sorry, that failed, try again",
7:15 we could grab the error message out of the Stripe colon, things like that and carry some more messages on in the fail thing,
7:24 but for the most part those were already done by Stripe, there will be a few that could fail, so for example, if I hit the back button here,
7:32 and I refreshed it, trying to apply this token twice would cause an error, things like that.
7:36 OK, so there you have it, we have added credit card processing via Stripe checkout to our application. Remember, all you need is a bank account,
7:47 that can accept direct deposits or automatic, electronic transfers to have a Stripe account, so theoretically,
7:53 if this were set up correctly and it was not in test mode, that money would be transferred two days later, there is a two day delay
8:02 and transferring the money that you get, Stripe holds on to it for a little bit in case there is some kind of complaint,
8:07 and then they send it on your way.