WEBVTT

00:00.080 --> 00:00.590
Hi everyone.

00:00.590 --> 00:01.190
Welcome back.

00:01.190 --> 00:02.570
So here is our design right.

00:02.600 --> 00:04.310
So we'll start with the payment gateway.

00:04.310 --> 00:10.550
So this payment gateway is going to be a sealed class which is going to have one function named make

00:10.550 --> 00:11.450
payment okay.

00:11.480 --> 00:16.640
So let's go ahead and start with that implementation I'm back in IntelliJ here I'm in the payment package.

00:16.640 --> 00:20.180
So in here I'm going to create a class named Payment gateway.

00:20.540 --> 00:27.110
So in this case create this is going to be payment gateway.

00:27.140 --> 00:27.710
There you go.

00:27.710 --> 00:29.030
So this is our class.

00:29.030 --> 00:33.350
And this class is going to be a sealed class and an abstract class.

00:33.350 --> 00:38.750
Because this is something like a contract where all the different implementations is going to provide

00:38.750 --> 00:40.160
their own implementation.

00:40.160 --> 00:45.890
So and then this is going to be using the permits class as we know the syntax by now, anytime you use

00:45.890 --> 00:50.450
a sealed keyword then you need to make sure you use a permits to allow.

00:50.480 --> 00:54.020
What are the different implementation your sealed class is going to support.

00:54.020 --> 00:59.210
So number one is if you go ahead and take a look at the design here we have debit card payment rewards

00:59.210 --> 01:01.500
card payment and the credit card payment.

01:01.500 --> 01:05.070
So the very first thing I'm going to create a class named debit card payment.

01:05.070 --> 01:08.370
So we can provide debit card payment over here.

01:08.400 --> 01:09.960
This class is not available yet.

01:09.960 --> 01:11.790
Let's go ahead and create this one.

01:11.790 --> 01:15.990
So create class and it's going to be part of the package named payment.

01:15.990 --> 01:17.220
And then press okay.

01:17.220 --> 01:21.240
So the debit card payment extends the payment gateway right.

01:21.240 --> 01:22.230
So that's a deal.

01:22.230 --> 01:27.600
And then anytime you create a subclass for a class what you need to do you need to provide one of the

01:27.600 --> 01:30.960
modifier which should be non-final or sealed.

01:30.960 --> 01:32.850
Or it can be final.

01:32.850 --> 01:38.160
So in this case I'm going to keep it final because I don't want any other class to extend this specific

01:38.160 --> 01:40.260
class which is debit card payment.

01:40.260 --> 01:42.360
So now let's go back to the payment gateway.

01:42.360 --> 01:44.070
All the compilation issues are gone.

01:44.070 --> 01:48.720
So now this abstract class is going to provide an abstract function okay.

01:48.750 --> 01:51.690
So in this case it's going to be public abstract.

01:51.690 --> 01:55.680
And then the function name is going to be make payment okay.

01:55.710 --> 02:00.770
And then this is going to accept the card information and the amount you want to make the payment for.

02:00.800 --> 02:02.690
So in this case it's going to be card.

02:02.690 --> 02:05.900
So we have this card as part of the domain class already.

02:05.930 --> 02:08.240
You can actually go ahead and import that.

02:08.240 --> 02:10.040
And the amount is going to be double.

02:10.070 --> 02:16.190
So in this case double amount I could have used Bigdecimal I just wanted to keep it double to ease our

02:16.190 --> 02:16.970
implementation.

02:16.970 --> 02:19.010
So what this function is going to return.

02:19.010 --> 02:22.040
This is going to return something called payment response.

02:22.040 --> 02:24.170
This enum is already part of the domain package.

02:24.170 --> 02:26.510
If you go here we have two different statuses.

02:26.540 --> 02:28.880
Number one is a success and number is a failure.

02:28.910 --> 02:34.130
Again one another thing to call out is that we are not going to be calling a real time payment gateway

02:34.130 --> 02:35.870
to get this payment completed.

02:35.900 --> 02:40.610
This is still going to be a static response, but the point that I'm trying to make here is that this

02:40.610 --> 02:46.100
lecture is going to provide you an approach to design and implement these kind of use cases.

