NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

Write a note in this area. It's really easy to share with others. Click here ...To implement the functionality to set a specific card as the default on Stripe when clicking on it, you can modify your controller and Blade file. Below is the step-by-step implementation:

---

### **1. Modify the `CardController`**

Add a new method, `makeDefault`, to handle setting a card as the default payment method in Stripe.

```php
public function makeDefault(Request $request)
{
$stripe = new StripeStripeClient(env('STRIPE_SECRET'));

try {
// Update the customer's default payment method
$stripe->customers->update(Auth::user()->stripe_customer_id, [
'invoice_settings' => [
'default_payment_method' => $request->payment_method_id,
],
]);

return redirect()->route('cards.index')->with('success', 'Default card updated successfully.');
} catch (Exception $e) {
return redirect()->route('cards.index')->with('error', 'Failed to update default card: ' . $e->getMessage());
}
}
```

---

### **2. Update the Blade File**

Add a button or link to trigger the `makeDefault` functionality for each card. Use a form to send the `payment_method_id` to the `makeDefault` route.

Update your `Actions` column in the Blade file:

```blade
<td>
<form action="{{ route('make.default.card') }}" method="POST" style="display:inline;">
@csrf
<input type="hidden" name="payment_method_id" value="{{ $card->id }}" />
<button type="submit" class="btn btn-primary" style="background-color: #007bff;">Make Default</button>
</form>

<form action="{{ route('delete.card') }}" method="POST" style="display:inline;">
@csrf
<input type="hidden" name="pay_method" value="{{ $card->id }}" />
<button type="submit" class="btn btn-danger" style="background-color: #de163d;">Delete</button>
</form>
</td>
```

---

### **3. Define the Route**

Add a new route for the `makeDefault` method in your `web.php`:

```php
use AppHttpControllersCardController;

Route::post('/cards/make-default', [CardController::class, 'makeDefault'])->name('make.default.card');
```

---

### **4. Display the Default Card**

In your Blade file, you can highlight the default card by checking if it matches the `invoice_settings.default_payment_method`.

Modify your loop to include a condition for displaying the default card:

```blade
@foreach($existingCard->data as $card)
<tr style="{{ $card->id == Auth::user()->default_payment_method ? 'background-color: #d4edda;' : '' }}">
<td>{{ $card->card->brand }}</td>
<td>{{ $card->card->last4 }}</td>
<td>{{ $card->card->exp_month }}</td>
<td>{{ $card->card->exp_year }}</td>
<td>
@if($card->id == Auth::user()->default_payment_method)
<span class="badge badge-success">Default</span>
@else
<form action="{{ route('make.default.card') }}" method="POST" style="display:inline;">
@csrf
<input type="hidden" name="payment_method_id" value="{{ $card->id }}" />
<button type="submit" class="btn btn-primary" style="background-color: #007bff;">Make Default</button>
</form>
@endif

<form action="{{ route('delete.card') }}" method="POST" style="display:inline;">
@csrf
<input type="hidden" name="pay_method" value="{{ $card->id }}" />
<button type="submit" class="btn btn-danger" style="background-color: #de163d;">Delete</button>
</form>
</td>
</tr>
@endforeach
```

---

### **5. Update the User Model (Optional)**

If you want to store the default payment method in your database, you can update the user's record when setting the default card:

```php
public function makeDefault(Request $request)
{
$stripe = new StripeStripeClient(env('STRIPE_SECRET'));

try {
// Update the customer's default payment method
$stripe->customers->update(Auth::user()->stripe_customer_id, [
'invoice_settings' => [
'default_payment_method' => $request->payment_method_id,
],
]);

// Optionally update the user's default payment method in your database
Auth::user()->update(['default_payment_method' => $request->payment_method_id]);

return redirect()->route('cards.index')->with('success', 'Default card updated successfully.');
} catch (Exception $e) {
return redirect()->route('cards.index')->with('error', 'Failed to update default card: ' . $e->getMessage());
}
}
```

---

### **6. Update the Database (Optional)**

If you're storing the default payment method in your database, ensure your users table has a `default_payment_method` column:

```php
Schema::table('users', function (Blueprint $table) {
$table->string('default_payment_method')->nullable();
});
```

---

This setup will allow you to click a button on any card to make it the default both on Stripe and in your Laravel application.
     
 
what is notes.io
 

Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 14 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.