pinax/pinax-stripe-light

Bug: in subscriptions.create, trial_days using timezone naive utcnow()

miaoz2001 opened this issue · 3 comments

Issue Summary

in subscriptions.create, when calculating the trial_end value, using

    if trial_days:
        subscription_params["trial_end"] = datetime.datetime.utcnow() + datetime.timedelta(days=trial_days)

However utcnow is timezone naive so the trial_end has no timezone info.
So when stripe calculate the timestamp with the code below:

def _encode_datetime(dttime):
    if dttime.tzinfo and dttime.tzinfo.utcoffset(dttime) is not None:
        utc_timestamp = calendar.timegm(dttime.utctimetuple())
    else:
        utc_timestamp = time.mktime(dttime.timetuple())

    return int(utc_timestamp)

It will get the wrong timestamp, which is, for example, if my local timezone is UTC+10, then the timestamp of trial_end will be 10 hours earlier.

The code should be

    if trial_days:
        subscription_params["trial_end"] = datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(days=trial_days)

If this is a bug instead of a question or feature request, please fill out the sections below.


Steps to Reproduce

To re-produce, just pass the trial_days=10 in subscriptions.create()

What were you expecting to happen?

in the pinax_stripe_subscription table it shows as below
image
However trial_end should be 2018-07-14 01:20:49

What actually happened?

trial_end should be 2018-07-14 01:20:49

Nice catch!

Please consider creating a PR (with a test).

Thanks!
I will try, maybe take some time though...

pull request created