ExAlipay v0.1.2 ExAlipay.Client View Source

ExAlipay Client that export API and perform request to alipay backend.

This module defined some common used api and you can easily add new api:

  • page_pay - alipay.trade.page.pay
  • wap_pay - alipay.trade.wap.pay
  • app_pay - alipay.trade.app.pay
  • query - alipay.trade.query
  • refund - alipay.trade.refund
  • close - alipay.trade.close
  • refund_query - alipay.trade.fastpay.refund.query
  • bill_downloadurl_query - alipay.data.dataservice.bill.downloadurl.query
  • auth_token - alipay.system.oauth.token
  • user_info - alipay.user.info.share
  • transfer - alipay.fund.trans.toaccount.transfer
  • transfer_query - alipay.fund.trans.order.query

Example Usage:

Define a module AlipayClient that use ExAlipay.Client, AlipayClient module will define new functions that calling the same ExAlipay.Client functions, the difference is that it stores the client with module property @client for convenient:

defmodule AlipayClient do
  use ExAlipay.Client, Application.fetch_env!(:my_app, __MODULE__)
end

Config your AlipayClient in config/config.exs:

config :my_app, AlipayClient,
  appid: "APPID",
  pid: "PID",
  public_key: "-- public_key --",
  private_key: "-- private_key --",
  sandbox?: false

Use the page_pay:

AlipayClient.page_pay(%{
  out_trade_no: "out_trade_no",
  total_amount: 100,
  subject: "the subject",
  return_url: "http://example.com/return_url",
  notify_url: "http://example.com/notify_url",
})

In the handler view of alipay notify:

if AlipayClient.verify_notify_sign?(body) do
  # process the payment success logic
  # ...
  # response a plain text `success` to alipay
else
  # response with error
end

Extend new api you need that isn't provided by ExAlipay.Client.

defmodule AlipayClient do
  use ExAlipay.Client, Application.fetch_env!(:my_app, __MODULE__)

  # access the public api request that defined in ExAlipay.Client
  # also possible to use functions in ExAlipay.Utils directly
  # see: https://docs.open.alipay.com/api_1/alipay.trade.precreate
  def pre_create(params) do
    {params, ext_params} = prepare_trade_params(params)
    request(@client, "alipay.trade.precreate", params, ext_params)
  end
end

# now we can use the new api
# AlipayClient.pre_create(%{})

Link to this section Summary

Functions

Get auth string for alipay app auth.

Create trade string for app pay.

Get user auth_token by auth_code.

Get auth url for alipay web auth.

Fetch bill download url.

Close trade.

Create trade url for web page.

Pop return_url and notify_url from create trade params as ext_params.

Query trade info.

Refund trade.

Query refund info.

Perform the request to alipay backend.

Transfer money to users' alipay acoount.

Query transfer order.

Get user info by auth_token.

Verify the sign of alipay notify, used in handler of notify_url.

Create trade url for mobile page.

Link to this section Types

Link to this type

t()

View Source
t() :: %ExAlipay.Client{
  appid: binary(),
  charset: binary(),
  format: binary(),
  pid: binary(),
  private_key: binary(),
  public_key: binary(),
  sandbox?: boolean(),
  sign_type: binary(),
  version: binary()
}

Link to this section Functions

Link to this function

app_auth_str(client, map)

View Source

Get auth string for alipay app auth.

See: https://docs.open.alipay.com/218/105327/

Examples:

ExAlipay.Client.app_auth_str(client, %{
  target_id: "target_id"
})

Create trade string for app pay.

See: https://docs.open.alipay.com/204/105465/

Examples:

ExAlipay.Client.app_pay(client, %{
  out_trade_no: "out_trade_no",
  total_amount: 100,
  subject: "the subject",
  notify_url: "http://example.com/notify_url",
})
Link to this function

auth_token(client, params)

View Source

Get user auth_token by auth_code.

See: https://docs.open.alipay.com/api_9/alipay.system.oauth.token

Examples:

ExAlipay.Client.auth_token(client, %{
  grant_type: "authorization_code",
  code: "an auth_code",
})
Link to this function

