flippa/pp-adaptive

undefined method pay_key

Closed this issue · 5 comments

riaw commented

Not sure if it's a coding mistake on my part or a bug, but I'm getting error, undefined method pay_key when i try to redirect to paypal, Link is:

<%= link_to "confirm donation", 'https://www.sandbox.paypal.com/webscr?cmd=_ap-payment&paykey='"#{response.pay_key}" %>

What is the response object? Can you should the code that creates it?

riaw commented

Once the object with paypal details are saved, the user is redirected to the below method that generates the paykey and then the link to go to paypal is shown in the view. In the logs I see the pay key being generated. It shows Pay Key: AP-XXXXXXXXX. wher xxx is the pay key.

Controller:

def new
@purchase = Purchase.new
end

def create
@purchase = Purchase.new(params[:purchase])
if @purchase.save
redirect_to confirm_purchase_purchases_path
else
render('new')
end
end

def confirm_purchase
client = AdaptivePayments::Client.new(
:user_id => "username here",
:password => "password here",
:signature => "signature here",
:app_id => "APP-80W284485P519543T",
:sandbox => true
)
client.execute(:Pay,
:action_type => "PAY",
:receiver_email => "receiver email here",
:receiver_amount => amount here,
:currency_code => "USD",
:cancel_url => "https://localhost:3000",
:return_url => "https://localhost:3000/thank_you"
) do |response|

      if response.success?
        render "Pay key: #{response.pay_key}"
        puts "Status: #{response.payment_exec_status}"
      else
        puts "#{response.ack_code}: #{response.error_message}"
      end
    end 
end

View

<%= link_to "confirm donation", 'https://www.sandbox.paypal.com/webscr?cmd=_ap-payment&paykey='"#{response.pay_key}" %>

That response object will not be available inside the view, since it is not an instance variable and it just a regular local variable. I think the response object in the view will be an instance of the Rails HTTP Response. You should pass the pay key into the view as an instance variable and use that.

# in your controller
if response.success?
    @pay_key = response.pay_key
else
    # error case
end

# in your view
<%= link_to "confirm donation", 'https://www.sandbox.paypal.com/webscr?cmd=_ap-payment&paykey='"#{@pay_key}" %>
riaw commented

Got it. Thanks

Does this gem support IPN notifications ? How did you guys handle the IPN notifications ?