ExWechatpay.VirtualPay.TokenAgent (ex_wechatpay v0.4.0)

View Source

虚拟支付 access_token 缓存代理

管理微信 access_token 的缓存和自动刷新。 access_token 有效期 7200 秒,过期前 300 秒自动刷新。

每个商户配置对应一个独立的 TokenAgent 实例。

Summary

Functions

Returns a specification to start this module under a supervisor.

获取 access_token

强制刷新 access_token

启动 TokenAgent

Functions

child_spec(arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

get_token(name, appid, secret)

@spec get_token(atom(), String.t(), String.t()) ::
  {:ok, String.t()} | {:error, ExWechatpay.Exception.t()}

获取 access_token

如果 token 已过期或即将过期(300 秒内),自动刷新。

参数

  • name - TokenAgent 进程名称
  • appid - 小程序 AppID
  • secret - 小程序 secret

返回值

  • {:ok, token} - 获取成功
  • {:error, exception} - 获取失败

refresh(name, appid, secret)

@spec refresh(atom(), String.t(), String.t()) ::
  {:ok, String.t()} | {:error, ExWechatpay.Exception.t()}

强制刷新 access_token

参数

  • name - TokenAgent 进程名称
  • appid - 小程序 AppID
  • secret - 小程序 secret

返回值

  • {:ok, token} - 刷新成功
  • {:error, exception} - 刷新失败

start_link(opts)

@spec start_link(keyword()) :: {:ok, pid()} | {:error, term()}

启动 TokenAgent

参数

  • opts - 选项
    • :name - 进程名称(必需)

返回值

  • {:ok, pid()} - 启动成功
  • {:error, reason} - 启动失败