Skip to content

Instantly share code, notes, and snippets.

@imetallica
Created January 22, 2016 23:00
Show Gist options
  • Save imetallica/2d0b20b58f807d44ac63 to your computer and use it in GitHub Desktop.
Save imetallica/2d0b20b58f807d44ac63 to your computer and use it in GitHub Desktop.
defmodule PayPal.Transaction do
@moduledoc """
A Transaction structure. All *fields* are required:
- description: String. (Give a description to the transaction.)
- *ammount*:
- *currency*: String. (A 3-letter currency code. All supported
currencies are listed below.)
- *total*: String. (Total to be charged. 10 characters max with
support for 2 decimal places.)
- details:
- shipping: String. (Amount charged for shipping. 10 characters
max with support for 2 decimal places.)
- subtotal: String. (Amount of the subtotal of the items.
Required if line items are specified. 10
characters max, with support for 2 decimal
places.)
- tax: String. (Amount charged for tax. 10 characters max
with support for 2 decimal places.)
## Supported currencies Currency Code
Australian dollar AUD
Brazilian real** BRL
Canadian dollar CAD
Czech koruna CZK
Danish krone DKK
Euro EUR
Hong Kong dollar HKD
Hungarian forint* HUF
Israeli new shekel ILS
Japanese yen* JPY
Malaysian ringgit** MYR
Mexican peso MXN
New Taiwan dollar* TWD
New Zealand dollar NZD
Norwegian krone NOK
Philippine peso PHP
Polish złoty PLN
Pound sterling GBP
Russian ruble RUB
Singapore dollar SGD
Swedish krona SEK
Swiss franc CHF
Thai baht THB
Turkish lira** TRY
United States dollar USD
* This currency does not support decimals. Passing a decimal amount will
result in an error.
** This currency is supported as a payment currency and a currency balance
for in-country PayPal accounts only.
"""
@derive [Poison.Encoder]
defstruct [description: "", ammount: [total: "", currency: "USD", details: [shipping: "", subtotal: "", tax: ""]]]
end
## TRY THIS ON iex
Poison.encode! %PayPal.Transaction{description: "yo"}
@imetallica
Copy link
Author

The error

(Poison.EncodeError) unable to encode value: {:details, [shipping: "", subtotal: "", tax: ""]}
    lib/poison/encoder.ex:339: Poison.Encoder.Any.encode/2
    lib/poison/encoder.ex:232: anonymous fn/3 in Poison.Encoder.List.encode/3
    lib/poison/encoder.ex:233: Poison.Encoder.List."-encode/3-lists^foldr/2-1-"/3
    lib/poison/encoder.ex:233: Poison.Encoder.List.encode/3
    lib/poison/encoder.ex:213: anonymous fn/4 in Poison.Encoder.Map.encode/3
    lib/poison/encoder.ex:214: Poison.Encoder.Map."-encode/3-lists^foldl/2-0-"/3
    lib/poison/encoder.ex:214: Poison.Encoder.Map.encode/3
    lib/poison.ex:41: Poison.encode!/2

@davoclavo
Copy link

Perhaps you need to do something like this:

defmodule PayPal.Transaction.Amount.Details do
  defstruct shipping: "", subtotal: "", tax: ""
end

defmodule PayPal.Transaction.Amount do
  defstruct total: "", currency: "USD", details: %PayPal.Transaction.Amount.Details{}
end

defmodule PayPal.Transaction do
  @derive [Poison.Encoder]
  defstruct description: "", amount: %PayPal.Transaction.Amount{}
end

Poison.encode! %PayPal.Transaction{description: "yo"}

@davoclavo
Copy link

Related issue in Poison: devinus/poison#32

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment