Laravel Cashier, the elegant bridge between your Laravel application and Stripe's robust payment infrastructure, offers a streamlined approach to managing subscriptions, payments, and customer accounts. This comprehensive guide delves into the intricacies of leveraging Cashier, covering everything from creating and updating customer profiles to handling complex billing scenarios. Forget wrestling with Stripe's API directly – let's unlock the power of seamless integration.
use Laravel\Cashier\Cashier;
// Retrieve the user based on their Stripe ID
$user = Cashier::findBillable('cus_XXXXXXXXXXXXXXX'); // Replace with actual Stripe ID
// Access user attributes
if ($user) {
echo "User ID: " . $user->id . "\n";
echo "Email: " . $user->email . "\n";
// Access other user details as needed
} else {
echo "User not found.\n";
}
Creating Stripe Customers: Beyond Subscriptions
use Laravel\Cashier\Cashier;
use Stripe\Stripe;
use Stripe\Customer;
// Set your Stripe Secret Key (ensure this is securely managed, e.g., in environment variables)
Stripe::setApiKey(config('cashier.secret'));
// Function to create a Stripe customer
function createStripeCustomer($user, $paymentMethodId) {
try {
$customer = Customer::create([
'email' => $user->email, // Using the user's email address
'payment_method' => $paymentMethodId, // The payment method ID received from Stripe's elements
'invoice_settings' => [
'default_payment_method' => $paymentMethodId, // Automatically sets this as default
],
]);
// Update the user's Stripe ID
$user->stripe_id = $customer->id;
$user->save();
return $customer;
} catch (\Exception $e) {
// Handle Stripe API errors appropriately (log, display error message, etc.)
Log::error('Stripe Customer Creation Error: ' . $e->getMessage());
return null;
}
}
// Example usage (assuming you have a registered user and a payment method ID)
$user = User::find(1); // Replace with your user ID
$paymentMethodId = request('paymentMethodId'); // Get payment method ID from your form submission
$customer = createStripeCustomer($user, $paymentMethodId);
if ($customer) {
// Customer created successfully, perform any necessary actions
// e.g., send confirmation email, redirect to dashboard
} else {
// Handle customer creation failure
}
Managing Customer Details: Updates and Beyond
use Stripe\Stripe;
use Stripe\Customer;
Stripe::setApiKey(config('cashier.secret'));
$user = Cashier::findBillable('cus_XXXXXXXXXXXXXXX'); //Fetch the user object using their Stripe ID.
if($user){
try {
$customer = Customer::retrieve($user->stripe_id);
$customer->email = 'new_email@example.com';
$customer->save();
//Update your database accordingly if you need to store the email address there as well.
} catch (\Exception $e) {
Log::error('Stripe Customer Update Error: ' . $e->getMessage());
}
}
Handling Single Charges: Streamlining One-Time Payments
use Laravel\Cashier\Cashier;
use Stripe\Stripe;
use Stripe\Charge;
Stripe::setApiKey(config('cashier.secret'));
$user = Cashier::findBillable('cus_XXXXXXXXXXXXXXX'); // Retrieve the user
if ($user) {
try {
$charge = Charge::create([
'amount' => 1000, // Amount in cents
'currency' => 'usd',
'customer' => $user->stripe_id,
'description' => 'One-time purchase',
]);
// Handle the successful charge (e.g., send confirmation)
// Access charge details using $charge->id, $charge->status, etc.
} catch (\Exception $e) {
Log::error('Stripe Charge Creation Error: ' . $e->getMessage());
// Handle the error (e.g., display an error message)
}
}
0 comments:
Post a Comment