Class: PaystackSdk::Resources::Customers
- Defined in:
- lib/paystack_sdk/resources/customers.rb
Overview
The ‘Customers` class provides methods for interacting with the Paystack Customers API. It allows you to create, list, fetch, update, and manage customers on your integration.
Example usage: “‘ruby
customers = PaystackSdk::Resources::Customers.new(secret_key:)
# Create a customer
payload = { email: "[email protected]", first_name: "Zero", last_name: "Sum" }
response = customers.create(payload)
if response.success?
puts "Customer created successfully."
puts "Customer code: #{response.customer_code}"
else
puts "Error creating customer: #{response.error_message}"
end
# List customers
response = customers.list(per_page: 50, page: 1)
# Fetch a customer
response = customers.fetch("CUS_xxxxx")
# Update a customer
response = customers.update("CUS_xxxxx", { first_name: "John" })
“‘
Constant Summary
Constants included from Utils::ConnectionUtils
Utils::ConnectionUtils::BASE_URL
Instance Method Summary collapse
-
#create(payload) ⇒ PaystackSdk::Response
Creates a new customer.
-
#deactivate_authorization(payload) ⇒ PaystackSdk::Response
Deactivates a customer’s authorization.
-
#fetch(email_or_code) ⇒ PaystackSdk::Response
Fetches details of a single customer by email or code.
-
#list(per_page: 50, page: 1, **params) ⇒ PaystackSdk::Response
Lists all customers.
-
#set_risk_action(payload) ⇒ PaystackSdk::Response
Sets the risk action for a customer.
-
#update(code, payload) ⇒ PaystackSdk::Response
Updates a customer’s details.
-
#validate(code, payload) ⇒ PaystackSdk::Response
Validates a customer’s identity.
Methods inherited from Base
Methods included from Utils::ConnectionUtils
#create_connection, #initialize_connection
Methods included from Validations
#validate_allowed_values!, #validate_currency!, #validate_date_format!, #validate_email!, #validate_fields!, #validate_hash!, #validate_positive_integer!, #validate_presence!, #validate_reference_format!, #validate_required_params!
Constructor Details
This class inherits a constructor from PaystackSdk::Resources::Base
Instance Method Details
#create(payload) ⇒ PaystackSdk::Response
Creates a new customer.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/paystack_sdk/resources/customers.rb', line 44 def create(payload) validate_fields!( payload: payload, validations: { email: {type: :email, required: true}, first_name: {type: :string, required: false}, last_name: {type: :string, required: false}, phone: {type: :string, required: false} } ) response = @connection.post("customer", payload) handle_response(response) end |
#deactivate_authorization(payload) ⇒ PaystackSdk::Response
Deactivates a customer’s authorization.
175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/paystack_sdk/resources/customers.rb', line 175 def (payload) validate_fields!( payload: payload, validations: { authorization_code: {type: :string, required: true} } ) response = @connection.post("customer/deactivate_authorization", payload) handle_response(response) end |
#fetch(email_or_code) ⇒ PaystackSdk::Response
Fetches details of a single customer by email or code.
85 86 87 88 89 |
# File 'lib/paystack_sdk/resources/customers.rb', line 85 def fetch(email_or_code) validate_presence!(value: email_or_code, name: "email_or_code") response = @connection.get("customer/#{email_or_code}") handle_response(response) end |
#list(per_page: 50, page: 1, **params) ⇒ PaystackSdk::Response
Lists all customers.
67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/paystack_sdk/resources/customers.rb', line 67 def list(per_page: 50, page: 1, **params) validate_positive_integer!(value: per_page, name: "per_page", allow_nil: true) validate_positive_integer!(value: page, name: "page", allow_nil: true) validate_date_format!(date_str: params[:from], name: "from") if params[:from] validate_date_format!(date_str: params[:to], name: "to") if params[:to] query_params = {perPage: per_page, page: page}.merge(params) response = @connection.get("customer", query_params) handle_response(response) end |
#set_risk_action(payload) ⇒ PaystackSdk::Response
Sets the risk action for a customer.
“‘ruby
payload = { customer: "CUS_xxxxx", risk_action: "allow" }
response = customers.set_risk_action(payload)
if response.success?
puts "Risk action updated successfully"
end
“‘
156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/paystack_sdk/resources/customers.rb', line 156 def set_risk_action(payload) validate_fields!( payload: payload, validations: { customer: {type: :string, required: true}, risk_action: {type: :inclusion, required: true, allowed_values: %w[default allow deny]} } ) response = @connection.post("customer/set_risk_action", payload) handle_response(response) end |
#update(code, payload) ⇒ PaystackSdk::Response
Updates a customer’s details.
101 102 103 104 105 106 107 |
# File 'lib/paystack_sdk/resources/customers.rb', line 101 def update(code, payload) validate_presence!(value: code, name: "code") validate_hash!(input: payload, name: "payload") response = @connection.put("customer/#{code}", payload) handle_response(response) end |
#validate(code, payload) ⇒ PaystackSdk::Response
Validates a customer’s identity.
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/paystack_sdk/resources/customers.rb', line 122 def validate(code, payload) validate_presence!(value: code, name: "code") validate_fields!( payload: payload, validations: { country: {type: :string, required: true}, type: {type: :string, required: true}, account_number: {type: :string, required: true}, bank_code: {type: :string, required: true} } ) response = @connection.post("customer/#{code}/identification", payload) handle_response(response) end |