Вопрос

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
Это было полезно?

Решение 2

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...

Другие советы

I hope I understand this question correctly. Yes there is a way, you could just add a column e.g. paypalkey to your Submission model. (You can also name it paid, I dont know if you want to keep the key).

In order to do this:

$ rails generate migration AddPayPalKeyToSubmission paypalkey:string
$ rake db:migrate
$ rake db:test:prepare

Then you could just:

if response.success?
  submission.update_attributes(paypalkey: response.pay_key)

and

def paid?
   self.paypalkey.present?
end

and from that point onwards would know that this submission has a paypal key.

Edit:

Just as a disclaimer, I don't have prior experience with PayPal and don't know if the paypal key is confidential or should be stored in a secure manner.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top