Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix donations for Houdini #1066

Merged
merged 11 commits into from
Apr 14, 2022
Merged
2 changes: 1 addition & 1 deletion app/controllers/campaigns_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def peer_to_peer # rubocop:disable Metrics/AbcSize
private

def check_nonprofit_status
raise ActionController::RoutingError, 'Not Found' if !current_role?(:super_admin) && !current_nonprofit.published
# raise ActionController::RoutingError, 'Not Found' if !current_role?(:super_admin) && !current_nonprofit.published
end

def campaign_params # rubocop:disable Metrics/MethodLength
Expand Down
16 changes: 4 additions & 12 deletions app/controllers/cards_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,14 @@ class CardsController < ApplicationController
# post /cards
def create
account = Supporter.find(card_params[:holder_id]).nonprofit.stripe_account_id
render(
JsonResp.new(params) do |_d|
requires(:card).nested do
requires(:name, :stripe_card_token).as_string
requires(:holder_id).as_int
requires(:holder_type).one_of('Supporter')
end
end.when_valid do |d|
InsertCard.with_stripe(d[:card], account, params[:event_id], current_user)
end
)

@source_token = InsertCard.with_stripe(card_params, acct, params[:event_id], current_user)

end

private

def card_params
params.require(:card).permit(:cardholders_name, :email, :name, :failure_message, :status, :stripe_card_token, :stripe_card_id, :stripe_customer_id, :holder, :inactive)
params.require(:card).permit(:cardholders_name, :email, :name, :failure_message, :status, :stripe_card_token, :stripe_card_id, :stripe_customer_id, :holder_id, :holder_type, :inactive)
end
end
9 changes: 4 additions & 5 deletions app/controllers/nonprofits/donations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ def index

# post /nonprofits/:nonprofit_id/donations
def create
if params[:token]
donations_params[:token] = params[:token]
render_json { InsertDonation.with_stripe(donations_params, current_user) }
if params[:token]
@result = InsertDonation.with_stripe(donations_params.merge(token:params[:token]), current_user)

elsif params[:direct_debit_detail_id]
render JsonResp.new(donations_params) do |_data|
requires(:amount).as_int
Expand Down Expand Up @@ -82,9 +82,8 @@ def authenticate_campaign_editor!
end

private

def donations_params
params.require(:donation).permit(:date, :amount, :recurring, :anonymous, :email, :designation, :dedication, :comment, :origin_url, :nonprofit_id, :card_id, :supporter_id, :profile_id, :campaign_id, :payment_id, :event_id, :direct_debit_detail_id, :payment_provider)
params.require(:donation).permit(:date, :amount, :recurring, :anonymous, :email, :designation, :dedication, :comment, :origin_url, :nonprofit_id, :card_id, :supporter_id, :profile_id, :campaign_id, :payment_id, :event_id, :direct_debit_detail_id, :token)
end
end
end
2 changes: 1 addition & 1 deletion app/controllers/nonprofits/supporters_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def info_card

# post /nonprofits/:nonprofit_id/supporters
def create
render_json { InsertSupporter.create_or_update(current_nonprofit, create_supporter_params.to_h) }
@supporter = InsertSupporter.create_or_update(current_nonprofit, create_supporter_params.to_h)
end