02:46.220 --> 02:46.550
Okay.

02:46.580 --> 02:48.770
So in here we have this make payment.

02:48.800 --> 02:53.420
Now if we go to the debit card payment class you will be asked to provide an implementation.

02:53.420 --> 02:57.290
So what we will do we'll go ahead and implement that abstract function.

02:57.290 --> 02:58.460
So implement methods.

02:58.460 --> 02:59.330
So make payment.

02:59.330 --> 03:01.680
So this is what we need to implement.

03:02.010 --> 03:02.310
Okay.

03:02.340 --> 03:07.890
So as I mentioned we are actually not going to be calling any real time servers to acquire the payment.

03:07.890 --> 03:09.660
So I'm just going to print out here.

03:09.660 --> 03:12.180
So it's going to be a simple logger message.

03:12.180 --> 03:18.750
So in this case I'm going to provide acquire debit card payment for the amount.

03:18.750 --> 03:21.420
So we shouldn't be printing the card details at any cost.

03:21.420 --> 03:23.130
So I'm going to remove that away.

03:23.160 --> 03:23.760
There you go.

03:23.760 --> 03:28.980
And then the response is going to be payment response dot success.

03:28.980 --> 03:31.110
So it's always going to return success.

03:31.110 --> 03:37.320
But in reality if you have a payment gateway interacting with the actual bank based on what the banking

03:37.350 --> 03:42.150
application response, we will be able to get the real time status for that one.

03:42.150 --> 03:45.720
But with this implementation, it's going to be always success.

03:45.720 --> 03:49.140
So now this is the debit card payment implementation.

03:49.140 --> 03:53.850
So we need to provide the same implementation for the credit card and the rewards card too.

03:54.120 --> 03:54.510
Right.

03:54.510 --> 03:57.450
Because that's what our payment service design is.

03:57.450 --> 04:00.470
So we have the rewards card payment and then the debit card payment.

04:00.470 --> 04:06.020
So in here, in order to keep things simple, what we can do is we can actually copy this and then put

04:06.020 --> 04:06.800
it over here.

04:06.800 --> 04:11.240
And then we can call this one as credit card payment.

04:11.240 --> 04:16.970
So the credit card payment is going to be having its own implementation rights.

04:16.970 --> 04:19.280
But in this case we are just printing the value.

04:19.280 --> 04:23.690
So in this case let's go ahead and change this one to acquire credit card payment.

04:23.690 --> 04:29.720
But the payment gateway class complains about it because this credit card payment is not part of the

04:29.720 --> 04:31.370
permits class yet.

04:31.400 --> 04:31.730
Right.

04:31.760 --> 04:37.250
So in this case we need to add the credit card payment over here.

04:37.250 --> 04:40.190
So once you add that that particular error is resolved.

04:40.190 --> 04:45.170
So this function is going to just print the acquire credit card payment for the amount.

04:45.170 --> 04:48.980
And then the next thing is we need one for the rewards card payment.

04:48.980 --> 04:52.160
So let's go ahead and make a change over there.

04:52.160 --> 04:54.770
So this is going to be rewards card payment.

04:54.770 --> 04:57.800
And then the changes are going to be pretty much the same.

04:57.800 --> 05:02.130
So in here Acquire rewards card payment for the amount.

05:02.130 --> 05:05.550
And then you go ahead and add the rewards card over here.

05:05.580 --> 05:08.520
So in this case it's going to be rewards card payment.

05:08.580 --> 05:09.090
Okay.

05:09.120 --> 05:15.960
So now we have the implementations ready for all the different types of payment or checkout service.

05:15.960 --> 05:17.640
And the payment service supports.

05:17.640 --> 05:20.340
So if you go ahead and take a look at the reference of our design.

05:20.340 --> 05:25.530
So we have the payment gateway class ready, debit card payment ready, rewards card payment ready and

05:25.530 --> 05:26.700
credit card payment ready.

05:26.700 --> 05:30.930
The next step is we are going to implement a factory class based on the card type.

05:30.930 --> 05:35.640
It is going to give you an instance of this appropriate payment instance okay.

05:35.670 --> 05:37.890
So let's go ahead and create the factory class.

