Facing issue with stripe failed payment webhook - laravel-5

I managing the failed payment webhook in my website but I am facing issue that invoice.payment_failed gets received before invoice.created which creating issue. Because on failed payment the system is updating the status of invoice but the invoice havnt been created becasue the invoice.created havent been called. I dont know what am I missing. I am using laravel ans spark for stripe management

Given the order of delivery for webhook events is not guaranteed, you have a couple options.
Create the record of the invoice in your system a soon as you receive a webhook notification for any new invoice ID.
When you receive a webhook notification, add it to a queue for processing, and process the queue in priority order where invoice.created events are processed first. You'll likely want to build in some sort of delay.

Related

How to handle a error inside webhook after checkout payment

I am using Stripe Checkout(Payment intent) method in my Laravel site to get the payment from the customers and then add order to the database using webhook. What if I have an error inside the webhook while inserting to a database or any error like soldout, in this case, the customer will pay but the order will not be added to the database, how to refund to the customer if the order not completed?
How to handle this situation?

Laravel Cashier (Stripe) - Checkout - invoice.paid & invoice.payment_failed events (webhooks)

When using Checkout Subscriptions, the Stripe documentation states that the minimum event types to monitor are:
checkout.session.completed - When you receive a checkout.session.completed event, you can provision the subscription.
invoice.paid - Sent each billing interval when a payment succeeds.
invoice.payment_failed -Sent each billing interval if there is an issue with your customer’s payment method.
When you receive a checkout.session.completed event, you can provision the subscription. Continue to provision each month (if billing monthly) as you receive invoice.paid events. If you receive an invoice.payment_failed event, notify your customer and send them to the customer portal to update their payment method.
https://stripe.com/docs/billing/subscriptions/checkout#provision-and-monitor
However, I am still confused about invoice.paid and invoice.payment_failed events and Laravel Cashier:
Do I need to make handlers for these two events?
I'm asking this because Cashier already handles customer.subscription.updated and customer.subscription.deleted events and, if I understood well (correct me if I'm wrong):
if the payment (for renewal of the subscription) is successful next month, then I will receive customer.subscription.updated and the subscription will continue being 'active': $user->subscribed('default') will be true (in subscriptions table stripe_status will remain active).
However, if the payment fails next month (for example, due to insufficient funds) - then customer.subscription.updated event will be triggered/sent and Cashier will change the subscription's status (to canceled or incomplete?) and $user->subscribed('default') will be false, right?
If so, then I see no reason why I should make handlers for invoice.paid and invoice.payment_failed events. I will know if the payment (renewal of the subscription) was successful because customer.subscription.updated event will be triggered/sent and Cashier will update thestripe_status in subscriptions table.
Or I'm wrong and it won’t work that way? If I didn't understand well, then I guess that customer.subscription.updated event will not be triggered/sent at all, or it will but it will not contain information if the payment (renewal of the subscription) was successful or not (and if it failed, then in handler method for the invoice.payment_failed event I will have to update the stripe_status (in subscriptions table) to canceled or incomplete?
Whether the subscription gets deleted or not after a failed invoice payment is up to your subscription settings: https://dashboard.stripe.com/settings/billing/automatic
As such it's possible to get a invoice.payment_failed event and not have the subscription be cancelled.
Since those events are very different it's safest to listen to all of them and handle accordingly. customer.subscription.updated and invoice.paid are also very different, the former will fire whenever any update to the subscription is made (e.g. if you update the metadata) whereas the latter will only fire specifically when an invoice payment succeeds.
It's ultimately up to you, but to ensure that you don't miss any important events you should consider listening to all the aforementioned events.

Laravel mail automation

I'm developing an invoicing system in order to collect payments from customers. Once I create an invoice, an email including invoice details will be sent to the customers with payment link. I want to re-generate payment token(once it expires) and send renewed payment link to the customers, and want to automate the process.
I created a Job called renewingInvoiceLinks and inside the handle method, I wrote code to retrieve all invoices with expired links and create new token and send new link. In App->Console->Kernel.php scheduled the job as following. I referred Laravel Docs but did not get the flow properly to dispatch the Job.
protected function schedule(Schedule $schedule)
{
$schedule->job(new renewingInvoiceLinks, 'renewingInvoiceLinks')->daily();
}
Things I want to get clear.
Whether using Job to do the requirement is correct or not? If not what is the perfect way to do it.
How to dispatch the Job? (Where I need to call the dispatch method?)
I think the question is quite subjective and opinionated, but here is how I'd do it:
Whenever the payment link is updated, I would fire a custom PaymentLinkUpdated event. You can either fire this event in the place(s) where you update the link, or you listen for the saving event on the affected model and fire the PaymentLinkUpdated event only when the original value is different from the updated one.
A custom event listener will listen for the update of payment links and will send out the email containing the payment details and link. This event listener should be a queued one so that emails are sent in the background and do not block the UI.
A scheduled job will check for expired payment links and will update the affected records, casting a PaymentLinkUpdated event where necessary. This will trigger step 2 (which is automatically queued again).
This way, you'd have all the logic implemented only once and sending mails would not be duplicated. If you have to send a different mail when the payment link is renewed, you can either do so in the event listener or you have to change your strategy entirely.

How do I receive only the messages that are direct messages to my bot user?

I have successfully setup my slack bot app, have enabled events etc. I want to receive any direct messages that the members of my slack team send to my bot. For this, I have only enabled Bot Events and No Team Events like below
However, I do not get any event on my webhook on this setting.
If I enable message.im event under Team Events, then I start getting events. But then, I get every message that the user (who has installed the app) sends to any other user in the team.
How do I get only the messages that are sent to my bot user?
Update 1
Based on this SO question I created a private channel with the bot user. Even then, the messages in the private channel do not arrive in my webhook with the above event subscriptions.
Update 2
After reinstalling the app, it seems to be working fine. I have only used it for few minutes after the reinstall so far. I will keep posting here how it goes. It would still be interesting to know what went wrong where.
It is normal behavior that your script receives all messages (for every channel your bot is a member of), not only the messages sent directly to your bot. At this time you can not filter further in the event configuration. So you need to filter out any unwanted message in your script handling the event.
See also this answer.

Regarding uable to trigger Paypal REST API webhook events from developer sandbox

What is wrong with my Paypal sandbox account process
to receive webhook notifications? We are unable to receive webhook event on
my URL.
URL working with "Webhooks simulator". Please let me know what
should I do for receive webhook event on above URL.
Webhooks simulator sends sample payloads for the events you configured. It does not send a notification on triggering of an actual event.
If you are getting notification via Webhooks Simulator, it means the URL configured by you is able to intercept POST requests.
Now for the "Paypal sandbox account unable to process to receive Webhook notifications" part, please check if the transaction is actually created at https://developer.paypal.com/developer/dashboard/sandbox/ . If the transaction you are looking for is not present there, there won't be any notification generated. In case transaction is present there and still you are not getting any notifications, please share debug id.
Here is a silly thing that I overlooked in the documentation and only found out after contacting support.
After you've followed the approval url and gave your approval, dit you execute the payment with the REST API?
https://developer.paypal.com/docs/api/payments/#payment_execute
Webhooks are only called after you've executed the payment.
The confusing part is; there is no webhook being called, when a user approves or cancels a payment. There is only a redirect back to a return url or cancel url. So you either need to monitor the redirect back to your site and trigger the execute call then, or find another way (like polling the payment) to see the status change before calling the execute call.

Resources