Skip to content
This repository has been archived by the owner on Mar 27, 2023. It is now read-only.

Commit

Permalink
Added source parameter for post donation page
Browse files Browse the repository at this point in the history
  • Loading branch information
shivashankar-ror committed Sep 18, 2019
1 parent 625e280 commit cf51fe0
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 15 deletions.
2 changes: 2 additions & 0 deletions app/controllers/api/go_cardless_controller.rb
Expand Up @@ -56,6 +56,8 @@ def payment_options
session_token: session[:go_cardless_session_id]
}.tap do |options|
options[:extra_params] = unsafe_params[:extra_action_fields] if unsafe_params[:extra_action_fields].present?
options[:extra_params] ||= {}
options[:extra_params][:source] = params[:source] if params[:source].present?
end
end

Expand Down
2 changes: 2 additions & 0 deletions app/controllers/api/payment/braintree_controller.rb
Expand Up @@ -58,6 +58,8 @@ def payment_options
store_in_vault: store_in_vault?
}.to_hash.tap do |options|
options[:extra_params] = unsafe_params[:extra_action_fields] if unsafe_params[:extra_action_fields].present?
options[:extra_params] ||= {}
options[:extra_params][:source] = params[:source] if params[:source].present?
end
end

Expand Down
2 changes: 2 additions & 0 deletions app/javascript/components/Payment/Payment.js
Expand Up @@ -170,6 +170,7 @@ export class Payment extends Component {
...this.donationData(),
device_data: this.state.deviceData,
provider: 'GC',
source: window.champaign.personalization.urlParams.source,
};
const url = `/api/go_cardless/pages/${
this.props.page.id
Expand Down Expand Up @@ -241,6 +242,7 @@ export class Payment extends Component {
...this.donationData(),
payment_method_nonce: data.nonce,
device_data: this.state.deviceData,
source: window.champaign.personalization.urlParams.source,
recaptcha_token,
recaptcha_action,
};
Expand Down
36 changes: 23 additions & 13 deletions app/liquid/liquid_renderer.rb
@@ -1,20 +1,27 @@
# frozen_string_literal: true

# rubocop:disable Metrics/ClassLength

class LiquidRenderer
class LiquidRenderer # rubocop:disable Metrics/ClassLength
include Rails.application.routes.url_helpers

HIDDEN_FIELDS = %w[source bucket referrer_id rid akid referring_akid].freeze

def initialize(page, location: nil, member: nil, url_params: {}, payment_methods: [])
@page = page
@location = location
@member = member
@url_params = url_params
@page = page
@location = location
@member = member
@payment_methods = payment_methods
@url_params = url_params

# if @url_params.dig(:source)
# @url_params.merge!(source: formatted_source, original_source: url_params[:source])
# end
end

# def formatted_source
# # @page.ak_donation_resource_uri
# # "post-action-#{@page.try(:id)}-#{@url_params[:source]}" : nil)
# end

def render
render_layout(@page.liquid_layout)
end
Expand All @@ -37,8 +44,8 @@ def render_follow_up
def personalization_data
{
url_params: @url_params,
member: member_data,
location: location,
member: member_data,
location: location,
form_values: form_values,
outstanding_fields: outstanding_fields,
donation_bands: donation_bands,
Expand Down Expand Up @@ -66,11 +73,11 @@ def render_layout(layout, extra_data = {})
# are not used when rendering markup
def markup_data
{
images: images,
named_images: named_images,
images: images,
named_images: named_images,
primary_image: image_urls(@page.image_to_display),
shares: Shares.get_all(@page),
locale: @page.language&.code || 'en',
shares: Shares.get_all(@page),
locale: @page.language&.code || 'en',
follow_up_url: follow_up_url
}
.merge(@page.liquid_data)
Expand Down Expand Up @@ -148,19 +155,22 @@ def isolate_from_plugin_data(field)

def location
return @location if @location.blank?

country_code = if @member.try(:country) && @member.country.length == 2
@member.country
else
@location.country_code
end
return @location.data if country_code.blank?
return { country: 'US' } if country_code == 'RD'

currency = Donations::Utils.currency_from_country_code(country_code)
@location.data.merge(currency: currency, country: country_code)
end

def image_urls(img)
return { urls: { large: '', small: '', original: '' } } if img.blank? || img.content.blank?

{ urls: { large: img.content.url(:large), small: img.content.url(:thumb), original: img.content.url(:original) } }
end

Expand Down
6 changes: 5 additions & 1 deletion app/models/page.rb
Expand Up @@ -55,7 +55,7 @@
# fk_rails_... (primary_image_id => images.id)
#

class Page < ApplicationRecord
class Page < ApplicationRecord # rubocop:disable ClassLength
extend FriendlyId
has_paper_trail

Expand Down Expand Up @@ -179,6 +179,10 @@ def subscriptions_count
braintree_subscriptions.count + go_cardless_subscriptions.count
end

def donation_followup?
follow_up_liquid_layout.try(:title).to_s.include?('donate')
end

private

def switch_plugins
Expand Down
21 changes: 20 additions & 1 deletion app/services/manage_braintree_donation.rb
Expand Up @@ -36,7 +36,9 @@ def create
action_express_donation: 0,
store_in_vault: @store_in_vault
}.tap do |params|
params[:recurrence_number] = 0 if @is_subscription
params[:recurrence_number] = 0 if @is_subscription

params[:source] = request_source if @page.try(:akit_donation_page_id)
end
)

Expand All @@ -45,6 +47,23 @@ def create

private

def page
@page ||= Page.find(@params[:page_id])
end

def akit_donation_page_id
@page.ak_donation_resource_uri.to_s.split('/').last
end

def request_source
page.donation_followup? ? "post-action-#{akit_donation_page_id}-#{original_source}" : nil
end

def original_source
# By default AK takes website if source parameter is empty
@params.fetch(:source, 'website')
end

def transaction
@transaction ||= @braintree_result.transaction || @braintree_result.subscription.transactions.try(:last)
end
Expand Down
9 changes: 9 additions & 0 deletions app/services/manage_donation.rb
Expand Up @@ -2,6 +2,15 @@

class ManageDonation
def self.create(params:, extra_params: {})
page = Page.find(params[:page_id])
akit_donation_page_id = page.ak_donation_resource_uri.to_s.split('/').last

source = params[:source] || 'website'
if page.donation_followup?
source = "post-action-#{akit_donation_page_id}-#{source}"
params[:source] = source
end

ManageAction.create(params, extra_params: { donation: true }.merge(extra_params.clone))
end
end
1 change: 1 addition & 0 deletions spec/liquid/liquid_renderer_spec.rb
Expand Up @@ -358,6 +358,7 @@
url_params.merge!(hiddens)
renderer = LiquidRenderer.new(page, member: member, url_params: url_params)
expected = { country: 'NI', email: 'sup@dude.com' }.merge(hiddens)
expected[:source] = "post-action-#{page.id}-c"
expect(renderer.personalization_data['form_values']).to eq(expected.stringify_keys)
end
end
Expand Down

0 comments on commit cf51fe0

Please sign in to comment.