Fireship · @fireship

Process Stripe Payments with Firebase Cloud Functions - Part 2

Uploaded July 11, 2017 · Length 4:53

Process Stripe Payments with Firebase Cloud Functions - Part 2

Summary

how to integrate stripe payments with firebase cloud functions in angular 4

  • stripe payments
  • firebase cloud functions
  • angular 4
  • payment gateway
  • firebase database

Transcript preview

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

  1. 0:00

    welcome to part two of stripe payments

  2. 0:02

    with angular 4 and firebase in part 1 we

  3. 0:06

    collected the payment token from stripe

  4. 0:08

    and in this episode we're going to build

  5. 0:10

    a fire based cloud function that will

  6. 0:12

    actually charge the card hole stripe

  7. 0:14

    transactions need to occur on a real

  8. 0:16

    back-end server and that's exactly what

  9. 0:18

    we're going to use cloud functions for

  10. 0:21

    the first step is to initialize cloud

  11. 0:23

    functions if they're not already set up

  12. 0:25

    in your project run firebase in it and

  13. 0:27

    then CD into that functions directory

  14. 0:30

    then run NPM install stripe with the

  15. 0:33

    save flag to save it to the local

  16. 0:36

    project your package dot JSON file

  17. 0:39

    should look something like this and from

  18. 0:42

    here we need to set an environment

  19. 0:43

    variable with our stripe API key run

  20. 0:47

    firebase functions config set stripe

  21. 0:50

    test key followed by your own stripe API

  22. 0:53

    key check out the link in the

  23. 0:54

    description to just copy and paste this

  24. 0:56

    command now we can start building the

  25. 0:58

    function in the index.jsp file first

  26. 1:01

    step is to import the firebase functions

  27. 1:04

    library as well as the admin database

  28. 1:05

    and for stripe we'll initialize it with

  29. 1:09

    our API key using that environment

  30. 1:11

    variable we just set the name of the

  31. 1:13

    function will be striped charge and

  32. 1:15

    we'll use the on right database callback

  33. 1:18

    to trigger this function in other words

  34. 1:21

    whenever a new payment is written to the

  35. 1:23

    database this function will be invoked

  36. 1:24

    it has an event object that has the data

  37. 1:28

    from that node in the database which is

  38. 1:30

    the payment along with the token so we

  39. 1:33

    can get that by calling event data eval

  40. 1:35

    and we'll also set variables for the

  41. 1:38

    user ID as well as the payment ID just

  42. 1:41

    to make our code a little more concise

  43. 1:42

    and then we want the function to return

  44. 1:44

    null if there is no payment or if the

  45. 1:47

    payment already has a charge from there

  46. 1:49

    we'll chain together a bunch of promises

  47. 1:51

    the first one being the user in the

  48. 1:52

    database this isn't completely necessary

  49. 1:54

    but it's a good idea if you have

  50. 1:56

    recurring charges or if you want to save

  51. 1:58

    customer data in the database we call