Skip to content
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

added refund support for card payments #1128

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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