Module: EffectivePaypalHelper

Defined in:
app/helpers/effective_paypal_helper.rb

Defined Under Namespace

Classes: ConfigReader

Constant Summary collapse

PAYPAL_CERT_PEM =
ConfigReader.cert_or_key(:paypal_cert)
APP_CERT_PEM =
ConfigReader.cert_or_key(:app_cert)
APP_KEY_PEM =
ConfigReader.cert_or_key(:app_key)

Instance Method Summary collapse

Instance Method Details

#paypal_encrypted_payload(order) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/helpers/effective_paypal_helper.rb', line 19

def paypal_encrypted_payload(order)
  raise 'required paypal paypal_cert is missing' unless PAYPAL_CERT_PEM.present?
  raise 'required paypal app_cert is missing' unless APP_CERT_PEM.present?
  raise 'required paypal app_key is missing' unless APP_KEY_PEM.present?

  values = {
    business: EffectiveOrders.paypal[:seller_email],
    custom: EffectiveOrders.paypal[:secret],
    cmd: '_cart',
    upload: 1,
    return: effective_orders.order_purchased_url(order),
    notify_url: effective_orders.paypal_postback_url,
    cert_id: EffectiveOrders.paypal[:cert_id],
    currency_code: EffectiveOrders.paypal[:currency],
    invoice: order.id,
    amount: (order.subtotal / 100.0).round(2),
    tax_cart: (order.tax / 100.0).round(2)
  }

  order.order_items.each_with_index do |item, x|
    values["item_number_#{x+1}"] = x+1
    values["item_name_#{x+1}"] = item.name
    values["quantity_#{x+1}"] = item.quantity
    values["amount_#{x+1}"] = '%.2f' % (item.price / 100.0)
    values["tax_#{x+1}"] = '%.2f' % ((item.tax / 100.0) / item.quantity)  # Tax for 1 of these items
  end

  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