-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added refund support for card payments
- Loading branch information
1 parent
697fafe
commit 7c573e9
Showing
14 changed files
with
226 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class CaptureContract < Dry::Validation::Contract | ||
# include BaseContract | ||
|
||
params do | ||
required(:amount).filled(:decimal) | ||
required(:payment_reference).filled(:string) | ||
end | ||
|
||
rule(:payment_reference) do | ||
key.failure(I18n.t('api_errors.invalid_payment_reference')) unless Invoice.exists?(payment_reference: value) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class VoidContract < Dry::Validation::Contract | ||
# include BaseContract | ||
|
||
params do | ||
required(:payment_reference).filled(:string) | ||
end | ||
|
||
rule(:payment_reference) do | ||
key.failure(I18n.t('api_errors.invalid_payment_reference')) unless Invoice.exists?(payment_reference: value) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
module Api | ||
module V1 | ||
module Refund | ||
class CaptureController < ApplicationController | ||
before_action :load_invoice | ||
|
||
def create | ||
unless @invoice.payment_method == 'card' | ||
render json: { message: 'Invoice was not paid by card' }, status: :ok and return | ||
end | ||
|
||
response = CaptureService.call(amount: @invoice.transaction_amount, payment_reference: @invoice.payment_reference) | ||
|
||
if response.result? | ||
@invoice.update(status: 'captured') | ||
render json: { message: 'Invoice was captured' }, status: :ok | ||
else | ||
render json: { error: response.errors }, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
private | ||
|
||
def load_invoice | ||
@invoice = ::Invoice.find_by(invoice_number: params[:params][:invoice_number]) | ||
return if @invoice.present? | ||
|
||
render json: { error: 'Invoice not found' }, status: :not_found | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
class CaptureService | ||
include Request | ||
include ApplicationService | ||
include ActionView::Helpers::NumberHelper | ||
|
||
attr_reader :amount, :payment_reference | ||
|
||
def initialize(amount:, payment_reference:) | ||
@amount = amount | ||
@payment_reference = payment_reference | ||
end | ||
|
||
def self.call(amount:, payment_reference:) | ||
new(amount:, payment_reference:).call | ||
end | ||
|
||
def call | ||
contract = CaptureContract.new | ||
result = contract.call(amount:, payment_reference:) | ||
|
||
if result.success? | ||
response = base_request | ||
struct_response(response) | ||
else | ||
parse_validation_errors(result) | ||
end | ||
end | ||
|
||
private | ||
|
||
def base_request | ||
uri = URI("#{GlobalVariable::BASE_ENDPOINT}#{GlobalVariable::CAPTURE_ENDPOINT}") | ||
post(direction: 'everypay', path: uri, params: body) | ||
end | ||
|
||
def body | ||
{ | ||
'api_username' => GlobalVariable::API_USERNAME, | ||
'amount' => number_with_precision(amount, precision: 2), | ||
'payment_reference' => payment_reference, | ||
'nonce' => nonce, | ||
'timestamp' => "#{Time.zone.now.to_formatted_s(:iso8601)}" | ||
} | ||
end | ||
|
||
def nonce | ||
rand(10**30).to_s.rjust(30, '0') | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
class VoidService | ||
include Request | ||
include ApplicationService | ||
|
||
attr_reader :payment_reference | ||
|
||
def initialize(payment_reference:) | ||
@payment_reference = payment_reference | ||
end | ||
|
||
def self.call(payment_reference:) | ||
new(payment_reference: payment_reference).call | ||
end | ||
|
||
def call | ||
contract = VoidContract.new | ||
result = contract.call(payment_reference: payment_reference) | ||
|
||
puts '-------- VOID SERVICE --------' | ||
puts result.inspect | ||
puts '-------- END VOID SERVICE --------' | ||
|
||
if result.success? | ||
response = base_request | ||
struct_response(response) | ||
else | ||
parse_validation_errors(result) | ||
end | ||
end | ||
|
||
private | ||
|
||
def base_request | ||
uri = URI("#{GlobalVariable::BASE_ENDPOINT}#{GlobalVariable::VOID_ENDPOINT}") | ||
post(direction: 'everypay', path: uri, params: body) | ||
end | ||
|
||
def body | ||
{ | ||
'api_username' => GlobalVariable::API_USERNAME, | ||
'payment_reference' => payment_reference, | ||
'nonce' => nonce, | ||
'timestamp' => "#{Time.zone.now.to_formatted_s(:iso8601)}" | ||
} | ||
end | ||
|
||
def nonce | ||
rand(10**30).to_s.rjust(30, '0') | ||
end | ||
end |
Oops, something went wrong.