3.2 KiB
3.2 KiB
Manage Customers
How to create, update, and delete customers using the Mollie API.
Create a Customer
use Mollie\Api\Http\Requests\CreateCustomerRequest;
try {
// Create a new customer
$customer = $mollie->send(
new CreateCustomerRequest(
name: 'Luke Skywalker',
email: 'luke@example.com',
locale: 'en_US',
metadata: [
'isJedi' => true
]
)
);
echo "New customer created: {$customer->id}\n";
} catch (\Mollie\Api\Exceptions\ApiException $e) {
echo "API call failed: " . htmlspecialchars($e->getMessage());
}
Update a Customer
use Mollie\Api\Http\Requests\GetCustomerRequest;
use Mollie\Api\Http\Requests\UpdateCustomerRequest;
try {
// First retrieve the customer you want to update
$customer = $mollie->send(
new GetCustomerRequest(
id: 'cst_8wmqcHMN4U'
)
);
// Update specific customer fields
$customer = $mollie->send(
new UpdateCustomerRequest(
id: $customer->id,
name: 'Luke Sky',
email: 'luke@example.com',
locale: 'en_US',
metadata: [
'isJedi' => true
]
// Fields we don't specify will keep their current values
)
);
echo "Customer updated: {$customer->name}\n";
} catch (\Mollie\Api\Exceptions\ApiException $e) {
echo "API call failed: " . htmlspecialchars($e->getMessage());
}
Delete a Customer
use Mollie\Api\Http\Requests\DeleteCustomerRequest;
try {
// Delete a customer
$mollie->send(
new DeleteCustomerRequest(
id: 'cst_8wmqcHMN4U'
)
);
echo "Customer deleted\n";
} catch (\Mollie\Api\Exceptions\ApiException $e) {
echo "API call failed: " . htmlspecialchars($e->getMessage());
}
The Response
$customer->id; // "cst_8wmqcHMN4U"
$customer->name; // "Luke Skywalker"
$customer->email; // "luke@example.com"
$customer->locale; // "en_US"
$customer->metadata; // Object containing custom metadata
$customer->mode; // "live" or "test"
$customer->createdAt; // "2024-02-24T12:13:14+00:00"
Additional Notes
- Customers are used to store recurring payment details
- Each customer can have multiple mandates for different payment methods
- The customer ID is required for:
- Creating recurring payments
- Creating subscriptions
- Retrieving payment history
- Customer data should be kept in sync with your own database
- Available locales:
en_USEnglish (US)en_GBEnglish (UK)nl_NLDutch (Netherlands)nl_BEDutch (Belgium)fr_FRFrench (France)fr_BEFrench (Belgium)de_DEGerman (Germany)de_ATGerman (Austria)de_CHGerman (Switzerland)es_ESSpanish (Spain)ca_ESCatalan (Spain)pt_PTPortuguese (Portugal)it_ITItalian (Italy)nb_NONorwegian (Norway)sv_SESwedish (Sweden)fi_FIFinnish (Finland)da_DKDanish (Denmark)is_ISIcelandic (Iceland)hu_HUHungarian (Hungary)pl_PLPolish (Poland)lv_LVLatvian (Latvia)lt_LTLithuanian (Lithuania)