More on ActiveMerchant and Paypal
Written by Robin Fisher in 734 words
18
May
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:
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:
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.
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)?
Yes, this enables you to accept all forms of credit and debit cards without the need for your users to have a Paypal account.
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
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
Hello! I’m using this same approach for recurring billing.
One question I have…when you MODIFY an existing profile on PayPal by passing in profile_id, do you know if there’s a way around having the send the credit card again?
I’d prefer not to store credit card numbers and then pass them again when updating a profile on paypal.
Thanks,
Andy