Skip to content
AtlanticBT Monogram Atlanticbt.com
Menu
code
August 26, 2009

.NET Online Payment Gateways: A Factory Pattern Model

One thing that any good development company should do is strive to create reusable code.  Thinking back to projects I’ve worked on in my first year here, it occurred to me that quite a few of them have involved some form of online payment processing.  However, each client has used a different payment processor that has required custom programming.  I began thinking how nice it would be to submit a generic payment object to a gateway with large disregard to what particular vendor the payment is being submitted to.   Early on in developing this, it occurred to me that a factory pattern would be the way to go; essentially what I want is a “factory” that will figure out what type of online payment gateway I need and construct that for me.

To start things, I made a simple payment gateway interface to make sure my gateway objects will do the one thing I need them to:


public interface IPaymentGateway
{
string SubmitPayment(Payment payment);
}

Essentially what I’m saying here is that no matter what type of gateway I’m using, I’ll need to submit a payment.

Next, we have a simple factory pattern to return some type of gateway:

public class PaymentGatewayFactory
{
public IPaymentGateway CreatePaymentGateway()
{
PaymentGatewayRepository paymentGatewayRepository = new PaymentGatewayRepository();
string gatewayType = paymentGatewayRepository.GetGatewayType();

switch(gatewayType)
{
case “Authnet”:
return new Authnet();
case “Paypal”:
return new Paypal();
case “Payflow”:
return new PayflowPro();
default:
throw new NotSupportedException(“This payment gateway is not supported.”);
}
}
}

Here we have a call to a PaymentGatewayRepository class (not included), which will read a config file and return a string representing what type of payment gateway the client is using. Next, a simple switch is done that will return the appropriate payment gateway class (or a custom exception if the gateway is not yet supported). Each of the gateway classes implements the IPaymentGateway interface.

The beauty of this is that once this project is built, submitting payments is a breeze. Just include the assembly in a project, reference it, and then follow these three easy steps:

  1. Add the necessary config keys.<!-- Gateway types | Possible Values: Authnet, Paypal, Payflow -->
    <add key="GatewayType" value="Authnet">
    <!– Authnet Keys –>
    <add key=”Anet_URL” value=”http://test.authorize.net/gateway/transact.dll”/>
    <add key=”payment_login_key” value=”123456789″/>
    <add key=”payment_tran_key” value=”987654321″/>
    <add key=”payment_auth_name” value=”Example”/>
  2. Build the payment object.Payment payment = new Payment();
    payment.BillingFirstName = "Jeremy";
    payment.BillingLastName = "Wiggins";
    etc…
  3. Submit the payment.IPaymentGateway gateway = new PaymentGatewayFactory().CreatePaymentGateway();
    gateway.SubmitPayment(payment);

And that’s all there is to it. This will be a nice addition to a reusable component library. Once the logic has been implemented for a new payment gateway, it never has to be thought about again. Just add a few lines to a config file and submit payments in a couple of easy steps.

The Atlantic BT Manifesto

The Ultimate Guide To Planning A Complex Web Project

Partner With Us

Ready for modern web technology and a sharp, user-friendly design? We want to give you exactly that. Contact us to get started.

Contact

Insights

Atlantic BT's Insights

We’re sharing the latest concepts in tech, design, and software development. Learn more about our findings.

Questions & Answers

What is the best web development framework?
Many people commonly ask “what is a framework in web development?” Web development frameworks can easily be confused with web development tools, languages, or parts of the web development stack (like PHP, Ruby, or Javascript).
Learn More
What is the best programming language for web development?
If there was one “best” programming language, then everything else would be obsolete. The reality is that there are so many different programming languages because there is no “best” language for any situation.
Learn More
How much does web development cost?
Web development can vary from a few hundred to millions of dollars depending on what is needed. You may simply need some changes to something that already exists, or you'd like to build a large or complex application.
Learn More
What is PHP web development?
PHP is a back end language primarily used for custom applications, content management systems (such as Wordpress), eCommerce engines (such as Magento), or even massive sites like Facebook.
Learn More
What is the best way to become a web developer?
We get lots of questions from university students working on projects -- How do I get into web development? How long does it take to learn? How much do web developers make?
Learn More
What is front end vs. back end development?
As web development evolved, it separated into logical specialization: front end web development and back end development. While back end development involves the server-side development, front end is the final rendering.
Learn More
What is full stack web development?
Full stack web development as a term evolved due to the separation of roles between front end and back end developers. A “full stack” developer is a developer that can work in both front end and back end technologies.
Learn More