0
votes

Im running Stripe in test mode.

I've created a Yearly billing plan for 100GBP amount, with a 7 days trial ( directly onto Stripe dashboard. )

However, to test the webhooks i've hardcoded the trial_end:

$trialEnd = new DateTime();
    $trialEnd->setTimestamp(time()+120);

    $user = Users::find($this->user()['user_id']);
    $user->subscription($stripe_plan['stripe_plan'])->trialFor($trialEnd)->create($data['stripeToken'], [
        'email' => $this->user()['email']
    ]);
    $user->save();

Basically all goes well, but into stripe dashboard the first invoice for 0 GBP is shown, and after one minute i get the Subscription will end in a minute event. After all, the subscription become Active ( from Trialing ) state.

All the webhooks and even the first subscription add reponse i get the trial ends period instead subscription ends.

How can i get the subscription_ends_at timestamp ?

All webhook requests are having the following timestamps:

{
  "id": "evt_18baRrIzJLF7fe6PMDPYD0NM",
  "object": "event",
  "api_version": "2016-07-06",
  "created": 1469558315,
  "data": {
    "object": {
      "id": "sub_8tNBbqy0AmSk8p",
      "object": "subscription",
      "application_fee_percent": null,
      "cancel_at_period_end": false,
      "canceled_at": null,
      "created": 1469558268,
      "current_period_end": 1469558384,
      "current_period_start": 1469558268,
      "customer": "cus_8tNB1tWYw3Jw7L",
      "discount": null,
      "ended_at": null,
      "livemode": false,
      "metadata": {
      },
      "plan": {
        "id": "yearly_200",
        "object": "plan",
        "amount": 20000,
        "created": 1469545724,
        "currency": "gbp",
        "interval": "year",
        "interval_count": 1,
        "livemode": false,
        "metadata": {
        },
        "name": "Full Club Membership - Pay Anually",
        "statement_descriptor": "FULL MEMBERSHIP",
        "trial_period_days": 7
      },
      "quantity": 1,
      "start": 1469558268,
      "status": "trialing",
      "tax_percent": null,
      "trial_end": 1469558384,
      "trial_start": 1469558268
    }
  },
  "livemode": false,
  "pending_webhooks": 1,
  "request": null,
  "type": "customer.subscription.trial_will_end"
}

So if you look at trial_start and trial_end is same with current_period_start and current_period_end.

I've though initially that if this is the current period.. fine, but after trial expires the current period shouldn't be trials one.

There is any method to take the subscription_ends_at field from Stripe api ? And also, after the trial period ends, shouldn't send a invoice with the real amount ?

Also, i created a subscription plan with no trial period. That plan after a client subscribed, i get the correct timestamps.

Thanks in advance!

2

2 Answers

1
votes

It looks like you figured it out. Basically, the delay comes from the fact that when the timestamp passes for your trial expiration, your request to create a new Invoice on that billing cycle gets added to a queue. Typically the queue will create the new invoice ~immediately, but it can sometimes go several minutes before triggering.

The first Invoice will always have timestamps for the current_period_* that map to the trial_period_* ones. Whereas, the second Invoice (that shows up with the invoice.created-event) will have the accurate timestamps for the billing period.

0
votes

Oh, now i understand .. i will explain maybe will help someone :D.

Basically if a subscription got a trial period when you subscribe you will get a invoice for 0. Then, even if you set the trial to expire in 2 minutes with the request , the first payment will occur in about 10 minutes :D ( with that payment (if you set a webhook url) you will get a "type": "customer.subscription.updated" event who will contain all desired informations. At that time you can update your subscription_ends_at .

I didn't wait 10 minutes to see if the new invoice will be triggered.. and created -> removed -> recreated -> removed and so on for 4 hours with different tests.