Selling Digital Content in Angular with Stripe Payments - Part 3

Fireship · Beginner ·🧠 Large Language Models ·8y ago

Key Takeaways

This video teaches how to sell digital content in Angular with Stripe Payments and Firebase

Full Transcript

welcome to part three of strike payments with angular and firebase in the last two episodes we integrated the stripe check out and charge the card with firebase cloud functions in this third episode we're going to use that charge to enable the user to purchase digital content in our app in this example we keep a balance on the user's account which can be increased by making payments or decreased by purchasing content when a user has sufficient funds on their account they can click a button that will bring up a modal window to confirm or cancel the purchase if they click confirm their account balance is automatically updated with the new balance the database will be updated with a purchases collection which tracks the user ID and the item IDs that they purchased the users current balance will be tracked on their actual account with a new property called balance which takes a number let's start by updating the cloud function we need it to update both the charge as well as the users balance in the same operation we want it to be atomic meaning if the charge fails the user balance update will fail also and vice-versa the first step is to create a balance variable defaults to zero and then when we get the customer record we'll update it with their existing balance when we get the stripe charge back we'll first check to make sure it exists and we'll also check to make sure that it's actually paid if these conditions are met we create an object where the key is a reference point in the database and the value is the data we want to update this allows us to update multiple database locations at the same time but be careful it performs a set operation so make sure you point to the very deepest node that you want to update now we can use the charge amount from stripe to increase the users balance and then send these updates together to the root of the database back in the app we can see the users balance is updated by the cloud function when they make a payment now we need to update the service to handle the payment process in order to get the user balance we first need the auth user ID so we'll use the switch map operator on the in Euler fire off observable we'll move this out into its own function and then subscribe to it and then struktur the balance itself will be emitted as an object so we just want to map that object to its actual value so we just get a number back in the observable from there we need to determine if the user has already purchased a product which we can do by seeing if that key exists under their purchases in the database this will also return an observable and we'll map it to a boolean value to get the true false value back instead of the entire object itself and the last step is to create a function that enables the user to actually make the purchase it requires the unique ID of the item being purchased as well as the amount and it records the time stamp from the firebase server this will guarantee your time stamps are consistent which would not be the case if you just use the JavaScript data object this function also needs to make an atomic update to update the balance as well as the purchase history in the same operation so we'll create another updates object and pass it the corresponding data the user balance gets decreased by the amount of the purchase and the purchase history gets the purchase object we can then pass these updates to the root of the database and call the update function now let's create a new component called the Buy Now component it's designed to be reusable so we'll take the viable ID as well as the amount from the parent component the buyable ID just represents any item that can be purchased we also create a variable to toggle the visibility of the modal during ng on an it we define the variables for the user balance observable and that has purchased observable then we create an event handler to change the visibility of the modal and the last function we need is for the user to actually confirm the purchase so this will just run the buy digital content function that we defined in the service when the purchase is complete we close the modal and you could also consider adding a success message or confirmation at this point but before we get into the HTML we're going to create a custom stripe pipe to format currency amounts this is strictly for the user and it will turn the value into a string with the proper decimal now striped amounts are 1/100 of the underlying currency so 500 and stripe equals five US dollars from a parent component we passed the unique ID as well as the price to the Buy Now component in the component itself we add the is active class when the show modal variable is true this is specific to Bulma CSS we can unwrap the balance observable to see if the user has sufficient funds on their account to make the purchase if so we'll show them that confirm or cancel buttons we can also show the user what their current balance is and what their balance will look like after the purchase the confirm button will actually make the purchase and the cancel button will just toggle the modal back to false if the user doesn't have enough money we'll show different modal content that says insufficient funds with their current balance the last step is to create a button that will open the modal in the first place if the users already purchased the item then we'll go ahead and just show button it says already purchased it's disabled notice how we always unwrap the observables in parentheses using the async pipe and then call any logic outside those parentheses before we start using the system let's also add some back-end data security rules on firebase when a purchase is made we want to make sure that user ID always matches the current auth user ID and we also want to prevent purchasers from being updated or deleted after they've been created so the data exists then we won't allow any write operations to take place now going back to the app we can take a look and make sure that everything is working as expected I currently have five dollars on my account which is insufficient to buy this product so we'll add additional credits through stripe which should increase the account balance from $5 to $25 and we'll see the account balance here update asynchronously once it's done processing then scribe and now we have sufficient cleanse on the account so we should be able to go ahead and buy the product that pulls up the confirmation screen if we click confirm it should reduce the balance by twenty dollars which it does and now we're back at five dollars again and we have access to the product that's it for stripe payments if this video helped you please like and subscribe and if you want a free copy of that book the angularfire Bank Survival Guide consider becoming a pro member or just 9 bucks a month you'll get a free copy of that book as well as access to our proton slack or one on one project consulting thanks for watching and I'll see you next time [Music]

