Skip to content

Commit

Permalink
feat: add more apis for WeChat.Pay.Transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
feng19 committed Feb 5, 2024
1 parent b737618 commit fc05697
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 13 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
### Links

- [WeChat SDK 使用指南](https://feng19.com/2022/07/08/wechat_for_elixir_usage/)
- [示例项目 - github - feng19/wechat_demo](https://github.com/feng19/wechat_demo)
- [示例项目 - wechat_demo](https://github.com/feng19/wechat_demo)
- [示例项目(pay) - wechat_pay_demo](https://github.com/feng19/wechat_pay_demo)
- [微信官方文档 - 开发前必读](https://developers.weixin.qq.com/doc/offiaccount/Getting_Started/Getting_Started_Guide.html)
- [微信官方文档 - 在线文档](https://hex.pm/packages/wechat_sdk)

Expand All @@ -30,7 +31,7 @@ You can use `wechat` in your projects by adding it to your `mix.exs` dependencie
```elixir
def deps do
[
{:wechat, "~> 0.13", hex: :wechat_sdk}
{:wechat, "~> 0.14", hex: :wechat_sdk}
]
end
```
Expand Down
8 changes: 3 additions & 5 deletions lib/wechat/pay/pay.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ defmodule WeChat.Pay do
@moduledoc """
微信支付
**注意**: 未经上线测试,请谨慎使用
## 引入 x509 依赖
## 添加依赖
def deps do
[
Expand All @@ -14,7 +12,7 @@ defmodule WeChat.Pay do
]
end
## 定义 Client 模块
## 定义支付 Client 模块
defmodule YourApp.WeChatAppCodeName do
@moduledoc "CodeName"
Expand All @@ -30,7 +28,7 @@ defmodule WeChat.Pay do
## V2 SSL 配置
使用到 v2 的接口,如:撤销订单 需要配置证书
部分 v2 的接口请求时需要用到证书,如:撤销订单,因此如果有使用到这部分接口,必须添加下面的配置
config :wechat, YourApp.WeChatAppCodeName,
v2_ssl: [
Expand Down
69 changes: 64 additions & 5 deletions lib/wechat/pay/transactions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,7 @@ defmodule WeChat.Pay.Transactions do
timestamp = WeChat.Utils.now_unix() |> to_string()
nonce_str = :crypto.strong_rand_bytes(24) |> Base.encode64()
package = "prepay_id=#{prepay_id}"

sign =
"#{appid}\n#{timestamp}\n#{nonce_str}\n#{package}\n"
|> :public_key.sign(:sha256, client.private_key())
|> Base.encode64()
sign = request_sign(client, appid, timestamp, nonce_str, package)

%{
"appId" => appid,
Expand All @@ -97,6 +93,69 @@ defmodule WeChat.Pay.Transactions do
}
end

defp request_sign(client, appid, timestamp, nonce_str, package) do
"#{appid}\n#{timestamp}\n#{nonce_str}\n#{package}\n"
|> :public_key.sign(:sha256, client.private_key())
|> Base.encode64()
end

@doc """
APP 下单 -
[官方文档](#{pay_doc_link_prefix()}/merchant/apis/in-app-payment/direct-jsons/app-prepay.html){:target="_blank"}
商户系统先调用该接口在微信支付服务后台生成预支付交易单,返回正确的预支付交易会话标识后再按Native、JSAPI、APP等不同场景生成交易串调起支付
"""
@spec app(Pay.client(), body) :: WeChat.response()
def app(client, body) do
client.post("/v3/pay/transactions/app", body)
end

@doc """
APP 调起支付 -
[官方文档](#{pay_doc_link_prefix()}/merchant/apis/in-app-payment/app-transfer-payment.html){:target="_blank"}
通过App下单接口获取到发起支付的必要参数prepay_id,可以按照接口定义中的规则,使用微信支付提供的SDK调起App支付
"""
@spec app_request_payment_args(Pay.client(), WeChat.appid(), prepay_id) :: map
def app_request_payment_args(client, appid, prepay_id) do
timestamp = WeChat.Utils.now_unix() |> to_string()
nonce_str = :crypto.strong_rand_bytes(24) |> Base.encode64()
sign = request_sign(client, appid, timestamp, nonce_str, prepay_id)

%{
"appid" => appid,
"partnerid" => client.mch_id(),
"prepayid" => prepay_id,
"package" => "Sign=WXPay",
"noncestr" => nonce_str,
"timestamp" => timestamp,
"sign" => sign
}
end

@doc """
H5 下单 -
[官方文档](#{pay_doc_link_prefix()}/merchant/apis/h5-payment/direct-jsons/h5-prepay.html){:target="_blank"}
商户系统先调用该接口在微信支付服务后台生成预支付交易单,返回正确的预支付交易会话标识后再按Native、JSAPI、APP等不同场景生成交易串调起支付
"""
@spec h5(Pay.client(), body) :: WeChat.response()
def h5(client, body) do
client.post("/v3/pay/transactions/h5", body)
end

@doc """
Native 下单 -
[官方文档](#{pay_doc_link_prefix()}/merchant/apis/h5-payment/direct-jsons/h5-prepay.html){:target="_blank"}
通过本接口来生成支付链接参数code_url,然后将该参数值生成二维码图片展示给用户。
用户在使用微信客户端扫描二维码后,可以直接跳转到微信支付页面完成支付操作
"""
@spec native(Pay.client(), body) :: WeChat.response()
def native(client, body) do
client.post("/v3/pay/transactions/native", body)
end

@spec query_by_out_trade_no(Pay.client(), out_trade_no) :: WeChat.response()
def query_by_out_trade_no(client, out_trade_no) do
client.get(
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule WeChat.MixProject do
use Mix.Project
alias WeChat.{Requester, Refresher, ServerMessage, Storage, MiniProgram, Work, Pay}

@version "0.13.9"
@version "0.14.0"
@source_url "https://github.com/feng19/wechat"

def project do
Expand Down

0 comments on commit fc05697

Please sign in to comment.