Class: YandexMoney::Api

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/yandex_money/api.rb,
lib/yandex_money/api/version.rb

Constant Summary collapse

VERSION =
"0.10.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Api

Returns url to get token



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/yandex_money/api.rb', line 17

def initialize(options)
  # TOKEN provided
  if options.length == 1 && options[:token] != nil
    @token = options[:token]
  else
    @client_id = options[:client_id]
    @redirect_uri = options[:redirect_uri]
    @instance_id = options[:instance_id]
    if options[:scope] != nil
      @client_url = send_authorize_request(
        client_id: @client_id,
        response_type: "code",
        redirect_uri: @redirect_uri,
        scope: options[:scope]
      )
    end
  end
end

Instance Attribute Details

#client_urlObject

Returns the value of attribute client_url.



14
15
16
# File 'lib/yandex_money/api.rb', line 14

def client_url
  @client_url
end

#codeObject

Returns the value of attribute code.



14
15
16
# File 'lib/yandex_money/api.rb', line 14

def code
  @code
end

#instance_idObject

Returns the value of attribute instance_id.



14
15
16
# File 'lib/yandex_money/api.rb', line 14

def instance_id
  @instance_id
end

#tokenObject

Returns the value of attribute token.



14
15
16
# File 'lib/yandex_money/api.rb', line 14

def token
  @token
end

Instance Method Details

#account_infoObject

obtains account info



52
53
54
55
# File 'lib/yandex_money/api.rb', line 52

def 
  check_token
  OpenStruct.new send_request("/api/account-info").parsed_response
end

#get_instance_idObject



102
103
104
105
106
107
108
109
# File 'lib/yandex_money/api.rb', line 102

def get_instance_id
  request = send_request("/api/instance-id", client_id: @client_id)
  if request["status"] == "refused"
    raise YandexMoney::ApiError.new request["error"]
  else
    request["instance_id"]
  end
end

#incoming_transfer_accept(operation_id, protection_code = nil) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/yandex_money/api.rb', line 111

def incoming_transfer_accept(operation_id, protection_code = nil)
  uri = "/api/incoming-transfer-accept"
  if protection_code
    request_body = {
      operation_id: operation_id,
      protection_code: protection_code
    }
  else
    request_body = { operation_id: operation_id }
  end
  request = send_request("/api/incoming-transfer-accept", request_body)

  if request["status"] == "refused"
    raise YandexMoney::AcceptTransferError.new request["error"], request["protection_code_attempts_available"]
  else
    true
  end
end

#incoming_transfer_reject(operation_id) ⇒ Object



130
131
132
133
134
135
136
137
138
# File 'lib/yandex_money/api.rb', line 130

def incoming_transfer_reject(operation_id)
  request = send_request("/api/incoming-transfer-reject", operation_id: operation_id)

  if request["status"] == "refused"
    raise YandexMoney::ApiError.new request["error"]
  else
    true
  end
end

#obtain_token(client_secret = nil) ⇒ Object

obtains and saves token from code



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/yandex_money/api.rb', line 37

def obtain_token(client_secret = nil)
  raise YandexMoney::FieldNotSetError.new(:code) if @code == nil
  uri = "/oauth/token"
  options = {
    code: @code,
    client_id: @client_id,
    grant_type: "authorization_code",
    redirect_uri: @redirect_url
  }
  options[:client_secret] = client_secret if client_secret
  @token = self.class.post(uri, body: options)
                     .parsed_response["access_token"]
end

#operation_details(operation_id) ⇒ Object

obtains operation details



64
65
66
67
68
69
70
71
72
73
# File 'lib/yandex_money/api.rb', line 64

def operation_details(operation_id)
  check_token
  request = send_request("/api/operation-details", operation_id: operation_id)
  details = OpenStruct.new request.parsed_response
  if details.error
    raise YandexMoney::ApiError.new details.error
  else
    details
  end
end

#operation_history(options = nil) ⇒ Object

obtains operation history



58
59
60
61
# File 'lib/yandex_money/api.rb', line 58

def operation_history(options=nil)
  check_token
  OpenStruct.new send_request("/api/operation-history", options).parsed_response
end

#process_external_payment(payment_options) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/yandex_money/api.rb', line 150

def process_external_payment(payment_options)
  payment_options[:instance_id] ||= @instance_id
  request = send_request("/api/process-external-payment", payment_options)

  if request["status"] == "refused"
    raise YandexMoney::ApiError.new request["error"]
  elsif request["status"] == "in_progress"
    raise YandexMoney::ExternalPaymentProgressError.new request["error"], request["next_retry"]
  else
    OpenStruct.new request.parsed_response
  end
end

#process_payment(options) ⇒ Object

basic process payment method



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/yandex_money/api.rb', line 90

def process_payment(options)
  check_token
  request = send_request("/api/process-payment", options)
  response = OpenStruct.new request.parsed_response

  if response.error
    raise YandexMoney::ApiError.new response.error
  else
    response
  end
end

#request_external_payment(payment_options) ⇒ Object



140
141
142
143
144
145
146
147
148
# File 'lib/yandex_money/api.rb', line 140

def request_external_payment(payment_options)
  payment_options[:instance_id] ||= @instance_id
  request = send_request("/api/request-external-payment", payment_options)
  if request["status"] == "refused"
    raise YandexMoney::ApiError.new request["error"]
  else
    OpenStruct.new request.parsed_response
  end
end

#request_payment(options) ⇒ Object

basic request payment method



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/yandex_money/api.rb', line 76

def request_payment(options)
  check_token
  response = with_http_retries do
    request = send_request("/api/request-payment", options)
    OpenStruct.new request.parsed_response
  end
  if response.error
    raise YandexMoney::ApiError.new response.error
  else
    response
  end
end