Sha256: d3ec6858d001819117c9952686c534444109dffeeaab682471e4077fa6602f0b

Contents?: true

Size: 1.74 KB

Versions: 4

Compression:

Stored size: 1.74 KB

Contents

module Workarea
  decorate Payment::Authorize::CreditCard, with: :braintree do
    decorated { delegate :address, to: :tender }

    def complete!
      transaction.response = handle_active_merchant_errors do
        if tender.token.present?
          gateway.authorize(
            transaction.amount.cents,
            tender.token,
            {
              payment_method_token: true,
              order_id: order_id,
              email: email,
              billing_address: billing_address
            }
          )
        else
          gateway.authorize(
            transaction.amount.cents,
            tender.to_active_merchant,
            {
              store: true,
              order_id: order_id,
              email: email,
              billing_address: billing_address
            }
          )
        end
      end

      if transaction.response.success? && tender.token.blank?
        tender.token = transaction.response.params["braintree_transaction"]["credit_card_details"]["token"]
        tender.save!
      end
    end

    def cancel!
      return unless transaction.success?

      transaction.cancellation = handle_active_merchant_errors do
        gateway.void(transaction.response.authorization)
      end
    end

    private

    def email
      return unless tender.profile.present?

      tender.profile.email
    end

    def order_id
      tender.payment.id
    end

    def billing_address
      {
        name:       nil,
        company:    nil,
        address1:   address.street,
        address2:   address.street_2,
        city:       address.city,
        state:      address.region,
        country:    address.country.try(:alpha2),
        zip:        address.postal_code,
        phone:      nil
      }
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
workarea-braintree-1.1.2 app/models/workarea/payment/authorize/credit_card.decorator
workarea-braintree-1.1.1 app/models/workarea/payment/authorize/credit_card.decorator
workarea-braintree-1.1.0 app/models/workarea/payment/authorize/credit_card.decorator
workarea-braintree-1.0.3 app/models/workarea/payment/authorize/credit_card.decorator