Any help anyone can provide would be greatly appreciated.
I am trying to use the payKey that PayPal returns on a successful payment, as a variable? so that I can use an if else statement to flag something as being paid or not paid; to tell the app to either show the pay now or download option... This is my paypal lib file, the model where the paid method is defined and the controller where the paid method is used:
lib/pay_pal_api.rb
require "pp-adaptive"
class PayPalAPI
def self.payment_url(submission)
amount = submission.amount_in_cents.to_f / 100.0
recipient_cut = amount * 0.9
recipient_email = submission.submitter.paypal_email
client.execute(:Pay,
:action_type => "PAY",
:currency_code => "USD",
:cancel_url => "http://session-logix.herokuapp.com/my-studio",
:return_url => "http://session-logix.herokuapp.com/submissions/#{submission.id}",
:feesPayer => "PRIMARYRECEIVER",
:receivers => [
{ :email => "barrypas37@gmail.com", :amount => amount, :primary => true },
{ :email => recipient_email, :amount => recipient_cut, :primary => false }
]
) do |response|
if response.success?
puts "Pay key: #{response.pay_key}"
# send the user to PayPal to make the payment
# e.g. https://www.sandbox.paypal.com/webscr?cmd=_ap-payment&paykey=abc
return client.payment_url(response)
else
puts "#{response.ack_code}: #{response.error_message}"
end
end
return nil
end
models/submission.rb
class Submission < ActiveRecord::Base
belongs_to :submitter, :class_name => "User"
belongs_to :project
def price
sprintf "%.2f", (self.amount_in_cents.to_f/100.0)
end
def price=(val)
self.amount_in_cents = (val.to_f*100.0).to_i
end
def paid?
!!self.$payKey
end
end
submissions_controller
def pay
@submission = Submission.find(params[:id])
if @submission.project.user_id == current_user.id
if !@submission.paid?
redirect_to PayPalAPI.payment_url(@submission)
return
end
flash[:notice] = "You have already paid for this submission"
else
flash[:error] = "You are not authorized to view this page"
end
end
I realize the payKey is not going to be the answer to my problem. The payKey is given just for trying to go to a payment, and even if you cancel; it will return a payKey...
I need to figure out instead what is actually returned when the payment is successful (like a confirmation number)...and how to save that number to a string instead...