gpt4 book ai didi

ruby-on-rails - 有没有办法在 Rails 中保存 Paypal 的 payKey

转载 作者:太空宇宙 更新时间:2023-11-03 16:31:10 25 4
gpt4 key购买 nike

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

我正在尝试使用 PayPal 在成功付款时返回的 payKey 作为变量?这样我就可以使用 if else 语句将某些东西标记为已支付或未支付;告诉应用程序显示立即付款或下载选项...这是我的 paypal lib 文件,定义付费方法的模型和使用付费方法的 Controller :

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

最佳答案

我希望我能正确理解这个问题。是的,有一种方法,您可以只添加一列,例如paypalkey 到您的提交 模型。 (你也可以命名为paid,不知道你要不要保留ke​​y)。

为了做到这一点:

$ 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 key 。

编辑:

作为免责声明,我之前没有使用 PayPal 的经验,也不知道 paypal key 是否保密或是否应该以安全的方式存储。

关于ruby-on-rails - 有没有办法在 Rails 中保存 Paypal 的 payKey,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23669805/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com