Anvil: Web Apps With Nothing but Python Transcripts
Chapter: Ecommerce and accepting payments
Lecture: Charging a credit card

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Well we have our Stripe service and our Stripe account all added. Let's go and try this. Over here what we want to do is to try and to run that code
0:09 when they click this button so if we double click that it'll generate the little event handler here for us. And let's be naive first here
0:17 and let's just try to run this code. I'm going to put this import statement up at the top. Just now look at that it's already there.
0:25 That's interesting. There we go. So we're going to take this payment and we're going to say the charge is this.
0:33 And I'm going to attempt to print out a charge. What a charge is, is a dictionary and it has all sorts of information in it.
0:41 What we want to get is this thing called the result and the result is going to be succeeded or something. Something else like fail, declined, whatever.
0:50 Succeeded is what we're hoping for. And let's change this to dollars. You can use whatever you want. I'm going to use USD.
0:59 And down here we want to have fitnessed like this. And the description will be Fitness Pro, or something like that.
1:11 All right and notice this is in cents here. All right so 9.99 times 100. Don't forget you'll be undercharging dramatically.
1:22 Okay so we're going to need to log in cause we want to get to our account and make it Go Pro. Perfect, we go over to our account.
1:32 And now we have our Go Pro Button. What happens if we click it? Boom look how awesome that is! Down here on the bottom left Powered by Stripe.
1:40 Top right Test Mode. And we can put in our email. I don't like this remember me. Forgot how to make it go away. I think is a way to hide it.
1:53 There certainly is a way to hide it in Stripe checkout. I don't remember the way to hide it in Envo. We'll see what we can find there.
1:59 Just don't check it. Now we need to attempt to do a credit card charge. Let's do a successful one first. Now here's the the task card we want to use:
2:09 4-2 straight across the board 6 well, 8, 4-2's, right? 16 digits, so 4-2-4-2-4-2-4-2-4-2-4-2-4-2. How do I know that?
2:19 If you search Stripe test credit card numbers there's a huge table of here are successful ones here are ones that are expired
2:27 and here are ones that are right all these different kinds. And then long as you put a date in the future and anything there, this will work.
2:35 So let's hit pay 9.99, now let's see what happens. If it works, we should print out the result and it should say succeeded.
2:44 That checkbox is good. We wait and we're not dealing with the outcome, yet, but here you can see this is all the information we got back.
2:56 Here's the payment ID. What we got the country, all that stuff is good on. Here's the charge ID. Sorry. And here is the...
3:07 Yeah, I guess those are the same. And here's the thing we're actually going to test against. All right, we can go back and get this
3:12 out of our Stripe Account through their reporting and stuff but we need to know did it succeed, or did it not succeed?
3:20 So let's go down here, and we'll say instead of printing it out, we'll say if charged, remember this is a dictionary so we say, get result.
3:31 If it equals succeeded let's give ourselves a little room to move that up the screen. If that's the case, we'll print. Yay! You're a Pro.
3:40 We're going to try this else print. Oops, that failed. Now we could even try to set our label. We have a label error. Like this.
3:51 It said it's text, to this, to do this. Like that, otherwise, let's make this invisible. Here, we'll just hide it.
4:06 Wow we already tested the success case. Let me just show you. We got a little more work to do and I'll come back to fix this in a second.
4:13 So if you go over here and we put in an email address doesn't really matter. Our test card that's going to succeed. Do the charge.
4:31 Here's our little print, yay we're a pro. But let's go back here again and we decide, you know what we don't want this. We hit cancel or escape.
4:40 Boom! Exception. Our app has crashed. Wait a minute. That is weird. What happened? So it turns out, that when this fails
4:51 it doesn't return not succeeded it just throws an exception. Okay. For better or worse, but that's what it does.
4:58 So we need to put this whole bit in here to a try. except. And now we got to come down here and do some more error handling.
5:10 I mean just copy some stuff over for you. Because there's... Notice this is the exception we get when it failed. This is when you canceled it.
5:20 There's other exceptions when it's expired or whatever. So we're going to say if that text is in the string representation
5:30 we're going to say that the purchase was canceled. Otherwise there's some error we don't understand and we're going to tell people about it.
5:36 All right, one more time. Go here. It's going to work for when we successfully enter a code but let's hit cancel. Name exception.
5:47 Spelling's hard, isn't it? Let's go down here. Try again. We hit cancel. Boom, down here, purchase canceled. Instead of our app going boom it crashed
6:00 we're just going to say, you know what? You didn't want to buy it, apparently. Or we could do nothing like we don't have to show them they canceled it.
6:06 They canceled it. Right? But what we don't want to happen is to have our app crash. All right, well we're working with Stripe and if this was live
6:13 we would be taking real money from real people. But are they pro? I refresh this page, or I go away and I come back? No. It still has the Go Pro
6:23 and our account type is basic, so we need to actually record this in our app.


Talk Python's Mastodon Michael Kennedy's Mastodon