Programming Chunks

Explore Python And Data Science with me!!

Home ยป Formulating the ecommerce payment flow with python design patterns-Part1

Formulating the ecommerce payment flow with python design patterns-Part1

So, I was working on a mock project of mine. This is my brainchild project in which I intend to explore various components of python and programming in general.

I just thought of documenting the same. So, here I am…….

It starts with creating the viewset for order service. The idea is that when in the UI, the user hits a pay button which could be anything be it Razorpay, paypal or stripe, it hits the create order viewset at the backend and then from this block it intercommunicates with two other microservices- payments and notifications service.

I have just added the code for creating the order and payment initiation. I intend to add call to notification service that it will send mails to the customers. In addition, I intend to record the session informations, transaction details like which payment methods users prefer, when the user initiate and complete the payment, failed trasactions and reasons for the same and api error, unsual activity like if there are multiple failed attempts and geological location from where the trasactions is being made. All these would be dumped in an audit table, I am still thinking there should be a separate audit table by grouping the categories or only one, but that’s something to decide later on.

Coming back to what I did today was establish intercommunication between order and payment service and razor pay external payment gateway integration.

Here is the starter format. The user would provide his identity and amount to be payments, I will include the items choose once I integrate the cart system.

Lots of try excepts configure the blocks of code here. Plus I am going to add tons of debugs statements when I start testing the flow tommorrow.

If the order creating is successful, then payment service is called. This is the call to the external service.

Of course this time, I have spared myself from the scorching heat and blocks of the synchronous programming by using sync programming, and I dont need to do that right? because all of this should happen only when the prev one is done.

This is the view which that end point hits in the payments service.

Here I have used design pattern because I want to make room for adding more and more payment processors in future. This is actually factory pattern which instantiates various payment processors based on condition that the user chooses.

The above is the strategy pattern. An abstract class that exposes a method that gets implemented in child classes which inherits the parent class.

Once the payment strategy is set up, the instance calls the payment service which also has a initiate payment method but this method take the instance and sets up the circuit pattern which only calls the razorpay api when the state is close, once retry counts exceeds the max retry counts it opens the circuits and raises and exception. Basically, it gets opens when retry is greater than max retry.

Tommorrow I am gonna test this flow and I am sure there would be a tons of debugs statements coming in ๐Ÿ˜€

pallavy.com

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top