PlugNPay for ActiveMerchant

My PlugNPay module was recently added to the ActiveMerchant payment processing library for Rails.

At Lovetastic, we were forced by nit-picky bureaucratic circumstances beyond our control to use the PlugNPay gateway for our merchant account, even though there wasn’t a Ruby interface to it at the time. Implementing the gateway interface myself, I was, however, pleasantly surprised by its features.

PlugNPay supports member/subscription management. Yes, this allows you to submit automatically rebilling charges, but it also simply allows you to save a customer in the gateway under a unique ID and charge that customer again at a later date using that unique ID without having to store their credit card details in the interim. This allows you to “remember” the credit card details of a cusomter without assuming the liability of actually storing the data on your servers. It’s a wonderful thing.

The unique ID is a “username” in PlugNPay gateway parlance, but you’ll probably have better luck if you use ActiveRecord IDs. That way if you allow users to change their account usernames there won’t be any conflicts.

Here’s how it works.

We begin by initializing the gateway using PlugNPay.

@gateway = ActiveMerchant::Billing::Base.gateway(:plugnpay).new(MERCHANT_GATEWAY_ACCOUNT_DETAILS)

Where MERCHANT_GATEWAY_ACCOUNT_DETAILS is a hash that specifies your merchant account login info, given to you by your payments provider.

Then we can add the member to the gateway database with a simple call to add_member.

     @order = params[:order]

add_member

      def add_member(money, creditcard, options = {})
        # If you're just adding a member without setting up a recurring billing, just
        # pass nil through the money parameter.
        post = {}
        add_creditcard(post, creditcard)        
        add_address(post, options)        
        add_customer_data(post, options)
        post[:username]  = options[:customer_username]       unless options[:customer_username].blank?
        post[:startdate] = options[:start_date]              unless options[:start_date].blank?
        post[:enddate]   = options[:end_date]                unless options[:end_date].blank?
        post[:recfee]    = money.blank? ? '0' : money
        post[:billcycle] = options[:months_in_billing_cycle] unless options[:months_in_billing_cycle].blank?
        commit('add_member', money, post)
      end

Note that member management is an add-on feature to a PlugNPay merchant account, so it may not be enabled by default by your merchant account provider. Some persistent nagging may be required, as was the case with our provider.