#sxe-auth and capture

1 messages · Page 1 of 1 (latest)

dawn lark
#

👋 happy to help

boreal junco
#

Hi

dawn lark
boreal junco
#

I think so, I want to capture later,

#

but if I don't want to capture it anymore, refund it back to user.

#

The process will be this:

dawn lark
boreal junco
#
  1. User buys one, or more items.
  2. User pays.
  3. Server checks if the products are out of stock or not. (via php &mysqli queries)
  4. If not out of stock => confirm payment.
  5. If out of stock => instantly deliver the payment back. (do not allow it)
dawn lark
#

yes that doable with capture later and you can either capture or cancel the Payment Intent

boreal junco
#

Are there any fees for the cancel the payment?

dawn lark
#

no

boreal junco
#

'payment_method' => '{{PAYMENT_METHOD_ID}}',

#

What is this?

dawn lark
#

where did you find this?

boreal junco
#
// Set your secret key. Remember to switch to your live secret key in production.
// See your keys here: https://dashboard.stripe.com/apikeys
\Stripe\Stripe::setApiKey('x');

\Stripe\PaymentIntent::create([
  'amount' => 1099,
  'currency' => 'ron',
  'payment_method_types' => ['card'],
  'capture_method' => 'manual',
  'payment_method' => '{{PAYMENT_METHOD_ID}}',
]);
dawn lark
boreal junco
#

Does checkout.js collect card details ?

#

Because I think it does?

dawn lark
#

What do you mean by checkout.js?

boreal junco
dawn lark
#

sorry I'm a bit lost are we still talking about capture later?

boreal junco
#

Yes.

#

I'm using this docs.

#

With it's .js

#

Does this .js collect card details?

dawn lark
#

the docs you sent are not for capture later

#

this is the normal flow of accepting directly a payment

boreal junco
#

I have a question.

#

On the hold docs, it tells this>

#
\Stripe\PaymentIntent::create([
  'amount' => 1099,
  'currency' => 'ron',
  'payment_method_types' => ['card'],
  'capture_method' => 'manual',
]);
#

but on capture later, it adds the payment_method too.

#

Should I use the Hold method, or capture_later method ?

dawn lark
#

yes it's doable also this way

boreal junco
#

I think Hold suits the best.

dawn lark
#

both work

boreal junco
#

Can I cancel the paymentintent even if it's on hold, right?

dawn lark
#

yes

#

the only difference in those two approaches is with how you collect the payment method, either you collect it before creating the PaymentIntent and you pass the payment_method id for the Payment Intent or you first create the PaymentIntent and then use the client_secret with Stripe Elements on the front-end to collect the payment method

boreal junco
#

You cannot cancel this PaymentIntent because it has a status of canceled. Only a PaymentIntent with one of the following statuses may be canceled: requires_payment_method, requires_capture, requires_confirmation, requires_action, processing.

#

when I m testing it with insufficient_funds
card

wooden sky
#

sounds self explanatory, the PaymentIntent was already canceled and you tried to cancel it again?

boreal junco
#

But why was it cancelled in the first place?

wooden sky
#

if you look at the PaymentIntent in the dashboard, at the bottom of the page there is a timeline of events and API requests that will likely answer that

#

I could tell you if you shared the pi_xxx ID

boreal junco
#

pi_3KvfFELKPu9R0yzS06lionIG

wooden sky
#

probably your code does the same thing twice or gets called multiple times or something, hard to say but you can debug it

boreal junco
#

Yes, you're right. No, my code doesn't do the thing twice, but, I tested this:

#

There are 10 items on stock.
An user grabs 10 on his bag.

These 10 items are still available for anyone.

#

But, an user, purchased 1 item, and paid it => there are 9 items remained available on stock.

#

But the initial user had 10 items on his bag, while he was on payment page.

#

And he paid for 10 items.

#

PHP saw that he wants to pay for more items that there are on stock.

#

And cancelled it.

#

I was a bit confuse why it cancelled twice.

#

Uncaught (Status 400) (Request req_bkOZuNGYaatHBH) This PaymentIntent could not be captured because it has a status of requires_payment_method. Only a PaymentIntent with one of the following statuses may be captured: requires_capture.

