Search code examples
ruby-on-railsruby-on-rails-3paypalactivemerchantpaypal-adaptive-payments

This transaction is invalid. Please return to the recipient's website and try again


This is my controller:

setup_purchase method does works fine for me with my data api, but preapprove_payment method does not works.

def pay
 gateway =  ActiveMerchant::Billing::PaypalAdaptivePayment.new(
  :login => "email",
  :password => "pass",
  :signature => "signature",
  :appid => "APP-80W284485P519543T" )
  response = gateway.preapprove_payment( 
  :return_url => user_orders_url(current_user),
  :cancel_url => user_orders_url(current_user),
  :sender_email =>"email",
  :start_date => Time.now,
  :end_date => Time.now,
  :currency_code =>"USD",
  :max_amount => "20",
  :maxNumberOfPayments => "2")
  puts response.preapproval_key
  puts gateway.debug
  # for redirecting the customer to the actual paypal site to finish the payment.
  redirect_to (gateway.redirect_url_for(response["preapproval_key"]))
end

I get in log:

PA-8K9332086D720151L
{:url=>#<URI::HTTPS:0xdf9bd18 URL:https://svcs.sandbox.paypal.com/AdaptivePayments/Preapproval>, :request=>"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<PreapprovalRequest>\n  <requestEnvelope>\n    <detailLevel>ReturnAll</detailLevel>\n    <errorLanguage>en_US</errorLanguage>\n    <senderEmail>email</senderEmail>\n  </requestEnvelope>\n  <endingDate>2012-07-20T19:09:20</endingDate>\n  <startingDate>2012-07-20T19:09:20</startingDate>\n  <maxTotalAmountOfAllPayments>20</maxTotalAmountOfAllPayments>\n  <maxNumberOfPayments>2</maxNumberOfPayments>\n  <currencyCode>USD</currencyCode>\n  <cancelUrl>http://localhost:3000/en/u/maserranocaceres/orders</cancelUrl>\n  <returnUrl>http://localhost:3000/en/u/maserranocaceres/orders</returnUrl>\n</PreapprovalRequest>\n", :response=>"{\"responseEnvelope\":{\"timestamp\":\"2012-07-20T10:09:22.459-07:00\",\"ack\":\"Success\",\"correlationId\":\"ada6a3e7da93d\",\"build\":\"DEV\"},\"preapprovalKey\":\"PA-8K9332086D720151L\"}"}

Full response:

#<ActiveMerchant::Billing::AdaptivePaymentResponse:0xc817278 @json="{\"responseEnvelope\":{\"timestamp\":\"2012-07-23T07:43:56.603-07:00\",\"ack\":\"Success\",\"correlationId\":\"7f759c5da73ad\",\"build\":\"DEV\"},\"preapprovalKey\":\"PA-1M101813XU7801314\"}", @response=#<Hashie::Rash preapproval_key="PA-1M101813XU7801314" response_envelope=#<Hashie::Rash ack="Success" build="DEV" correlation_id="7f759c5da73ad" timestamp="2012-07-23T07:43:56.603-07:00">>, @xml_request="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<PreapprovalRequest>\n  <requestEnvelope>\n    <detailLevel>ReturnAll</detailLevel>\n    <errorLanguage>en_US</errorLanguage>\n    <senderEmail>[email protected]</senderEmail>\n  </requestEnvelope>\n  <endingDate>2012-08-22T16:43:54</endingDate>\n  <startingDate>2012-07-23T16:43:54</startingDate>\n  <maxTotalAmountOfAllPayments>20</maxTotalAmountOfAllPayments>\n  <maxNumberOfPayments>1</maxNumberOfPayments>\n  <currencyCode>USD</currencyCode>\n  <cancelUrl>http://localhost:3000/en/u/maserranocaceres/orders</cancelUrl>\n  <returnUrl>http://localhost:3000/en/u/maserranocaceres/orders</returnUrl>\n</PreapprovalRequest>\n", @request={"PreapprovalRequest"=>{"requestEnvelope"=>{"detailLevel"=>"ReturnAll", "errorLanguage"=>"en_US", "senderEmail"=>"email"}, "endingDate"=>"2012-08-22T16:43:54", "startingDate"=>"2012-07-23T16:43:54", "maxTotalAmountOfAllPayments"=>"20", "maxNumberOfPayments"=>"1", "currencyCode"=>"USD", "cancelUrl"=>"http://localhost:3000/en/u/maserranocaceres/orders", "returnUrl"=>"http://localhost:3000/en/u/maserranocaceres/orders"}}, @action="Preapproval">

What am I doing wrong?


Solution

  • The problem was fixed.

    Change

    gateway.redirect_url_for(response["preapprovalKey"])

    to

    gateway.redirect_pre_approval_url_for(response["preapprovalKey"])

    The correct method is redirect_pre_approval_url_for

    You can see the fix in this post:

    preapproved payments with paypal in rails 3.2

    Thank you very much!