Sha256: b88c2b83ae8d2a64be9c8173794be5b7eae8e523c4f0a5c054c835762582b49d

Contents?: true

Size: 1.36 KB

Versions: 4

Compression:

Stored size: 1.36 KB

Contents

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

    def complete!
      transaction.response = handle_active_merchant_errors do
        if tender.token.present?
          gateway.purchase(
            transaction.amount.cents,
            tender.token,
            { payment_method_token: true }
          )
        else
          gateway.purchase(
            transaction.amount.cents,
            tender.to_active_merchant,
            { store: true, 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 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/purchase/credit_card.decorator
workarea-braintree-1.1.1 app/models/workarea/payment/purchase/credit_card.decorator
workarea-braintree-1.1.0 app/models/workarea/payment/purchase/credit_card.decorator
workarea-braintree-1.0.3 app/models/workarea/payment/purchase/credit_card.decorator