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

处理回调消息

详情请看 WeChat.Pay.EventHandler

Summary

Types

平台证书列表 - 官方文档

商户 API 私钥 - 官方文档

商户API 证书序列号 - 官方文档

商户号

构建参数

平台 证书序列号 - 官方文档

证书的序列号

启动参数

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()
]

构建参数

参数说明

默认参数:

@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()}]

启动参数

Functions

Link to this function

build_client(client, options)

View Source
@spec build_client(client(), options()) :: {:ok, client()}

动态构建 client

Link to this function

get_requester_specs(client)

View Source
@spec get_requester_specs(client()) :: [map()]
@spec init_certs(client()) :: {:ok, cacerts :: [map()]}

初始化平台证书