Skip to content

Commit

Permalink
Merge pull request #315 from mlibrary/bugfix-receipt-handles-declined…
Browse files Browse the repository at this point in the history
…-payments

fixing receipt to return an Error when the payment has been declined
  • Loading branch information
niquerio authored Oct 2, 2024
2 parents 4103d59 + 4079ef0 commit 3ede09c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 24 deletions.
51 changes: 27 additions & 24 deletions models/fines/receipt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,34 @@ def initialize(payment:, balance:)

def self.for(uniqname:, nelnet_params:, order_number:, is_valid: Nelnet.verify(nelnet_params))
payment = Payment.new(nelnet_params)
if is_valid
payment_verification = Fines.verify_payment(uniqname: uniqname, order_number: order_number)
if payment_verification.instance_of?(AlmaError)
S.logger.error("Fine payment error: order_number: #{payment.order_number}; message: #{payment_verification.message}")
ErrorReceipt.new("There was an error in processing your payment.<br>Your payment order number is: #{payment.order_number}<br>Server error: #{payment_verification.message}</br>")
elsif payment_verification[:has_order_number]
S.logger.error("Fine payment error: order number #{order_number} is already in Alma.")
ErrorReceipt.new("Your payment order number, #{order_number}, is already in the fines database.")
elsif payment_verification[:total_sum].to_f.to_s == "0.0"
S.logger.error("Fine payment error: order number #{order_number} tried to pay $0.00")
ErrorReceipt.new("You do not have a balance. Your payment order number is: #{order_number}.")
else # has not already paid
resp = Fines.pay(uniqname: uniqname, amount: payment.amount, order_number: order_number)
if resp.code != 200
error = AlmaError.new(resp)
S.logger.error("Fine payment error: order number #{order_number}; message: #{error.message}")
ErrorReceipt.new("#{error.message}<br>Your payment order number is: #{order_number}")
else
S.logger.info("Fine payment success")
Receipt.new(payment: payment, balance: resp.parsed_response["total_sum"])
end
end
else # not valid
if !is_valid
S.logger.error("Fine payment error: order number #{order_number} payment could not be validated.")
ErrorReceipt.new("Your payment could not be validated. Your payment order number is: #{payment.order_number}")
return ErrorReceipt.new("Your payment could not be validated. Your payment order number is: #{payment.order_number}")
end
if !/Approved/.match?(nelnet_params["transactionResultMessage"])
return ErrorReceipt.new("There was an error processing your payment.<br/>The error message is: #{nelnet_params["transactionResultMessage"]}<br/>Your payment order number is: #{order_number}")
end

payment_verification = Fines.verify_payment(uniqname: uniqname, order_number: order_number)
if payment_verification.instance_of?(AlmaError)
S.logger.error("Fine payment error: order_number: #{payment.order_number}; message: #{payment_verification.message}")
ErrorReceipt.new("There was an error in processing your payment.<br>Your payment order number is: #{payment.order_number}<br>Server error: #{payment_verification.message}</br>")
elsif payment_verification[:has_order_number]
S.logger.error("Fine payment error: order number #{order_number} is already in Alma.")
ErrorReceipt.new("Your payment order number, #{order_number}, is already in the fines database.")
elsif payment_verification[:total_sum].to_f.to_s == "0.0"
S.logger.error("Fine payment error: order number #{order_number} tried to pay $0.00")
ErrorReceipt.new("You do not have a balance. Your payment order number is: #{order_number}.")
else # has not already paid
resp = Fines.pay(uniqname: uniqname, amount: payment.amount, order_number: order_number)
if resp.code != 200
error = AlmaError.new(resp)
S.logger.error("Fine payment error: order number #{order_number}; message: #{error.message}")
ErrorReceipt.new("#{error.message}<br>Your payment order number is: #{order_number}")
else
S.logger.info("Fine payment success")
Receipt.new(payment: payment, balance: resp.parsed_response["total_sum"])
end
end
end

Expand Down
5 changes: 5 additions & 0 deletions spec/models/fines/receipt_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@
expect(subject.class.name).to eq("ErrorReceipt")
expect(subject.message).to include("balance")
end
it "returns ErrorReceipt if transactionResultMessage is not Approved" do
@params["transactionResultMessage"] = "Declined"
expect(subject.class.name).to eq("ErrorReceipt")
expect(subject.message).to include("Declined")
end
end
describe Payment do
before(:each) do
Expand Down

0 comments on commit 3ede09c

Please sign in to comment.