captured sparks

Live from Stevenage, England
18 May, 2008

More on ActiveMerchant and Paypal

I’ve had a few comments asking for some more information setting up AM with Paypal. I won’t profess to be an expert and there may be better ways of doing this.

I am using Paypal UK Website Payments Pro and for this have set up an account that gives me access to Paypal Manager.

As per my previous post, an account is created using the account key as subdomain model:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def create
    @account = Account.new(params[:account])
    @account.initial_plan = params[:plan_id]
    if @account.save
      @account.set_admin
      if @account.initial_plan == "2" 
        redirect_to :host => account_host(@account.name), :controller => 'sessions', :action => 'new'
      else
        redirect_to :host => account_host(@account.name), :controller => 'subscriptions', :action => 'new'
      end
    else
      flash[:error] = "Could not create account. Please try again." 
      redirect_to :controller => 'accounts', :action => 'new'
    end
  end

If the customer has selected anything other than a free account, they are redirect to the new action of the subscriptions controller. I am using the subscription model to act as a join between an account and a particular plan.

The template for the new action is a simple form with space for credit card information. Creating a subscription calls the following create action in the controller:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def create
    @plan = Plan.find_by_id(@account.initial_plan)
    first_name = params[:cc_name].split(' ')[0]
    surname = params[:cc_name].split(' ')[1]
    name = params[:cc_name]
    number = params[:cc_number]
    amount = @plan.price*100
    credit_card = {
      :number     => "#{number}", 
      :first_name => "#{first_name}", 
      :last_name  => "#{surname}",
      :name       => "#{name}",
      :month      => "#{params[:cc_month]}", 
      :year       => "#{params[:cc_year]}",        
      :verification_value => "#{params[:cc_cvv]}", 
      :type       => "#{params[:cc_type]}" 
    }
    subscription = Subscription.recurring(amount, credit_card, options = {:ip => request.remote_ip})
    if subscription
      subscription.plan_id = @plan.id
      subscription.account_id = @account.id
      subscription.save
      flash[:notice] = "Your account has been setup.  You can now login!" 
      redirect_to matters_url
    else
      flash[:error] = "YIKES!" 
      redirect_to new_subscription_url
    end
  end

I have no doubt that a lot of this could be refactored into the model. At the moment, I am focussing on getting the app running, not tidying up the code. You will note the class method “recurring”, which is in the subscription.rb file. The code for this is taken straight from the AM pdf mentioned in my earlier post so I cannot repost that here. This method creates a connection through AM to Paypal and creates a recurring subscription. Information that is returned includes a profile id that can be used to uniquely identify a particular subscription in both the app and Paypal manager.

Updating the subscription is fairly similar to creating a new subscription except you pass the profile id through so that Paypal knows which account to change.

One thing to note is that it is important to ensure consistency between Paypal and the subscriptions table in your app. It is very easy to update a subscription in Paypal so that a customer is billed the correct amount but fail to update the internal record in your app. This will result in revenues being either significantly higher or lower than you expect.

One of the questions in the comments to the original post is how much information in the pdf was relevant to Paypal US. I can’t directly answer that but I can say that the code works as a great starting point but some re-coding will necessary to make it work for your app.


Comments

01 July, 2008

Matt Collins pronounces:

Thanks for sharing this. I'm looking at setting up something very similar. Am I right in thinking you can accept and update recurring payments made via credit or debit card (not just via a PayPal account)?

02 July, 2008

Robin Fisher states:

Yes, this enables you to accept all forms of credit and debit cards without the need for your users to have a Paypal account.

01 August, 2008

Chris Cera says:

Thank you for your posts, they were helpful to me when I was getting started. I have written an implementation of Paypal Website Payments Pro (US) WITH recurring billing: http://blog.vuzit.com/2008/08/01/paypal-website-payments-pro-us-with-recurring-billing-and-activemerchant/ I hope it is useful to this audience: -Chris

18 August, 2008

Jon Baker states:

Hi Robin, Thanks for your articles on this. I have the AM PDF by Cody but cannot see which section you used for creating recurring payments. Chris above has rolled his own AM gateway and I have seen Cody has submitted a patch for PayPal Express Checkout to do Recurring payments. Looking at the docs for AM it suggests that the PayPal gateway does not support recurring payments. Can you confirm whether you are using standard method calls on the AM code for example: profile_id = PayFlowGateway.recurring(amount, credit_card, options) - I think PayPal Websie Payemnts Pro (UK) is indeed PayFlow. And if you did this, do you need to setup an authorisation first. Alternatively, have you had to customise or extend the PayPal gateway to create the recurring payment request manually? Thanks in advance

Leave a comment