Fireship · @fireship

Selling Digital Content in Angular with Stripe Payments - Part 3

Uploaded July 16, 2017 · Length 7:21

Selling Digital Content in Angular with Stripe Payments - Part 3

Summary

Implementing digital content purchases with Stripe payments in Angular using Firebase.

  • angular stripe payments
  • firebase digital content
  • stripe checkout angular
  • angular stripe integration
  • firebase cloud functions

Transcript preview

The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.

  1. 0:00

    welcome to part three of strike payments

  2. 0:02

    with angular and firebase in the last

  3. 0:05

    two episodes we integrated the stripe

  4. 0:06

    check out and charge the card with

  5. 0:08

    firebase cloud functions in this third

  6. 0:11

    episode we're going to use that charge

  7. 0:12

    to enable the user to purchase digital

  8. 0:15

    content in our app in this example we

  9. 0:17

    keep a balance on the user's account

  10. 0:19

    which can be increased by making

  11. 0:21

    payments or decreased by purchasing

  12. 0:23

    content when a user has sufficient funds

  13. 0:26

    on their account they can click a button

  14. 0:27

    that will bring up a modal window to

  15. 0:29

    confirm or cancel the purchase if they

  16. 0:32

    click confirm their account balance is

  17. 0:34

    automatically updated with the new

  18. 0:36

    balance the database will be updated

  19. 0:39

    with a purchases collection which tracks

  20. 0:41

    the user ID and the item IDs that they

  21. 0:44

    purchased the users current balance will

  22. 0:47

    be tracked on their actual account with

  23. 0:48

    a new property called balance which

  24. 0:50

    takes a number let's start by updating

  25. 0:53

    the cloud function we need it to update

  26. 0:56

    both the charge as well as the users

  27. 0:57

    balance in the same operation we want it

  28. 1:00

    to be atomic meaning if the charge fails

  29. 1:02

    the user balance update will fail also

  30. 1:04

    and vice-versa the first step is to

  31. 1:07

    create a balance variable defaults to

  32. 1:09

    zero and then when we get the customer

  33. 1:11

    record we'll update it with their

  34. 1:13

    existing balance when we get the stripe

  35. 1:16

    charge back we'll first check to make

  36. 1:17

    sure it exists and we'll also check to

  37. 1:20

    make sure that it's actually paid if

  38. 1:22

    these conditions are met we create an

  39. 1:23

    object where the key is a reference

  40. 1:25

    point in the database and the value is

  41. 1:28

    the data we want to update this allows

  42. 1:30

    us to update multiple database locations

  43. 1:32

    at the same time but be careful it

  44. 1:34

    performs a set operation so make sure

  45. 1:37

    you point to the very deepest node that

  46. 1:38

    you want to update now we can use the

  47. 1:41

    charge amount from stripe to increase

  48. 1:42

    the users balance and then send these

  49. 1:44

    updates together to the root of the

  50. 1:47

    database back in the app we can see the

  51. 1:49

    users balance is updated by the cloud

  52. 1:52

    function when they make a payment now we

  53. 1:56

    need to update the service to handle the

  54. 1:58

    payment process in order to get the user