有没有办法在Rails中保存Paypal的payKey

时间:2014-05-15 05:19:27

标签: ruby-on-rails paypal paykey

任何人都可以提供的帮助将不胜感激。

我正在尝试使用PayPal成功付款时返回的payKey作为变量?这样我就可以使用if else语句来标记付费或不付费的内容;告诉应用程序要么现在显示付款还是下载选项...这是我的paypal lib文件,定义付费方法的模型和使用付费方法的控制器:

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

模型/ 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 个答案:

答案 0 :(得分:1)

我希望我能正确理解这个问题。是的,有办法,你可以添加一个列,例如paypalkey模型的Submission。 (您也可以将其命名为paid,我不知道您是否要保留密钥。

为了做到这一点:

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

然后你可以:

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

def paid?
   self.paypalkey.present?
end

从此以后就知道这个提交有一个PayPal密钥。

编辑:

正如免责声明一样,我之前没有PayPal的经验,也不知道PayPal密钥是保密还是应该以安全的方式存储。

答案 1 :(得分:0)

我意识到payKey不会成为我问题的答案。 payKey仅用于尝试付款,即使您取消也是如此;它将返回payKey ......

我需要弄清楚付款成功时实际返回的内容(如确认号码)......以及如何将该号码保存到字符串中......