I’m using the paypal-express gem in my ruby on rails project. I want users to be able to purchase digital goods on my web application. I’ve created a controller with a new and create action. The new action generates a PayPal link which users visit. Users are returned from PayPal to the create action where I attempt to complete the order.
Everything works fine in development when using sandbox credential, when using production credentials I receive a “10007 – permission denied” error from PayPal.
I have triple checked the username/password/API signature i’ve entered for production and they are correct (The user is sent to the correct PayPal store).
Here is my controller:
class DigitalPaymentsController < ApplicationController
before_filter :authenticate_user!, :only => [ :new, :create ]
def new
Paypal.sandbox! unless Rails.env.production?
response = paypal_request.setup(
payment_request,
success_url,
cancel_url,
:no_shipping => true
)
@paypal_url = response.redirect_uri
end
def create
begin
response = paypal_request.checkout!(params[:token], params[:PayerID], payment_request)
if response.payment_info.first.payment_status == 'Completed'
# TODO: Handle complete payments
else
# TODO: Handle non complete payments
end
rescue Paypal::Exception::APIError => e
# Payment has failed, failure details are in e.message, also check params
end
end
private
def paypal_request
@request ||= Paypal::Express::Request.new(
:username => PAYPAL_USERNAME,
:password => PAYPAL_PASSWORD,
:signature => PAYPAL_SIGNATURE
)
end
def payment_request
@payment_request ||= Paypal::Payment::Request.new(
:currency_code => :USD,
:amount => 15,
:items => [{
:name => "Awesome Product",
:description => 'Description of awesomeness',
:amount => 15,
:category => :Digital
}]
)
end
end
Having a look into the paypal-express gem, it seems to call DoExpressCheckoutPayment on the PayPal API and pass the PayerID and the Token. The PayPal API documentation didn’t list a way to resolve my error (10007).
The reason for the PayPal API error was that i needed to call the PayPal
GetExpressCheckoutDetailsaction before checking out the transaction. I did this by adding a line from the paypal-express gem wiki over here: https://github.com/nov/paypal-express/wiki/Instant-PaymentAll I needed was
paypal_request.details(params[:token])