Paymongo Elixir v1.2.0 PaymongoElixir View Source

Documentation for PaymongoElixir. for further references visit paymongo api references

Link to this section Summary

Functions

get request

Generates list of payments

Post requests

Link to this section Functions

get request

Pass argument request and id

request can be:

  • :retrieve_payment retrieves a payment + id with format pay_123
  • :retrieve_payment_intent retrieves a payment intent + id with format pi_123
  • :retrieve_payment_method retrieves a payment method + id with format pm_123

if none of the above request was called, an {:error, :request_not_found} will be returned.

Example: retrieve a payment

iex> id = "pay_123"
iex> PaymongoElixir.get(:retrieve_payment, id)
%{
  "data" => %{
    "id" => pay_id,
    "type" => "payment",
    "attributes" => %{
      "status" => "paid"
      ...
    }
  }
} 

Example: retrieve a payment intent

iex> id = "pi_123"
iex> PaymongoElixir.get(:retrieve_payment_intent, id)
%{
  "data" => %{
    "id" => "pi_123", 
    "type" => "payment_intent",
    "attributes" => %{
      "amount" => 10000,
      "status" => "awaiting_payment_method",
    }
  }
} 

Example: retrieve payment method

iex> id = "pm_123"
iex> PaymongoElixir.get(:retrieve_payment_method, id)
%{
  "data" => %{
    "id" => "pm_123",
    "type" => "payment_method",
    "attributes" => %{
      "livemode" => false,
      "type" => "card",
      "billing" => nil,
      "details" => %{
        "last4" => "4345",
        "exp_month" => 12,
        "exp_year" => 2030,
        "cvc" => "123"
      }
    }
  }
}

Generates list of payments

Pass an argument of :list_payments to generate the lists.

Examples

iex> PaymongoElixir.list(:list_payments)
%{
  "data" => [
    %{
      "id" => id,
      "type" => "payment",
      "attributes" => %{
        ...
        "status" => "paid"
      }
    }
  ]
}

Post requests

Pass arguments request + params. You can check out paymongo docs for the parameters.

request can be:

  • :create_source = creates a source. source can be gcash or grab_pay.
  • :create_payment_source = creates a payment for a source. source needs to be chargeable in order for you to get the payment.
  • :cancel_payment_intent = cancels a payment intent.
  • :attach_payment_intent = attaches a payment method to a payment intent.
  • :create_payment_intent = creates a payment intent.
  • :create_payment_method = creates a payment method.

if none of the above request was called, an {:error, :request_not_found} will be returned.

Example: create a source

iex> params = %{
...>         "data" => %{
...>           "attributes" => %{
...>             "type" => "gcash",
...>             "amount" => 10_000,
...>             "currency" => "PHP",
...>             "redirect" => %{
...>               "success" => "https://localhost:4001/gcash",
...>               "failed" => "https://localhost:4001/gcash"
...>             }
...>           }
...>         }
...>       }
iex> PaymongoElixir.post(:create_source, params)
%{
  "data" => %{
    "id" => "src_123",
    "type" => "source",
    "attributes" => %{
      "amount" => 10_000,
      "billing" => nil,
      "currency" => "PHP",
      "livemode" => false,
      "redirect" => %{
        "success" => "https://localhost:4001/gcash",
        "failed" => "https://localhost:4001/gcash"
      },
      "status" => "pending",
      "type" => "gcash"
    }
  }
}

Example: create a payment source

iex> source_id = "src_2RyFqt9C1TD5iuRAytzT41eg"
iex> params = %{
...>  "data" => %{
...>    "attributes" => %{
...>      "amount" => 10_000,
...>      "currency" => "PHP",
...>      "source" => %{
...>        "id" => source_id,
...>        "type" => "source"
...>      }
...>    }
...>  }
...> }
iex> PaymongoElixir.post(:create_payment_source, params)
%{
  "data" => %{
    "attributes" => %{
      "access_url" => nil,
      "amount" => 10_000,
      "billing" => nil,
      "currency" => "PHP",
      "description" => nil,
      "external_reference_number" => nil,
      "fee" => 290,
      "livemode" => false,
      "status" => "paid",
    },
    "id" => "pay_123",
    "type" => "payment"
  }
}

Example: cancel payment intent

iex> payment_intent_id = "pi_123"
iex> PaymongoElixir.post(:cancel_payment_intent, payment_intent_id)
%{
  "data" => %{
    "attributes" => %{
      "amount" => 10_000,
      "currency" => "PHP",
      "description" => nil,
      "last_payment_error" => nil,
      "livemode" => false,
      "metadata" => nil,
      "next_action" => nil,
      "payment_method_allowed" => ["card"],
      "payments" => [],
      "statement_descriptor" => nil,
      "status" => "cancelled",
    },
    "id" => "pi_123",
    "type" => "payment_intent"
  }
}

Example: attach a payment method to a payment intent

iex> client_key = "client_key_123"
iex> payment_intent_id = "pi_123"
iex> payment_method_id = "pm_123"
iex> query_params = %{"id" => payment_intent_id, "client_key" => client_key}
iex> body_params =  %{
...>  "data" => %{
...>    "attributes" => %{
...>      "payment_method" => payment_method_id
...>    }
...>  }
...>}
iex>  params = %{
...>  "query_params" => query_params,
...>  "body_params" => body_params
...>}
iex> PaymongoElixir.post(:attach_payment_intent, params)
%{
  "data" => %{
    "attributes" => %{
      "amount" => 10_000,
      "status" => "succeeded",
      "livemode" => false,
      "payment_method_allowed" => ["card"],
      "payments" => [
        %{
          "attributes" => %{
            "amount" => 10_000,
            "billing" => nil,
            "currency" => "PHP",
            "description" => nil,
            "livemode" => false,
            "status" => "paid",
          },
          "id" => "pay_123",
          "type" => "payment"
        }
      ],
      "next_action" => nil,
      "metadata" => nil
      }
    }
  }

Example: create payment intent

iex> params = %{
...>  "data" => %{
...>    "attributes" => %{
...>      "amount" => 10_000,
...>      "payment_method_allowed" => ["card"],
...>      "currency" => "PHP"
...>    }
...>  }
...>}
iex> PaymongoElixir.post(:create_payment_intent, params)
%{
  "data" => %{
    "attributes" => %{
      "amount" => 10_000,
      "currency" => "PHP",
      "description" => nil,
      "statement_descriptor" => nil,
      "status" => "awaiting_payment_method",
      "livemode" => false,
      "payment_method_allowed" => ["card"],
      "payments" => [],
      "next_action" => nil,
      "metadata" => nil
    }
  }
}

Example: create payment method

iex> params = %{
...>  "data" => %{
...>    "attributes" => %{
...>      "type" => "card",
...>      "details" => %{
...>        "card_number" => "4343434343434345",
...>        "exp_month" => 12,
...>        "exp_year" => 2030,
...>        "cvc" => "123"
...>      }
...>    }
...>  }
...>}
iex> PaymongoElixir.post(:create_payment_method, params)
%{
  "data" => %{
    "id" => "pm_123",
    "type" => "payment_method",
    "attributes" => %{
      "livemode" => false,
      "type" => "card",
      "billing" => nil,
      "details" => %{
        "last4" => "4345",
        "exp_month" => 12,
        "exp_year" => 2030,
        "cvc" => "123"
      }
    }
  }
}