Skip to content

Commit

Permalink
Add reverse_payment (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
engshien authored Jun 18, 2024
1 parent a7bfe71 commit 04e02bc
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/checkout_sdk/payments/base_payments_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ def void_payment(payment_id, void_request = nil, idempotency_key = nil)
void_request,
idempotency_key)
end

# @param [String] payment_id
# @param [Hash, ReverseRequest] reverse_request
# @param [String, nil] idempotency_key
def reverse_payment(payment_id, reverse_request = nil, idempotency_key = nil)
api_client.invoke_post(build_path(PAYMENTS_PATH, payment_id, 'reversals'),
sdk_authorization,
reverse_request,
idempotency_key)
end
end
end
end
14 changes: 14 additions & 0 deletions lib/checkout_sdk/payments/reverse_request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

module CheckoutSdk
module Payments
# @!attribute reference
# @return [String]
# @!attribute metadata
# @return [Hash{String => Object}]
class ReverseRequest
attr_accessor :reference,
:metadata
end
end
end
35 changes: 35 additions & 0 deletions spec/checkout_sdk/payments/reverse_payments_integration_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
RSpec.describe CheckoutSdk::Payments do
include PaymentsHelper

describe '.reverse_payment' do
context 'when attempt payment' do
it 'reverse payment' do
payment_response = make_card_payment

request = CheckoutSdk::Payments::ReverseRequest.new
request.reference = SecureRandom.uuid

response = default_sdk.payments.reverse_payment(payment_response.id, request)
assert_response(response, %w[reference
action_id
_links])
end

it 'reverse payment idempotent' do
payment_response = make_card_payment

request = CheckoutSdk::Payments::ReverseRequest.new

request.reference = SecureRandom.uuid
idempotency_key = new_idempotency_key

proc = lambda { default_sdk.payments.reverse_payment(payment_response.id, request, idempotency_key) }
proc2 = lambda { default_sdk.payments.reverse_payment(payment_response.id, request, idempotency_key) }

reverse_response_1 = retriable(proc)
reverse_response_2 = retriable(proc2)
expect(reverse_response_1.action_id).to eq(reverse_response_2.action_id)
end
end
end
end

0 comments on commit 04e02bc

Please sign in to comment.