05:37.890 --> 05:41.670
So in this case what we are going to do we are going to create the payment factory.

05:41.670 --> 05:44.640
So in this case let's call payment factory.

05:44.670 --> 05:45.270
There you go.

05:45.270 --> 05:48.060
So this payment factory class is a regular class.

05:48.060 --> 05:50.550
And then this function is going to be a static function.

05:50.550 --> 05:52.800
So that is going to be a static function.

05:52.800 --> 05:56.940
And the name of this function is going to be payment gateway okay.

05:56.970 --> 05:59.480
So this is going to accept a card type.

05:59.510 --> 05:59.810
Okay.

05:59.840 --> 06:01.790
So in this case it's going to be card type.

06:01.790 --> 06:06.320
So based on the card type it is going to return the appropriate payment gateway.

06:06.320 --> 06:10.670
So what this return type is going to be it's going to be payment gateway.

06:10.700 --> 06:15.770
What we are implementing is a factory pattern just to call it out so that you know what we are going

06:15.770 --> 06:17.120
to be implementing.

06:17.120 --> 06:19.730
So in this case we can use the enhanced switch.

06:19.730 --> 06:19.970
Right.

06:19.970 --> 06:21.800
Because what we have is a card type.

06:21.800 --> 06:23.210
So the card type is credit.

06:23.210 --> 06:25.640
Then we need to return the credit card payment.

06:25.640 --> 06:29.420
If the card type is debit then we can return the debit card payment.

06:29.450 --> 06:33.290
If the card type is rewards then we can return the rewards card payment.

06:33.290 --> 06:36.050
So we can use the return here.

06:36.080 --> 06:39.350
Because switch is an expression by now you all know that.

06:39.350 --> 06:41.450
So in this case I'm going to pass the card type.

06:41.450 --> 06:42.440
So in the card type.

06:42.440 --> 06:44.600
So if the card is debit.

06:44.600 --> 06:50.690
So in this case we have a debit then it's going to return a new debit card payment okay.

06:50.690 --> 06:56.960
So if the card is credit then it's going to return a new credit card payment okay.

06:56.990 --> 07:02.880
Now if the card is rewards, then in this case it's going to be new rewards card payment.

07:02.910 --> 07:04.830
Okay then end this with semi.

07:04.980 --> 07:07.890
So there is no default block required for this one.

07:07.890 --> 07:10.740
The reason being what we are dealing with is a enum, right?

07:10.740 --> 07:16.380
So if you have a specific enum which just has three different types, then you just need to provide

07:16.380 --> 07:19.770
the implementation for all those supported values.

07:19.770 --> 07:24.210
So this is the exhaustive nature of the enhanced switch I was referring to.

07:24.240 --> 07:28.200
But in this case there is a possibility the card type can still be null.

07:28.200 --> 07:30.630
So we can have a case null.

07:30.630 --> 07:36.090
In the case of this one we can throw an illegal argument exception, and then we can actually provide

07:36.090 --> 07:40.650
the message as card type null not supported okay.

07:40.680 --> 07:42.360
So this is still possible.

07:42.360 --> 07:47.370
It's better to have a null implementation to handle any kind of null pointer exceptions.

07:47.370 --> 07:50.310
So this is our payment factory implementation.

07:50.310 --> 07:54.570
So the payment service is going to be interacting with the payment factory.

07:54.570 --> 07:59.660
So we have completed the payment gateway and the implementation of each and every car type, and then

07:59.660 --> 08:01.640
the payment factor is also completed.

08:01.670 --> 08:05.690
Now let's go ahead and integrate this flow into the payment service.

08:05.720 --> 08:10.700
If you go to the payment service currently, it doesn't deal with any of the classes that we created

08:10.700 --> 08:11.000
yet.

08:11.000 --> 08:12.950
So the very first thing is what we are going to do.

08:12.980 --> 08:17.090
We are going to call payment factory, dot payment gateway.

08:17.090 --> 08:18.740
And then what do we pass to this one.

08:18.740 --> 08:23.030
So in this case we pass the order details dot card dot card type.

08:23.030 --> 08:27.170
So when you pass this what you are going to get is a payment gateway implementation.

