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