Skip to content

Instantly share code, notes, and snippets.

@itSQualL
Created August 6, 2021 07:18
Show Gist options
  • Save itSQualL/3a9d71d8aef5639a332ea3d00395f455 to your computer and use it in GitHub Desktop.
Save itSQualL/3a9d71d8aef5639a332ea3d00395f455 to your computer and use it in GitHub Desktop.
# We want to be sure that we keep a log into our Bank.Log table of all responses got from the Bank.
# Can you state if that will happen with this code? Please answer yes/no and *why*.
#
# Please consider all input values correct.
class User < ApplicationRecord
validates :customer_confidence, numericality: { greater_than_or_equal_to: 0 }
end
class Merchant
def self.pay_purchase(user, purchase_id, amount)
ActiveRecord::Base.transaction do
response = Bank::API::Http.pay_with(credit_card: user.credit_card, amount: amount)
Bank::Log.create(user: user,
purchase_id: purchase_id,
credit_card_id: user.credit_card.id,
amount: amount,
data: response.to_json)
user.customer_confidence += (response.status == "201") ? 1 : -1
user.save!
response.status == "201"
rescue StandardError => e
Rollbar.error("Unexpected error: #{e.message}")
raise ActiveRecord::Rollback.new(e)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment