Anvil: Web apps with nothing but Python Transcripts
Chapter: Ecommerce and accepting payments
Lecture: Converting the user account to pro

Login or purchase this course to watch this video and the rest of the course contents.
0:00 So the next thing we to do is switch this user account
0:03 into actually being a pro.
0:04 We saw that if we click this and we make them a pro
0:08 it still just says that they're basic.
0:10 And that's because we're taking their money
0:11 yay, that's fun
0:13 but we're not actually changing their account.
0:17 Down here, instead of doing yay, you're a pro
0:19 we need to do something.
0:20 Let's go to this data access
0:24 and say go pro.
0:27 That's not a thing yet, but it's going to be.
0:29 So over here
0:30 this is where we've been hiding away all
0:31 of our interaction with the server, right?
0:34 Here's how we get our averages, set our details and so on.
0:37 So let's just do something real similar.
0:41 Here. Like this. So, we want the user.
0:48 It's what we're going to work with.
0:51 And what we need to do is we need to go
0:53 and make a change over on the server.
0:58 We look at our data table here
1:00 and the user service
1:01 they have an is pro, true or false?
1:04 Right, so what we need to do is set that to true.
1:09 But because we're setting it to true
1:10 this user is being cached, right?
1:13 We got a big performance boost from doing this here
1:17 where we automatically cache it
1:19 and only get a new bit of information from the server
1:22 if it's not here.
1:23 So we need to set this to none
1:26 so that next time someone asks for the user
1:29 they have to go over to the account, pull it down
1:32 and they'll see that it has a new value for its pro.
1:35 So we'll say go pro here.
1:37 Going to be something on the server side
1:39 remember the management service data side is the only thing
1:42 that has access to write against the database
1:46 which is appropriate.
1:53 So then we're going to do this.
1:59 And get the user, for some reason if there's no user
2:02 I don't know what to do about this, we can't fix it.
2:04 This is pretty hard, I want to say is pro equals true.
2:09 Done.
2:10 We've implemented the server side, that's pretty sweet.
2:12 Let's see, let's work our way back
2:14 we go to data_access.
2:15 We're calling go pro and we're refreshing
2:18 or knocking out the user there.
2:20 We got to work our way back here to the account page.
2:23 One thing that we could do is we could change the UI around
2:27 so now it says pro in the account type
2:31 and it says, you know, hides that button.
2:33 We could also just reload this page
2:35 which I think is probably the right thing to do.
2:37 And so let's just go up here and import navigation.
2:43 It'll reload itself, it'll recompute.
2:46 You know, is the user a pro
2:47 and all this stuff will rerun again
2:48 so that's probably best, I'm going to go with that.
2:51 Go account, boom.
2:54 So let's refresh the page basically and we'll be good.
2:57 Go to the database, record that, they are a pro.
3:01 In reality, I would also record some of the details
3:03 about this charge. That they attempted it
3:05 that it either succeeded or failed.
3:07 You want to have some record of people trying
3:09 to do e-commerce stuff on your site.
3:12 That would be a separate data table.
3:14 We're not going to do it, 'cause it's not real, but, you know.
3:16 Auditing in a real app around accounting
3:19 and credit cards is a good idea.
3:22 All right, are we ready to pros?
3:23 I'm feeling like a pro already.
3:25 So here we go.
3:27 I come over here, I want to be a pro.
3:29 Cancel, actually no.
3:31 I want to be a pro, let's go pro again.
3:33 Put my email address, do my four twos.
3:36 All right, so what this should do is it should go
3:38 make the charge, that should succeed
3:40 'cause this number, this credit card is valid
3:42 and then it's going to go to the server
3:44 and tell account that it is pro.
3:46 It's going to reload the page
3:47 which should make this button go away
3:48 and make that text go to pro.
3:50 Ready? Here we go.
3:54 Boom. Yeah, that purchase canceled
3:57 I need to hide that, don't I?
3:59 All right, cool.
4:00 I guess that was already there
4:01 from the first time I hit it
4:02 but yeah, perfect, this looks great.
4:05 So now our account is pro
4:06 if we go and look at the account
4:08 of course it should be also a pro over here.
4:13 And our users, and sure enough, there it is.
4:17 I guess one super small improvement we could make is
4:24 when I hit that, it kind of looked weird
4:27 that it was left over from before.
4:30 So let's, whenever they click that button
4:32 first thing, the label goes away.
4:35 So while Stripe is thinking and the server is thinking
4:37 we're not going to have some kind of remnant error
4:40 that might freak people out.
4:41 I think we're good.
4:43 How many lines is this?
4:44 This is like 22 lines of code.
4:47 Over here.
4:49 Probably another four or five over here
4:54 another five or six.
4:57 That's it for our e-commerce
4:58 and in like 60 lines of code
5:00 we just added e-commerce support to our site.
5:03 And we also want to send them an email probably
5:05 with a receipt and all this kind of stuff.
5:06 There's, like, real e-commerce is a little more complicated.
5:09 But that was pretty quick and easy and, yeah
5:13 I use Stripe checkout for my business all the time.
5:16 I think it's great, so definitely recommend
5:18 that you give it a try.