# put /nonprofits/:nonprofit_id/supporters/:id
Expand Down
10 changes: 0 additions & 10 deletions app/javascript/legacy/components/card-form.es6
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ const view = state => {
, expMonthInput(field)
, expYearInput(field)
, zipInput(field, state.card$().address_zip)
, profileInput(field, app.profile_id) // XXX global
])
, h('div.u-centered.u-marginTop--20', [
state.hideButton ? '' : button({
Expand Down Expand Up @@ -177,14 +176,5 @@ const zipInput = (field, zip) =>
])


const profileInput = (field, profile_id) =>
field(h('input'
, { props: {
type: 'hidden'
, name: 'profile_id'
, value: profile_id || ''
}}
))

module.exports = {view, init}

6 changes: 5 additions & 1 deletion app/javascript/legacy/nonprofits/donate/payment-step.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ const sepaTab = 'sepa'
const cardTab = 'credit_card'

function init(state) {
const payload$ = flyd.map(supp => ({card: {holder_id: supp.id, holder_type: 'Supporter'}}), state.supporter$)
const payload$ = flyd.map(supp => {

return {card: {holder_id: supp.id, holder_type: 'Supporter'}};

}, state.supporter$)
const supporterID$ = flyd.map(supp => supp.id, state.supporter$)
const card$ = flyd.merge(
flyd.stream({})
Expand Down
11 changes: 0 additions & 11 deletions app/javascript/legacy/recurring_donations/edit/card-form.es6
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ const view = state => {
, expMonthInput(field)
, expYearInput(field)
, zipInput(field, state.card$().address_zip)
, profileInput(field, app.profile_id) // XXX global
])
, h('div.u-centered.u-marginTop--20', [
state.hideButton ? '' : button({
Expand Down Expand Up @@ -172,15 +171,5 @@ const zipInput = (field, zip) =>
))
])


const profileInput = (field, profile_id) =>
field(h('input'
, { props: {
type: 'hidden'
, name: 'profile_id'
, value: profile_id || ''
}}
))

module.exports = {view, init}

29 changes: 13 additions & 16 deletions app/legacy_lib/insert_card.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ def self.with_stripe(card_data, _stripe_account_id = nil, event_id = nil, curren
name: { not_blank: true, required: true },
event_id: { is_reference: true })
rescue ParamValidation::ValidationError => e
return { json: { error: "Validation error\n #{e.message}", errors: e.data }, status: :unprocessable_entity }
raise e
end

# validate that the user is with the correct nonprofit

card_data = card_data.keep_keys(:holder_type, :holder_id, :stripe_card_id, :stripe_card_token, :name)
card_data = card_data.slice(:holder_type, :holder_id, :stripe_card_id, :stripe_card_token, :name)
holder_types = { 'Nonprofit' => :nonprofit, 'Supporter' => :supporter }
holder_type = holder_types[card_data[:holder_type]]
holder = nil
Expand All @@ -45,7 +45,7 @@ def self.with_stripe(card_data, _stripe_account_id = nil, event_id = nil, curren
holder = Supporter.select('id, email, nonprofit_id').includes(:cards, :nonprofit).find(card_data[:holder_id])
end
rescue ActiveRecord::RecordNotFound
return { json: { error: 'Sorry, you need to provide a nonprofit or supporter' }, status: :unprocessable_entity }
raise 'Sorry, you need to provide a nonprofit or supporter'
end

begin
Expand All @@ -63,10 +63,10 @@ def self.with_stripe(card_data, _stripe_account_id = nil, event_id = nil, curren
raise AuthenticationError
end
end
rescue AuthenticationError
return { json: { error: "You're not authorized to perform that action" }, status: :unauthorized }
rescue AuthenticationError => e
raise e
rescue StandardError => e
return { json: { error: "Oops! There was an error: #{e.message}" }, status: :unprocessable_entity }
raise "Oops! There was an error: #{e.message}"
end
stripe_account_hash = {} # stripe_account_id ? {stripe_account: stripe_account_id} : {}
begin
Expand All @@ -80,9 +80,9 @@ def self.with_stripe(card_data, _stripe_account_id = nil, event_id = nil, curren

card_data[:stripe_customer_id] = stripe_customer.id
rescue Stripe::CardError => e
return { json: { error: "Oops! #{e.json_body[:error][:message]}" }, status: :unprocessable_entity }
raise "Oops! #{e.json_body[:error][:message]}"
rescue Stripe::StripeError => e
return { json: { error: "Oops! There was an error processing your payment, and it did not complete. Please try again in a moment. Error: #{e}" }, status: :unprocessable_entity }
raise "Oops! There was an error processing your payment, and it did not complete. Please try again in a moment. Error: #{e}"
end

card = nil
Expand All @@ -97,19 +97,16 @@ def self.with_stripe(card_data, _stripe_account_id = nil, event_id = nil, curren
card = holder.cards.create(card_data)
params = {}
params[:event] = event if event
source_token = InsertSourceToken.create_record(card, params).token
source_token = InsertSourceToken.create_record(card, params)
end
card.save!
end
rescue ActiveRecord::ActiveRecordError => e
return { json: { error: "Oops! There was an error saving your card, and it did not complete. Please try again in a moment. Error: #{e}" }, status: :unprocessable_entity }
# rescue ActiveRecord::ActiveRecordError => e
# return { json: { error: "Oops! There was an error saving your card, and it did not complete. Please try again in a moment. Error: #{e}" }, status: :unprocessable_entity }
rescue e
return { json: { error: "Oops! There was an error saving your card, and it did not complete. Please try again in a moment. Error: #{e}" }, status: :unprocessable_entity }
rescue e
return { json: { error: "Oops! There was an error saving your card, and it did not complete. Please try again in a moment. Error: #{e}" }, status: :unprocessable_entity }
raise "Oops! There was an error saving your card, and it did not complete. Please try again in a moment. Error: #{e}"
end

{ status: :ok, json: card.attributes.with_indifferent_access.merge(token: source_token) }
source_token
end

def self.customer_data(holder, card_data)
Expand Down
1 change: 0 additions & 1 deletion app/legacy_lib/insert_charge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ def self.with_stripe(data)
# Catch errors thrown by the stripe gem so we can respond with a 422 with an error message rather than 500
begin
stripe_customer_id = card.stripe_customer_id
stripe_account_id = StripeAccount.find_or_create(data[:nonprofit_id])
rescue StandardError => e
raise e
end
Expand Down
4 changes: 2 additions & 2 deletions app/legacy_lib/insert_donation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ module InsertDonation
# designation, dedication
# recurring_donation if is recurring
def self.with_stripe(data, current_user = nil)
data = data.with_indifferent_access

data = data.to_h.with_indifferent_access
ParamValidation.new(data, common_param_validations
.merge(token: { required: true, format: UUID::Regex }))

Expand Down
2 changes: 1 addition & 1 deletion app/listeners/sepa_payment_listener.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later
# Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE
class SepaPaymentListener < ApplicationListener
def self.donation_create(donation)
def self.donation_create(donation, locale, user=nil)
if donation.payment_provider == :sepa
DirectDebitCreateNotifyNonprofitJob.perform_later(donation.id)
DirectDebitCreateNotifyDonorJob.perform_later donation.id, locale
Expand Down
2 changes: 1 addition & 1 deletion app/models/supporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def profile_picture(size = :normal)
end

def as_json(options = {})
h = super(options)
h = super(**options)
h[:pic_tiny] = profile_picture(:tiny)
h[:pic_normal] = profile_picture(:normal)
h[:url] = profile && Rails.application.routes.url_helpers.profile_path(profile)
Expand Down
9 changes: 9 additions & 0 deletions app/views/cards/create.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later
# Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE

json.(@source_token, :token)

json.holder_id @source_token.tokenizable.holder_id
json.holder_type @source_token.tokenizable.holder_type
10 changes: 10 additions & 0 deletions app/views/nonprofits/donations/create.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later
# Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE

donation = @result['donation']

json.donation do
json.id donation.id
end
6 changes: 6 additions & 0 deletions app/views/nonprofits/supporters/create.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later
# Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE

json.(@supporter, :id, :name, :email, :zip_code)
2 changes: 2 additions & 0 deletions config/initializers/stripe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE
require 'stripe'

Stripe.api_version = '2017-06-05'

#Stripe.api_key = Houdini.payment_providers.stripe.stripe.private_key

#Rails.application.config.Houdini.hoster.support_email = "[email protected]"
2 changes: 1 addition & 1 deletion spec/controllers/cards_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
include_context :shared_user_context
describe 'accept all' do
describe 'create' do
include_context :open_to_all, :post, :create, nonprofit_id: :__our_np
include_context :open_to_all, :post, :create, nonprofit_id: :__our_np, with_status: 200
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/nonprofits/donations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
end
describe 'accept all users' do
describe 'create' do
include_context :open_to_all, :get, :create, nonprofit_id: :__our_np
include_context :open_to_all, :get, :create, nonprofit_id: :__our_np, with_status: 200
end

describe 'follow up' do
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/nonprofits/supporters_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

describe 'accept all users' do
describe 'create' do
include_context :open_to_all, :post, :create, nonprofit_id: :__our_np
include_context :open_to_all, :post, :create, nonprofit_id: :__our_np, with_status: 200
end
end
end
Loading