Skip to content

Commit

Permalink
feat: change return value of AlipayKit.V3.verify_response/2
Browse files Browse the repository at this point in the history
  • Loading branch information
c4710n committed Oct 5, 2024
1 parent 5060529 commit 7090120
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Changelog

## 0.3.0-dev

- change return value of `AlipayKit.V3.verify_response/2` from `{:ok, map()} | {:error, :bad_response | :bad_signature}` to `:ok | {:error, :bad_response | :bad_signature}`
9 changes: 2 additions & 7 deletions lib/alipay_kit/v3.ex
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ defmodule AlipayKit.V3 do
"""
@spec verify_response(HTTPSpec.Response.t(), verify_response_opts()) ::
{:ok, map()} | {:error, :bad_response | :bad_signature}
:ok | {:error, :bad_response | :bad_signature}
def verify_response(%HTTPSpec.Response{} = response, opts) do
opts = NimbleOptions.validate!(opts, @verify_response_opts_definition)
alipay_public_key = Keyword.fetch!(opts, :alipay_public_key)
Expand All @@ -182,7 +182,7 @@ defmodule AlipayKit.V3 do
)

if verify?(sign_type, string_to_sign, alipay_public_key, signature),
do: {:ok, json_decode!(response.body)},
do: :ok,
else: {:error, :bad_signature}
else
_ ->
Expand Down Expand Up @@ -265,11 +265,6 @@ defmodule AlipayKit.V3 do
:crypto.strong_rand_bytes(16) |> Base.encode16(case: :lower)
end

defp json_decode!(binary) when is_binary(binary) do
{:ok, term} = JXON.decode(binary)
term
end

defp timestamp do
DateTime.utc_now() |> DateTime.to_unix(:millisecond)
end
Expand Down
8 changes: 6 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule AlipayKit.MixProject do
@version "0.2.0"
@description "A kit for Alipay."
@source_url "https://github.com/cozy-elixir/alipay_kit"
@changelog_url "https://github.com/cozy-elixir/alipay_kit/blob/v#{@version}/CHANGELOG.md"

def project do
[
Expand Down Expand Up @@ -46,7 +47,7 @@ defmodule AlipayKit.MixProject do

defp docs do
[
extras: ["README.md"],
extras: ["README.md", "CHANGELOG.md"],
source_url: @source_url,
source_ref: "v#{@version}"
]
Expand All @@ -55,7 +56,10 @@ defmodule AlipayKit.MixProject do
defp package do
[
licenses: ["Apache-2.0"],
links: %{GitHub: @source_url}
links: %{
GitHub: @source_url,
Changelog: @changelog_url
}
]
end

Expand Down
7 changes: 4 additions & 3 deletions test/alipay_kit/v3_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ defmodule AlipayKit.V3Test do
]
}

assert :ok = V3.verify_response(response, alipay_public_key: alipay_public_key)

assert {:ok,
%{
"buyer_pay_amount" => "0.00",
Expand All @@ -108,8 +110,7 @@ defmodule AlipayKit.V3Test do
"out_trade_no" => "70501111111S001111120",
"point_amount" => "0.00",
"receipt_amount" => "0.00"
}} =
V3.verify_response(response, alipay_public_key: alipay_public_key)
}} = JXON.decode(response.body)
end
end

Expand Down Expand Up @@ -155,7 +156,7 @@ defmodule AlipayKit.V3Test do
headers: headers
)

assert {:ok, _} = V3.verify_response(response, alipay_public_key: alipay_public_key)
assert :ok = V3.verify_response(response, alipay_public_key: alipay_public_key)
end
end

Expand Down

0 comments on commit 7090120

Please sign in to comment.