Skip to content

Commit

Permalink
added refund support for card payments
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegPhenomenon committed Sep 18, 2023
1 parent e8b7b19 commit f1e16e5
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 19 deletions.
63 changes: 63 additions & 0 deletions app/jobs/capture_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
class CaptureJob < ApplicationJob
BASE_URL = AuctionCenter::Application.config
.customization[:billing_system_integration]
&.compact&.fetch(:eis_billing_system_base_url, '')
PATH = '/api/v1/refund/capture'.freeze
INITIATOR = 'auction'.freeze
PAID = 'paid'.freeze

def perform(domain_participate_auction_id, invoice_number)
response = post(PATH, params: { invoice_number: })
d = DomainParticipateAuction.find(domain_participate_auction_id)

if response.status == 200
d.update(status: PAID)
else
inform(d, response.body)
end

JSON.parse response.body
end

def inform(domain_participate_auction, error_message)
admin_emails = User.where(roles: ['administrator']).pluck(:email)

InvoiceMailer.refund_failed(admin_emails, domain_participate_auction.auction, domain_participate_auction.user,
error_message).deliver_later
Rails.logger.info error_message
end

def post(path, params = {})
connection.post(path, JSON.dump(params))
end

def connection
Faraday.new(options) do |faraday|
faraday.adapter Faraday.default_adapter
end
end

private

def options
{
headers: {
'Authorization' => "Bearer #{generate_token}",
'Content-Type' => 'application/json'
},
url: BASE_URL
}
end

def generate_token
JWT.encode(payload, billing_secret)
end

def payload
{ initiator: INITIATOR }
end

def billing_secret
AuctionCenter::Application.config.customization[:billing_system_integration]&.compact&.fetch(:billing_secret, '')
end
end
25 changes: 12 additions & 13 deletions app/jobs/refund_job.rb
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
class RefundJob < ApplicationJob
BASE_URL = AuctionCenter::Application.config
.customization[:billing_system_integration]
.customization[:billing_system_integration]
&.compact&.fetch(:eis_billing_system_base_url, '')
PATH = '/api/v1/refund/auction'.freeze

INITIATOR = 'auction'.freeze
RETURNED = 'returned'.freeze

def perform(domain_participate_auction_id, invoice_number)
response = post(PATH, params: { invoice_number: invoice_number })
response = post(PATH, params: { invoice_number: })
d = DomainParticipateAuction.find(domain_participate_auction_id)

if response.status == 200
d.update(status: 'returned')

JSON.parse response.body
d.update(status: RETURNED)
else
inform(d, response.body)

JSON.parse response.body
end

JSON.parse response.body
end

def inform(domain_participate_auction, error_message)
admin_emails = User.where(roles: ["administrator"]).pluck(:email)
admin_emails = User.where(roles: ['administrator']).pluck(:email)

InvoiceMailer.refund_failed(admin_emails, domain_participate_auction.auction, domain_participate_auction.user, error_message).deliver_later
InvoiceMailer.refund_failed(admin_emails, domain_participate_auction.auction, domain_participate_auction.user,
error_message).deliver_later
Rails.logger.info error_message
end

def post(path, params = {})
connection.post(path, JSON.dump(params))
connection.post(path, JSON.dump(params))
end

def connection
Expand All @@ -44,9 +43,9 @@ def options
{
headers: {
'Authorization' => "Bearer #{generate_token}",
'Content-Type' => 'application/json',
'Content-Type' => 'application/json'
},
url: BASE_URL,
url: BASE_URL
}
end

Expand Down
7 changes: 4 additions & 3 deletions app/models/domain_participate_auction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ class DomainParticipateAuction < ApplicationRecord

enum status: ['paid', 'prepayment', 'returned']

scope :search_by_auction_name_and_user_email, ->(origin) {
scope :search_by_auction_name_and_user_email, -> (origin) {
if origin.present?
self.joins(:user).joins(:auction).where('users.email ILIKE ? OR auctions.domain_name ILIKE ?', "%#{origin}%", "%#{origin}%")
joins(:user).joins(:auction)
.where('users.email ILIKE ? OR auctions.domain_name ILIKE ?', "%#{origin}%", "%#{origin}%")
end
}

def self.search(params = {})
self.search_by_auction_name_and_user_email(params[:search_string])
search_by_auction_name_and_user_email(params[:search_string])
end
end
17 changes: 14 additions & 3 deletions app/models/result_creator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,20 @@ def refund_deposit

participants = DomainParticipateAuction.where(auction_id: auction.id)
participants.each do |participant|
next if participant.user.id == winning_offer.user_id

RefundJob.perform_later(participant.id, participant.invoice_number)
if participant.user.id == winning_offer.user_id
CaptureJob.perform_later(participant.id, participant.invoice_number)
else
RefundJob.perform_later(participant.id, participant.invoice_number)
end

# void - card payment
# other - refund

# TODO:
# Need to indeticate which payment method was used
# if it was paid by credit card, then we need to void it
# VoidJob.perform_later(participant.id, participant.invoice_number)
# otherwise it could be linkpay, then we need to refund it
end
end
end

0 comments on commit f1e16e5

Please sign in to comment.