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

joh*_*mes 5 ruby paypal ruby-on-rails

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

发布到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
Run Code Online (Sandbox Code Playgroud)

然后我上传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
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

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

- 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'
Run Code Online (Sandbox Code Playgroud)

我知道输出中似乎没有换行符或其他字符会塞满该过程。

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

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

joh*_*mes 0

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

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