14

I want to test for the invoice_payment.failed event web hook in stripe. I have set a web hook end point and tried to send test data from stripe but that does not work because the event id is non existent. There are a few solutions mentioned in stripe guidelines. Here is the link I saw.

https://support.stripe.com/questions/test-failed-invoice-payment

It tells me to create a plan and subscribe a customer with the trial_end period set to 1-2 minutes and use a certain credit card number for creating the customer object. I set the trial_end period to "now" and got only the charge.failed using the credit card number given but not the invoice_payment.failed event. I am new to stripe and would like to know how to set the trial_end period to 1-2 minutes from now and how exactly to test for the invoice_payment.failed event. I am working in php. Any help would be appreciated.

jai
  • 391
  • 2
  • 6
  • 14

3 Answers3

23

When creating a subscription, you can set the trial period with the trial_end parameter.

In order to test the invoice.payment_failed event, you can do something like this:

  1. First, create a customer with a card token (from Checkout or Stripe.js) created with the special testing number 4000 0000 0000 0341:

    $customer = \Stripe\Customer::create([
        "source" => $token,
        "description" => "Test customer"
    ]);
    
  2. Then, create the subscription to the plan with a short trial period:

    $subscription = \Stripe\Subscription::create([
        "customer" => $customer->id,
        "plan" => $plan,
        "trial_end" => strtotime("+1 minute")
    ]);
    

This will create the subscription with a one minute trial period. After one minute, an invoice will be created, and approximately one hour later, payment for the invoice will be attempted.

If you don't want to wait one hour, you can manually retrieve the invoice after it's been created and trigger the payment attempt:

$invoice = \Stripe\Invoice::retrieve($invoice_id);
$invoice->pay();
Ywain
  • 16,854
  • 4
  • 51
  • 67
  • 1
    My plan gets created first and then I try to subscribe a customer to the plan. Will it work if I try to set the trial_end period to strtotime("+2 minutes") while creating the customer? I am yet to try it out. – jai Jun 13 '16 at 04:09
  • I am willing to wait for 1 hour and I am using the api for doing everything – jai Jun 13 '16 at 04:09
  • Yes, you can create the customer and the subscription in a single API call by including the `plan` and `trial_end` parameter in the customer creation request. – Ywain Jun 13 '16 at 08:16
  • Ywain thanks. I got it working. Both the charge.declined event and the invoice_payment failed event get fired. First time there is a charge decline for $0 and then the invoice gets updated and then again after one hour both charge declined and invoice_payment failed get fired. I wrote my web hook end point. Thanks a lot. – jai Jun 14 '16 at 12:11
  • Glad I could help! :) – Ywain Jun 14 '16 at 13:27
  • 1
    I have done as you said, but after I manually hit the inovice pay then it shows card declined, but why my webhook have not worked in this case. Even there is no entry in logs. The toppest entry in logs is about 402 card declined for invoice.I am using invoice.payment_failed this evint only in my webhook – harbrinder_singh Nov 16 '18 at 20:05
  • Webhook is hitting successfully after run $invoice->pay(); but there is no type name keyword to detect which type of event it is so unable to handle invoice.payment.failed event........any help please – harbrinder_singh Nov 16 '18 at 20:20
  • You can also do `strtotime("+10 seconds")` so you don't have to wait that long haha – Edward Nov 21 '19 at 22:06
  • After one hour not invoice.payment_failed event call. – shazim ali Apr 13 '23 at 17:46
9

If you just want to test and handle invoice.payment_failed event and others which you can and can't create, then you should go to stripe dashboard and under webhooks settings, send test event of required type.

Mukarram Ishaq
  • 748
  • 1
  • 8
  • 18
2

In new terminal (assuming you have Stripe CLI) just type: stripe trigger invoice.payment_failed

https://stripe.com/docs/webhooks/test#forward-events

Marius P
  • 326
  • 1
  • 4
  • 9
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – scandav Oct 17 '21 at 10:41