Here's how you can use the Stripe Customer Library to perform various customer-related actions in your Laravel application.
use Code4mk\LaraStripe\Lib\StripeCustomer;
// Initialize the Stripe Customer instance
$stripeCustomer = new StripeCustomer();
You can create a new customer by specifying their name, email, and optional metadata.
$customer = $stripeCustomer
->name($customerName)
->email($customerEmail)
->metadata($metadata)
->create();
if ($customer->isError) {
// Handle errors
echo $customer->message;
} else {
// Customer created successfully
}
You can retrieve a customer by their ID.
$customer = $stripeCustomer->retrieve($customerId);
if ($customer->isError) {
// Handle errors
echo $customer->message;
} else {
// Use $customer to access customer data
}
You can delete a customer by their ID.
$customer = $stripeCustomer->delete($customerId);
if ($customer->isError) {
// Handle errors
echo $customer->message;
} else {
// Customer deleted successfully
}
You can retrieve a list of all customers.
$customers = $stripeCustomer->lists();
if ($customers->isError) {
// Handle errors
echo $customers->message;
} else {
// Use $customers to access the list of customers
}
You can manage a customer's cards, such as listing, adding, deleting, or setting a default card.
$cards = $stripeCustomer->cards($customerId);
if ($cards->isError) {
// Handle errors
echo $cards->message;
} else {
// Use $cards to access the list of customer cards
}
$cardToken = 'your_stripe_card_token';
$response = $stripeCustomer->addCard($customerId, $cardToken);
if ($response->isError) {
// Handle errors
echo $response->message;
} else {
// Card added successfully
}
$cardId = 'stripe_card_id';
$response = $stripeCustomer->deleteCard($customerId, $cardId);
if ($response->isError) {
// Handle errors
echo $response->message;
} else {
// Card deleted successfully
}
$defaultCardId = 'stripe_default_card_id';
$response = $stripeCustomer->setDefaultCard($customerId, $defaultCardId);
if ($response->isError) {
// Handle errors
echo $response->message;
} else {
// Default card set successfully
}