Skip to content

Commit

Permalink
Added docs, added more features
Browse files Browse the repository at this point in the history
  • Loading branch information
ravirocx committed Apr 27, 2018
1 parent d69e3aa commit bf788dc
Showing 1 changed file with 115 additions and 93 deletions.
208 changes: 115 additions & 93 deletions lib/gringotts/gateways/we_pay.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,70 +2,42 @@ defmodule Gringotts.Gateways.WePay do
@moduledoc """
[wepay][home] gateway implementation.
## Instructions!
A module for working with the WePay payment gateway.
***This is an example `moduledoc`, and suggests some items that should be
documented in here.***
Refer the official WePay [API docs][docs].
The quotation boxes like the one below will guide you in writing excellent
documentation for your gateway. All our gateways are documented in this manner
and we aim to keep our docs as consistent with each other as possible.
**Please read them and do as they suggest**. Feel free to add or skip sections
though.
The following set of functions for WePay have been implemented:
If you'd like to make edits to the template docs, they exist at
`templates/gateway.eex`. We encourage you to make corrections and open a PR
and tag it with the label `template`.
***Actual docs begin below this line!***
--------------------------------------------------------------------------------
> List features that have been implemented, and what "actions" they map to as
> per the wepay gateway docs.
> A table suits really well for this.
| Action | Method |
| ------ | ------ |
| Authorize a Credit Card | `authorize/3` |
| Capture a previously authorized amount | `capture/3` |
| Charge a Credit Card | `purchase/3` |
| Refund a transaction | `refund/3` |
| Void a transaction | `void/2` |
| Create Customer Profile | `store/2` |
| Delete Customer Profile | `unstore/2` |
## Optional or extra parameters
Most `Gringotts` API calls accept an optional `Keyword` list `opts` to supply
optional arguments for transactions with the gateway.
> List all available (ie, those that will be supported by this module) keys, a
> description of their function/role and whether they have been implemented
> and tested.
> A table suits really well for this.
## Registering your wepay account at `Gringotts`
Explain how to make an account with the gateway and show how to put the
`required_keys` (like authentication info) to the configuration.
> Here's how the secrets map to the required configuration parameters for wepay:
>
> | Config parameter | wepay secret |
> | --------------- | ---------- |
> | `:access_token` | **AccessToken**|
> Your Application config **must include the `[:access_token]` field(s)** and would look
> something like this:
>
> config :gringotts, Gringotts.Gateways.WePay,
> access_token: "your_secret_access_token"
## Scope of this module
To know more about these keywords visit [Request and Response][req-resp] tabs for each
API method.
> It's unlikely that your first iteration will support all features of the
> gateway, so list down those items that are missing.
[docs]: hhttps://developer.wepay.com/
[req-resp]: https://developer.wepay.com/api/reference/structures
## Supported currencies and countries
> It's enough if you just add a link to the gateway's docs or FAQ that provide
> info about this.
WePay supports the countries listed [here][all-country-list]
[all-country-list]: [https://support.wepay.com/hc/en-us/articles/203611643-Is-WePay-International-]
## Following the examples
1. First, set up a sample application and configure it to work with MONEI.
1. First, set up a sample application and configure it to work with WePay.
- You could do that from scratch by following our [Getting Started][gs] guide.
- To save you time, we recommend [cloning our example
repo][example] that gives you a pre-configured sample app ready-to-go.
Expand All @@ -82,14 +54,12 @@ defmodule Gringotts.Gateways.WePay do
year: 2099, month: 12,
verification_code: "123", brand: "VISA"}
```
> Add any other frequently used bindings up here.
We'll be using these in the examples below.
[gs]: https://github.com/aviabird/gringotts/wiki/
[home]: https://go.wepay.com
[example]: https://github.com/aviabird/gringotts_example
"""

