1

做 railscast #143。代码如下。当我添加安全内容时,我得到“我们无法解密证书 ID”。开发中。当我把安全的东西拿出来时,它又能正常工作了。我已经用新证书等重做了整个过程几次。没运气。

关于接下来要尝试什么的任何想法?

我遇到了与这篇文章完全相同的问题,它在生产中遇到了它,它神奇地开始工作:

无法在 Rails 中使用 PayPal 加密网站付款

在“购买这些”页面中:

<%= form_tag "https://www.sandbox.paypal.com/cgi-bin/webscr" do %>
<%= hidden_field_tag :cmd, "_s-xclick" %>
<%= hidden_field_tag :encrypted, @cart.paypal_encrypted("#{@url}/buy_these", payment_notifications_url) %>
<p><%= submit_tag "Buy these for #{number_to_currency(@cart.total_price)}" %></p>

在购物车.rb 中:

PAYPAL_CERT_PEM = File.read("#{Rails.root}/certs/paypal_cert.pem")
APP_CERT_PEM = File.read("#{Rails.root}/certs/app_cert.pem")
APP_KEY_PEM = File.read("#{Rails.root}/certs/app_key.pem")

def encrypt_for_paypal(values)
    signed = OpenSSL::PKCS7::sign(OpenSSL::X509::Certificate.new(APP_CERT_PEM), OpenSSL::PKey::RSA.new(APP_KEY_PEM, ''), values.map { |k, v| "#{k}=#{v}" }.join("\n"), [], OpenSSL::PKCS7::BINARY)
    OpenSSL::PKCS7::encrypt([OpenSSL::X509::Certificate.new(PAYPAL_CERT_PEM)], signed.to_der, OpenSSL::Cipher::Cipher::new("DES3"), OpenSSL::PKCS7::BINARY).to_s.gsub("\n", "")
end

def paypal_encrypted(return_url, notify_url)
  values = {
    :business => 'seller_1316654707_biz@myurl.com',
    :cmd => '_cart',
    :upload => 1,
    :return => return_url,
    :invoice => id,
    :notify_url => notify_url,
    :cert_id => 'DVFY6JS476MR8'
  }
things.each_with_index do |item, index|
    values.merge!({
      "amount_#{index+1}" => item.price,
      "item_name_#{index+1}" => item.id,
      "item_number_#{index+1}" => item.id,
      "quantity_#{index+1}" => 1
    })
  end
  encrypt_for_paypal(values)
end
4

3 回答 3

2

我又重复了整个过程几次,它开始工作了。还在类似于下一个答案的过程中查看了每个值。不幸的是,每当我切换部署平台时,我似乎都会遇到同样的问题。最终,它又开始工作了。

于 2011-12-06T21:53:44.540 回答
1

我遇到了同样的问题,但问题与paypal_cert.pem文件有关,即 Paypal 的证书文件。

Paypal 使用不同的证书进行登台和直播环境。请检查paypal_cert.pem文件,您会看到第一行提到它应该使用的环境。

我使用以下代码

paypal_cert_file_name = ENV["paypal_cert_file_name"] || "paypal_cert_prod";

PAYPAL_CERT_PEM = File.read("#{Rails.root}/certs/#{paypal_cert_file_name}.pem")

有两个文件paypal_cert_prod.pempaypal_cert_sandbox.pem每个环境一个。

于 2014-10-06T11:59:18.550 回答
0

由于我们在 paypal_encrypted 方法中加密了许多值,因此当在同一过程中发生一些加密错误时,可能会出现此错误。

确保问题不是因为加密错误的最佳方法,尝试从上述给定值哈希中一一删除键值对并发出付款请求。

于 2011-10-23T15:33:42.297 回答