1 00:00:02,170 --> 00:00:06,730 Back inside of our handler right after we create this charge then going to take the response so we get 2 00:00:06,730 --> 00:00:07,420 back. 3 00:00:07,420 --> 00:00:10,710 That's gonna be an object that tells us some details about the charge that was created. 4 00:00:10,720 --> 00:00:15,580 We're gonna take the I.D. out of that thing and then use it to create a new payment record to get started 5 00:00:15,580 --> 00:00:16,190 at the very top. 6 00:00:16,180 --> 00:00:27,050 This file I will import the payment model from up one directory models payment then back down. 7 00:00:27,050 --> 00:00:31,370 Right after we create that charge let's take a look at our documentation really quickly just to understand 8 00:00:31,370 --> 00:00:35,360 what comes back when we create a charge because I keep on saying oh yeah we get back something that 9 00:00:35,360 --> 00:00:40,700 represents the charge but again let's just look at the docs really quick all right so back on the stripe 10 00:00:40,760 --> 00:00:46,500 API docs can take a look at creating a charge and we could see the response we get back right here. 11 00:00:46,640 --> 00:00:51,170 So it's gonna be this big object that has a lot of information about the charge that was created and 12 00:00:51,170 --> 00:00:54,340 very simply you'll notice it has that I.D. property that's pretty much it. 13 00:00:54,350 --> 00:00:56,090 That's what we want to store. 14 00:00:56,090 --> 00:01:00,560 Once we store that I.D. if we ever want to look up this charge at some point the future we could use 15 00:01:00,560 --> 00:01:04,410 this retrieve a charge endpoint or the stripe client. 16 00:01:04,460 --> 00:01:09,260 So we would do a stripe charges retrieve put in the idea right there and that would give us the full 17 00:01:09,290 --> 00:01:10,680 charge object. 18 00:01:10,880 --> 00:01:14,870 And so that's how we could retrieve a charge potentially at some point on the future if we needed to 19 00:01:14,870 --> 00:01:19,280 look up any further details about it. 20 00:01:19,830 --> 00:01:25,940 So that entire big response is going to come out of this create call don't receive that as a variable 21 00:01:25,940 --> 00:01:35,400 called charge and then immediately after all build up a new payment that'll be a payment billed going 22 00:01:35,580 --> 00:01:42,730 to pass in the order I.D. as ordered i.e. and actually we do not have an order I ordered dot I.D. property 23 00:01:42,730 --> 00:01:48,010 we've got just the order I.D. itself which actually suits pretty well for our uses open and just order 24 00:01:48,010 --> 00:01:58,310 I.D. and then our stripe I.D. is charge dot I.D. then after creating the payment we'll do it in a wait 25 00:01:58,910 --> 00:02:02,800 a minute dot save. 26 00:02:03,030 --> 00:02:03,890 Well that's pretty much it. 27 00:02:03,930 --> 00:02:07,510 Not the worst thing in the world now as usual. 28 00:02:07,590 --> 00:02:11,310 It'd be fantastic to write out a real quick test around this. 29 00:02:11,380 --> 00:02:15,730 Let's go back over to our root handler test file. 30 00:02:15,880 --> 00:02:20,570 There it is right here I think we could put together a test for this very very quickly. 31 00:02:20,700 --> 00:02:23,520 As a matter of fact we'll just reuse the test we already put together. 32 00:02:23,570 --> 00:02:33,540 So at the very top I will import the payment model from up to directories models payment I'll then go 33 00:02:33,540 --> 00:02:40,280 down to the most recent test we put together that is the success case on right here and we'll just add 34 00:02:40,280 --> 00:02:42,730 in an additional expectation to this thing. 35 00:02:42,730 --> 00:02:47,240 So at the very bottom of the test after we make sure that the stripe charge was actually created let's 36 00:02:47,240 --> 00:02:51,980 take a look in our payment collection and just make sure that a payment was actually created and saved 37 00:02:52,220 --> 00:02:59,170 with a given stripe I.D. or alternately with a given order I.D. either one would be fine so I will do 38 00:02:59,200 --> 00:03:10,410 a payment is a wait payment not find a one they'll try to find one payment saved to the payment collection. 39 00:03:10,470 --> 00:03:15,670 And how about we tried to find a payment with the I.D. equal to these stripe charges I.D. and they give 40 00:03:15,670 --> 00:03:19,800 an order idea as well that'll really make sure that we save the payment with all the correct details 41 00:03:20,660 --> 00:03:24,970 so I can say that I want to find a payment with this order I.D. order. 42 00:03:25,000 --> 00:03:35,440 I.D. and with a stripe I.D. of Stripe charge that I.D. once again you'll notice we get an ID right there 43 00:03:35,470 --> 00:03:38,320 because this thing might be undefined. 44 00:03:38,360 --> 00:03:41,860 We're gonna say typescript don't sweat it because you're really checked for the existence of that variable 45 00:03:41,900 --> 00:03:42,350 right there 46 00:03:45,140 --> 00:03:50,550 after that we write out in expectation to make sure that we actually found a payment. 47 00:03:50,550 --> 00:03:53,220 Now there is a real small gotcha here. 48 00:03:53,220 --> 00:03:57,570 You'll notice that on the last step when we looked up all of our different charges tried to find a given 49 00:03:57,570 --> 00:04:02,520 charge the actual expectation we wrote out was expect this thing to be defined. 50 00:04:02,610 --> 00:04:08,480 The reason we use the to be defined matter was that when we use the fine method right here we are they're 51 00:04:08,490 --> 00:04:14,550 going to get the record we were looking for or a value of undefined to be defined match or itself. 52 00:04:14,550 --> 00:04:21,390 Checks to see that a value is not undefined specifically the value not undefined in the case of payment 53 00:04:21,390 --> 00:04:22,040 right here. 54 00:04:22,110 --> 00:04:28,140 If we are not able to find a payment with the given filters rather than being given undefined we get 55 00:04:28,140 --> 00:04:32,160 a value of null null and undefined are two different things. 56 00:04:32,160 --> 00:04:39,700 So if we expect payment to be defined this will always evaluate successfully or truthfully. 57 00:04:39,780 --> 00:04:43,830 In other words we will not fail to matter because the payment variable is either going to be the payment 58 00:04:43,830 --> 00:04:47,170 we are looking for or it will be a value of No. 59 00:04:47,190 --> 00:04:51,750 And both those are going to pass this expectation because again this thing is looking for a value to 60 00:04:51,750 --> 00:04:58,320 be undefined because payment is either going to be a payment ordinal we need to specifically say that 61 00:04:58,320 --> 00:05:00,240 we expect a thing not to be. 62 00:05:00,240 --> 00:05:01,260 No. 63 00:05:01,440 --> 00:05:08,430 We're gonna update that matter and say that we expect payment not to be. 64 00:05:08,430 --> 00:05:08,950 No. 65 00:05:08,970 --> 00:05:15,510 Like so yes let's now save that we'll back over to our terminal 66 00:05:19,460 --> 00:05:22,770 and back over here. 67 00:05:22,860 --> 00:05:24,970 It looks like we are good to go as usual. 68 00:05:24,990 --> 00:05:28,350 Let's make this test fail so I will flip back over. 69 00:05:28,350 --> 00:05:32,580 Let's go back over to our request handler and we'll just comment out the line where we tried to actually 70 00:05:32,580 --> 00:05:36,530 save the payment outcome and that out save the file. 71 00:05:36,680 --> 00:05:40,820 And now we will expect something to come up and say Hey sorry about this value is actually know when 72 00:05:40,820 --> 00:05:42,860 you expected it wouldn't be. 73 00:05:42,860 --> 00:05:45,090 And there we go. 74 00:05:45,120 --> 00:05:48,040 Looks good like going to Uncle Matt that save it again. 75 00:05:48,120 --> 00:05:50,150 And we are all set. 76 00:05:50,230 --> 00:05:50,560 All right. 77 00:05:50,590 --> 00:05:51,960 So we have created the charge. 78 00:05:52,030 --> 00:05:55,780 We have saved a record that relates the charge Andi ordered together. 79 00:05:55,920 --> 00:05:59,710 And the last thing we really had to do inside of Europe beyond responding with some more meaningful 80 00:05:59,710 --> 00:06:04,450 value than what we have right here less than we really have to do is make sure we publish an event to 81 00:06:04,450 --> 00:06:08,540 say that well we created a charge or we create a payment or something like that. 82 00:06:08,560 --> 00:06:12,690 Let's take a pause here and build up that publisher and the event definition in the next video.