Skip to content

refactor: Improve Paypal webhook timestamp handling

stephen requested to merge paypal-webhook-timestamp-handling into main

Incoming Paypal webhooks have timestamps formatted in a particular flavor of ISO 8601 which includes a trailing Z. This format is in the subset of ISO 8601 formats not currently parsed by datetime.fromisoformat, so we use the library dateutil to accomplish this.

However, the implementation of this parsing was needlessly verbose; it assigned the returned datetime object to a new variable, then converted that object to a POSIX timestamp and assigned that to another new variable, converted that back to a datetime object again, and finally converted it to a string via strftime('%Y-%m-%d %H:%M:%S'), assigning this result to a third variable, before almost immediately using that variable to define the value of a key in a dict.

Like the city council in Flashdance, we have removed this song and dance. dateparser.parse is still used to convert the incoming UTC- timezone timestamp to a datetime object, but that result is immediately converted to string via strftime - Paypal will always send timestamps in UTC, and CiviCRM wants dates in UTC, so there's no need to worry about strftime stripping any useful timezone data - and we assign that result directly to the aforementioned dict's relevant key. No fuss, no muss.

The in-code comment which addresses the use of dateutil has also been expanded to fully explain the string->datetime->string conversion chain.

Merge request reports