README.md

# PayJP for Elixir

[![Travis](https://img.shields.io/travis/shufo/payjp-elixir.svg)](https://travis-ci.org/shufo/payjp-elixir)
[![Hex.pm](https://img.shields.io/hexpm/v/payjp.svg)](https://hex.pm/packages/payjp)
[![Hex Docs](https://img.shields.io/badge/hex-docs-9768d1.svg)](https://hexdocs.pm/payjp)
[![Coverage Status](https://coveralls.io/repos/github/shufo/payjp-elixir/badge.svg?branch=master)](https://coveralls.io/github/shufo/payjp-elixir?branch=master)

An Elixir library for working with [PAY.JP](https://pay.jp/).

Features:

* manage accounts (your own)
* manage customers
* manage subscriptions
* manage plans
* manage tokens for credit card
* list and retrieve events
* manage/capture charges with or without an existing Customer

### TODO

- [ ] Transfer
- [ ] OAuth

## Installation

Install the dependency:

```elixir
def deps do
  [{:payjp, "~> 0.1.0"}]
end
```

Next, add to your applications:

```ex
defp application do
  [applications: [:payjp]]
end
```

## Configuration

To make API calls, it is necessary to configure your PAY.JP secret key.

```elixir
use Mix.Config

config :payjp, secret_key: "YOUR SECRET KEY"
```

or add secret key to the environment variables

```bash
export PAYJP_SECRET_KEY="yourkey"
```

## Usage

### Token

- create a token

```elixir
params = [
  card: [
    number: "4242424242424242",
    exp_month: 12,
    exp_year: 2020,
    cvc: "123"
  ]
]

Payjp.Tokens.create(params)
```

### Customer

- create a customer with default card

```elixir
customer = [
  email: "test@test.com",
  description: "An Elixir Test Account",
  metadata: [
    app_attr1: "xyz"
  ],
  card: [
    number: "4242424242424242",
    exp_month: 01,
    exp_year: 2020,
    cvc: 123,
    name: "Joe Test User"
  ]
]

Payjp.Customers.create(customer)
```

### Card

- create a card for customer

```elixir
new_card = [
  number: "4242424242424242",
  cvc: 123,
  exp_month: 12,
  exp_year: 2020
]

Payjp.Cards.create :customer, customer.id, new_card
```

### Charge

- create a charge

with card information.

```elixir
params = [
  card: [
    number: "4242424242424242",
    exp_month: 12,
    exp_year: 2020,
    cvc: "123"
  ]
]

Payjp.Charges.create(1000, params)
```

with token

```elixir
params = [
  card: [
    number: "4242424242424242",
    exp_month: 12,
    exp_year: 2020,
    cvc: "123"
  ]
]

{:ok, token} = Payjp.Tokens.create(params)
Payjp.Charges.create(1000, card: token.id)
```

with customer id

```elixir
{:ok, customer} = Payjp.Customers.create(customer)
Payjp.Charges.create(1000, customer: customer.id)
```

more usage is available at [Documentation](https://hexdocs.pm/payjp/)

## Options

You can override the request configuration with HTTPoison options.

`config.exs`

```elixir
config :payjp,
  httpoison_options: [
    timeout: 60000, # default: 30000
    recv_timeout: 100000, # default: 80000
  ]
```

## Testing
If you start contributing and you want to run mix test, first you need to export PAYJP_SECRET_KEY environment variable in the same shell as the one you will be running mix test in.

```bash
export PAYJP_SECRET_KEY="yourkey"
mix test
```

## Contributing

1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request

## License

MIT