Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Commit

Permalink
Merge pull request #11 from adammathys/cancel
Browse files Browse the repository at this point in the history
Add Gateway#cancel
  • Loading branch information
stewart authored Sep 7, 2016
2 parents ef11a9c + a544c82 commit 74836a9
Show file tree
Hide file tree
Showing 5 changed files with 1,111 additions and 20 deletions.
26 changes: 26 additions & 0 deletions app/models/solidus_paypal_braintree/gateway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ class Gateway < ::Spree::PaymentMethod
:order_id
]

VOIDABLE_STATUSES = [
Braintree::Transaction::Status::SubmittedForSettlement,
Braintree::Transaction::Status::Authorized
].freeze

# This is useful in feature tests to avoid rate limited requests from
# Braintree
preference(:client_sdk_enabled, :boolean, default: true)
Expand Down Expand Up @@ -109,6 +114,23 @@ def void(response_code, _source, _gateway_options)
Response.build(result)
end

# Will either refund or void the payment depending on its state.
#
# If the transaction has not yet been settled, we can void the transaction.
# Otherwise, we need to issue a refund.
#
# @api public
# @param response_code [String] the transaction id of the payment to void
# @return [Response]
def cancel(response_code)
transaction = Braintree::Transaction.find(response_code)
if VOIDABLE_STATUSES.include?(transaction.status)
void(response_code, nil, {})
else
credit(cents(transaction.amount), nil, response_code, {})
end
end

def create_profile(_payment)
end

Expand Down Expand Up @@ -146,6 +168,10 @@ def dollars(cents)
Money.new(cents).dollars
end

def cents(dollars)
dollars.to_money.cents
end

def transaction_options(source, options)
params = options.select do |key, _|
ALLOWED_BRAINTREE_OPTIONS.include?(key)
Expand Down
58 changes: 58 additions & 0 deletions spec/fixtures/cassettes/braintree/cancel/not_found.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 74836a9

Please sign in to comment.