Sha256: 4a40ec214ebcf5b69121a78d201852c1d3ee0a32f98c956fae48f351cef6fb53

Contents?: true

Size: 933 Bytes

Versions: 1

Compression:

Stored size: 933 Bytes

Contents

# frozen_string_literal: true

require 'securerandom'

module Payment
  class ChargeCreditCard
    include ::BCDD::Result::Context.mixin(config: { addon: { continue: true } })

    attr_reader :payment_gateway

    def initialize(payment_gateway)
      @payment_gateway = ::PaymentGateways::Contract.new(payment_gateway)
    end

    def call(amount:, details: {})
      Given(amount:)
        .and_then(:validate_amount)
        .and_then(:charge_credit_card, details:)
        .and_expose(:payment_charged, %i[payment_id])
    end

    private

    def validate_amount(amount:)
      return Continue() if amount.is_a?(::Numeric) && amount.positive?

      Failure(:invalid_amount, erros: ['amount must be positive'])
    end

    def charge_credit_card(amount:, details:)
      response = payment_gateway.charge_credit_card(amount:, details:)

      Continue(payment_id: ::SecureRandom.uuid) if response.success?
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bcdd-contract-0.1.0 examples/anti_corruption_layer/app/models/payment/charge_credit_card.rb