cartalyst / stripe-laravel

Cartalyst Stripe package integration for Laravel.
BSD 3-Clause "New" or "Revised" License
335 stars 57 forks source link

Wrong documentation for "Create a setup intent" #71

Open atomos1 opened 3 years ago

atomos1 commented 3 years ago

In this part of the doc "Create a setup intent"

the provided example code I think is wrong and refer to payment methods.

brunogaspar commented 3 years ago

upps, you're right, my bad 😅

Are you up to send a pull request to revise that? If not, i'll take a look a bit later!

Thanks!

atomos1 commented 3 years ago

Hi Bruno, I'm really newbie and learning right now about git and laravel.
If you tell me where to look to to this I will for sure help you. (also other examples have the same problem).

Bye the way just an help from you... is this flow correct?

  1. When user go to checkout page I create a paymentIntent
public function checkout(){

    $customer = Stripe::customers()->create([
        'email' => 'john@doe.com',
        'name' => 'John Doe',
    ]);

    $payIntent = Stripe::paymentIntents()->create([
        'amount' => 2000,
        'currency' => 'eur',
        'customer' => $customer['id'],
    ]);

    return view('checkout', compact('payIntent','customer'));

}

With this form

<form action="{{ route('pay') }}" method="POST" id="payment-form">
...

With this js


    $('#payment-button').on('click', function() {
        $('#payment-button').attr('disabled', true);

        stripe
            .confirmCardPayment('{{$payIntent['client_secret']}}', {
                payment_method: {
                    card: cardElement,
                },
            })
            .then(function(result) {
                if (result.error) {
                    $('#card-error').text(result.error.message).removeClass('d-none');
                    $('#payment-button').attr('disabled', false);
                } else {
                    $('#payment-method').val(result.paymentIntent.payment_method);
                    $('#payment-form').submit();
                }
            });
    })

In Stripe when the user visit /checkoutpage a payment intent is created on status requires_payment_method

  1. When the user click on PAY (and if needed provide SCA auth) the charge is immediately taken and the payment got to "status": "succeeded"

Is this correct? Can be done in a better way?

I thought the intent should be confirmed after pay click with something like this...

    public function pay(Request $request){
        $paymentMethodID = $request->input('payment_method');
        $payment_intentID = $request->input('payment_intent');

        $paymentIntent = Stripe::paymentIntents()->find($payment_intentID);

        Stripe::paymentIntents()->update($paymentIntent['id'], [
            'payment_method' =>  $paymentMethodID,
        ]);

         Stripe::paymentIntents()->confirm($paymentIntent['id']);
         // return thank you page
    }

But i can see that this is enough, why I don't have to confirm in the controller?

    public function pay(Request $request){
       // return thank you page
    }

Routes

Route::get('/checkout', 'PaymentController@checkout')->name('checkout');
Route::post('/pay', 'PaymentController@pay')->name('pay');