Coding

Laravel 5.2 PHP – Build a Shopping Cart – #12 Stripe Charging a Credit Card

  • 00:00:00 in order to actually submit the charge
  • 00:00:03 which we fetched here right which we
  • 00:00:06 validated on the arm front end I go to
  • 00:00:10 my product controller and I will create
  • 00:00:13 a new function which I'll name public
  • 00:00:15 function post checkout keep in mind this
  • 00:00:19 is the action I'm referring to in the
  • 00:00:21 routes file post check out here and this
  • 00:00:26 post check out method here will actually
  • 00:00:29 take a request or the request being and
  • 00:00:32 check that into it since it is a post
  • 00:00:34 route and with that I can now handle the
  • 00:00:39 request which will have this hidden
  • 00:00:40 stripe token send with it keep that in
  • 00:00:43 mind to actually make a charge for this
  • 00:00:47 I will first again check if we do have a
  • 00:00:50 shopping cart just as security check at
  • 00:00:53 the very beginning and kind the user
  • 00:00:54 gets to this route or any other strange
  • 00:00:57 way if we do have all that or don't want
  • 00:01:01 to return a view though I want to return
  • 00:01:03 a redirect redirect to a route named
  • 00:01:08 shop shopping cart I think you all to
  • 00:01:14 this route here I will redirect this
  • 00:01:16 route so that's my security check but if
  • 00:01:19 we do have a cart
  • 00:01:21 well then I want to continue you already
  • 00:01:23 know the steps with the old card and a
  • 00:01:25 new card right this pattern we use
  • 00:01:28 throughout this application and next I
  • 00:01:31 want to use stripe now I can do this by
  • 00:01:34 simply adding the import at the top here
  • 00:01:38 use stripe slash stripe this is
  • 00:01:41 important this line and then you can
  • 00:01:44 access all these stripe methods here
  • 00:01:46 basically now what does stripe offer me
  • 00:01:49 well I can set my API key remember on
  • 00:01:53 the front end and you check out dot J as
  • 00:01:56 file we set this publishable key now on
  • 00:01:59 the back end we need to set the secret
  • 00:02:01 key and you can get the secret key from
  • 00:02:05 your account the account settings the
  • 00:02:07 API keys tab here and well I need to
  • 00:02:11 login again
  • 00:02:13 just a little security better and then
  • 00:02:15 here it is hidden and for in the video
  • 00:02:18 it is hidden it will be visible to you
  • 00:02:20 just copy this key go back to your app
  • 00:02:23 and then paste it in here now I'm not
  • 00:02:26 doing it right now I will do it later so
  • 00:02:27 that they don't have to blur this out
  • 00:02:29 all out in post-production
  • 00:02:31 that's all the reason but you may
  • 00:02:32 already copy in your key right now and
  • 00:02:35 then I will have a try-catch block where
  • 00:02:39 I will try to make this charge so I will
  • 00:02:42 make tripod using stripe create and then
  • 00:02:47 try to make this charge however how does
  • 00:02:49 this work let's go back to the
  • 00:02:51 documentation let's have a look at the
  • 00:02:53 API libraries and then here you can see
  • 00:02:57 we get the PHP page and you can check
  • 00:03:00 out the PHP API Docs as the link says
  • 00:03:03 here you have a convenient way of
  • 00:03:06 learning how to make a charge so let's
  • 00:03:08 click on charges create a charge and on
  • 00:03:13 the right you see an example request and
  • 00:03:15 we already set the IP Ikey but now it's
  • 00:03:18 time to create charge this part here now
  • 00:03:21 as you saw I already created stripe
  • 00:03:23 create I will now just copied that here
  • 00:03:27 the argument an array and pass this
  • 00:03:30 array Tudor's stripe create method now
  • 00:03:34 of course amount shouldn't be 400
  • 00:03:36 instead it should be card total price
  • 00:03:39 now here's an important catch though
  • 00:03:41 stripe will use sense as its default
  • 00:03:45 unit now since the default unit in our
  • 00:03:48 application however is well dollars I
  • 00:03:50 need to multiply it with 100 otherwise
  • 00:03:54 if it costs $40 in our app I would only
  • 00:03:57 charge 40 cents on stripe I'll set the
  • 00:04:01 currency to u.s. dollar and regarding
  • 00:04:06 this toe kit here this of course is just
  • 00:04:08 dummy data we need to use token send
  • 00:04:12 with the request this token stored in
  • 00:04:15 the hidden input field added fruity
  • 00:04:17 checkout dot J's file so there's tope we
  • 00:04:20 got back from stripe then we validated
  • 00:04:22 to credit card information so here I
  • 00:04:26 will basically access
  • 00:04:27 request input and then stripe token
  • 00:04:31 which is simply the name this hidden
  • 00:04:34 input fillable 1/2 if you have a look at
  • 00:04:37 your checkout J's file you see that it
  • 00:04:39 has this name stripe token so that's all
  • 00:04:42 I'm doing here I'm accessing this token
  • 00:04:44 here and now the description set it to
  • 00:04:46 whatever you want I will name a test
  • 00:04:48 charge again this is freely settable by
  • 00:04:52 you of course in a real application you
  • 00:04:54 would probably set it to the user name
  • 00:04:56 product ID whatever whatever you like
  • 00:05:00 since we have a try plug here I also
  • 00:05:03 need a catch block here I will get my
  • 00:05:09 exception slash exception no import
  • 00:05:12 needed here just slash exception built
  • 00:05:14 in PHP exception and I want to basically
  • 00:05:19 show this exception I will redirect to
  • 00:05:23 the checkout route again and then I will
  • 00:05:29 redirect with a message which I store in
  • 00:05:32 the error object and then I want to
  • 00:05:35 print DD error there so oops no string
  • 00:05:40 instead II get message so just a message
  • 00:05:45 of a potential error I got here of
  • 00:05:48 course here no arrow should be used by
  • 00:05:51 the comma since we have multiple
  • 00:05:53 arguments here and with that I'm just
  • 00:05:55 using again this charge error
  • 00:05:57 div I set up earlier and I'm now
  • 00:06:01 handling the case that the charge is not
  • 00:06:03 successful as well next if well if I'm
  • 00:06:08 reaching this point I'm not returning so
  • 00:06:10 it seems like the charge was successful
  • 00:06:12 because I'm not in the catch block
  • 00:06:14 otherwise I would have left this
  • 00:06:15 function here right next are therefore
  • 00:06:18 want to forget my card which basically
  • 00:06:21 means delete it from the session because
  • 00:06:24 we checked out I don't want it to be
  • 00:06:26 there anymore now of course I'm not
  • 00:06:28 really storing the order in the database
  • 00:06:30 right now that's something I'll add in
  • 00:06:32 the future because currently we would
  • 00:06:34 get the money but we we wouldn't do
  • 00:06:36 anything else the customer probably
  • 00:06:38 wouldn't be too happy but
  • 00:06:40 now I would simply clear D card from the
  • 00:06:42 session and then I will return a
  • 00:06:45 redirect to another route let's say the
  • 00:06:48 product oops the product index route so
  • 00:06:54 this index page with let's say a success
  • 00:06:57 message so with success successfully
  • 00:07:02 purchased products like this that could
  • 00:07:09 be a success message we show now in
  • 00:07:11 order to show this message I need to
  • 00:07:14 adjust my index page a little bit so
  • 00:07:16 this index the plight or PHP file there
  • 00:07:19 I will need to add something right after
  • 00:07:22 entering my content here before and
  • 00:07:24 looping through all the products I want
  • 00:07:26 to enter a bootstrapper oh and then
  • 00:07:29 let's basically say Col sm6 call MD for
  • 00:07:36 just some food strips styling column
  • 00:07:38 positioning here again call MD offset
  • 00:07:43 for and call SM offset free something
  • 00:07:48 like that and I will create a new div
  • 00:07:53 here with an idea of charge error or
  • 00:07:57 let's say charge message whatever you
  • 00:08:00 want class alert and alert success
  • 00:08:07 because it will show me cell success
  • 00:08:09 messages in the end and then I will just
  • 00:08:11 print session get success here but of
  • 00:08:16 course I only want to show this if well
  • 00:08:20 if my session has the success object
  • 00:08:26 inside of it otherwise there is no
  • 00:08:29 reason to show this block here so that
  • 00:08:32 allows me to print out a success message
  • 00:08:34 whenever we are successful and now we're
  • 00:08:37 making this charge here now for me it's
  • 00:08:40 time to enter my private key and with
  • 00:08:44 that we should be good to go and see if
  • 00:08:47 that all works so I will reload my
  • 00:08:50 checkout page here I still have a valid
  • 00:08:53 shopping
  • 00:08:54 now I will fill in my my data here now
  • 00:08:58 regarding stripe I will go back to the
  • 00:09:01 our documentation here to the very
  • 00:09:05 starting getting started page here to
  • 00:09:08 just use that dummy data here it is
  • 00:09:11 dummy
  • 00:09:11 credit card number which has the right
  • 00:09:13 amount of digits enter this here give it
  • 00:09:16 any valid expiration month in here so in
  • 00:09:19 the future it should be any free ditches
  • 00:09:22 CVC here and click buy now and create
  • 00:09:27 call to end the find function
  • 00:09:29 controllers create it doesn't like that
  • 00:09:31 let's see what's wrong yeah I definitely
  • 00:09:33 need an extra colon here and I still got
  • 00:09:38 a problem here I'm leaving this
  • 00:09:39 individual because I think it might be
  • 00:09:40 helpful the create method here is not
  • 00:09:44 available on the stripe object here
  • 00:09:46 instead it is available on stripe slash
  • 00:09:51 charge however this wouldn't work too
  • 00:09:55 because now I just need charge now in
  • 00:09:58 order to use charge though I need to
  • 00:10:01 import it and I need to import it from
  • 00:10:03 slide stripe slash charge so I do have
  • 00:10:06 to imports needed stripe slash stripe
  • 00:10:08 and stripe / charge over solve that
  • 00:10:10 before excuse me but I think it's
  • 00:10:12 helpful to see this error here and you
  • 00:10:15 will see that you will need to be
  • 00:10:16 careful and that we are using two
  • 00:10:18 different well pieces of the stripe
  • 00:10:21 package here the overall stripe package
  • 00:10:24 to set the key and then the charged part
  • 00:10:26 of it to make the charge so back to the
  • 00:10:29 application if I now hit submit we got
  • 00:10:38 the successful purchase and if I reload
  • 00:10:42 the page here in my dashboard at test
  • 00:10:45 dashboard we should see yes
  • 00:10:48 before we had 20 euros and my case years
  • 00:10:50 since I'm in Europe and now as you can
  • 00:10:53 see we made an 18 dollar purchase today
  • 00:10:57 which I think makes sense since we
  • 00:10:59 bought two books which each costs $10
  • 00:11:02 and if you convert this to dollar do you
  • 00:11:04 euros 18 euros sound
  • 00:11:05 about right so this is how you can
  • 00:11:09 charge with stripe as I said currently
  • 00:11:12 we're not storing the order information
  • 00:11:14 we're not able to really ship something
  • 00:11:16 we're also doing this anonymous keep
  • 00:11:19 that in mind the user is not locked in
  • 00:11:21 which is one of the reasons why we're
  • 00:11:23 not storing the order information so
  • 00:11:26 there is still room for improvement but
  • 00:11:28 you saw how stripe works and how to use
  • 00:11:31 it and I hope this is useful and now you
  • 00:11:34 might already try out implementing it
  • 00:11:36 with a locked-in user instead of an
  • 00:11:39 anonymous one and actually storing the
  • 00:11:42 well-ordered
  • 00:11:43 information in the database but I will
  • 00:11:45 also do this in the next videos see you
  • 00:11:48 there
  • 00:11:48 bye