-
Notifications
You must be signed in to change notification settings - Fork 0
Refactor PaymentsController for current_user and enhance validations #85
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
Changes from all commits
0856f7b
dba48b0
afb9f1a
77a5f52
7d8b592
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,8 @@ | |
| require 'time' | ||
|
|
||
| class PaymentsController < ApplicationController | ||
| MAX_PAYMENT_AMOUNT = 2000 | ||
|
|
||
| protect_from_forgery with: :exception | ||
| skip_before_action :verify_authenticity_token, only: [:payment_receipt] | ||
| before_action :verify_payment_callback, only: [:payment_receipt] | ||
|
|
@@ -41,7 +43,13 @@ def payment_receipt | |
| end | ||
|
|
||
| def make_payment | ||
| processed_url = generate_hash(@current_user, params['amount']) | ||
| amount = validated_payment_amount(params['amount']) | ||
| if amount.nil? | ||
| redirect_to all_payments_path, alert: 'Please enter a valid payment amount.' | ||
| return | ||
| end | ||
|
|
||
| processed_url = generate_hash(current_user, amount) | ||
| redirect_to processed_url, allow_other_host: true | ||
| end | ||
|
|
||
|
|
@@ -55,6 +63,7 @@ def payment_show | |
| @cost_subscription = current_application_settings.subscription_cost.to_f | ||
| @total_cost = cost_lodging + cost_partner + (@has_subscription ? @cost_subscription : 0) | ||
| @balance_due = @total_cost - @ttl_paid | ||
| @max_payment_amount = max_payment_amount_for(@balance_due) | ||
| end | ||
|
|
||
| def delete_manual_payment | ||
|
|
@@ -115,6 +124,36 @@ def generate_hash(current_user, amount=100) | |
| final_url = connection_hash[url_to_use] + '?' + url_for_payment + 'hash=' + encoded_hash | ||
| end | ||
|
|
||
| def validated_payment_amount(raw_amount) | ||
| amount = Integer(raw_amount, exception: false) | ||
| return nil if amount.nil? || amount <= 0 | ||
|
|
||
| balance_due = current_balance_due | ||
| return nil if balance_due <= 0 | ||
|
|
||
| max_amount = max_payment_amount_for(balance_due) | ||
| return nil if amount > max_amount | ||
|
Comment on lines
+131
to
+135
|
||
|
|
||
| amount | ||
| end | ||
|
|
||
| def max_payment_amount_for(balance_due) | ||
| [balance_due.floor, MAX_PAYMENT_AMOUNT].min | ||
| end | ||
|
|
||
| def current_balance_due | ||
| current_application | ||
| return 0.0 if @current_application.nil? | ||
|
|
||
| cost_lodging = Lodging.find_by(description: @current_application.lodging_selection)&.cost.to_f | ||
| cost_partner = @current_application.partner_registration&.cost.to_f | ||
| has_subscription = @current_application.subscription | ||
| cost_subscription = current_application_settings.subscription_cost.to_f | ||
| total_cost = cost_lodging + cost_partner + (has_subscription ? cost_subscription : 0) | ||
| total_paid = Payment.current_conference_payments.where(user_id: current_user, transaction_status: '1').pluck(:total_amount).map(&:to_f).sum / 100 | ||
| total_cost - total_paid | ||
| end | ||
|
|
||
| def url_params | ||
| params.permit(:amount, :transactionType, :transactionStatus, :transactionId, :transactionTotalAmount, :transactionDate, :transactionAcountType, :transactionResultCode, :transactionResultMessage, :orderNumber, :timestamp, :hash, :conf_year) | ||
| end | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -61,10 +61,13 @@ | |
| <div class="input-group-prepend"> | ||
| <span class="input-group-text">$</span> | ||
| </div> | ||
| <%= f.number_field :amount, value: "#{@balance_due.to_i}", | ||
| within: 1..2000, required: true, class: 'form-control' %> | ||
| <%= f.number_field :amount, value: @max_payment_amount, | ||
| within: 1..@max_payment_amount, required: true, class: 'form-control' %> | ||
| <%= f.submit "Pay Now", class: 'btn btn-sm btn-success ml-2' %> | ||
| </div> | ||
| <small class="form-text text-muted mt-2"> | ||
| The amount is prefilled with your full balance due, but you can enter a different amount to make a partial payment. | ||
| </small> | ||
|
Comment on lines
+68
to
+70
|
||
| </div> | ||
| </div> | ||
| <% end %> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MAX_PAYMENT_AMOUNTis introduced here, but the view still hard-codeswithin: 1..2000. This risks the UI and server validations drifting apart if the cap changes. Consider referencing the controller (or shared) constant from the view, or extracting the cap into a shared configuration constant used by both.