Original Description

Full Code and Lesson: https://angularfirebase.com/lessons/angular-stripe-payments-part-3-sell-digital-content/ This is the third installment of Stripe Payments with Angular 4 and Firebase. Learn how to sell digital content, make atomic updates, and manage user balances. Get the book: https://leanpub.com/angularfirebase
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Fireship · Fireship · 26 of 60

1 Angular 4 Development and Production Environments with Firebase
Angular 4 Development and Production Environments with Firebase
Fireship
2 OAuth with Angular and Firebase Tutorial
OAuth with Angular and Firebase Tutorial
Fireship
3 Anonymous Authentication with Angular and Firebase - Lazy Registration
Anonymous Authentication with Angular and Firebase - Lazy Registration
Fireship
4 Angular Router Guards for Firebase Users
Angular Router Guards for Firebase Users
Fireship
5 Angular Firebase CRUD App with NoSQL Database Tutorial
Angular Firebase CRUD App with NoSQL Database Tutorial
Fireship
6 Upload Files from Angular to Firebase Storage
Upload Files from Angular to Firebase Storage
Fireship
7 How to Deploy an Angular App to Firebase Hosting
How to Deploy an Angular App to Firebase Hosting
Fireship
8 Sharing Data between Components in Angular
Sharing Data between Components in Angular
Fireship
9 Loading Spinners for Asynchronous Firebase Data
Loading Spinners for Asynchronous Firebase Data
Fireship
10 Angular 4 Transactional Email with Google Firebase Cloud Functions
Angular 4 Transactional Email with Google Firebase Cloud Functions
Fireship
11 Firebase Database Rules Tutorial
Firebase Database Rules Tutorial
Fireship
12 Autocomplete Search with Angular4 and Firebase
Autocomplete Search with Angular4 and Firebase
Fireship
13 Reddit Inspired Upvoting System with Angular and Firebase NoSQL
Reddit Inspired Upvoting System with Angular and Firebase NoSQL
Fireship
14 Angular Drag-and-Drop File Uploads to Firebase Storage
Angular Drag-and-Drop File Uploads to Firebase Storage
Fireship
15 Text Translation with Firebase Cloud Functions onWrite and Angular 4
Text Translation with Firebase Cloud Functions onWrite and Angular 4
Fireship
16 Custom Usernames with Firebase Authentication
Custom Usernames with Firebase Authentication
Fireship
17 Twitter-Inspired Follow Unfollow Feature with Firebase and Angular 4
Twitter-Inspired Follow Unfollow Feature with Firebase and Angular 4
Fireship
18 Simple Pagination with Firebase and Angular 4
Simple Pagination with Firebase and Angular 4
Fireship
19 How to Connect Firebase Users to their Data - 3 Methods
How to Connect Firebase Users to their Data - 3 Methods
Fireship
20 Add Toast Message Notifications to your Angular App
Add Toast Message Notifications to your Angular App
Fireship
21 Facebook-Inspired Reactions System with Angular and Firebase
Facebook-Inspired Reactions System with Angular and Firebase
Fireship
22 Learn NgModule in Angular with Examples
Learn NgModule in Angular with Examples
Fireship
23 Lazy Loading Components in Angular 4
Lazy Loading Components in Angular 4
Fireship
24 Stripe Checkout Payments with Angular and Firebase - Part 1
Stripe Checkout Payments with Angular and Firebase - Part 1
Fireship
25 Process Stripe Payments with Firebase Cloud Functions - Part 2
Process Stripe Payments with Firebase Cloud Functions - Part 2
Fireship
Selling Digital Content in Angular with Stripe Payments - Part 3
Selling Digital Content in Angular with Stripe Payments - Part 3
Fireship
27 Angular 4 Full Text Search with Algolia - Part 1
Angular 4 Full Text Search with Algolia - Part 1
Fireship
28 Algolia with Firebase Cloud Functions - Part 2
Algolia with Firebase Cloud Functions - Part 2
Fireship
29 Firebase Phone Authentication in Angular 4
Firebase Phone Authentication in Angular 4
Fireship
30 Top 7 RxJS Concepts for Angular Developers
Top 7 RxJS Concepts for Angular Developers
Fireship
31 Learn Angular Animations with 5 Examples
Learn Angular Animations with 5 Examples
Fireship
32 Advanced Firebase Data Filtering (Multi-Property)
Advanced Firebase Data Filtering (Multi-Property)
Fireship
33 Realtime Maps with Mapbox + Firebase + Angular
Realtime Maps with Mapbox + Firebase + Angular
Fireship
34 Angular Reactive Forms with Firebase Database Backend
Angular Reactive Forms with Firebase Database Backend
Fireship
35 Send Push Notifications in Angular with Firebase Cloud Messaging
Send Push Notifications in Angular with Firebase Cloud Messaging
Fireship
36 Top 7 Ways to Debug Angular 4 Apps
Top 7 Ways to Debug Angular 4 Apps
Fireship
37 Infinite Scroll with Angular and Firebase
Infinite Scroll with Angular and Firebase
Fireship
38 Use TypeScript with Firebase Cloud Functions
Use TypeScript with Firebase Cloud Functions
Fireship
39 Realtime Graphs and Charts with Plotly and Firebase
Realtime Graphs and Charts with Plotly and Firebase
Fireship
40 Role-Based User Permissions in Firebase
Role-Based User Permissions in Firebase
Fireship
41 User Presence System in Realtime - Online, Offline, Away
User Presence System in Realtime - Online, Offline, Away
Fireship
42 Location-based Queries with GeoFire and Angular Google Maps
Location-based Queries with GeoFire and Angular Google Maps
Fireship
43 Angular ngrx Redux Quick Start Tutorial
Angular ngrx Redux Quick Start Tutorial
Fireship
44 Angular Ngrx Effects with Firebase Database
Angular Ngrx Effects with Firebase Database
Fireship
45 Progressive Web Apps with Angular
Progressive Web Apps with Angular
Fireship
46 Angular Ngrx with Firebase Google OAuth User Authentication
Angular Ngrx with Firebase Google OAuth User Authentication
Fireship
47 RxJS Quick Start with Practical Examples
RxJS Quick Start with Practical Examples
Fireship
48 Send SMS Text Messages with Twilio and Firebase
Send SMS Text Messages with Twilio and Firebase
Fireship
49 Firebase Database Performance Profiling
Firebase Database Performance Profiling
Fireship
50 Native Desktop Apps with Angular and Electron
Native Desktop Apps with Angular and Electron
Fireship
51 Subscription Payments with Stripe, Angular, and Firebase
Subscription Payments with Stripe, Angular, and Firebase
Fireship
52 Firestore with AngularFire5 Quick Start Tutorial
Firestore with AngularFire5 Quick Start Tutorial
Fireship
53 Angular HTTP Client Quick Start Tutorial
Angular HTTP Client Quick Start Tutorial
Fireship
54 Google Sign-In with Firestore Custom User Data
Google Sign-In with Firestore Custom User Data
Fireship
55 Star Review System from Scratch with Firestore + Angular
Star Review System from Scratch with Firestore + Angular
Fireship
56 Angular Chatbot with Dialogflow (API.ai)
Angular Chatbot with Dialogflow (API.ai)
Fireship
57 Learn @ngrx/entity and Feature Modules
Learn @ngrx/entity and Feature Modules
Fireship
58 Infinite Scroll Pagination with Firestore
Infinite Scroll Pagination with Firestore
Fireship
59 Faster Firestore via Data Aggregation
Faster Firestore via Data Aggregation
Fireship
60 Contentful - CMS for Angular Progressive Web Apps
Contentful - CMS for Angular Progressive Web Apps
Fireship

Related Reads

📰
**AI Life: Answer, Repeat, Existential Dread**
Learn how AI models like Electra process and respond to questions, and why this raises existential questions about their purpose and function
Dev.to AI
📰
Mining for Emotion: Automatically Finding the Heart of Your Interviews
Automate emotion mining from interviews using AI tools like ChatGPT to gain valuable insights
Dev.to AI
📰
Weekly Update: ✨ fix(llm,npu): openrouter LLMResponse field names + depreca
Learn about the latest updates in AI, including fixes to LLMResponse field names and deprecated pydantic .json(), and new features like role-curated skill bundles
Dev.to AI
📰
Day 2: Understanding Large Language Models (LLMs)
Learn the fundamentals of Large Language Models (LLMs) and their role in Generative AI
Medium · Machine Learning
Up next
5 Levels of AI Agents - From Simple LLM Calls to Multi-Agent Systems
Dave Ebbelaar (LLM Eng)
Watch →