Class: Besepa::Customer

Inherits:
Resource show all
Includes:
ApiCalls::Create, ApiCalls::Destroy, ApiCalls::List, ApiCalls::Search, ApiCalls::Update
Defined in:
lib/besepa/customer.rb

Constant Summary collapse

FIELDS =
[:id, :name, :taxid, :reference,
:contact_name, :contact_email, :contact_phone, :contact_language,
:address_street, :address_city, :address_postalcode, :address_state, 
:address_country, :group_ids, :status, :created_at]

Constants inherited from Resource

Resource::ALLOWED_NILS

Constants included from Utils::Request

Utils::Request::END_POINT_URL_PREFIX

Instance Attribute Summary

Attributes inherited from Resource

#activities

Instance Method Summary collapse

Methods included from ApiCalls::Destroy

#destroy

Methods included from ApiCalls::Update

#save

Methods included from ApiCalls::Create

included

Methods included from ApiCalls::Search

included

Methods included from ApiCalls::List

included

Methods inherited from Resource

#allowed_nils, api_path, #as_json, handle_errors, #initialize, klass_name, #klass_name, query_params, #serializable_hash, #to_hash

Methods included from Utils::Request

#delete, #get, #post, #put

Methods included from Utils::Connection

#connection

Constructor Details

This class inherits a constructor from Besepa::Resource

Instance Method Details

#add_bank_account(iban, bic = nil, bank_name = nil, mandate_options = {}) ⇒ Object

Adds a bank account to this customer. IBAN and BIC are the only mandatory fields. If you already have the mandate signed, you can pass mandate detail’s and account will be activated by default. Otherwise BankAccount will be marked as inactive (not usable for creating debits or subscriptions) until mandate is signed. BankAccount includes mandate’s info, including signature URL.

Parameters:

  • iban
  • bic (defaults to: nil)
  • bank_name (defaults to: nil)
  • mandate_options (defaults to: {})

    options to configure mandate

    • scheme: CORE|COR1|B2B. Default: CORE

    • signature_date: Date in which this mandate was signed if already signed (Format: YYYY-MM-DD)

    • reference: Mandate’s reference. If none, Besepa will create one.

    • used: Says if this mandate has already been used or not.

    • signature_type: Signature to be used: checkbox|sms|biometric

    • phone_number: Phone number where the signature SMS will be sent in case signature_type==sms is used.

    • type: ONETIME | RECURRENT. Default: RECURRENT

    • redirect_after_signature: URL the user should be redirect to after mandate is signed. Default: Besepa’s thank you page

Returns:

  • new created Besepa::BankAccount



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/besepa/customer.rb', line 103

def (iban, bic=nil, bank_name=nil, mandate_options={})
  params = {:iban => iban }
  params[:bank_name] = bank_name if bank_name
  params[:bic] = bic if bic

  params[:mandate] = {      scheme: (mandate_options[:scheme] || "CORE"),
                              used:  (mandate_options[:used] || false),
                      mandate_type: (mandate_options[:type] || "RECURRENT") }

  if mandate_options[:signature_date]
    params[:mandate][:signed_at] = mandate_options[:signature_date]
    params[:mandate][:reference] = mandate_options[:reference] if mandate_options[:reference]
  else
    params[:mandate][:signature_type] = mandate_options[:signature_type] || 'checkbox'
    params[:mandate][:phone_number] = mandate_options[:phone_number] if mandate_options[:phone_number]
  end
  params[:mandate][:redirect_after_signature] = mandate_options[:redirect_after_signature] if mandate_options[:redirect_after_signature]

  BankAccount.create( params, {:customer_id => id} )
end

#add_debit(debtor_bank_account_id, reference, description, amount, collect_at, creditor_account_id = nil, metadata = nil) ⇒ Object

Generates a direct debit that will be charged to this customer.

Parameters:

  • debtor_bank_account_id

    Customer’s BankAccount code this Debit shouyd be charged to.

  • reference

    Debit’s unique reference

  • description

    Debit’s description. Customer will see this in his Bank’s statements

  • amount

    Amount to be charged. Integer, last two digits represent decimals: 10.25 should be 1025

  • collect_at

    Date in which the charge should be made (Format: YYYY-MM-DD).

  • creditor_account_id (defaults to: nil)

    Business’ BankAccount that should receive the money. If none passed, account marked as default in Besepa’s dashboard will be used.

  • metadata (defaults to: nil)

    Optional Hash with metadata related to this debit, if any.

Returns:

  • new created Besepa::Debit



135
136
137
138
139
140
141
# File 'lib/besepa/customer.rb', line 135

def add_debit(, reference, description, amount, collect_at, =nil, =nil)
  params = {:reference => reference, :description => description, :debtor_bank_account_id => ,
            :amount => amount, :collect_at => collect_at}
  params[:creditor_bank_account_id] =  if 
  params[:metadata] =  if 
  Debit.create( params, {:customer_id => id} )
end

#add_subscription(starts_at, product_code, bank_account_code, setup_fee = 0, metadata = nil) ⇒ Object

Creates a new subscription for this customer

Parameters:

  • starts_at

    The date this subscription should start (Format: YYYY-MM-DD)

  • product_code

    The ID of the product the customer is subscribing to

  • bank_account_code

    The ID of the bank account where debits should be sent to

  • setup_fee (defaults to: 0)

    Initial set-up fee. Optional, default: 0

  • metadata (defaults to: nil)

    Optional Hash with metadata related to this subscription, if any.

Returns:

  • new created Besepa::Subscription



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

def add_subscription(starts_at, product_code, , setup_fee=0, =nil)
  params = {:starts_at => starts_at, :product_id => product_code, :debtor_bank_account_id => }
  params[:setup_fee] = setup_fee if setup_fee
  params[:metadata] =  if 
  Subscription.create( params, {:customer_id => id} )
end

#add_to_group(group_id) ⇒ Object

Adds this customer to the given group

Parameters:

  • group_id

    The ID of the group to which this user should be added

Returns:

  • true if user is now a member of the group



52
53
54
55
# File 'lib/besepa/customer.rb', line 52

def add_to_group(group_id)
  response = post "#{self.class.api_path}/#{id}/memberships/#{group_id}"
  response['response'].select{|c| c['id'] == group_id}.any?
end

#bank_accountsObject

Customer’s bank accounts

Returns:

  • collection of Besepa::BankAccount



22
23
24
# File 'lib/besepa/customer.rb', line 22

def bank_accounts
  BankAccount.all( {:customer_id => id} )
end

#debitsObject

Debits sent to this customer

Returns:

  • collection of Besepa::Debits



29
30
31
# File 'lib/besepa/customer.rb', line 29

def debits
  Debit.all( {:customer_id => id} )
end

#groupsObject

List of groups this customers blongs to

Returns:

  • collection of Besepa::Group



43
44
45
# File 'lib/besepa/customer.rb', line 43

def groups
  Group.all( {:customer_id => id} )
end

#remove_from_group(group_id) ⇒ Object

Removed this customer from the given group

Parameters:

  • group_id

    The ID of the group from which this user should be removed

Returns:

  • true if user is no longer a member of the group



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

def remove_from_group(group_id)
  response = delete "#{self.class.api_path}/#{id}/memberships/#{group_id}"
  response['response'].select{|c| c['id'] == group_id}.empty?
end

#subscriptionsObject

Subscriptions from this customer

Returns:

  • collection of Besepa::Subscription



36
37
38
# File 'lib/besepa/customer.rb', line 36

def subscriptions
  Subscription.all( {:customer_id => id} )
end