Skip to content

Commit

Permalink
A2U payments: Protect against double-paying (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
hklee93 authored Jan 25, 2023
1 parent c439f34 commit 1c4fd7e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
10 changes: 10 additions & 0 deletions lib/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,15 @@ def initialize(message, payment_id)
@payment_id = payment_id
end
end

class TxidAlreadyLinkedError < StandardError
attr_reader :payment_id
attr_reader :txid

def initialize(message, payment_id, txid)
super(message)
@payment_id = payment_id
@txid = txid
end
end
end
6 changes: 5 additions & 1 deletion lib/pi_network.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ def create_payment(payment_data)
def submit_payment(payment_id)
payment = @open_payments[payment_id]

if payment.nil?
if payment.nil? || payment["identifier"] != payment_id
payment = get_payment(payment_id)
txid = payment["transaction"]["txid"]
raise Errors::TxidAlreadyLinkedError.new("This payment already has a linked txid", payment_id, txid) if txid.present?
end

set_horizon_client(payment["network"])
Expand Down Expand Up @@ -89,6 +91,7 @@ def complete_payment(identifier, txid)
http_headers
)

@open_payments.delete(identifier)
handle_http_response(response, "An unknown error occurred while completing the payment")
end

Expand All @@ -99,6 +102,7 @@ def cancel_payment(identifier)
http_headers,
)

@open_payments.delete(identifier)
handle_http_response(response, "An unknown error occurred while cancelling the payment")
end

Expand Down

0 comments on commit 1c4fd7e

Please sign in to comment.