Click here to Skip to main content
11,490,973 members (72,397 online)
Click here to Skip to main content

A Simple Gateway - Proxy Pattern

, 12 Aug 2013 CPOL 56.9K 926 73
Rate this:
Please Sign up or sign in to vote.
An example of a simple payment gateway.

Introduction

Here’s an approach I've used a few times to consume similar APIs into an application. A gateway of sorts, if you will. I'll try and keep it as brief and simple as possible. Having a look at the sample code will help you along as well. I also assume that you know something about Reflection and Web services.

Why would you implement such a thing?

Well (hypothetically, of course), imagine you get a third party payment solution that only accepts Visa cards. OK, you've implemented the Visa payment provider. Now, some of your customers would want to pay with MasterCard. And, you'd suspect that someone down the track would like to use Amex as well.

You would be able to add every payment provider into your code as you need, but this will get messy very quickly. Whereas, loosely coupled API wrappers (proxy wrappers, in this example) in a “gateway” will allow you to add and remove payment providers as you please. Even add some business logic that will decide which provider is the cheapest for your users... So, if you'd implemented a “gateway” from the start, adding new payment providers through the duration of your application life is a breeze, well, a lot easier and cleaner at least.

Here’s a quick example of creating an API gateway. I've used two web services in this example, but libraries can be plugged-in in a similar way.

Design

Looking at the image above, I'll briefly go though the entities of the solution. I'll be focusing mainly on the Gateway and API 1 and 2 entities.

  • Solution: This would typically be your application's business logic.
  • Gateway: The connection between your application and the API wrappers.
  • API 1 & 2: These are the wrappers around API Proxy 1 & 2.
  • API Proxy: These represent our payment gateway providers. These are the services that we will consume.

The gateway

I am going to go through the gateway first and then the API wrappers, and after that, I'll explain how it all hooks up.

The interface is the part that really gets the gateway together. As shown in the image above, the Gateway and API wrappers both implement the interface. So, let's whack one together.

In my example, I created IGateway:

public interface IGateway
{
   bool ValidateUser(Userobj user);
   string GetUserAddress(Userobj user);
}

I'm using Reflection in the gateway to call the API wrappers. By implementing an interface, we'd know exactly what method to call, parameters to pass down, and what to expect in return.

In this example, I want to validate a user and get his address. Now that we have defined what we need, we can define our interface. Let's add it to the gateway.

public class Gateway: IGateway
{
    IGateway proxy;

    public Gateway(String dllNamePath)
    {
       LoadIGhostAssembly(dllNamePath);
    }

    void LoadIGhostAssembly(String dllNamePath)
    {
       Assembly asm = Assembly.LoadFrom(dllNamePath);
       Type[] typesInAssembly = asm.GetTypes();

       foreach(Type t in typesInAssembly)
       {
          if (t.GetInterface(typeof(IGateway).FullName) != null) 
          {
             proxy = (IGateway)Activator.CreateInstance(t);
          }                       
       }
    }
        
    //IGateway Members
    public bool ValidateUser(Userobj user)
    {
       return proxy.ValidateUser(user);
    }

    public string GetUserAddress(Userobj user)
    {
       try
       {
          return proxy.GetUserAddress(user);
       }
       catch { return "Facility doesn't exist"; }            
             
    }
}

LoadIGhostAssembly is where we get the correct API wrapper to call. My business logic will pass down the Visa or Mastercard library location, depending on which one you want to use. As shown in the diagram above, the API wrappers also implement the IGhost interface. We will instantiate the correct reference and create a direct mapping to the API wrapper's interface methods.

The API wrappers

I've created a new library and added a class that implements my interface. Now, I will consume my desired web service (called WebServiceMasterCard and WebServiceVisa in the sample code), and match it to my interface methods.

public class Proxy : Global.IGateway
{
   #region IGateway Members

   public bool ValidateUser(Global.Userobj user)
   {
      try
      {
      ProxyWebServiceVisa.localhost.Service1 apiProxy = 
        new ProxyWebServiceVisa.localhost.Service1();

         return apiProxy.CheckUser(user.UserID);
      }
      catch { return false; }
   }

   public string GetUserAddress(Global.Userobj user)
   {
      ProxyWebServiceVisa.localhost.Service1 apiProxy = 
        new ProxyWebServiceVisa.localhost.Service1();

      return apiProxy.UserAddress(user.UserID, user.FirstName, user.LastName);
   }

   #endregion
}

Repeat this for every payment provider you want to add to your gateway.

The glue

simplegateway/gatewaysolution.jpg

This is how I would call the gateway from my application:

UserGateway.Gateway gateway = 
    new UserGateway.Gateway("c:\\temp\\ProxyWebServiceMasterCard.dll");

gateway.ValidateUser(user)
gateway.GetUserAddress(user)

As you can see, I call the gateway and pass down the desired service I need. As long as all the API wrappers have implemented the proper interface, you can add as many as you wish, even take some out. I hardcoded the location of the library, but I usually add it to an XML, database or common directory, so the code wouldn't need to be recompiled every time I need to add a service to the gateway. Once you've specified the library you want to use, the gateway will create an instance of it by using Reflection, and with the method mapping, you can call any method you desire.

And presto, create as many API wrappers as you wish, as long as they implement your interface and your gateway will recognise them.

