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

.retrieve(access_token, refresh_token) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/synapse_client/customer.rb', line 43

def self.retrieve(access_token, refresh_token)
  response = SynapseClient.request(:post, url + "show", {:access_token => access_token})

  return response unless response.successful?
  Customer.new(response.data.user.merge({
    :access_token  => access_token,
    :refresh_token => refresh_token
  }))
end

Instance Method Details

#add_bank_account(params = {}) ⇒ Object



66
67
68
69
70
71
# File 'lib/synapse_client/customer.rb', line 66

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

#add_order(params = {}) ⇒ Object



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

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



59
60
61
# File 'lib/synapse_client/customer.rb', line 59

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



79
80
81
82
83
# File 'lib/synapse_client/customer.rb', line 79

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


73
74
75
76
77
# File 'lib/synapse_client/customer.rb', line 73

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

#loginObject

TODO



54
55
56
# File 'lib/synapse_client/customer.rb', line 54

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

#ordersObject



86
87
88
# File 'lib/synapse_client/customer.rb', line 86

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

#primary_bank_accountObject



62
63
64
# File 'lib/synapse_client/customer.rb', line 62

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

#refresh_tokensObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/synapse_client/customer.rb', line 102

def refresh_tokens
  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)

  @access_token  = rt.new_access_token
  @refresh_token = rt.new_refresh_token
  @expires_in    = rt.new_expires_in

  return self
end