-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #653 from code-corps/649-track-charge-succeeded-ev…
…ent-on-connect Add stripe charge model and event tracking
- Loading branch information
Showing
21 changed files
with
601 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
lib/code_corps/stripe_service/adapters/stripe_connect_charge.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
defmodule CodeCorps.StripeService.Adapters.StripeConnectChargeAdapter do | ||
|
||
alias CodeCorps.{ | ||
StripeConnectAccount, StripeConnectCustomer, Repo | ||
} | ||
|
||
alias CodeCorps.StripeService.Util | ||
|
||
# Mapping of stripe record attributes to locally stored attributes | ||
# Format is {:local_key, [:nesting, :of, :stripe, :keys]} | ||
# | ||
# TODO: | ||
# | ||
# Relationships we have | ||
# * customer, user | ||
# Relationship we store but do not define | ||
# * application, invoice, connect account, balance transaction, source transfer, transfer | ||
@stripe_mapping [ | ||
{:amount, [:amount]}, | ||
{:amount_refunded, [:amount_refunded]}, | ||
{:application_id_from_stripe, [:application]}, | ||
{:application_fee_id_from_stripe, [:application_fee]}, | ||
{:balance_transaction_id_from_stripe, [:balance_transaction]}, | ||
{:captured, [:captured]}, | ||
{:created, [:created]}, | ||
{:currency, [:currency]}, | ||
{:customer_id_from_stripe, [:customer]}, | ||
{:description, [:description]}, | ||
{:failure_code, [:failure_code]}, | ||
{:failure_message, [:failure_message]}, | ||
{:id_from_stripe, [:id]}, | ||
{:invoice_id_from_stripe, [:invoice]}, | ||
{:paid, [:paid]}, | ||
{:refunded, [:refunded]}, | ||
{:review_id_from_stripe, [:review]}, | ||
{:source_transfer_id_from_stripe, [:source_transfer]}, | ||
{:statement_descriptor, [:statement_descriptor]}, | ||
{:status, [:status]} | ||
] | ||
|
||
@doc """ | ||
Transforms a `%Stripe.Charge{}` and a set of local attributes into a | ||
map of parameters used to create or update a `StripeConnectCharge` record. | ||
""" | ||
def to_params(%Stripe.Charge{} = stripe_charge, %StripeConnectAccount{id: connect_account_id}) do | ||
result = | ||
stripe_charge | ||
|> Map.from_struct | ||
|> Util.transform_map(@stripe_mapping) | ||
|> Map.put(:stripe_connect_account_id, connect_account_id) | ||
|> add_other_associations() | ||
|
||
{:ok, result} | ||
end | ||
|
||
defp add_other_associations(%{customer_id_from_stripe: customer_id_from_stripe} = attributes) do | ||
%StripeConnectCustomer{id: customer_id, user_id: user_id} = | ||
Repo.get_by(StripeConnectCustomer, id_from_stripe: customer_id_from_stripe) | ||
|
||
attributes | ||
|> Map.put(:user_id, user_id) | ||
|> Map.put(:stripe_connect_customer_id, customer_id) | ||
end | ||
end |
5 changes: 5 additions & 0 deletions
5
lib/code_corps/stripe_service/events/connect_charge_succeeded.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
defmodule CodeCorps.StripeService.Events.ConnectChargeSucceeded do | ||
def handle(%{data: %{object: %{id: id_from_stripe}}, user_id: connect_account_id_from_stripe}) do | ||
CodeCorps.StripeService.StripeConnectChargeService.create(id_from_stripe, connect_account_id_from_stripe) | ||
end | ||
end |
20 changes: 20 additions & 0 deletions
20
lib/code_corps/stripe_service/stripe_connect_charge_service.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
defmodule CodeCorps.StripeService.StripeConnectChargeService do | ||
alias CodeCorps.{ | ||
Repo, StripeConnectAccount, StripeConnectCharge | ||
} | ||
alias CodeCorps.StripeService.Adapters.StripeConnectChargeAdapter | ||
|
||
@api Application.get_env(:code_corps, :stripe) | ||
|
||
def create(id_from_stripe, connect_account_id_from_stripe) do | ||
with %StripeConnectAccount{} = stripe_connect_account <- Repo.get_by(StripeConnectAccount, id_from_stripe: connect_account_id_from_stripe), | ||
{:ok, %Stripe.Charge{} = api_charge} <- @api.Charge.retrieve(id_from_stripe, connect_account: connect_account_id_from_stripe), | ||
{:ok, params} = StripeConnectChargeAdapter.to_params(api_charge, stripe_connect_account) | ||
do | ||
%StripeConnectCharge{} | ||
|> StripeConnectCharge.create_changeset(params) | ||
|> Repo.insert | ||
|> CodeCorps.Analytics.Segment.track(:created) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
defmodule CodeCorps.StripeTesting.Charge do | ||
import CodeCorps.StripeTesting.Helpers | ||
|
||
def retrieve(_id, _opts) do | ||
{:ok, load_fixture(Stripe.Charge, "charge")} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
{ | ||
"id": "charge", | ||
"object": "charge", | ||
"amount": 100, | ||
"amount_refunded": 0, | ||
"application": null, | ||
"application_fee": null, | ||
"balance_transaction": "test_balance_transaction_for_charge", | ||
"captured": true, | ||
"created": 1484869309, | ||
"currency": "usd", | ||
"customer": "test_customer_for_charge", | ||
"description": "Test Charge (created for fixture)", | ||
"destination": null, | ||
"dispute": null, | ||
"failure_code": null, | ||
"failure_message": null, | ||
"fraud_details": {}, | ||
"invoice": null, | ||
"livemode": false, | ||
"metadata": { | ||
}, | ||
"order": null, | ||
"outcome": null, | ||
"paid": true, | ||
"receipt_email": null, | ||
"receipt_number": null, | ||
"refunded": false, | ||
"refunds": { | ||
"object": "list", | ||
"data": [], | ||
"has_more": false, | ||
"total_count": 0, | ||
"url": "/v1/charges/ch_123/refunds" | ||
}, | ||
"review": null, | ||
"shipping": null, | ||
"source": { | ||
"id": "card_123", | ||
"object": "card", | ||
"address_city": null, | ||
"address_country": null, | ||
"address_line1": null, | ||
"address_line1_check": null, | ||
"address_line2": null, | ||
"address_state": null, | ||
"address_zip": null, | ||
"address_zip_check": null, | ||
"brand": "Visa", | ||
"country": "US", | ||
"customer": null, | ||
"cvc_check": null, | ||
"dynamic_last4": null, | ||
"exp_month": 8, | ||
"exp_year": 2018, | ||
"funding": "credit", | ||
"last4": "4242", | ||
"metadata": { | ||
}, | ||
"name": null, | ||
"tokenization_method": null | ||
}, | ||
"source_transfer": null, | ||
"statement_descriptor": null, | ||
"status": "succeeded" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
priv/repo/migrations/20170121014100_add_stripe_connect_charge.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
defmodule CodeCorps.Repo.Migrations.AddStripeConnectCharge do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create table(:stripe_connect_charges) do | ||
add :amount, :integer | ||
add :amount_refunded, :integer | ||
add :application_id_from_stripe, :string | ||
add :application_fee_id_from_stripe, :string | ||
add :balance_transaction_id_from_stripe, :string | ||
add :captured, :boolean | ||
add :created, :integer | ||
add :currency, :string | ||
add :customer_id_from_stripe, :string | ||
add :description, :string | ||
add :failure_code, :string | ||
add :failure_message, :string | ||
add :id_from_stripe, :string | ||
add :invoice_id_from_stripe, :string | ||
add :paid, :boolean | ||
add :refunded, :boolean | ||
add :review_id_from_stripe, :string | ||
add :source_transfer_id_from_stripe, :string | ||
add :statement_descriptor, :string | ||
add :status, :string | ||
|
||
add :stripe_connect_account_id, references(:stripe_connect_accounts) | ||
add :stripe_connect_customer_id, references(:stripe_connect_customers), null: false | ||
add :user_id, references(:users), null: false | ||
|
||
timestamps() | ||
end | ||
|
||
create index(:stripe_connect_charges, [:id_from_stripe], unique: true) | ||
end | ||
end |
Oops, something went wrong.