View Source WeChat.Pay (wechat v0.15.1)
微信支付
添加依赖
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
: 商户IDmch_id/0
- 必填api_secret_v2_key
: API v2密钥api_secret_key/0
- 必填api_secret_key
: API v3密钥api_secret_key/0
- 必填client_serial_no
: 客户端证书序列哈client_serial_no/0
- 必填client_key
: 客户端私钥client_key/0
- 必填storage
: 存储器WeChat.Storage.Adapter.t/0
requester
: 请求客户端 -module/0
默认参数:
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()
证书的序列号
@type start_options() :: [{:refresher, module()}]
启动参数
refresher
: 刷新器 -module/0
, 可选, 默认值:WeChat.Refresher.Pay
Functions
动态构建 client
初始化平台证书