Skip to content

Commit

Permalink
Allow payment cancelations when payment is cancelable.
Browse files Browse the repository at this point in the history
  • Loading branch information
vernondegoede committed Oct 10, 2018
1 parent f6640d3 commit 8a772c8
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ source "https://rubygems.org"

git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }

gem 'mollie-api-ruby', '~> 4.0.0'
gem 'mollie-api-ruby', '~> 4.1.1'

# Specify your gem's dependencies in spree_mollie_gateway.gemspec
gemspec
36 changes: 25 additions & 11 deletions app/models/spree/gateway/mollie_gateway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def create_customer(user)
customer = Mollie::Customer.create(
email: user.email,
api_key: get_preference(:api_key),
)
)
MollieLogger.debug("Created a Mollie Customer for Spree user with ID #{customer.id}")
customer
end
Expand Down Expand Up @@ -167,6 +167,20 @@ def credit(credit_cents, payment_id, options)
end
end

def cancel(transaction_id)
begin
mollie_payment = ::Mollie::Payment.get(
transaction_id,
api_key: get_preference(:api_key)
)
mollie_payment.delete(transaction_id) if mollie_payment.is_cancelable
ActiveMerchant::Billing::Response.new(true, 'Payment cancelled successful')
rescue Mollie::Exception => e
MollieLogger.debug("Refund failed for order #{order_number}: #{e.message}")
ActiveMerchant::Billing::Response.new(false, 'Payment cancelation unsuccessful')
end
end

def available_methods(params = nil)
method_params = {
api_key: get_preference(:api_key),
Expand Down Expand Up @@ -208,16 +222,16 @@ def update_payment_status(payment)

def update_by_mollie_status!(mollie_payment, payment)
case mollie_payment.status
when 'paid'
payment.complete! unless payment.completed?
payment.order.finalize!
payment.order.update_attributes(:state => 'complete', :completed_at => Time.now)
when 'canceled', 'expired', 'failed'
payment.failure! unless payment.failed?
payment.order.update_attributes(:state => 'payment', :completed_at => nil)
else
MollieLogger.debug('Unhandled Mollie payment state received. Therefore we did not update the payment state.')
payment.order.update_attributes(state: 'payment', completed_at: nil)
when 'paid'
payment.complete! unless payment.completed?
payment.order.finalize!
payment.order.update_attributes(:state => 'complete', :completed_at => Time.now)
when 'canceled', 'expired', 'failed'
payment.failure! unless payment.failed?
payment.order.update_attributes(:state => 'payment', :completed_at => nil)
else
MollieLogger.debug('Unhandled Mollie payment state received. Therefore we did not update the payment state.')
payment.order.update_attributes(state: 'payment', completed_at: nil)
end

payment.source.update(status: payment.state)
Expand Down
18 changes: 18 additions & 0 deletions app/models/spree/payment/processing_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,26 @@ def process!(amount = nil)
end
end

def cancel!
if payment_method.is_a? Spree::Gateway::MollieGateway
cancel_with_mollie
else
cancel_with_spree
end
end

private

def cancel_with_spree
response = payment_method.cancel(response_code)
handle_response(response, :void, :failure)
end

def cancel_with_mollie
response = payment_method.cancel(transaction_id)
handle_response(response, :void, :failure)
end

def process_with_spree
if payment_method && payment_method.auto_capture?
purchase!
Expand Down
2 changes: 1 addition & 1 deletion spree_mollie_gateway.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'coffee-rails'
spec.add_development_dependency 'database_cleaner'

spec.add_runtime_dependency 'mollie-api-ruby', '~> 4.0.0'
spec.add_runtime_dependency 'mollie-api-ruby', '~> 4.1.1'
end

0 comments on commit 8a772c8

Please sign in to comment.