Skip to content

Instantly share code, notes, and snippets.

@shahryarjb
Created December 9, 2019 09:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shahryarjb/ed055433a652bba1a081e0c1d1bc982a to your computer and use it in GitHub Desktop.
Save shahryarjb/ed055433a652bba1a081e0c1d1bc982a to your computer and use it in GitHub Desktop.
defmodule BankError.Extera.BankProvider.Zarinpal do
def get_token(type) do
sms_body = Jason.encode!(
%{
"MerchantID" => "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"Amount" => "1000",
"CallbackURL" => "https://YORSITE.com/back-pay/zarinpal",
"Description" => type
}
)
send_info = HTTPoison.post "https://sandbox.zarinpal.com/pg/rest/WebGate/PaymentRequest.json",
sms_body,
[
{"Content-Type", "application/json"},
]
zarinpal_status(send_info)
end
def get_token_extended(type) do
sms_body = Jason.encode!(
%{
"MerchantID" => "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"Amount" => "1000",
"CallbackURL" => "https://YORSITE.com/back-extended/zarinpal",
"Description" => type
}
)
send_info = HTTPoison.post "https://sandbox.zarinpal.com/pg/rest/WebGate/PaymentRequest.json",
sms_body,
[
{"Content-Type", "application/json"},
]
zarinpal_status(send_info)
end
def validate(zarinpal_token) do
sms_body = Jason.encode!(
%{
"MerchantID" => "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"Amount" => "1000",
"Authority" => "#{zarinpal_token}"
}
)
send_info = HTTPoison.post "https://sandbox.zarinpal.com/pg/rest/WebGate/PaymentVerification.json",
sms_body,
[
{"Content-Type", "application/json"},
]
zarinpal_status(send_info)
end
def zarinpal_status(req_http) do
case req_http do
{:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
{:ok, :zarinpal_status, Jason.decode!(body)}
{:ok, %HTTPoison.Response{status_code: 404}} ->
{:error, :zarinpal_status_status, 404}
{:error, %HTTPoison.Error{id: nil, reason: :timeout}} ->
{:error, :zarinpal_status_status, :timeout}
{:error, %HTTPoison.Error{reason: _reason}} ->
{:error, :zarinpal_status_status, :unknow_error}
_ ->
{:error, :zarinpal_status_status, :unknow_error}
end
end
def zarinpal_check(authority, cach_authority) do
if authority == cach_authority, do: {:ok, :zarinpal_check}, else: {:error, :zarinpal_check}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment