Skip to content

Commit

Permalink
Merge pull request #512 from ashkan18/payment-service-confirm-return-…
Browse files Browse the repository at this point in the history
…transaction

Make sure payment service returns failed transaction on failed confirm
  • Loading branch information
sweir27 committed Oct 9, 2019
2 parents a6ae90a + 332584e commit 84dc3b3
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Expand Up @@ -33,7 +33,7 @@ Metrics/MethodLength:
- "db/migrate/*"

Metrics/ModuleLength:
Max: 170
Max: 180
Exclude:
- "spec/**/*"

Expand Down
1 change: 0 additions & 1 deletion app/controllers/errors/error_types.rb
Expand Up @@ -55,7 +55,6 @@ module Errors
processing: %i[
artwork_version_mismatch
cannot_capture
cannot_confirm
capture_failed
charge_authorization_failed
insufficient_inventory
Expand Down
2 changes: 1 addition & 1 deletion app/jobs/undeduct_line_item_inventory_job.rb
@@ -1,7 +1,7 @@
class UndeductLineItemInventoryJob < ApplicationJob
queue_as :default
discard_on(Errors::ValidationError) do |job, _error|
Rails.warn.info "Line item #{job.line_item.id} has deleted artwork, skipping inventory undeduction"
Rails.logger.warn("Line item #{job.line_item.id} has deleted artwork, skipping inventory undeduction")
end

attr_accessor :line_item
Expand Down
8 changes: 3 additions & 5 deletions app/services/order_service.rb
Expand Up @@ -58,11 +58,9 @@ def self.submit!(order, user_id)
order_processor.hold
order_processor.store_transaction

if order_processor.failed_payment?
order_processor.revert!
raise Errors::FailedTransactionError.new(:charge_authorization_failed, order_processor.transaction)
elsif order_processor.requires_action?
order_processor.revert!
raise Errors::FailedTransactionError.new(:charge_authorization_failed, order_processor.transaction) if order_processor.failed_payment?

if order_processor.requires_action?
Exchange.dogstatsd.increment 'submit.requires_action'
raise Errors::PaymentRequiresActionError, order_processor.action_data
end
Expand Down
13 changes: 12 additions & 1 deletion lib/payment_service.rb
Expand Up @@ -65,7 +65,7 @@ def self.refund_payment(external_id)

def self.confirm_payment_intent(payment_intent_id)
payment_intent = Stripe::PaymentIntent.retrieve(payment_intent_id)
raise Errors::ProcessingError, :cannot_confirm unless payment_intent.status == 'requires_confirmation'
return payment_intent_transaction_failure(payment_intent_id: payment_intent_id, transaction_type: Transaction::CONFIRM, failure_code: 'cannot_confirm', failure_message: 'Payment intent is not in a confirmable state') if payment_intent.status != 'requires_confirmation'

payment_intent.confirm
Transaction.new(
Expand Down Expand Up @@ -182,4 +182,15 @@ def self.transaction_from_payment_intent_failure(exc, transaction_type: nil)
payload: exc.json_body
)
end

def self.payment_intent_transaction_failure(payment_intent_id:, transaction_type:, failure_code:, failure_message:)
Transaction.new(
external_id: payment_intent_id,
external_type: Transaction::PAYMENT_INTENT,
transaction_type: transaction_type,
failure_code: failure_code,
failure_message: failure_message,
status: Transaction::FAILURE
)
end
end
11 changes: 9 additions & 2 deletions spec/lib/payment_service_spec.rb
Expand Up @@ -291,9 +291,16 @@
end

describe '#confirm_payment_intent' do
it 'raises error if payment_intent not in expected state' do
it 'returns failed transaction if payment_intent is not in expected state' do
mock_retrieve_payment_intent(status: 'requires_action')
expect { PaymentService.confirm_payment_intent('pi_1') }.to raise_error(Errors::ProcessingError)
transaction = PaymentService.confirm_payment_intent('pi_1')
expect(transaction).to have_attributes(
external_id: 'pi_1',
external_type: Transaction::PAYMENT_INTENT,
transaction_type: Transaction::CONFIRM,
status: Transaction::FAILURE,
failure_code: 'cannot_confirm'
)
end

it 'confirms the payment intent and stores transaction' do
Expand Down

0 comments on commit 84dc3b3

Please sign in to comment.