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.



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

def initialize(params = {})
  @userhash = params[:userhash]
  @currency = params[:currency]
  @team_id = params[:team_id]
  @campaign_id = campaigns.first unless params[:campaign_id]
end

Instance Attribute Details

#campaign_idObject

Returns the value of attribute campaign_id.



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

def campaign_id
  @campaign_id
end

#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

#campaignsObject



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/stm_api.rb', line 48

def campaigns
  campaigns_raw = RestClient.get("https://api.sharethemeal.org/api/meta",
                                 content_type: :json, accept: :json,
                                 Authorization: "Bearer #{BEARER}")
  campaigns_json = JSON.parse(campaigns_raw)
  found_campaigns = []
  campaigns_json["campaigns"].each do |camp, v|
    found_campaigns << camp
  end
  found_campaigns
end


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/stm_api.rb', line 70

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,
    'campaignId' => @campaign_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



60
61
62
63
64
65
66
67
68
# File 'lib/stm_api.rb', line 60

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

#statisticsObject



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

def statistics
   = RestClient.get("https://api.sharethemeal.org/api/campaigns/zomba/status",
                                      content_type: :json, accept: :json,
                                      Authorization: "Bearer #{BEARER}")
   = JSON.parse()
  return 
end

#user_infoObject



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

def 
   = RestClient.get("https://api.sharethemeal.org/api/users/#{@userhash}",
                                      content_type: :json, accept: :json,
                                      Authorization: "Bearer #{BEARER}")
   = JSON.parse()
  return 
end

#user_teamsObject



39
40
41
42
43
44
45
46
# File 'lib/stm_api.rb', line 39

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