Sha256: febc86c520b7c0082ee5e03d799ba2c8e0061ce263482aeac1e3dfd204087781

Contents?: true

Size: 925 Bytes

Versions: 2

Compression:

Stored size: 925 Bytes

Contents

# frozen_string_literal: true

require 'securerandom'

module Payment
  class ChargeCreditCard
    include ::Solid::Output.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

2 entries across 2 versions & 1 rubygems

Version Path
solid-adapters-1.1.0 examples/anti_corruption_layer/app/models/payment/charge_credit_card.rb
solid-adapters-1.0.0 examples/anti_corruption_layer/app/models/payment/charge_credit_card.rb