-
Notifications
You must be signed in to change notification settings - Fork 12
/
get_sandbox_signkey.ex
56 lines (44 loc) · 1.18 KB
/
get_sandbox_signkey.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
defmodule Mix.Tasks.WechatPay.GetSandboxSignkey do
@moduledoc """
Get WechatPay's Sandbox API Key.
## Example
```shell
$ mix wechat_pay.get_sandbox_signkey -a wx8888888888888888 -m 1900000109
af11b9e92929e3f6d3847d7a291d5f64
```
## Command line options
- `-a`, `--api_key` - the production API key
- `-m`, `--mch_id` - the merchant ID
"""
use Mix.Task
@shortdoc "Get WechatPay's Sandbox API Key"
@impl Mix.Task
@spec run([binary()]) :: any()
def run(args) do
{:ok, _} = Application.ensure_all_started(:wechat_pay)
{opts, _} =
args
|> OptionParser.parse!(
aliases: [a: :api_key, m: :mch_id],
strict: [api_key: :string, mch_id: :string]
)
case WechatPay.Helper.get_sandbox_signkey(
get_value(opts, :api_key),
get_value(opts, :mch_id)
) do
{:ok, key} ->
Mix.shell().info(inspect(key))
{:error, any} ->
Mix.shell().error(inspect(any))
end
end
defp get_value(opts, key) do
case Keyword.get(opts, key) do
nil ->
Mix.shell().error("Please specific the #{key} with `--#{key}`")
exit({:shutdown, 1})
v ->
v
end
end
end