auth_url(client, params)

View Source

Get auth url for alipay web auth.

See: https://docs.open.alipay.com/289/105656

Examples:

ExAlipay.Client.auth_url(client, %{
  redirect_uri: http://example.com/auth_redirect_url,
  scope: "auth_user",
  state: "state"
})
Link to this function

bill_downloadurl_query(client, params)

View Source

Fetch bill download url.

See: https://docs.open.alipay.com/api_15/alipay.data.dataservice.bill.downloadurl.query

Examples:

ExAlipay.Client.bill_downloadurl_query(client, %{
  bill_type: "trade",
  bill_date: "2019-06-06",
})

Close trade.

See: https://docs.open.alipay.com/api_1/alipay.trade.close

Examples:

ExAlipay.Client.close(client, %{
  out_trade_no: "out_trade_no",
})
Link to this function

page_pay(client, params)

View Source

Create trade url for web page.

See: https://docs.open.alipay.com/270/alipay.trade.page.pay

Examples:

ExAlipay.Client.page_pay(client, %{
  out_trade_no: "out_trade_no",
  total_amount: 100,
  subject: "the subject",
  return_url: "http://example.com/return_url",
  notify_url: "http://example.com/notify_url",
})
Link to this function

prepare_trade_params(params)

View Source

Pop return_url and notify_url from create trade params as ext_params.

Examples:

params = %{
  out_trade_no: "out_trade_no",
  total_amount: 100,
  subject: "the subject",
  notify_url: "http://example.com/notify_url",
}

ExAlipay.Client.prepare_trade_params(params)
# Result:
# {
#   %{out_trade_no: "out_trade_no", subject: "the subject", total_amount: 100},
#   %{notify_url: "http://example.com/notify_url", return_url: nil}
# }

Query trade info.

See: https://docs.open.alipay.com/api_1/alipay.trade.query

Examples:

ExAlipay.Client.query(client, %{
  out_trade_no: "out_trade_no",
})

Refund trade.

See: https://docs.open.alipay.com/api_1/alipay.trade.refund

Examples:

ExAlipay.Client.refund(client, %{
  out_trade_no: "out_trade_no",
  refund_amount: 100
})
Link to this function

refund_query(client, params)

View Source

Query refund info.

See: https://docs.open.alipay.com/api_1/alipay.trade.fastpay.refund.query

Examples:

ExAlipay.Client.refund_query(client, %{
  out_trade_no: "out_trade_no",
  out_request_no: "out_request_no",
})
Link to this function

request(client, method, content, ext_params \\ %{})

View Source
request(
  %ExAlipay.Client{
    appid: term(),
    charset: term(),
    format: term(),
    pid: term(),
    private_key: term(),
    public_key: term(),
    sandbox?: term(),
    sign_type: term(),
    version: term()
  },
  binary(),
  map(),
  map()
) :: map()

Perform the request to alipay backend.

Link to this function

transfer(client, params)

View Source

Transfer money to users' alipay acoount.

See: https://docs.open.alipay.com/api_28/alipay.fund.trans.toaccount.transfer

Examples:

ExAlipay.Client.transfer(client, %{
  payee_account: "an alipay account",
  out_biz_no: "an out_biz_no",
  amount: 100,
  payee_type: "ALIPAY_LOGONID",
})
Link to this function

transfer_query(client, params)

View Source

Query transfer order.

See: https://docs.open.alipay.com/api_28/alipay.fund.trans.order.query

Examples:

ExAlipay.Client.transfer_query(client, %{
  out_biz_no: "an out_biz_no",
})
Link to this function

user_info(client, params)

View Source

Get user info by auth_token.

See: https://docs.open.alipay.com/api_2/alipay.user.info.share

Examples:

ExAlipay.Client.user_info(client, %{
  auth_token: "an auth_token",
})
Link to this function

verify_notify_sign?(client, body)

View Source
verify_notify_sign?(ExAlipay.Client.t(), Map.t()) :: boolean()

Verify the sign of alipay notify, used in handler of notify_url.

Create trade url for mobile page.

See: https://docs.open.alipay.com/203/107090/