# The Base module has the (abstract) public API, and some utility
Expand All @@ -113,19 +83,18 @@ defmodule Gringotts.Gateways.WePay do
The authorization validates the `card` details with the banking network,
places a hold on the transaction `amount` in the customer’s issuing bank.
> ** You could perhaps:**
> 1. describe what are the important fields in the Response struct
> 2. mention what a merchant can do with these important fields (ex:
> `capture/3`, etc.)
## Note
WePay returns an ID string which can be used to:
> If there's anything noteworthy about this operation, it comes here.
* `capture/3` _an_ amount.
## Example
> A barebones example using the bindings you've suggested in the `moduledoc`.
```
iex> amount = Money.new(42, :USD)
iex> {:ok, auth_result} = Gringotts.authorize(Gringotts.Gateways.WePay, amount, card, opts)
iex> auth_result.id # This is the authorization ID
```
"""

@spec authorize(Money.t(), CreditCard.t(), keyword) :: {:ok | :error, Response}
def authorize(amount, card = %CreditCard{}, opts) do
{currency, value, _} = Money.to_integer(amount)
Expand Down Expand Up @@ -158,6 +127,35 @@ defmodule Gringotts.Gateways.WePay do
end
end

# authorize with card token.
def authorize(amount, card_token, opts) do
{currency, value, _} = Money.to_integer(amount)

body =
Poison.encode!(%{
account_id: opts[:account_id],
short_description: opts[:short_description],
type: opts[:type],
amount: value,
currency: currency,
long_description: opts[:long_description],
callback_uri: opts[:callback_uri],
auto_release: true,
unique_id: opts[:unique_id],
reference_id: opts[:reference_id],
delivery_type: opts[:delivery_type],
payment_method: %{
type: "credit_card",
credit_card: %{
id: card_token,
auto_capture: false
}
}
})

commit(:post, "/checkout/create/", body, opts)
end

@doc """
Captures a pre-authorized `amount`.
Expand All @@ -166,12 +164,12 @@ defmodule Gringotts.Gateways.WePay do
## Note
> If there's anything noteworthy about this operation, it comes here.
> For example, does the gateway support partial, multiple captures?
> WePay **do not** support partial captures.
## Example
> A barebones example using the bindings you've suggested in the `moduledoc`.
```
iex> {:ok, capture_result} = Gringotts.capture(Gringotts.Gateways.WePay, amount, auth_result.id, opts)
```
"""
@spec capture(String.t(), Money.t(), keyword) :: {:ok | :error, Response}
def capture(payment_id, amount, opts) do
Expand All @@ -190,13 +188,12 @@ defmodule Gringotts.Gateways.WePay do
debiting `amount` from the customer's account by charging the customer's
`card`.
## Note
> If there's anything noteworthy about this operation, it comes here.
## Example
> A barebones example using the bindings you've suggested in the `moduledoc`.
```
iex> amount = Money.new(42, :USD)
iex> {:ok, purchase_result} = Gringotts.purchase(Gringotts.Gateways.WePay, amount, card, opts)
iex> purchase_result.id # This is the checkout ID
```
"""
@spec purchase(Money.t(), CreditCard.t(), keyword) :: {:ok | :error, Response}
def purchase(amount, card = %CreditCard{}, opts) do
Expand Down Expand Up @@ -229,6 +226,34 @@ defmodule Gringotts.Gateways.WePay do
end
end

# purchase with card token.
def purchase(amount, card_token, opts) do
{currency, value, _} = Money.to_integer(amount)

body =
Poison.encode!(%{
account_id: opts[:account_id],
short_description: opts[:short_description],
type: opts[:type],
amount: value,
currency: currency,
long_description: opts[:long_description],
callback_uri: opts[:callback_uri],
auto_release: true,
unique_id: opts[:unique_id],
reference_id: opts[:reference_id],
delivery_type: opts[:delivery_type],
payment_method: %{
type: "credit_card",
credit_card: %{
id: card_token
}
}
})

commit(:post, "/checkout/create/", body, opts)
end

@doc """
Voids the referenced payment.
Expand All @@ -238,13 +263,13 @@ defmodule Gringotts.Gateways.WePay do
> As a consequence, the customer will never see any booking on his statement.
## Note
> Which transactions can be voided?
> Is there a limited time window within which a void can be perfomed?
> As a consequence, the customer will never see any booking on his statement.
> Checkout must be in purchased or captured state.
## Example
> A barebones example using the bindings you've suggested in the `moduledoc`.
```
iex> {:ok, void_result} = Gringotts.capture(Gringotts.Gateways.WePay, purchase_result.id, opts)
```
"""
@spec void(String.t(), keyword) :: {:ok | :error, Response}
def void(payment_id, opts) do
Expand All @@ -260,17 +285,17 @@ defmodule Gringotts.Gateways.WePay do
@doc """
Refunds the `amount` to the customer's account with reference to a prior transfer.
> Refunds are allowed on which kinds of "prior" transactions?
> Refunds are allowed on Captured / purchased transraction.
## Note
> The end customer will usually see two bookings/records on his statement. Is
> that true for wepay?
> Is there a limited time window within which a void can be perfomed?
* It is recommended to refund the transraction after 5 to 10 min.
* WePay does not support partial refunds.
## Example
> A barebones example using the bindings you've suggested in the `moduledoc`.
```
iex> {:ok, refund_result} = Gringotts.refund(Gringotts.Gateways.WePay, purchase_result.id, amount)
```
"""
@spec refund(Money.t(), String.t(), keyword) :: {:ok | :error, Response}
def refund(amount, payment_id, opts) do
Expand All @@ -286,15 +311,15 @@ defmodule Gringotts.Gateways.WePay do
@doc """
Stores the payment-source data for later use.
> This usually enable "One Click" and/or "Recurring Payments"
## Note
> If there's anything noteworthy about this operation, it comes here.
> Currently "Recurring Payments" are not implemented.
## Example
> A barebones example using the bindings you've suggested in the `moduledoc`.
```
iex> {:ok, store_result} = Gringotts.store(Gringotts.Gateways.WePay, card, opts)
iex> store_result.token #card token
```
"""
@spec store(CreditCard.t(), keyword) :: {:ok | :error, Response}
def store(%CreditCard{} = card, opts) do
Expand Down Expand Up @@ -327,14 +352,11 @@ defmodule Gringotts.Gateways.WePay do
Removes card or payment info that was previously `store/2`d
Deletes previously stored payment-source data.
## Note
> If there's anything noteworthy about this operation, it comes here.
## Example
> A barebones example using the bindings you've suggested in the `moduledoc`.
```
iex> {:ok, store_result} = Gringotts.unstore(Gringotts.Gateways.WePay, store_result.token, opts)
```
"""
@spec unstore(String.t(), keyword) :: {:ok | :error, Response}
def unstore(registration_id, opts) do
Expand Down

0 comments on commit bf788dc

Please sign in to comment.