WeChat.Pay (wechat v0.19.0)
View Source微信支付
添加依赖
def deps do
  [
    {:wechat, "~> x.x", hex: :wechat_sdk},
    {:saxy, "~> 1.2"},
    {:x509, "~> x.x"}
  ]
end定义支付 Client 模块
defmodule YourApp.WeChatAppCodeName do
  @moduledoc "CodeName"
  use WeChat.Pay,
    mch_id: "1900000109",
    api_secret_v2_key: "api_secret_v2_key",
    api_secret_key: "api_secret_v3_key",
    client_serial_no: "client_serial_no",
    client_key: {:file, "apiclient_key.pem"}
end定义参数说明请看 options/0
V2 SSL 配置
部分 v2 的接口请求时需要用到证书,如:撤销订单,因此如果有使用到这部分接口,必须添加下面的配置
config :wechat, YourApp.WeChatAppCodeName,
  v2_ssl: [
    certfile: "apiclient_cert.pem", # or {:app_dir, App, "priv/cert/apiclient_cert.pem"}
    keyfile: "apiclient_key.pem" # or {:app_dir, App, "priv/cert/apiclient_key.pem"}
  ]启动支付 Client 进程
defmodule YourApp.Application do
  def start(_type, _args) do
    children = [
      # ...
      YourApp.WeChatAppCodeName,
      # or
      {YourApp.WeChatAppCodeName, start_options}
    ]
    Supervisor.start_link(children, strategy: :one_for_one, name: YourApp.Supervisor)
  end
end启动参数说明请看 start_options/0
处理回调消息
Summary
Types
@type api_secret_key() :: binary() | WeChat.env_option()
API 密钥 - 官方文档
@type cacerts() :: [binary()]
平台证书列表 - 官方文档
@type client() :: module()
@type client_key() :: pem_file()
商户 API 私钥 - 官方文档
@type client_serial_no() :: serial_no()
商户API 证书序列号 - 官方文档
@type mch_id() :: binary()
商户号
@type options() :: [ mch_id: mch_id(), api_secret_v2_key: api_secret_key(), api_secret_key: api_secret_key(), client_serial_no: client_serial_no(), client_key: client_key(), requester: module(), storage: module() ]
构建参数
参数说明
- mch_id: 商户ID, 必填
- api_secret_v2_key: API v2密钥, 必填
- api_secret_key: API v3密钥, 必填
- client_serial_no: 客户端证书序列号, 必填
- client_key: 客户端私钥, 必填
- storage: 存储器,默认值:- WeChat.Storage.PayFile
- requester: 请求客户端, 默认值:- WeChat.Requester.Pay
@type pem_file() :: {:binary, binary()} | {:file, Path.t()} | {:app_dir, Application.app(), Path.t()}
@type platform_serial_no() :: serial_no()
平台 证书序列号 - 官方文档
@type requester_id() :: :A | :B
      @type requester_opts() :: %{id: requester_id(), name: atom()}
@type serial_no() :: binary()
证书的序列号
启动参数
- refresher: 刷新器, 可选, 默认值:- WeChat.Refresher.Pay
- finch_pool: Finch Pool 配置, 可选, 默认值:- [size: 32, count: 8]
- v2_ssl: V2 SSL 配置, 可选
Functions
动态构建 client
初始化平台证书
@spec shutdown_client(Supervisor.supervisor(), client()) :: :ok | {:error, error :: any()}
动态关闭 client
@spec start_client(Supervisor.supervisor(), client(), start_options()) :: Supervisor.on_start_child()
动态启动 client