From f1e16e581ef509fc2f774ac33262e79602f2cce0 Mon Sep 17 00:00:00 2001 From: Oleg Hasjanov Date: Mon, 18 Sep 2023 15:33:45 +0300 Subject: [PATCH] added refund support for card payments --- app/jobs/capture_job.rb | 63 ++++++++++++++++++++++++ app/jobs/refund_job.rb | 25 +++++----- app/models/domain_participate_auction.rb | 7 +-- app/models/result_creator.rb | 17 +++++-- 4 files changed, 93 insertions(+), 19 deletions(-) create mode 100644 app/jobs/capture_job.rb diff --git a/app/jobs/capture_job.rb b/app/jobs/capture_job.rb new file mode 100644 index 000000000..620405ba4 --- /dev/null +++ b/app/jobs/capture_job.rb @@ -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 diff --git a/app/jobs/refund_job.rb b/app/jobs/refund_job.rb index d1b267042..848c5ae50 100644 --- a/app/jobs/refund_job.rb +++ b/app/jobs/refund_job.rb @@ -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 @@ -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 diff --git a/app/models/domain_participate_auction.rb b/app/models/domain_participate_auction.rb index 315516159..d74b96798 100644 --- a/app/models/domain_participate_auction.rb +++ b/app/models/domain_participate_auction.rb @@ -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 diff --git a/app/models/result_creator.rb b/app/models/result_creator.rb index a835ec40d..fdcfaebbb 100644 --- a/app/models/result_creator.rb +++ b/app/models/result_creator.rb @@ -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