module Spree::Adyen::HPP

Constants

HPP
UrlHelper

Public Class Methods

details_url(order, payment_method, brand_code) click to toggle source
# File lib/spree/adyen/hpp.rb, line 26
def details_url order, payment_method, brand_code
  endpoint_url(
    "details", order, payment_method, { brandCode: brand_code })
end
details_url_with_issuer(order, payment_method, brand_code, issuer_id) click to toggle source
# File lib/spree/adyen/hpp.rb, line 31
def details_url_with_issuer order, payment_method, brand_code, issuer_id
  endpoint_url(
    "details",
    order,
    payment_method,
    {
      brandCode: brand_code,
      issuerId: issuer_id
    }
  )
end
directory_url(order, payment_method) click to toggle source
# File lib/spree/adyen/hpp.rb, line 22
def directory_url order, payment_method
  endpoint_url "directory", order, payment_method
end
endpoint_url(endpoint, order, payment_method, opts = {}) click to toggle source
# File lib/spree/adyen/hpp.rb, line 43
def endpoint_url endpoint, order, payment_method, opts = {}
  adyen_request = hpp_request order, payment_method, opts

  URI::parse("#{adyen_request.url(endpoint)}?#{adyen_request.flat_payment_parameters.to_query}")
end
pay_url(order, payment_method) click to toggle source
# File lib/spree/adyen/hpp.rb, line 14
def pay_url order, payment_method
  endpoint_url "pay", order, payment_method
end
payment_methods_from_directory(order, payment_method) click to toggle source
# File lib/spree/adyen/hpp.rb, line 10
def payment_methods_from_directory order, payment_method
  payment_methods(order, payment_method)
end
select_url(order, payment_method) click to toggle source
# File lib/spree/adyen/hpp.rb, line 18
def select_url order, payment_method
  endpoint_url "select", order, payment_method
end

Private Class Methods

default_params() click to toggle source

TODO set this in the adyen config

# File lib/spree/adyen/hpp.rb, line 124
def default_params
  { session_validity: 10.minutes.from_now,
    recurring: false }
end
form_issuer(issuer, order, payment_method, brand) click to toggle source
# File lib/spree/adyen/hpp.rb, line 81
def form_issuer issuer, order, payment_method, brand
  {
    name: issuer["name"],
    payment_url: details_url_with_issuer(
      order,
      payment_method,
      brand["brandCode"],
      issuer["issuerId"]
    ).to_s
  }
end
form_payment_method(brand, order, payment_method, issuers) click to toggle source
# File lib/spree/adyen/hpp.rb, line 93
def form_payment_method brand, order, payment_method, issuers
  {
    brand_code: brand["brandCode"],
    name: brand["name"],
    payment_url: details_url(
      order,
      payment_method,
      brand["brandCode"]
    ).to_s,
    issuers: issuers
  }
end
form_payment_methods_and_urls(response, order, payment_method) click to toggle source
# File lib/spree/adyen/hpp.rb, line 69
def form_payment_methods_and_urls response, order, payment_method
  response.fetch("paymentMethods").map do |brand|
    next unless payment_method_allows_brand_code?(payment_method, brand['brandCode'])

    issuers = brand.fetch("issuers", []).map do |issuer|
      form_issuer(issuer, order, payment_method, brand)
    end

    form_payment_method(brand, order, payment_method, issuers)
  end.compact
end
hpp_request(order, payment_method, opts) click to toggle source
# File lib/spree/adyen/hpp.rb, line 50
def hpp_request order, payment_method, opts
  server = payment_method.preferences.fetch(:server)
  parameters = params(order, payment_method).merge(opts)

  HPP::Request.new(parameters, environment: server,
                               skin: { skin_code: payment_method.skin_code },
                               shared_secret: payment_method.shared_secret)
end
merchant_return_data(order, payment_method) click to toggle source
# File lib/spree/adyen/hpp.rb, line 113
def merchant_return_data order, payment_method
  { merchantReturnData: [order.guest_token, payment_method.id].join("|") }
end
order_params(order) click to toggle source
# File lib/spree/adyen/hpp.rb, line 129
def order_params order
  { currency_code: order.currency,
    merchant_reference: order.number.to_s,
    country_code: order.billing_address.country.iso,
    payment_amount: (order.total * 100).to_int,
    shopper_locale: I18n.locale.to_s.gsub("-", "_"),
    shopper_email: order.email
  }
end
params(order, payment_method) click to toggle source
# File lib/spree/adyen/hpp.rb, line 106
def params order, payment_method
  default_params.
    merge(order_params order).
    merge(payment_method_params payment_method).
    merge(merchant_return_data order, payment_method)
end
payment_method_allows_brand_code?(payment_method, brand_code) click to toggle source
# File lib/spree/adyen/hpp.rb, line 117
def payment_method_allows_brand_code? payment_method, brand_code
  return true if payment_method.restricted_brand_codes.empty?

  payment_method.restricted_brand_codes.include?(brand_code)
end
payment_method_params(payment_method) click to toggle source
# File lib/spree/adyen/hpp.rb, line 139
def payment_method_params payment_method
  { merchant_account: payment_method.merchant_account,
    skin_code: payment_method.skin_code,
    shared_secret: payment_method.shared_secret,
    ship_before_date: payment_method.ship_before_date
  }
end
payment_methods(order, payment_method) click to toggle source
# File lib/spree/adyen/hpp.rb, line 59
def payment_methods order, payment_method
  url = directory_url(order, payment_method)

  form_payment_methods_and_urls(
    JSON.parse(::Net::HTTP.get(url)),
    order,
    payment_method
  )
end