08:27.200 --> 08:27.380
Right.

08:27.410 --> 08:30.260
So in this case it's going to be payment gateway.

08:30.290 --> 08:31.010
There you go.

08:31.040 --> 08:32.720
And in here what we are going to do.

08:32.720 --> 08:34.910
So we are going to be returning.

08:34.940 --> 08:36.920
So we can comment this one for now.

08:37.430 --> 08:40.100
Payment gateway dot make payment.

08:40.100 --> 08:42.500
And then you provide the card and the amount.

08:42.530 --> 08:42.710
Right.

08:42.710 --> 08:44.900
So that's what this make payment function accepts.

08:44.900 --> 08:46.970
It accepts a card and the final amount.

08:47.000 --> 08:51.680
So in this case we can get those information from the order details domain class.

08:51.680 --> 08:56.450
So dot card and then order details dot final amount.

08:56.480 --> 08:57.080
There you go.

08:57.080 --> 08:59.280
And then what this is going to respond with.

08:59.310 --> 09:01.800
This is going to respond with a payment response.

09:01.800 --> 09:07.410
So in the in our case, it's always going to be the success because we have some static implementations

09:07.410 --> 09:10.920
of credit card payment, debit card payment and rewards card payment.

09:10.920 --> 09:14.370
All those are returning the payment success okay.

09:14.400 --> 09:19.500
So now if you go to the caller of this function, so the checkout service, once it makes a call based

09:19.500 --> 09:24.540
on the type of the card that's been part of the order details, the appropriate implementation will

09:24.540 --> 09:26.790
be invoked and the response will be sent.

09:27.000 --> 09:30.120
Okay, let me quickly recap what we have done so far.

09:30.120 --> 09:35.430
So anytime the checkout service gets this order details to the checkout order function, it's going

09:35.430 --> 09:37.500
to make a call to the payment service.

09:37.500 --> 09:40.140
And then it's going to call the payment gateway.

09:40.140 --> 09:45.660
So the payment gateway based on the type of the card it's going to provide the appropriate implementation.

09:45.660 --> 09:51.090
So this type of implementation is called a factory pattern because this class acts as a factory of different

09:51.090 --> 09:53.730
implementations based on the input that you provide.

09:53.730 --> 09:58.080
It takes one implementation from the factory and sends that as a response.

09:58.080 --> 10:00.380
So once it's sent to the caller.

10:00.380 --> 10:05.000
So in this case, once the payment gateway is sent which has the appropriate implementation, then we

10:05.000 --> 10:06.260
make a call to the make payment.

10:06.260 --> 10:08.330
And it might go to one of the implementations.

10:08.330 --> 10:12.050
It could be a debit card payment credit card payment or rewards card payment.

10:12.050 --> 10:14.930
And then the appropriate response will be sent back.

10:14.960 --> 10:15.320
Okay.

10:15.350 --> 10:19.850
Now what we are going to do, we are going to write a simple test case to test this behavior.

10:19.850 --> 10:21.980
So there is a test case already available.

10:21.980 --> 10:28.070
So in this case if you go to the checkout service there is a checkout service test class you might be

10:28.100 --> 10:30.140
able to find in the test package.

10:30.140 --> 10:30.830
There you go.

10:30.830 --> 10:33.500
And then in here we can write the test case for this one.

10:33.500 --> 10:38.180
So we have this order details static function based on the card type that you pass.

10:38.210 --> 10:41.300
It's going to randomly create an order details object.

10:41.300 --> 10:43.280
And then it's going to give that as a response.

10:43.280 --> 10:47.540
So what we are going to do is we are going to create a parameterized test over here.

10:47.540 --> 10:50.240
So in this case let's go ahead and add the parameterized test.

10:50.240 --> 10:54.740
And I'm going to add the name equal to checkout for cart type.

10:54.740 --> 10:58.300
And then we are going to be getting the cart type from a enum source.

10:58.300 --> 11:04.570
So in this case it's going to be at enum source and then provide the card type value.

11:04.600 --> 11:06.370
So in this case card Typeclass.

11:06.370 --> 11:11.500
If you provide this information this value will be passed as a input to this test function.

