We've Always Got Something to Say.

Read our thoughts on what's happening in the industry.

August 26 , 2009

.NET Online Payment Gateways: A Factory Pattern Model

by

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 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="https://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.

One Response to “.NET Online Payment Gateways: A Factory Pattern Model”

  1. Varma says:

    Nice and simple. Can you please provide sample project for this?

Leave a Reply

 

In a Nutshell

Since 1998, Atlantic BT has been a full service web development company that offers the tools, resources and services to get your business moving. We focus on combining new ideas, specific requirements, and years of experience into high-quality, results-oriented web solutions for small to medium sized businesses. If you want the best website possible that generates real results, let's get started.

Atlantic Business Technologies, Inc.
4509 Creedmoor Road, 3rd Floor
Raleigh, North Carolina 27612
  • Pinnacle Business Award Winner
  • Triangle Business Journal's Top 40 Under 40
  • Yahoo Search Marketing Ambassador
  • Google Adwords Qualified Company