-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:MayOneUS/mayday-2.0-backend
- Loading branch information
Showing
52 changed files
with
1,214 additions
and
1,315 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
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 |
---|---|---|
|
@@ -47,3 +47,6 @@ bower.json | |
|
||
# Ignore pow environment settings | ||
.powenv | ||
|
||
# ignore byebug history | ||
.byebug_history |
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,87 @@ | ||
class V1::DonationPagesController < ApplicationController | ||
def index | ||
@donation_pages = DonationPage.by_funds_raised.limit(limit) | ||
|
||
render :index | ||
end | ||
|
||
def show | ||
@donation_page = find_donation_page | ||
|
||
render :show | ||
end | ||
|
||
def create | ||
person = find_or_initialize_person | ||
donation_page = person.donation_pages.new(donation_page_params) | ||
|
||
if person.save && donation_page.save | ||
donation_page.reload | ||
render json: { uuid: donation_page.uuid }, status: :created | ||
else | ||
render json: { errors: merge_errors(person, donation_page) }, | ||
status: :unprocessable_entity | ||
end | ||
end | ||
|
||
def update | ||
donation_page = find_donation_page | ||
|
||
if donation_page.authorize_and_update(donation_page_params) | ||
head :no_content | ||
else | ||
render json: { errors: @donation_page.errors }, | ||
status: :unprocessable_entity | ||
end | ||
end | ||
|
||
def destroy | ||
donation_page = find_donation_page | ||
|
||
donation_page.destroy | ||
|
||
head :no_content | ||
end | ||
|
||
def validate | ||
person = find_or_initialize_person | ||
donation_page = person.donation_pages.new(donation_page_params) | ||
|
||
valid = person.valid? # this will also validate person.donation_pages | ||
errors = person.errors.to_hash.merge(donation_page.errors.to_hash) | ||
render json: { valid: valid, errors: errors } | ||
end | ||
|
||
private | ||
|
||
def merge_errors(*records) | ||
errors = {} | ||
records.each do |record| | ||
errors.merge!(record.errors.to_hash) | ||
end | ||
errors | ||
end | ||
|
||
def find_donation_page | ||
DonationPage.find_by!(slug: params[:slug]) | ||
end | ||
|
||
def donation_page_params | ||
params.require(:donation_page).permit(:title, :slug, :visible_user_name, | ||
:photo_url, :intro_text, :goal_in_cents, :access_token) | ||
end | ||
|
||
def person_params | ||
params.require(:person).permit(Person::PERMITTED_PUBLIC_FIELDS) | ||
end | ||
|
||
def find_or_initialize_person | ||
person = Person.find_or_initialize_by(email: person_params[:email]) | ||
person.assign_attributes(person_params) | ||
person | ||
end | ||
|
||
def limit | ||
params[:limit] || 10 | ||
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,33 @@ | ||
class V1::DonationsController < V1::BaseController | ||
|
||
# Receives person, payment and action attributes. Creates/updates a person | ||
# creates an action, and creates a stripe single or recurring payment | ||
# Params: | ||
# * email - string - person email | ||
# * employer - string - person employer | ||
# * occupation - string - person occupation | ||
# * amount_in_cents - int - donation amount in cents | ||
# * recurring - true - pass true if recurring donation | ||
# * stripe_token - token returned by Stripe.js to identify credit card | ||
# * template_id - action template_id | ||
# * utm_source - action utm_source | ||
# * utm_medium - action utm_medium | ||
# * utm_campaign - action utm_campaign | ||
# * source_url - action source_url | ||
def create | ||
donation = Donation.new(donation_params) | ||
if donation.process | ||
render json: { status: 'success' } | ||
else | ||
render json: { errors: donation.errors } | ||
end | ||
end | ||
|
||
private | ||
|
||
def donation_params | ||
params.permit(:email, :employer, :occupation, :stripe_token, :recurring, | ||
:amount_in_cents, :utm_source, :utm_medium, :utm_campaign, | ||
:source_url, :template_id) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class NbDonationCreateJob < ActiveJob::Base | ||
queue_as :default | ||
|
||
def perform(amount_in_cents, person_attributes) | ||
nb_args = Integration::NationBuilder.person_params(person_attributes) | ||
response = Integration::NationBuilder.create_or_update_person(nb_args) || {} | ||
person_id = response['id'] | ||
|
||
Integration::NationBuilder.create_donation(amount_in_cents: amount_in_cents, | ||
person_id: person_id) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
class Donation | ||
include ActiveModel::Model | ||
|
||
attr_accessor :email, :employer, :occupation, :stripe_token, :recurring, | ||
:utm_source, :utm_medium, :utm_campaign, :source_url, :amount_in_cents | ||
|
||
attr_writer :template_id | ||
|
||
validates :email, presence: true, email_format: true | ||
validates :employer, presence: true | ||
validates :occupation, presence: true | ||
validates :stripe_token, presence: true | ||
validates :amount_in_cents, presence: true, | ||
numericality: { greater_than: 0, only_integer: true } | ||
|
||
def process | ||
if valid? | ||
find_or_create_person | ||
process_payment | ||
record_donation | ||
create_donate_action | ||
else | ||
false | ||
end | ||
|
||
rescue Stripe::CardError => e | ||
errors.add(:stripe_token, e.json_body[:error][:message]) | ||
false | ||
end | ||
|
||
private | ||
|
||
attr_reader :person | ||
|
||
def process_payment | ||
if recurring | ||
stripe_customer = StripeCustomer.new(create_stripe_customer) | ||
person.update(stripe_id: stripe_customer.id) | ||
person.create_subscription(remote_id: stripe_customer.subscription_id) | ||
else | ||
create_stripe_charge | ||
end | ||
end | ||
|
||
def record_donation | ||
person = { email: email, employer: employer, occupation: occupation } | ||
NbDonationCreateJob.perform_later(amount_as_integer, person) | ||
end | ||
|
||
def create_donate_action | ||
person.create_action(utm_source: utm_source, | ||
utm_medium: utm_medium, | ||
utm_campaign: utm_campaign, | ||
source_url: source_url, | ||
template_id: template_id, | ||
donation_amount_in_cents: amount_as_integer) | ||
end | ||
|
||
def template_id | ||
@template_id ||= Activity::DEFAULT_TEMPLATE_IDS[:donate] | ||
end | ||
|
||
def find_or_create_person | ||
@person = Person.find_or_initialize_by(email: email) | ||
@person.update(skip_nb_update: true) | ||
end | ||
|
||
def create_stripe_customer | ||
Stripe::Customer.create(source: stripe_token, | ||
plan: 'one_dollar_monthly', | ||
email: email, | ||
quantity: amount_as_integer/100) | ||
end | ||
|
||
def create_stripe_charge | ||
Stripe::Charge.create(amount: amount_as_integer, | ||
source: stripe_token, | ||
currency: 'usd', | ||
description: "donation from #{email}") | ||
end | ||
|
||
def amount_as_integer | ||
amount_in_cents.to_i | ||
end | ||
|
||
class StripeCustomer | ||
attr_reader :id, :subscription_id | ||
|
||
def initialize(stripe_customer) | ||
@id = stripe_customer.id | ||
@subscription_id = stripe_customer.subscriptions.first.id | ||
end | ||
end | ||
end |
Oops, something went wrong.