Class: StmApi::Donation

Inherits:
Object
  • Object
show all
Defined in:
lib/stm_api.rb

Overview

Your code goes here…

Constant Summary collapse

BEARER =
'LAXQszxcmpGMWi24y0NFt00YPWGJnJOo9Ba8ijLcI1fmiKHI1PDF7KG7PGJU7KcX'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Donation

Returns a new instance of Donation.



15
16
17
18
19
# File 'lib/stm_api.rb', line 15

def initialize(params = {})
  @userhash = params[:userhash]
  @currency = params[:currency]
  @team_id = params[:team_id]
end

Instance Attribute Details

#currencyObject

Returns the value of attribute currency.



11
12
13
# File 'lib/stm_api.rb', line 11

def currency
  @currency
end

#team_idObject

Returns the value of attribute team_id.



12
13
14
# File 'lib/stm_api.rb', line 12

def team_id
  @team_id
end

#userhashObject

Returns the value of attribute userhash.



10
11
12
# File 'lib/stm_api.rb', line 10

def userhash
  @userhash
end

Instance Method Details



38
39
40
41
42
43
44
45
46
47
48
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
# File 'lib/stm_api.rb', line 38

def donate(params = {})
  
  token_payload = {
    'userHash' => @userhash,
    'currency' => @currency
  }

  client_token = RestClient.post('https://api.sharethemeal.org/api/payment/braintree/client-tokens', token_payload.to_json,
                                 content_type: :json, accept: :json,
                                 Authorization: "Bearer #{BEARER}")

  client_token_response = JSON.parse(client_token)

  auth_reply = JSON.parse(Base64.decode64(client_token_response['clientToken']))
  finger_print =  URI.encode_www_form_component(auth_reply['authorizationFingerprint'])

  payment_infos = RestClient.get("https://api.braintreegateway.com/merchants/#{auth_reply['merchantId']}/client_api/v1/payment_methods?sharedCustomerIdentifierType=undefined&braintreeLibraryVersion=braintree%2Fweb%2F2.15.5&merchantAccountId=#{auth_reply['merchantAccountId']}&authorizationFingerprint=#{finger_print}&callback=")
  payment_infos_json = JSON.parse(payment_infos)

  transaction_payload = {
    'userHash' => @userhash,
    'amount' => params[:amount],
    'currency' => @currency,
    'paymentMethodNonce' => payment_infos_json['paymentMethods'].first['nonce'],
    'teamId' => @team_id
  }

  transaction_response = RestClient.post('https://api.sharethemeal.org/api/payment/braintree/transactions', transaction_payload.to_json, content_type: :json, accept: :json,
                                                                                                                                         'Authorization' => "Bearer #{bearer}")

  transaction_response_json = JSON.parse(transaction_response)
  if transaction_response_json['result']['donationCreated'] == true
    return true
  else
    return false
  end

#rescue
#  return false
end

#find_one_team(id) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/stm_api.rb', line 29

def find_one_team(id)
  teams = user_teams
  teams.each do | t |
    if t["teamId"] == id
      return t
    end
  end
  return false
end

#user_teamsObject



21
22
23
24
25
26
27
28
# File 'lib/stm_api.rb', line 21

def user_teams()
  team_statistic = RestClient.get("https://api.sharethemeal.org/api/users/#{@userhash}/teams",
                                 content_type: :json, accept: :json,
                                 Authorization: "Bearer #{BEARER}")
  team_statistic_json = JSON.parse(team_statistic)
  
  team_statistic_json["userTeams"]
end