Class: SynapseClient::Customer

Inherits:
APIResource show all
Defined in:
lib/synapse_client/customer.rb

Instance Attribute Summary collapse

Attributes inherited from APIResource

#id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from APIResource

class_name, #refresh, #successful?, #to_hash, #url, url

Constructor Details

#initialize(options = {}) ⇒ Customer

Returns a new instance of Customer.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/synapse_client/customer.rb', line 10

def initialize(options = {})
  options = Map.new(options)

  @id            = options[:id]            || options[:user_id]
  @email         = options[:email]
  @fullname      = options[:fullname]      || options[:name]
  @phonenumber   = options[:phonenumber]   || options[:phone_number]
  @ip_address    = options[:ip_address]

  @access_token  = options[:access_token]
  @refresh_token = options[:refresh_token]
  @expires_in    = options[:expires_in]
  @username      = options[:username]

  @force_create  = options[:force_create]
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



6
7
8
# File 'lib/synapse_client/customer.rb', line 6

def access_token
  @access_token
end

#emailObject

Returns the value of attribute email.



5
6
7
# File 'lib/synapse_client/customer.rb', line 5

def email
  @email
end

#expires_inObject

Returns the value of attribute expires_in.



6
7
8
# File 'lib/synapse_client/customer.rb', line 6

def expires_in
  @expires_in
end

#force_createObject

Returns the value of attribute force_create.



8
9
10
# File 'lib/synapse_client/customer.rb', line 8

def force_create
  @force_create
end

#fullnameObject

Returns the value of attribute fullname.



5
6
7
# File 'lib/synapse_client/customer.rb', line 5

def fullname
  @fullname
end

#ip_addressObject

Returns the value of attribute ip_address.



5
6
7
# File 'lib/synapse_client/customer.rb', line 5

def ip_address
  @ip_address
end

#phonenumberObject

Returns the value of attribute phonenumber.



5
6
7
# File 'lib/synapse_client/customer.rb', line 5

def phonenumber
  @phonenumber
end

#refresh_tokenObject

Returns the value of attribute refresh_token.



6
7
8
# File 'lib/synapse_client/customer.rb', line 6

def refresh_token
  @refresh_token
end

#usernameObject

Returns the value of attribute username.



7
8
9
# File 'lib/synapse_client/customer.rb', line 7

def username
  @username
end

Class Method Details

.api_resource_nameObject



27
28
29
# File 'lib/synapse_client/customer.rb', line 27

def self.api_resource_name
  "user" # see README.md
end

.create(opts = {}) ⇒ Object



31
32
33
# File 'lib/synapse_client/customer.rb', line 31

def self.create(opts={})
  Customer.new(opts).create
end

.get_user(opts = {}) ⇒ Object



147
148
149
150
151
152
153
154
155
# File 'lib/synapse_client/customer.rb', line 147

def self.get_user(opts={})
  response = SynapseClient.request(:post, url + "show", {:access_token => opts[:access_token]})

  return response unless response.successful?

  opts.delete(:expires_in) if opts[:expires_in].nil?

  Customer.new(response.data.user.merge(opts))
end

.refresh_tokens(access_token, refresh_token) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/synapse_client/customer.rb', line 132

def self.refresh_tokens(access_token, refresh_token)
  rt = SynapseClient::RefreshedTokens.new({
    :old_access_token  => access_token,
    :old_refresh_token => refresh_token
  }).refresh_old_tokens

  return rt if rt.instance_of?(SynapseClient::Error)

  Map.new({
    :access_token  => rt.new_access_token,
    :refresh_token => rt.new_refresh_token,
    :expires_in    => rt.new_expires_in
  })
end

.retrieve(access_token, refresh_token) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/synapse_client/customer.rb', line 43

def self.retrieve(access_token, refresh_token)
  response = Customer.get_user({
          :access_token  => access_token,
          :refresh_token => refresh_token
        })

  unless response.successful? # refresh tokens & try once more.
    new_tokens = Customer.refresh_tokens(access_token, refresh_token)
    return new_tokens if new_tokens.instance_of?(SynapseClient::Error)
    return Customer.get_user({
        :access_token  => new_tokens.access_token,
        :refresh_token => new_tokens.refresh_token,
        :expires_in    => new_tokens.expires_in
      })
  end

  response
end

Instance Method Details

#add_bank_account(params = {}) ⇒ Object



96
97
98
99
100
101
# File 'lib/synapse_client/customer.rb', line 96

def (params={})
  BankAccount.add(params.merge({
    :access_token => @access_token,
    :fullname     => @fullname
  }))
end

#add_kyc_info(opts = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/synapse_client/customer.rb', line 63

def add_kyc_info(opts={})
  response = SynapseClient.request(:post, "/api/v2/user/ssn/add", opts.merge({
    :access_token => @access_token
  }))

  unless response.data["question_set"].nil?
    return SynapseClient::QuestionSet.new(response.data.question_set)
  else
   return response
  end
end

#add_order(params = {}) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/synapse_client/customer.rb', line 120

def add_order(params={})
  if SynapseClient.merchant_synapse_id.nil?
    raise ArgumentError.new("You need to set SynapseClient.merchant_synapse_id before you can submit orders.")
  else
    Order.create(params.merge({
      :access_token => @access_token,
      :seller_id    => params[:seller_id] || SynapseClient.merchant_synapse_id,
      :bank_pay     => "yes"        # see README.md
    }))
  end
end

#bank_accountsObject



89
90
91
# File 'lib/synapse_client/customer.rb', line 89

def bank_accounts
  BankAccount.all({:access_token => @access_token})
end

#createObject



35
36
37
38
39
40
41
# File 'lib/synapse_client/customer.rb', line 35

def create
  headers  = {"REMOTE_ADDR" => @ip_address}
  response = SynapseClient.request(:post, url + "create", to_hash, headers)

  return response unless response.successful?
  update_attributes(response.data)
end

#finish_linking_bank_account(params = {}) ⇒ Object



109
110
111
112
113
# File 'lib/synapse_client/customer.rb', line 109

def (params={})
  BankAccount.finish_linking(params.merge({
    :access_token => @access_token
  }))
end


103
104
105
106
107
# File 'lib/synapse_client/customer.rb', line 103

def (params={})
  BankAccount.link(params.merge({
    :access_token => @access_token
  }))
end

#loginObject

TODO



84
85
86
# File 'lib/synapse_client/customer.rb', line 84

def 
  # use http://synapsepay.readme.io/v1.0/docs/authentication-login
end

#ordersObject



116
117
118
# File 'lib/synapse_client/customer.rb', line 116

def orders
  Order.all({:access_token => @access_token})
end

#primary_bank_accountObject



92
93
94
# File 'lib/synapse_client/customer.rb', line 92

def 
  @bank_accounts.select{|ba| ba.is_buyer_default}.first
end

#verify_kyc_info(opts = {}) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/synapse_client/customer.rb', line 75

def verify_kyc_info(opts={})
  response = SynapseClient.request(:post, "/api/v2/user/ssn/answer", opts.merge({
    :access_token => @access_token
  }))

  return response
end