ruby-on-rails - 无法让 PayPal 加密网站付款在 Rails 中工作

标签 ruby-on-rails ruby paypal

我在让 PayPal 加密网站付款在 Rails 网站上工作时遇到问题。

我在发布到 PayPal URL 时收到两条不同的错误消息 - 在我使用沙箱的登台网站上,我收到:

The certificate has been removed. Please use a valid certificate.

而在生产现场,我得到:

We were unable to decrypt the certificate id.

据我所知,它们的设置完全相同,只是一个使用 PayPal 沙盒公钥,另一个使用普通的 PayPal 公钥。我一定是忽略了一些细节,但我已经为此苦苦思索了几天。

我使用以下命令生成公钥和私钥:

openssl genrsa -out app_key.pem 1024

openssl req -new -key app_key.pem -x509 -days 365 -out app_cert.pem

然后我将 app_cert.pem 上传到 PayPal 并将我的证书 ID 放入这样的文件中:

development:
  user: seller_1259814545_biz@somedomain.com
  action_url: https://www.sandbox.paypal.com/cgi-bin/webscr 
  paypal_cert_file: certs/paypal_sandbox_cert.pem
  app_cert_file: certs/app_cert.pem
  app_key_file: certs/app_key.pem
  cert_id: CBDFN7JXBM2ZQ
  secret: dfasdkjh3453

test:
  user: seller_1259814545_biz@somedomain.com
  action_url: https://www.sandbox.paypal.com/cgi-bin/webscr 
  paypal_cert_file: certs/paypal_sandbox_cert.pem
  app_cert_file: certs/app_cert.pem
  app_key_file: certs/app_key.pem
  cert_id: CBDFN7JXBM2ZQ
  secret: dfasdkjh3453

staging:
  user: seller_1259814545_biz@somedomain.com
  action_url: https://www.sandbox.paypal.com/cgi-bin/webscr 
  paypal_cert_file: certs/paypal_sandbox_cert.pem
  app_cert_file: certs/app_cert.pem
  app_key_file: certs/app_key.pem
  cert_id: CBDFN7JXBM2ZQ
  secret: dfasdkjh3453

production:
  user: business@somedomain.com
  action_url: https://www.paypal.com/cgi-bin/webscr 
  paypal_cert_file: certs/paypal_cert.pem
  app_cert_file: certs/app_cert.pem
  app_key_file: certs/app_key.pem
  cert_id: QG2TTZZM9DUH6
  secret: dfasdkjh3453

然后我在购物车模型中使用以下代码来加密数据:

class Cart < ActiveRecord::Base
  has_many :line_items, :dependent => :destroy

  PAYPAL_CERT_PEM = File.read("#{Rails.root}/#{PAYPAL_CONFIG[:paypal_cert_file]}")
  APP_CERT_PEM = File.read("#{Rails.root}/#{PAYPAL_CONFIG[:app_cert_file]}")
  APP_KEY_PEM = File.read("#{Rails.root}/#{PAYPAL_CONFIG[:app_key_file]}")

  ...

  def paypal_data(return_url, notify_url)
    values = {
      :business => PAYPAL_CONFIG[:user],
      :cert_id => PAYPAL_CONFIG[:cert_id],
      :custom => PAYPAL_CONFIG[:secret],
      :cmd => '_cart',
      :upload => 1,
      :return => return_url,
      :notify_url => notify_url,
      :invoice => id,
      :currency_code => 'AUD'
    }
    line_items.each_with_index do |item, i|
      values.merge!({
        "amount_#{i+1}" => "%.2f" % item.unit_price_ex_gst,
        "tax_#{i+1}" => "%.2f" % item.unit_gst,
        "item_name_#{i+1}" => item.product.full_name,
        "item_number_#{i+1}" => item.id,
        "quantity_#{i+1}" => item.quantity
      })
    end
    encrypt_for_paypal(values)
  end

  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 { |key, value| "#{key}=#{value}" }.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
end

然后我在 View 中使用以下代码:

- form_tag PAYPAL_CONFIG[:action_url] do
  %div
    = hidden_field_tag :cmd, '_s-xclick'
    = hidden_field_tag :encrypted, cart.paypal_data(thanks_payments_url, payments_url)
    = image_submit_tag 'paypal-checkout.gif', :alt => 'Check out with PayPal: The safer, easier way to pay'

输出中似乎没有任何换行符或其他字符,据我所知,这些字符会阻塞进程。

我已经反复检查我是否使用了所有正确的证书和证书 ID,并且上传到 PayPal 的内容与我的 certs 目录中的内容相匹配。

我已经完全没有想法可以尝试了。任何帮助或想法将不胜感激。

最佳答案

现在一切都在准备和生产中。

我唯一能想到的是 PayPal 和他们这边的证书存在某种问题或延迟。

关于ruby-on-rails - 无法让 PayPal 加密网站付款在 Rails 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3818428/

相关文章:

ruby - 如何使用 RSpec 多次调用主题

ruby-on-rails - Ruby on Rails 上的私有(private) Assets 路径

ruby-on-rails - rails 4 : Model Logic - Counting Items by a User

ruby-on-rails - 为什么 rake db :migrate sometimes add trailing whitespace to structure. sql?

ruby-on-rails - cancan & rolify 与 Multi-Tenancy 架构

mysql - (Ruby) 如何转储数据库?

ruby-on-rails - 如何在 Rails 3 中使用 Comet?

php - PayPal 在交易成功后不返回任何变量

paypal - 我如何在 paypal 中创建开发者帐户 - 它与企业帐户相同吗?

用于 PayPal 自适应支付预批准 key 的 Php curl