This repository has been archived by the owner on Mar 27, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extracts service classes into their own files
- Loading branch information
Showing
8 changed files
with
257 additions
and
263 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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# frozen_string_literal: true | ||
|
||
class ActionParamsBuilder | ||
attr_reader :controller, :params | ||
|
||
delegate :browser, :request, to: :controller | ||
|
||
def initialize(controller, params) | ||
@controller = controller | ||
@params = params | ||
end | ||
|
||
def action_params | ||
build_params | ||
end | ||
|
||
private | ||
|
||
def mobile_value | ||
MobileDetector.detect(browser) | ||
end | ||
|
||
def referer_url | ||
{ action_referer: request.referer } | ||
end | ||
|
||
def build_params | ||
params.permit(fields + base_params).merge(donation: true) | ||
end | ||
|
||
def base_params | ||
%w(page_id form_id name source akid referring_akid email) | ||
end | ||
|
||
def fields | ||
Form.find(params[:form_id]).form_elements.map(&:name) | ||
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,59 @@ | ||
# frozen_string_literal: true | ||
|
||
module BraintreeServices | ||
class OneClickService | ||
attr_reader :params, :payment_options | ||
|
||
def initialize(params) | ||
@params = params | ||
@payment_options = BraintreeServices::PaymentOptions.new(params) | ||
end | ||
|
||
def run | ||
create_action | ||
|
||
if payment_options.recurring? | ||
sale = make_subscription | ||
store_subscription_locally(sale) if sale.success? | ||
else | ||
sale = make_sale | ||
store_sale_locally(sale) if sale.success? | ||
end | ||
end | ||
|
||
private | ||
|
||
def create_action | ||
action = ManageAction.create( | ||
params_for_action, | ||
skip_counter: true, | ||
skip_queue: false | ||
) | ||
|
||
action.update(donation: true) | ||
end | ||
|
||
def params_for_action | ||
ActionParamsBuilder.new(self, DonationActionParamsWrapper.params(params)).action_params | ||
end | ||
|
||
def make_sale | ||
@make_sale ||= ::Braintree::Transaction.sale( | ||
payment_method_token: payment_options.token, | ||
amount: payment_options.amount | ||
) | ||
end | ||
|
||
def make_subscription | ||
@make_subscription ||= ::Braintree::Subscription.create(payment_options.subscription_options) | ||
end | ||
|
||
def store_sale_locally(sale) | ||
TransactionBuilder.new(sale.transaction, payment_options).build | ||
end | ||
|
||
def store_subscription_locally(sale) | ||
SubscriptionBuilder.new(sale.subscription, payment_options).build | ||
end | ||
end | ||
end |
Oops, something went wrong.