#kwac.

1 messages · Page 1 of 1 (latest)

silver quarryBOT
#

Hello! We'll be with you shortly. Below are links to other discussions we've had with you in the past week in case you want to review that information. If your question is related to one of these previous discussions, please provide a comprehensive summary of the current state and what you need help with now. We help many users simultaneously, so a summary allows us to resolve your issue as soon as possible.

grizzled valve
#

`
const { setupIntent, error } = await stripe.confirmSetup({
elements,
confirmParams: {
return_url: "https://mywebsite.com/checkout",
},
redirect: 'if_required'
});

                  if(error){
                  if (error.type === "card_error" || error.type === "validation_error") {
                    showMessage(error.message);
                  } else {
                    showMessage("An unexpected error occurred.");
                  }
                  } else {
                     let payment_form =  document.querySelector("#stripe_form_checkout")
                     var hiddenInput = document.createElement("input");
                     hiddenInput.setAttribute('type', 'hidden');
                     hiddenInput.setAttribute('name', 'paymentMethod');
                     hiddenInput.setAttribute('value', setupIntent.payment_method);
                     payment_form.appendChild(hiddenInput);
                  }
                  
                  
                
                }`
quaint warren
#

Could you please hare more details about the issue ?

#

are you seeing any error in the console for example ?

grizzled valve
#

I get no errors in console, but on first submit paymentMethod is not detected but I have declared payment error check and get the message from ** wc_add_notice('Payment method is not set. Please try again.', 'error');** in window. But on second form submit it processes payment fine.
for this part $paymentMethod = $_POST['paymentMethod'];

`
public function process_payment( $order_id ) {

        $order = wc_get_order( $order_id );
        
        $paymentMethod = $_POST['paymentMethod'];
        

        try {
                
                 if (isset($_POST['paymentMethod'])) {
                    $paymentMethod = wc_clean($_POST['paymentMethod']);
                } else {
                    wc_add_notice('Payment method is not set. Please try again.', 'error');
                    return;
                }   

`

#

Maybe it's because the way I grab payment method? Maybe I need to do it using ajax call?

#

Oh wait I just checked logs in stripe and i see error 400 for setupintent (req_EYtGCnGFMrWAy5
)

quaint warren
quaint warren
#

The setup intent used is already submited

#

you need to use a Setupintent with a correct status

grizzled valve
quaint warren
#

In other word you need to use a SetupIntent that wasn't submitted before, a new one.

grizzled valve
#

okay but on first submit setup intent is fine req_G0orYA1oMlqD5d

#

why does it then processes payment still on second submit?

quaint warren
#

Ah that's your integration is trying to submit the same SetupIntent twice

#

you need to debug your integration and make sure that for each SetupIntent you are submitting it only once.

grizzled valve
#

So it's not related with how the payment method is grabbed? I thought it's because in payment process function it's immediately trying to processes payment on form submit even when payment method is not set to hidden field in form, i thought i would need it to wait to be added from javascript side before processing payment on backend

quaint warren
#

Yes it seems youa re processing the same Setupintent twice

grizzled valve
#

can tarzan come here, he might be more knowledgeable

quaint warren
#

The request id you've shared is very clear

#

You cannot update this SetupIntent because it has already succeeded.

grizzled valve
#

so it still can process payment? even on second submit?

quaint warren
#

What you mean by process payment ?

#

The Setupintent is for collecting the PaymentMethod

#

then you can use it for processing a payment

grizzled valve
#

Like I said I'm getting error 'Payment method is not set. Please try again on first submit but on second, i get no errors, and payment is processed, so I assumed it's related with how i grab payment method, I thought I need to use ajax call

grizzled valve
quaint warren
#

No worries! We are all happy to help!

quaint warren
#

Setupintent is just for collecting the PaymentMethod, there is no funds captured

#

In order to capture funds you need to use PaymentIntent

#

is this part clear ?

silver quarryBOT
grizzled valve
#

In following order I first:
Use setup intent mainly for grabbing client secret and attaching it onto javascript side

`
public function generate_setup_intent() {
if (!$this->client_secret) {
$intent = \Stripe\SetupIntent::create([
'payment_method_types' => ['card'],
]);
$this->client_secret = $intent->client_secret;
}

        return $this->client_secret;
    }

`

#

and attach goes here

const options = { currency: 'usd', appearance: {/*...*/}, clientSecret: '<?php echo esc_js($this->client_secret); ?>', };

Then I submit form and in process payment I try to grab payment method

        $paymentMethod = $_POST['paymentMethod'];

Then we check if paymentmethod exists if it doesn't , we print this message ( wc_add_notice('Payment method is not set. Please try again.', 'error');) - this message being printed on first submit but on second it doesn't because payment method exists, also after that i create customer and then create payment intent. Is this correct order?

#

if (isset($_POST['paymentMethod'])) {
$paymentMethod = wc_clean($_POST['paymentMethod']);
} else {
wc_add_notice('Payment method is not set. Please try again.', 'error');
return;
}

               $customer = \Stripe\Customer::create([
                     'name'                => $order->get_billing_first_name(),
                     'email'                => $order->get_billing_email(),
                     'payment_method'       => $paymentMethod,
                ]);
           
                $paymentIntent = \Stripe\PaymentIntent::create([
                    'payment_method'       => $paymentMethod,
                    'amount'               => $order->get_total() * 100, 
                    'currency'             => 'usd',
                    'payment_method_types' => ['card'],
                    'customer'             => $customer->id,
                    'confirmation_method'  => 'automatic',
                    'confirm'              => 'true',
                    'description' => 'Payment for Order #' . $order_id,
                    'statement_descriptor' => 'ORDER ' . $order_id,
                    'capture_method'       => 'manual',
                    'payment_method_options' => [ 'card_present' => ['request_incremental_authorization_support' => true], ],
                ]);
eternal knot
grizzled valve
#

but I first need to grab client secret for js side

eternal knot
#

the PaymentIntent has a client secret.