Packages

Elixir bindings for the Juspay Express Checkout API's

Current section

Files

Jump to
juspay_api_library lib elixir_lib Payments.ex
Raw

lib/elixir_lib/Payments.ex

defmodule Juspay.Payments do
import Juspay
import Juspay.Exception
@moduledoc """
Represents the payment for a particular order through card, netbanking or wallet.
"""
@doc """
Create a payment transaction for Credit or Debit card.
## Note:
For MAESTRO cards that do not have expiry & CVV, send 12/49 as expiry and 111 as CVV.
## Examples
# A regular card transaction
Juspay.Payments.create_card_payment({
:order_id => '1465893617',
:merchant_id => 'guest',
:payment_method_type => 'CARD',
:card_token => '68d6b0c6-6e77-473f-a05c-b460ef983fd8',
:redirect_after_payment => false,
:format => 'json',
:card_number => '5243681100075285',
:name_on_card => 'Customer',
:card_exp_year => '20',
:card_exp_month => '12',
:card_security_code => '123',
:save_to_locker => false
})
# A Stored card transaction
Juspay.Payments.create_card_payment({
:order_id => '1465893617',
:merchant_id => 'guest',
:payment_method_type => 'CARD',
:card_token => '68d6b0c6-6e77-473f-a05c-b460ef983fd8',
:redirect_after_payment => false,
:format => 'json',
:card_security_code => '123',
:save_to_locker => false
})
# response
%{
"order_id" => "1465893617",
"status" => "PENDING_VBV",
"txn_id" => "guest-1465893617-1",
"payment" =>
%{
"authentication" =>
%{
"url" => "https://api.juspay.in/pay/start/guest/8646a7a029974fa99995fec00340a507",
"method" => "GET",
}
,
}
,
}
"""
@spec create_card_payment(list|map()) :: map()
def create_card_payment(parameters) do
parameters = check_params(parameters)
method = 'POST'
url = '/txns'
parameters =parameters++ [ payment_method_type: "CARD", format: 'json' ]
given_params = Enum.map(parameters, fn {k, v} -> k end)
valid_params = [:order_id, :merchant_id, :payment_method_type, :payment_method, :card_token, :card_number, :name_on_card,
:card_exp_year, :card_exp_month, :card_security_code, :save_to_locker, :redirect_after_payment,
:format]
needed = [:card_number, :name_on_card, :card_exp_year,:card_exp_month, :card_security_code, :save_to_locker]
necessary_params = [:order_id, :merchant_id, :redirect_after_payment]
# Either token or card number validation
if !(:card_token in given_params) and (length(needed -- given_params)==0) do
raise Juspay.Exception.invalidArguementError("ERROR: Either [card_token] or [card_number, name_on_card, card_exp_year, card_exp_month, card_security_code, save_to_locker] are required arguments for Juspay.Payments.create_card_payment()")
end
if length(necessary_params -- given_params)>0 do
raise Juspay.Exception.invalidArguementError("ERROR : " <> necessary_params -- given_params <> "is/are required argument(s) for Juspay.Payments.create_card_payment")
end
if length(given_params -- valid_params)>0 do
raise Juspay.Exception.invalidArguementError("ERROR : " <> given_params -- valid_params <> "is/are invalid argument(s) for Juspay.Payments.create_card_payment")
end
if (:save_to_locker in given_params) &&parameters[:save_to_locker] != true and parameters[:save_to_locker] != false do
raise Juspay.Exception.invalidArguementError("ERROR: 'save_to_locker' should be true or false")
end
if parameters[:redirect_after_payment] != true and parameters[:redirect_after_payment] != false do
raise Juspay.Exception.invalidArgumentError("ERROR: 'redirect_after_payment' should be true or false")
end
response = request(method,url,parameters)
end
@doc """
Create a payment transaction for NetBanking. Once you have the response, depending on the “method” attribute,
you will have to take the next step. If you receive GET, then take the value in the “url” attribute and redirect the user to this location.
If you receive POST, then “params” attribute will hold a map containing key value pairs.
## Examples
# Example request
Juspay.Payments.create_net_banking_payment({
:order_id => "1465893617",
:merchant_id => "guest",
:payment_method_type => "NB",
:payment_method => "NB_ICICI",
:redirect_after_payment => false,
:format => "json"
})
# Example response having POST as method
%{
"order_id" => "order_id",
"status" => "PENDING_VBV",
"txn_id" => "txn_id",
"payment" =>
%{
"authentication" =>
%{
"url" => "https://bank-url.com/payment-path/start",
"method" => "POST",
"params" => {
"key1" => "v1",
"key2" => "v2",
"key3" => "v3"
}
}
,
}
,
}
# Example response having GET as method
%{
"order_id" => "1465893617",
"status" => "PENDING_VBV",
"txn_id" => "guest-1465893617-1",
"payment" => %{
"authentication" => %{
"url" => "https://api.juspay.in/pay/start/guest/8646a7a029974fa99995fec00340a507",
"method" => "GET",
}
,
}
,
}
"""
@spec create_net_banking_payment(list|map()) :: map()
def create_net_banking_payment(parameters) do
parameters = check_params(parameters)
method = 'POST'
url = '/txns'
parameters =parameters++ [ payment_method_type: "CARD", format: 'json' ]
given_params = Enum.map(parameters, fn {k, v} -> k end)
valid_params = [:order_id, :merchant_id, :payment_method_type, :payment_method, :redirect_after_payment, :format]
necessary_params = [:order_id, :merchant_id, :payment_method, :redirect_after_payment]
if length(necessary_params -- given_params)>0 do
raise Juspay.Exception.invalidArguementError("ERROR : " <> necessary_params -- given_params <> "is/are required argument(s) for Juspay.Payments.create_netbanking_payment")
end
if length(given_params -- valid_params)>0 do
raise Juspay.Exception.invalidArguementError("ERROR : " <> given_params -- valid_params <> "is/are invalid argument(s) for Juspay.Payments.create_netbanking_payment")
end
response = request(method,url,parameters)
end
@doc """
Refers to either Wallet Payment or Wallet Direct Debit
## WALLET PAYMENT
Create a payment transaction for paying from a prepaid Wallet. Once you have the response, depending on the “method” attribute, you will have to take the next step.
If you receive GET, then take the value in the “url” attribute and redirect the user to this location.
If you receive POST, then “params” attribute will hold a map containing key value pairs.
For Web, you can create a form with these parameters as hidden variables and auto submit the form. Example code in Javascript: "https://gist.github.com/ramanathanrv/31c0b687f0177c37b892".
For Android, serialize this data such that it can be loaded to WebView directly via postData. Example code in Java: "https://gist.github.com/ramanathanrv/59fb412e1c75a1d9398a".
##EXAMPLES
# Example request for Wallet Payment
Juspay.Payments.create_wallet_payment({
:order_id => "1465893617",
:merchant_id => "guest",
:payment_method_type => "WALLET",
:payment_method => "MOBIKWIK",
:redirect_after_payment => false,
:format => 'json'
})
# Example response having POST as method
%{
"order_id" => "order_id",
"status" => "PENDING_VBV",
"txn_id" => "txn_id",
"payment" =>
%{
"authentication" =>
%{
"url" => "https://wallet-url.com/payment-path/start",
"method" => "POST",
"params" => {
"key1" => "v1",
"key2" => "v2",
"key3" => "v3"
}
}
,
}
,
}
# Example response having GET as method
%{
"order_id" => "1465893617",
"status" => "PENDING_VBV",
"txn_id" => "shop-1465893617-2",
"payment" =>
%{
"authentication" =>
%{
"url" => "https://api.juspay.in/pay/start/shop/8646a7a029974fa99995fec00340a507",
"method" => "GET",
}
,
}
,
}
## WALLET DIRECT DEBIT
Directly debit the amount from a Wallet that is linked to a Customer’s account. If the status in the response is CHARGED, then the payment is immediatley successful.
Note: This is an authenticated call unlike other transaction APIs. This API must be invoked from your server directly.
DO NOT send the API Key to client
# Example response having POST as method
%{
"order_id" => "order_id",
"status" => "CHARGED",
"txn_id" => "txn_id",
"payment" =>
%{
"authentication" =>
%{
"url" => "https://api.juspay.in/pay/finish/:merchant-id/:txn-id",
"method" => "POST",
"params" => {
"key1" => "v1",
"key2" => "v2",
"key3" => "v3"
}
}
,
}
,
}
# Example response having GET as method
%{
"order_id" => "1465893617",
"status" => "CHARGED",
"txn_id" => "shop-1465893617-2",
"payment" =>
%{
"authentication" =>
%{
"url" => "https://api.juspay.in/pay/finish/:merchant-id/:txn-id",
"method" => "GET",
}
,
}
,
}
"""
@spec create_wallet_payment(list|map()) :: map()
def create_wallet_payment(parameters) do
parameters = check_params(parameters)
method = 'POST'
url = '/txns'
parameters =parameters++ [ payment_method_type: "WALLET", format: 'json' ]
given_params = Enum.map(parameters, fn {k, v} -> k end)
valid_params = [:order_id, :merchant_id, :payment_method_type, :payment_method, :redirect_after_payment, :format, :direct_wallet_token]
necessary_params = [:order_id, :merchant_id, :payment_method, :redirect_after_payment]
if length(necessary_params -- given_params)>0 do
raise Juspay.Exception.invalidArguementError("ERROR : " <> necessary_params -- given_params <> "is/are required argument(s) for Juspay.Payments.create_wallet_payment")
end
if length(given_params -- valid_params)>0 do
raise Juspay.Exception.invalidArguementError("ERROR : " <> given_params -- valid_params <> "is/are invalid argument(s) for Juspay.Payments.create_wallet_payment")
end
response = request(method,url,parameters)
end
end