11:11.530 --> 11:15.190
So in this case we can actually provide this value in this format.

11:15.220 --> 11:21.310
What this is going to do is this is going to print the card type value which represents each enum value.

11:21.310 --> 11:25.210
Because if you go and take a look at the card type, we have three different values, right.

11:25.210 --> 11:30.550
We have the credit, debit and rewards, which means this particular test case will be executed for

11:30.550 --> 11:32.020
three different inputs.

11:32.020 --> 11:33.760
And then I'm going to write the function.

11:33.790 --> 11:35.440
It's going to be void checkout.

11:35.440 --> 11:37.960
And then this is where the card type will be passed.

11:37.990 --> 11:40.420
So in this case card type will be passed.

11:40.420 --> 11:44.440
And the source for the card type is coming from this enum source.

11:44.470 --> 11:44.890
Okay.

11:44.920 --> 11:48.880
So the very first thing is we pass the card type to the order detail function.

11:48.880 --> 11:51.190
And this is going to return the order details.

11:51.190 --> 11:55.600
In this case it's going to be order details.

11:55.600 --> 12:00.150
And then we are going to be calling the checkout service, so checkout service is already available

12:00.150 --> 12:02.190
as an instance in this particular class.

12:02.190 --> 12:07.920
And then we call the checkout service dot checkout order and then pass the order details over here.

12:08.310 --> 12:13.050
And if you take a look at the checkout service we have an instance of payment service already passed

12:13.050 --> 12:13.410
to it.

12:13.440 --> 12:13.890
Right.

12:13.890 --> 12:17.760
Because that's needed for the whole orchestration of the flow to start.

12:17.760 --> 12:22.170
So when we call the checkout order from here, what this is going to do, this is going to make the

12:22.170 --> 12:24.180
call to the make payment function.

12:24.180 --> 12:25.470
This doesn't have to be V2.

12:25.500 --> 12:31.740
What I'm going to do I'm going to change this function name to be make payment okay.

12:31.770 --> 12:37.200
And then it makes a call to the gateway gets the appropriate implementation based on the card type.

12:37.200 --> 12:40.170
And then we make the call to the payment function okay.

12:40.200 --> 12:42.090
So now let's go back to the test case.

12:42.090 --> 12:45.750
So from the test case this is always going to give you the success response.

12:45.750 --> 12:48.000
In this case we can actually call this response.

12:48.000 --> 12:49.740
And then we can call Assertequals.

12:49.770 --> 12:54.210
It's going to be checkout status dot success comma response.

12:54.210 --> 12:59.340
So the response is always going to be success rate because we are not making any real time call.

12:59.370 --> 13:01.470
The response is always going to be static.

13:01.470 --> 13:03.810
So now let's go ahead and execute this test case.

13:03.810 --> 13:08.370
So this test case is going to call each and every card type.

13:08.370 --> 13:10.050
So in this case it starts with the credit.

13:10.050 --> 13:15.570
And you can see right in the logger we have the acquire credit card payment for the amount 99.

13:15.570 --> 13:18.810
And we have for the debit we have the acquired debit card payment.

13:18.810 --> 13:22.050
This means the debit card implementation is invoked right.

13:22.080 --> 13:25.020
Because that's what this particular logger is coming from.

13:25.020 --> 13:26.610
And then the next one is the rewards.

13:26.610 --> 13:30.540
So rewards is again it's coming from the reward card implementation.

13:30.840 --> 13:31.290
There we go.

13:31.290 --> 13:34.140
So the payment factory class acts as an entry point.

13:34.140 --> 13:38.040
So based on the card type it's going to return you the appropriate instance.

13:38.040 --> 13:40.620
And that particular instance will be invoked.

13:41.010 --> 13:47.160
So we wrote our test case that actually test all the behavior that we have expected to work.

13:47.190 --> 13:50.850
I hope you all have a pretty good idea about how this whole implementation works.

13:50.850 --> 13:53.850
If you have any questions, please go ahead and write it in Q&amp;A.

13:53.880 --> 13:55.110
I would be happy to help.

13:55.110 --> 13:56.520
This marks the end of this lecture.

13:56.520 --> 13:57.540
Thank you for watching.
