defmodule Juspay.Orders do import Juspay import Juspay.Exception @moduledoc """ Represents an order placed for your service. """ @doc """ Create an order that is a representation of your shopping cart. Order contains important information like amount, customer details, shipping address, billing address etc. Payment will be initiated on the order. Note: You can also send custom data in the following fields: udf1, udf2, …, udf10. ## Examples # input in the form of map iex> Juspay.Orders.create(%{:order_id => "142315694476", :amount => 90.30,:currency => "INR",:customer_id => "guest", :customer_email => "custome32@mail.com", :customer_phone => "9985115522",:product_id => "prod-141833", :return_url => "http://shop.merchant.com/pay/handleResponse",:description => "Sample description", :billing_address_first_name => "firstname",:billing_address_last_name => "lastname", :billing_address_line1 => "Line1", :billing_address_line2 => "Line2", :billing_address_line3 => "Line3", :billing_address_city => "Chennai", :billing_address_state => "TamilNadu", :billing_address_country => "India", :billing_address_postal_code => "600020", :billing_address_phone => "9988775566", :billing_address_country_code_iso => "IND", :shipping_address_first_name => "firstname", :shipping_address_last_name => "lastname", :shipping_address_line1 => "Line1", :shipping_address_line2 => "Line2",shipping_address_line3 => "Line3", :shipping_address_city => "Chennai", :shipping_address_state => "TamilNadu", :shipping_address_postal_code => "600020", :shipping_address_phone => "9962881912", :shipping_address_country_code_iso => "IND", :shipping_address_country => "Ind" }) # response %{ "amount" => 90.3, "amount_refunded" => 0, "currency" => "INR", "customer_email" => "custome32@mail.com", "customer_id" => "guest", "customer_phone" => "9985115522", "merchant_id" => "Srene", "order_id" => "142315694476", "product_id" => "", "refunded" => false, "return_url" => "http://shop.merchant.com/payments/handleResponse", "status" => "NEW", "status_id" => 10, "udf1" => "", "udf10" => "", "udf2" => "", "udf3" => "", "udf4" => "", "udf5" => "", "udf6" => "", "udf7" => "", "udf8" => "", "udf9" => "" } """ @spec create(list) :: map() @spec create(map()) :: map() def create(parameters) do parameters = check_params(parameters) order = parameters[:order_id] if order == nil do raise ArgumentError,Juspay.Exception.invalidArgumentError(" Error -> order_id is a required field for Juspay.Orders.create()") end method = "POST" url = "/order/create" response = request(method,url,parameters) end @doc """ Retreive the information associated with a perticular order_id ## Examples # input in the form of list iex> Juspay.Orders.status(["order_id": "145678234" }) # input in the form of map (works with both string keyed map and atom keyed map) iex> Juspay.Orders.status(%{ "order_id" => "145678234" }) or Juspay.Orders.status(%{ :order_id => "145678234" }) # response %{ "status" => "NEW", "amount_refunded" => 0, "status_id" => 10, "order_id" => "145678234", "refunded" => false, "currency" => "INR", "amount" => 1000, "merchant_id" => "shreyas" } """ @spec status(list) :: map() @spec status(map()) :: map() def status(parameters) do parameters = check_params(parameters) order =parameters[:order_id] if order == nil do raise ArgumentError,Juspay.Exception.invalidArgumentError(" Error -> order_id is a required field for Juspay.Orders.status()") end method = "GET" url = "/order/status" response = request(method,url,parameters) end @doc """ List the orders that were created. By default, the orders are listed in the reverse chronological order (starting from the order that was most recently created) ## Examples # Example input request iex> Juspay.Orders.list() # response %{ "count" => 10, "list" => [ %{"amount" => 90, "amount_refunded" => 0, "currency" => "INR", "customer_email" => "custovmer@mail.com", "customer_id" => "guest_101", "customer_phone" => "9908665522", "merchant_id" => "Srene", "order_id" => "1418304477", "product_id" => "", "refunded" => false, "return_url" => "http://shop.merchant.com/payments/handleResponse", "status" => "NEW", "status_id" => 10, "udf1" => "", "udf10" => "", "udf2" => "", "udf3" => "", "udf4" => "", "udf5" => "", "udf6" => "", "udf7" => "", "udf8" => "", "udf9" => ""}, %{"amount" => 90, "amount_refunded" => 0, "currency" => "INR", "customer_email" => "custovmer@mail.com", "customer_id" => "guest_101", "customer_phone" => "9908665522", "merchant_id" => "Srene", "order_id" => "1418304471", "product_id" => "", "refunded" => false, "return_url" => "http://shop.merchant.com/payments/handleResponse", "status" => "NEW", "status_id" => 10, "udf1" => "", "udf10" => "", "udf2" => "", "udf3" => "", "udf4" => "", "udf5" => "", "udf6" => "", "udf7" => "", "udf8" => "", "udf9" => ""}, .... ] } """ # @spec list() :: list # @spec list() :: list def list() do method = "GET" url = "/order/list" 1 # response = request(method,url,[]) end @doc """ Update an order that has already been created. Only amount, address and udf fields can be updated. Address fields can be optionally sent as explained in the /order/create API. ## Examples # input in the form of list iex> Juspay.Orders.update( [order_id: "142315694476", amount: 100.30]) # input in the form of map (works with both string keyed map and atom keyed map) iex> Juspay.Orders.update(%{ "order_id" => "142315694476", "amount" => 100.30 }) %{ "amount" => 100.3, "amount_refunded" => 0, "currency" => "INR", "customer_email" => "custome32@mail.com", "customer_id" => "guest", "customer_phone" => "9985115522", "merchant_id" => "Srene", "order_id" => "142315694476", "product_id" => "", "refunded" => false, "return_url" => "http://shop.merchant.com/payments/handleResponse", "status" => "NEW", "status_id" => 10, "udf1" => "", "udf10" => "", "udf2" => "", "udf3" => "", "udf4" => "", "udf5" => "", "udf6" => "", "udf7" => "", "udf8" => "", "udf9" => "" } """ @spec update(list | map()) :: map() def update(parameters) do parameters = check_params(parameters) order =parameters[:order_id] if order == nil do raise ArgumentError,Juspay.Exception.invalidArgumentError(" Error -> order_id is a required field for Juspay.Orders.status()") end method = "POST" url = "/order/create" response = request(method, url,parameters) end @doc """ Create a refund for a payment. ## Examples # input in the form of list iex> Juspay.Orders.refund([unique_request_id: '99', order_id: '1465833326', amount: 10]) # input in the form of map (works with both string keyed map and atom keyed map) iex> Juspay.Orders.refund(%{ :unique_request_id => '99', :order_id => '1465833326', :amount => 10}) # response %{ "gateway_id" => 100, "status" => "CHARGED", "customer_email" => "user@gmail.com", "txn_id" => "txn-1465833326-1", "refunds" => [ %{ "status" => "SUCCESS", "amount" => 10, "created" => "2016-06-13T15:19:34Z" }, ], "amount_refunded" => 10, "order_id" => "1465833326", "refunded" => false, "status_id" => 21, "bank_error_code" => "0", "bank_error_message" => "Payment Successful", "gateway_response" => %{ "epg_txn_id" => "1465831091168", "auth_id_code" => "834050", "resp_code" => "0", "txn_id" => "txn-1465833326-1", "resp_message" => "Payment Successful", }, "currency" => "INR", "amount" => 1000, "merchant_id" => "shop", "customer_id" => "shop_user_101", "card" => %{ "isin" => "424242", "brand" => "VISA", "type" =>"CREDIT", "fingerprint" => "3rfeetv0blcc6bkroo4rvkjk62", } } """ @spec refund(list|map()) :: map() def refund(parameters) do parameters = check_params(parameters) order =parameters[:order_id] unique_request_id = parameters[:unique_request_id] if order == nil or unique_request_id == nil do raise ArgumentError,Juspay.Exception.invalidArgumentError(" Error -> order_id & unique_request_id is a required field for Juspay.Orders.refund()") end method = "POST" url = "/order/refund" response = request(method, url,parameters) end def test do "hi..." end end