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.paywap_pay- alipay.trade.wap.payapp_pay- alipay.trade.app.payquery- alipay.trade.queryrefund- alipay.trade.refundclose- alipay.trade.closerefund_query- alipay.trade.fastpay.refund.querybill_downloadurl_query- alipay.data.dataservice.bill.downloadurl.queryauth_token- alipay.system.oauth.tokenuser_info- alipay.user.info.sharetransfer- alipay.fund.trans.toaccount.transfertransfer_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 section Functions
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",
})
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",
})
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"
})
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",
})
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",
})
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
})
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",
})
Perform the request to alipay backend.
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",
})
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",
})
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",
})
verify_notify_sign?(client, body)
View Sourceverify_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/