Update: This is also an example of the Proxy Pattern implementation, which I unknowingly implemented at the time of conception. Please look at Proxy Patterns for more details.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Johan Vorster
Software Developer
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
Question[My vote of 1] Almost not even an article Pin
jfriedman17-Jan-14 15:35
professionaljfriedman17-Jan-14 15:35 
GeneralMy vote of 5 Pin
Md. Marufuzzaman23-Dec-12 0:27
mentorMd. Marufuzzaman23-Dec-12 0:27 
GeneralMy vote of 5 Pin
Kanasz Robert28-Sep-12 8:12
mvpKanasz Robert28-Sep-12 8:12 
QuestionMIGS Implementation Pin
DinoDonk11-Jan-10 1:43
memberDinoDonk11-Jan-10 1:43 
QuestionPayment Providers and Gateway API Pin
stixoffire22-Dec-08 22:41
memberstixoffire22-Dec-08 22:41 
AnswerRe: Payment Providers and Gateway API Pin
Johan "BJ" Vorster23-Dec-08 0:09
memberJohan "BJ" Vorster23-Dec-08 0:09 
GeneralI liked the idea Pin
Member 441105731-Oct-08 14:58
memberMember 441105731-Oct-08 14:58 
JokeRe: I liked the idea Pin
Johan "BJ" Vorster2-Nov-08 2:42
memberJohan "BJ" Vorster2-Nov-08 2:42 
GeneralRe: I liked the idea Pin
yasmin8316-Nov-08 2:08
memberyasmin8316-Nov-08 2:08 
GeneralRe: I liked the idea Pin
Johan "BJ" Vorster17-Nov-08 4:19
memberJohan "BJ" Vorster17-Nov-08 4:19 
GeneralRe: I liked the idea Pin
sj17118-Jun-09 23:09
membersj17118-Jun-09 23:09 
GeneralRe: I liked the idea Pin
Johan "BJ" Vorster19-Jun-09 1:28
memberJohan "BJ" Vorster19-Jun-09 1:28 
QuestionOnline payment - MIGS Pin
Blumen27-Oct-08 22:25
memberBlumen27-Oct-08 22:25 
AnswerRe: Online payment - MIGS Pin
Xabatcha28-Oct-08 2:52
memberXabatcha28-Oct-08 2:52 
GeneralRe: Online payment - MIGS Pin
Blumen28-Oct-08 4:14
memberBlumen28-Oct-08 4:14 
AnswerRe: Online payment - MIGS Pin
Johan "BJ" Vorster28-Oct-08 3:05
memberJohan "BJ" Vorster28-Oct-08 3:05 
GeneralRe: Online payment - MIGS Pin
Blumen28-Oct-08 4:23
memberBlumen28-Oct-08 4:23 
GeneralRe: Online payment - MIGS Pin
Johan "BJ" Vorster28-Oct-08 5:12
memberJohan "BJ" Vorster28-Oct-08 5:12 
GeneralRe: Online payment - MIGS Pin
Blumen28-Oct-08 20:58
memberBlumen28-Oct-08 20:58 
Johan "BJ" Vorster wrote:
Enjoy the hot weather Manu!!

D'Oh! | :doh:

Johan "BJ" Vorster wrote:
It's cold here!

I wish I could live in such a place, or back home in India Smile | :)

Johan "BJ" Vorster wrote:
Rather then searching for MiGS, search for "payment solutions" or "online payment solutions providers".

Isn't MiGS similar to Pay Pal and Google Checkout? The reason why I was searching for "MiGS and ASP.NET shopping cart" was to get some idea on implementing MiGS with ASP.NET apps, I thought MiGS was a different service as a whole and its implementation would be different from how we could integrate Paypal or other service with ASP.NET Apps. I was also referring to this article from Rick Strahl:
http://www.west-wind.com/presentations/aspnetecommerce/aspnetecommerce.asp#WebSiteIntegration[^]

Johan "BJ" Vorster wrote:
Have you thought about delivery service for your solution?

Does that mean how to deliver ordered products to customers? Our group has a logistics division on its own, I guess they'll take care of it.

Now I'm bit confused on Gateway, payment provider, etc.. I had thought that payment provider is also the gateway. I'll get back to you once I more information on this project.

But I really appreciate that you've taken the time to reply to my queries Johan.

Laters
Manu

“The thing for you is a burial permit. You have only to speak and I will see that you get it.”

AnswerRe: Online payment - MIGS Pin
Johan "BJ" Vorster29-Oct-08 1:09
memberJohan "BJ" Vorster29-Oct-08 1:09 
GeneralRe: Online payment - MIGS Pin
Blumen29-Oct-08 3:38
memberBlumen29-Oct-08 3:38 
GeneralRe: Online payment - MIGS Pin
Johan "BJ" Vorster29-Oct-08 4:34
memberJohan "BJ" Vorster29-Oct-08 4:34 
GeneralRe: Online payment - MIGS Pin
Blumen29-Oct-08 19:05
memberBlumen29-Oct-08 19:05 
GeneralRe: Online payment - MIGS Pin
Blumen1-Nov-08 4:43
memberBlumen1-Nov-08 4:43 
GeneralRe: Online payment - MIGS Pin
Johan "BJ" Vorster2-Nov-08 3:10
memberJohan "BJ" Vorster2-Nov-08 3:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 12 Aug 2013
Article Copyright 2008 by Johan Vorster
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid