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

发布于 2024-09-25 04:45:07 字数 4326 浏览 0 评论 0原文

我在 Rails 网站上使用 PayPal 加密网站付款时遇到问题。

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

证书已被删除。请使用有效的证书。

而在生产现场,我得到:

我们无法解密证书 ID。

据我所知,它们的设置相同,只是一个使用 PayPal Sandbox 公钥,另一个使用普通的 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: [email protected]
  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: [email protected]
  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: [email protected]
  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: [email protected]
  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

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

- 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 目录中的内容相符。

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

I am having problems getting PayPal Encrypted Website payments to work on a Rails site.

I am getting two different error messages when posting to the PayPal URL - on my staging site, which uses the sandbox, I am getting:

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

Whereas on the production site, I get:

We were unable to decrypt the certificate id.

As far as I can tell they are set up identically, except that one uses the PayPal Sandbox public key, and the other uses the normal PayPal public key. I must be overlooking some detail but I have banging my head against this for a couple of days now.

I used the following commands to generate public and private keys:

openssl genrsa -out app_key.pem 1024

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

Then I uploaded app_cert.pem to PayPal and put my cert ID into a file like this:

development:
  user: [email protected]
  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: [email protected]
  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: [email protected]
  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: [email protected]
  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

Then I use the following code in my Cart model to encrypt the data:

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

Then I use the following code in the 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'

There don't seem to be any newlines or other characters in the output which I am aware can stuff up the process.

I have checked and double-checked that I am using all the right certificates and certificate ids, and that what has been uploaded to PayPal matches what is in my certs directory.

I have completely run out of ideas to try. Any help or ideas would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

洒一地阳光 2024-10-02 04:45:07

现在一切都在筹备和制作中。

我唯一能想到的是 PayPal 及其证书存在某种问题或延迟。

Everything is working in staging and production now.

The only thing I can think of is that there was some sort of problem or delay with PayPal and the certificates on their side.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文