0

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

我正在尝试使用 PayPal 在成功付款时返回的 payKey 作为变量?这样我就可以使用 if else 语句将某物标记为已付款或未付款;告诉应用程序显示立即付款或下载选项...这是我的贝宝 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

模型/提交.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

提交控制器

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
4

2 回答 2

1

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.

于 2014-05-15T09:18:01.517 回答
0

我意识到 payKey 不会解决我的问题。payKey 仅用于尝试付款,即使您取消;它会返回一个payKey ...

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

于 2014-05-16T03:54:13.383 回答