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


Talk Python's Mastodon Michael Kennedy's Mastodon