More on ActiveMerchant and Paypal
Posted on 18 May 2008 by Robin FisherI’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.
Matt Collins
Posted on 01 July, 2008