1

Doing railscast #143. Code is below. When I add the security stuff, I get "We were unable to decrypt the certificate id." in development. When I take the security stuff out, it works peachy again. I've redone the whole process a couple times with new certificates and such. No luck.

Any ideas of what to try next?

I'm having exactly the same problem as in this posting, which experienced it in production and it magically started working:

Can't get PayPal Encrypted Website Payments to work in Rails

In "buy these" page:

<%= 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>

In cart.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
Community
  • 1
  • 1
icewoman27
  • 33
  • 9
  • That Railscast is almost three years old. Have you compared those instructions against PayPal's current API docs? Have you considered using one of the [many PayPal gems](http://rubygems.org/search?utf8=%E2%9C%93&query=paypal) available? – Jordan Running Oct 23 '11 at 04:09
  • Thanks for the suggestion! Turns out I can get it to work, just not reliably. – icewoman27 Dec 06 '11 at 21:54

3 Answers3

2

I repeated the entire process a few more times, and it started working. Also reviewed each value in a process similar to the next answer. Unfortunately, any time I switch deployment platforms, I seem to run into the same issue. And eventually, it starts working again.

icewoman27
  • 33
  • 9
1

I faced the same issue but the issue was related to paypal_cert.pem file i.e. Paypal's certificate file.

Paypal uses different certificates for staging and live environment. Please check paypal_cert.pem file and you would see first line mentioning the environment it should be used in.

I use following code

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")

with two files paypal_cert_prod.pem and paypal_cert_sandbox.pem one for each environment.

amitamb
  • 1,067
  • 11
  • 22
0

Since we are encrypting a number of values in paypal_encrypted method, this error can arise when some encryption error occurs during the same process.

The best way to make sure that the issue is not because of the encryption error, try removing key-value pair one by one from the above given values hash and make payment request.

nkm
  • 5,844
  • 2
  • 24
  • 38