Class: Sofort::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/sofort/client.rb

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/sofort/client.rb', line 11

def initialize
  @options = {}
  @options[:basic_auth] = {
    username: Sofort.user_id,
    password: Sofort.api_key
  }
  @options[:headers] = {
    'Accept' => 'application/xml',
    'Content-Type' => 'application/xml'
  }
end

Instance Method Details

#details(token) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/sofort/client.rb', line 30

def details(token)
  xml = details_xml(token)
  results = self.class.post(Sofort.base_url, @options.merge(body: xml))
  transactions = results['transactions']
  if transactions && transactions['transaction_details']
    transactions['transaction_details']
  else
    results
  end
end

#details_xml(token) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/sofort/client.rb', line 41

def details_xml(token)
  <<-eos
  <transaction_request version="2">
  <transaction>#{token}</transaction>
  </transaction_request>
  eos
end

#pay(amount, name, *args) ⇒ Object



23
24
25
26
27
28
# File 'lib/sofort/client.rb', line 23

def pay(amount, name, *args)
  opts = args.extract_options!.symbolize_keys!
  xml = pay_xml(amount, name, opts)
  results = self.class.post(Sofort.base_url,  @options.merge(body: xml))
  results['new_transaction'].present? ? results['new_transaction'] : results
end

#pay_xml(amount, name, opts) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/sofort/client.rb', line 49

def pay_xml(amount, name, opts)
  reason = opts[:reason] || Sofort.reason
  currency_code = opts[:currency_code] || Sofort.currency_code
  country_code = opts[:country_code] ||  Sofort.country_code
  success_url = opts[:success_url] ||  Sofort.success_url
  abort_url = opts[:abort_url] ||  Sofort.abort_url
  email_customer = opts[:email_customer] ||  Sofort.email_customer
  language_code = opts[:language_code] || Sofort.language_code
  notification_email = opts[:notification_email] ||  Sofort.notification_email
  notification_url = opts[:notification_url] ||  Sofort.notification_url
  user_variable = opts[:user_variable] ||  Sofort.user_variable
  project_id = opts[:project_id] ||  Sofort.project_id

  {
    amount: amount,
    currency_code: currency_code,
    language_code: language_code,
    reasons: {
      reason: reason
    },
    sender: {
      holder: name,
      country_code: country_code
    },
    email_customer: email_customer,
    user_variables: {
      user_variable: user_variable
    },
    notification_emails: {
      notification_email: notification_email
    },
    notification_urls: {
      notification_url: notification_url
    },
    success_url: success_url,
    abort_url: abort_url,
    su: '',
    project_id: project_id
  }.to_xml(root: 'multipay', skip_types: true, dasherize: false)

end