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