class ActiveMerchant::Billing::SumUpGateway

Constants

STANDARD_ERROR_CODE_MAPPING

Public Class Methods

new(options = {}) click to toggle source
Calls superclass method ActiveMerchant::Billing::Gateway::new
# File lib/active_merchant/billing/gateways/sum_up.rb, line 20
def initialize(options = {})
  requires!(options, :access_token, :pay_to_email)
  super
end

Public Instance Methods

purchase(money, payment, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/sum_up.rb, line 25
def purchase(money, payment, options = {})
  MultiResponse.run do |r|
    r.process { create_checkout(money, payment, options) } unless options[:checkout_id]
    r.process { complete_checkout(options[:checkout_id] || r.params['id'], payment, options) }
  end
end
refund(money, authorization, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/sum_up.rb, line 32
def refund(money, authorization, options = {})
  transaction_id = authorization.split('#').last
  post = money ? { amount: amount(money) } : {}
  add_merchant_data(post, options)

  commit('me/refund/' + transaction_id, post)
end
scrub(transcript) click to toggle source
# File lib/active_merchant/billing/gateways/sum_up.rb, line 44
def scrub(transcript)
  transcript.
    gsub(%r((Authorization: Bearer )\w+), '\1[FILTERED]').
    gsub(%r(("pay_to_email\\?"\s*:\s*\\?")[^"]*)i, '\1[FILTERED]').
    gsub(%r(("number\\?"\s*:\s*\\?")[^"]*)i, '\1[FILTERED]').
    gsub(%r(("cvv\\?"\s*:\s*\\?")[^"]*)i, '\1[FILTERED]')
end
supports_scrubbing?() click to toggle source
# File lib/active_merchant/billing/gateways/sum_up.rb, line 40
def supports_scrubbing?
  true
end

Private Instance Methods

add_3ds_data(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/sum_up.rb, line 122
def add_3ds_data(post, options)
  post[:redirect_url] = options[:redirect_url] if options[:redirect_url]
end
add_address(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/sum_up.rb, line 90
def add_address(post, options)
  post[:personal_details] ||= {}
  if address = (options[:billing_address] || options[:shipping_address] || options[:address])
    post[:personal_details][:address] = {
      city:        address[:city],
      state:       address[:state],
      country:     address[:country],
      line_1:      address[:address1],
      postal_code: address[:zip]
    }
  end
end
add_customer_data(post, payment, options) click to toggle source
# File lib/active_merchant/billing/gateways/sum_up.rb, line 74
def add_customer_data(post, payment, options)
  post[:customer_id]      = options[:customer_id]
  post[:personal_details] = {
    email:      options[:email],
    first_name: payment&.first_name,
    last_name:  payment&.last_name,
    tax_id:     options[:tax_id]
  }
end
add_invoice(post, money, options) click to toggle source
# File lib/active_merchant/billing/gateways/sum_up.rb, line 103
def add_invoice(post, money, options)
  post[:checkout_reference] = options[:order_id]
  post[:amount]             = amount(money)
  post[:currency]           = options[:currency] || currency(money)
  post[:description]        = options[:description]
end
add_merchant_data(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/sum_up.rb, line 84
def add_merchant_data(post, options)
  # Required field: pay_to_email
  # Description: Email address of the merchant to whom the payment is made.
  post[:pay_to_email] = @options[:pay_to_email]
end
add_payment(post, payment, options) click to toggle source
# File lib/active_merchant/billing/gateways/sum_up.rb, line 110
def add_payment(post, payment, options)
  post[:payment_type] = options[:payment_type] || 'card'

  post[:card] = {
    name:         payment.name,
    number:       payment.number,
    expiry_month: format(payment.month, :two_digits),
    expiry_year:  payment.year,
    cvv:          payment.verification_value
  }
end
api_request(action, post, method) click to toggle source
# File lib/active_merchant/billing/gateways/sum_up.rb, line 140
def api_request(action, post, method)
  raw_response =
    begin
      ssl_request(method, live_url + action, post.to_json, auth_headers)
    rescue ResponseError => e
      e.response.body
    end
  response = parse(raw_response)
  response = response.is_a?(Hash) ? response.symbolize_keys : response

  return format_errors(response) if raw_response.include?('error_code') && response.is_a?(Array)

  response
end
auth_headers() click to toggle source
# File lib/active_merchant/billing/gateways/sum_up.rb, line 184
def auth_headers
  {
    'Content-Type' => 'application/json',
    'Authorization' => "Bearer #{options[:access_token]}"
  }
end
authorization_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/sum_up.rb, line 176
def authorization_from(response)
  return nil if response.is_a?(Integer)

  return response[:id] unless response[:transaction_id]

  [response[:id], response[:transaction_id]].join('#')
end
commit(action, post, method = :post) click to toggle source
# File lib/active_merchant/billing/gateways/sum_up.rb, line 126
def commit(action, post, method = :post)
  response = api_request(action, post.compact, method)
  succeeded = success_from(response)

  Response.new(
    succeeded,
    message_from(succeeded, response),
    action.include?('refund') ? { response_code: response.to_s } : response,
    authorization: authorization_from(response),
    test: test?,
    error_code: error_code_from(succeeded, response)
  )
end
complete_checkout(checkout_id, payment, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/sum_up.rb, line 66
def complete_checkout(checkout_id, payment, options = {})
  post = {}

  add_payment(post, payment, options)

  commit('checkouts/' + checkout_id, post, :put)
end
create_checkout(money, payment, options) click to toggle source
# File lib/active_merchant/billing/gateways/sum_up.rb, line 54
def create_checkout(money, payment, options)
  post = {}

  add_merchant_data(post, options)
  add_invoice(post, money, options)
  add_address(post, options)
  add_customer_data(post, payment, options)
  add_3ds_data(post, options)

  commit('checkouts', post)
end
error_code_from(succeeded, response) click to toggle source
# File lib/active_merchant/billing/gateways/sum_up.rb, line 191
def error_code_from(succeeded, response)
  response[:error_code] unless succeeded
end
format_error(error, key) click to toggle source
# File lib/active_merchant/billing/gateways/sum_up.rb, line 195
def format_error(error, key)
  {
    :error_code => error['error_code'],
    key => error['param']
  }
end
format_errors(errors) click to toggle source
# File lib/active_merchant/billing/gateways/sum_up.rb, line 202
def format_errors(errors)
  return format_error(errors.first, :message) if errors.size == 1

  return {
    error_code: STANDARD_ERROR_CODE_MAPPING[:multiple_invalid_parameters],
    message: 'Validation error',
    errors: errors.map { |error| format_error(error, :param) }
  }
end
handle_response(response) click to toggle source
# File lib/active_merchant/billing/gateways/sum_up.rb, line 212
def handle_response(response)
  case response.code.to_i
  # to get the response code (204) when the body is nil
  when 200...300
    response.body || response.code
  else
    raise ResponseError.new(response)
  end
end
message_from(succeeded, response) click to toggle source
# File lib/active_merchant/billing/gateways/sum_up.rb, line 166
def message_from(succeeded, response)
  if succeeded
    return 'Succeeded' if (response.is_a?(Hash) && response[:next_step]) || response == 204

    return response[:status]
  end

  response[:message] || response[:error_message] || response[:status]
end
parse(body) click to toggle source
# File lib/active_merchant/billing/gateways/sum_up.rb, line 155
def parse(body)
  JSON.parse(body)
end
success_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/sum_up.rb, line 159
def success_from(response)
  (response.is_a?(Hash) && response[:next_step]) ||
    response == 204 ||
    %w(PENDING PAID).include?(response[:status]) ||
    response[:transactions]&.all? { |transaction| transaction.symbolize_keys[:status] == 'SUCCESSFUL' }
end