Customer Management
The Customer Management module allows you to create and manage customer profiles in your CashOnRails account. This module provides functionality for creating new customers, retrieving customer information, and managing customer data.
Features
- Create new customers
- Retrieve customer details
- List all customers
- Update customer information
Create Customer
Create a new customer profile:
const customer = await client.customerService.create({
email: "johndoe@gmail.com",
first_name: "John",
last_name: "Doe",
phone: "08012345678"
});
console.log('Customer Details:', customer.data);
Parameters
Parameter | Type | Required | Description |
---|---|---|---|
email | string | Yes | Customer’s email address |
first_name | string | Yes | Customer’s first name |
last_name | string | Yes | Customer’s last name |
phone | string | Yes | Customer’s phone number |
Response
{
status: "success",
message: "Customer created successfully",
data: {
customer_code: "CUS_12345",
email: "johndoe@gmail.com",
first_name: "John",
last_name: "Doe",
phone: "08012345678",
created_at: "2023-05-15T10:30:00Z"
}
}
Retrieve All Customers
Get a list of all customers with pagination:
const customers = await client.customerService.retrieveAll();
console.log('All Customers:', customers.data);
Response
{
status: "success",
message: "Customers retrieved successfully",
data: [
{
customer_code: "CUS_12345",
email: "johndoe@gmail.com",
first_name: "John",
last_name: "Doe",
phone: "08012345678",
created_at: "2023-05-15T10:30:00Z"
},
// More customers...
],
pagination: {
limit: 20,
offset: 0,
next_offset: 20,
total: 35,
total_pages: 2
}
}
Retrieve One Customer
Get details of a specific customer:
const customer = await client.customerService.retrieveOne({
customer_code: "CUS_12345"
});
console.log('Customer Details:', customer.data);
Parameters
Parameter | Type | Required | Description |
---|---|---|---|
customer_code | string | Yes | Unique customer identifier |
Response
{
status: "success",
message: "Customer retrieved successfully",
data: {
customer_code: "CUS_12345",
email: "johndoe@gmail.com",
first_name: "John",
last_name: "Doe",
phone: "08012345678",
created_at: "2023-05-15T10:30:00Z",
metadata: {
// Additional customer information
}
}
}
Error Handling
try {
const customer = await client.customerService.create({
email: "johndoe@gmail.com",
first_name: "John",
last_name: "Doe",
phone: "08012345678"
});
} catch (error) {
console.error('Failed to create customer:', error.message);
}
Complete Example
const { CashOnRailsClient } = require('cashonrails-node');
const client = new CashOnRailsClient('YOUR_SECRET_KEY');
async function manageCustomers() {
try {
// Create a new customer
const newCustomer = await client.customerService.create({
email: "johndoe@gmail.com",
first_name: "John",
last_name: "Doe",
phone: "08012345678"
});
console.log('New Customer:', newCustomer.data);
// List all customers
const allCustomers = await client.customerService.retrieveAll();
console.log('All Customers:', allCustomers.data);
// Get specific customer
const customerDetails = await client.customerService.retrieveOne({
customer_code: "CUS_12345"
});
console.log('Customer Details:', customerDetails.data);
} catch (error) {
console.error('Error:', error.message);
}
}
manageCustomers();
Best Practices
- Always validate customer data before creating or updating profiles.
- Use unique identifiers for customers to avoid duplicates.
- Implement error handling to manage API failures gracefully.
- Regularly update customer information to keep records accurate.
- Use pagination when retrieving large lists of customers to improve performance.
- Secure sensitive customer data and comply with data protection regulations.