Sample code for 30+ languages & platforms
Visual FoxPro Requires Chilkat v11.0.0+

PayPal: Decide when to renew an access token

See more PayPal Examples

Use the UTC request time and expires_in from the token example. Renew five minutes early. This is a cache decision; a revoked token can still receive HTTP 401 before its calculated expiry.

PayPal documentation

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL loTiming
LOCAL loIssued
LOCAL lnLoadedTiming
LOCAL lnValidTimestamp
LOCAL loNow
LOCAL lnAge
LOCAL lnLifetime

*  Read the metadata saved by Get a sandbox access token.
loTiming = CreateObject('Chilkat.JsonObject')
loIssued = CreateObject('Chilkat.CkDateTime')
lnLoadedTiming = loTiming.LoadFile("paypal-token-timing.json")
IF (lnLoadedTiming <> 1) THEN
    ? "No timing metadata. Request a token."
    RELEASE loTiming
    RELEASE loIssued
    CANCEL
ENDIF

lnValidTimestamp = loIssued.SetFromTimestamp(loTiming.StringOf("issued_at"))
IF (lnValidTimestamp <> 1) THEN
    ? "Invalid issue time. Request a token."
    RELEASE loTiming
    RELEASE loIssued
    CANCEL
ENDIF

loNow = CreateObject('Chilkat.CkDateTime')
loNow.SetFromCurrentSystemTime()
lnAge = loNow.DiffSeconds(loIssued)
lnLifetime = loTiming.IntOf("expires_in")
IF ((lnAge < 0) OR (lnLifetime <= 300) OR (lnAge >= (lnLifetime - 300))) THEN
    ? "Request a new client-credentials token."
    RELEASE loTiming
    RELEASE loIssued
    RELEASE loNow
    CANCEL
ENDIF

? "Reuse the matching token from the credential store."

RELEASE loTiming
RELEASE loIssued
RELEASE loNow