#berlanbarbalho_webhooks
1 messages ¡ Page 1 of 1 (latest)
đ Welcome to your new thread!
â˛ď¸ We'll be here soon! Typically we respond in a few minutes, but sometimes we might take a bit longer if the server is busy or if you have a particularly tricky question.
âąď¸ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can always start a new thread if you have another question.
đ This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1240039830210084945
đ Have more to share? Add more details, code, screenshots, videos, etc. below.
Hello
I'm not sure I understand the flow you're trying to integrate here.
You can't send any data back to the webhook event as a response and use that to trigger different actions on Stripe.
Are there any docs you're following for this?
Function that receives the webhook:
\Stripe\Stripe::setApiKey($api_key);
$payload = @file_get_contents('php://input');
try {
$payment = \Stripe\Event::constructFrom(
json_decode($payload, true, 512, JSON_THROW_ON_ERROR)
);
} catch(JsonException $e) {
http_response_code(400);
exit();
}
if ($payment->type === 'checkout.session.completed') {
$session = $payment->data->object;
$external_reference = $session->metadata->order_id;
$max_discount = $this->vars['plugin_config']['max_discount'];
[$fee, $liquid] = $this->getFinancialDetails($this->vars['plugin_config'], $session->payment_intent, 'stripe');
if ($this->validateOrder($external_reference, $session->amount_total, $session->currency, $max_discount)) {
$this->processOrder($external_reference, $session->payment_intent, $session->payment_method_types[0], $fee, $liquid, $session->currency, 'stripe', $this->vars['plugin_config']);
http_response_code(200);
echo json_encode([
'status' => 'success',
'message' => 'Payment processed successfully',
'fee' => $fee,
'liquid' => $liquid
], JSON_THROW_ON_ERROR);
} else {
http_response_code(200);
echo json_encode(['status' => 'failed', 'message' => 'Order validation failed'], JSON_THROW_ON_ERROR);
}
} else {
http_response_code(200);
echo json_encode(['status' => 'failed', 'message' => 'Payment not approved or missing external reference'], JSON_THROW_ON_ERROR);
}```
getFinancialDetails:
``` $api_key = $config['stripe_sandbox'] ? $config['stripe_sandboxsecretkey'] : $config['stripe_secretkey'];
\Stripe\Stripe::setApiKey($api_key);
try {
$paymentIntent = \Stripe\PaymentIntent::retrieve([
'id' => $payment_id,
'expand' => ['latest_charge.balance_transaction']
]);
if ($paymentIntent->status == 'succeeded' && isset($paymentIntent->latest_charge)) {
$balanceTransaction = $paymentIntent->latest_charge->balance_transaction;
if ($balanceTransaction) {
$netAmount = $balanceTransaction->net / 100;
$feeAmount = $balanceTransaction->fee / 100;
}
}
return [$feeAmount ?? 0, $netAmount ?? 0];
} catch (Exception $e) {
throw new JsonException('Error retrieving financial details: ' . $e->getMessage());
}```
I may be mistaken, but I think the problem is that I am trying to get information about the payment data at the same time that I receive the Webhook, and I believe that in the stripe system still does not have this information available, and only after receiving the Webhook (from the payment being completed), the payment data is available for consultation.
Is this the problem I am facing ?
It often depends on the payment method type that you're working with. It is possible that the payment hasn't succeeded when you're making the API request for the details.
You'd likely want to check the PaymentIntent status before you look for the fees and net amount
But the "checkout.session.completed" means that the payment has already been successful, is not it?
that event means checkout session was completed.
There are payment methods which are considered delayed notification like ACH
https://docs.stripe.com/payments/ach-debit
See: https://docs.stripe.com/payments/payment-methods#payment-notification
So, I believe that if I change the event to "payment_intent.succeeded", it should work again, correct ?
All payment methods will generate me a "payment_intent.successed" event" ?
Yep