#

I get this error, when I enter on the payment page, without inserting card infos or pressing submit.

wooden sky
#

yep, makes sense right?

#

how can you capture if if you haven't begun processing the payment yet? If you have code trying to do that(capture immediately) then that code shouldn't be running.

boreal junco
#

But only captures I'm trying to make are on webhook.php

#

The payment page has only create.php

#

Which does not have any capture

#

Only the ' 'capture_method' => 'manual',
' on create method.

wooden sky
#

you must be mistaken then, sorry. You can only get that error and that request ID if are running code to capture the PaymentIntent so you need to carefully debug and add logs.

boreal junco
#

Hmm

#
    $intent->capture(['amount_to_capture' => $Price*100]);

#

We're talking about this, right?

wooden sky
#

yes

boreal junco
#

Is something wront my create methodf?

#
 $paymentIntent = \Stripe\PaymentIntent::create([
          'amount' => $Price*100,
          'currency' => 'ron',
          'payment_method_types' => ['card'],
          'capture_method' => 'manual',
          'metadata' => [
      'UserID' => $_SESSION['ID'],
      'DiscountAplicat' => $DiscountAplicatrow1008,
      'Transport' => $Transportrow1008,
      'NumeTransport' => $NumeTransportrow1008,
      'PrenumeTransport' => $PrenumeTransportrow1008,
      'Localitatea' => $Localitatearow1008,
      'Adresa' => $Adresarow1008,
      'Email' => $Emailrow1008,
      'Telefon' => $Telefonrow1008,
      'AdresaTransport' => $AdresaTransportrow1008
      
   ]

    ]);
wooden sky
#

nope, seems perfectly fine to me

boreal junco
#

Note : This is part of create.php

I am not submitting yet the payment (I'm not pressing the submit button) with credit card infos

wooden sky
#

all I can tell you is you should add some logs or breakpoints etc and figure out why that call to ->capture happens when you don't expect it

boreal junco
#

I jst removed the ->capture from code, let's see what happens.

#

ty!

#

Can retrieve generate same error as capture?

wooden sky
#

it can generate errors, but not that specific one This PaymentIntent could not be captured because it has a status of <status> , no.

boreal junco
#

So, I've removed the


    $intent = \Stripe\PaymentIntent::retrieve($intent_id);
    $intent->capture(['amount_to_capture' => $Price*100]);
}

=> no more errors.

#

Interesting.

wooden sky
#

cool!

#

looking again, probably you ran that code regardless of the event type in your webhook handler maybe?

boreal junco
#

But the problem is that I don't run the webook.php

#

Error generates even if I press the button "Proceed to payment"

wooden sky
#

like your endpoint listens to payment_intent.created, do you always just run that code? (in which case you shouldn't)

boreal junco
#

and the page of where I put the infos in the form appear./

wooden sky
#

yeah you don't run webhook.php though, it's triggered by Stripe sending you an event.

#

or maybe I'm wrong and it's not related to webhooks — I don't know, I don't have the context on the code you're writing, but maybe that's one option

boreal junco
wooden sky
#

ok never mind then!

boreal junco
#

This is the part of code.

#

webhook.php only.

#

try {
  $event = \Stripe\Event::constructFrom(
    json_decode($payload, true)
  );
} catch(\UnexpectedValueException $e) {
  // Invalid payload
  echo '⚠️  Webhook error while parsing basic request.';
  http_response_code(400);
  exit();
}
if ($endpoint_secret) {
  // Only verify the event if there is an endpoint secret defined
  // Otherwise use the basic decoded event
  $sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE'];
  try {
    $event = \Stripe\Webhook::constructEvent(
      $payload, $sig_header, $endpoint_secret
    );
  } catch(\Stripe\Exception\SignatureVerificationException $e) {
    // Invalid signature
    echo '⚠️  Webhook error while validating signature.';
    http_response_code(400);
    exit();
  }
}

$intent_id = $event->data->object->id;

$stripe = new \Stripe\StripeClient(
  'x'
);
$payment_intent = $stripe->paymentIntents->retrieve($intent_id,
  ['expand' => ['payment_method']]

);