Sha256: 500cb515576badb8f25071267508f199ccef46baec38f96311e5801caa2b0521

Contents?: true

Size: 1.94 KB

Versions: 3

Compression:

Stored size: 1.94 KB

Contents

module ActiveMerchant #:nodoc:
  module Billing #:nodoc:
    module Integrations #:nodoc:
      module BitPay
        class Helper < ActiveMerchant::Billing::Integrations::Helper
          def initialize(order_id, account, options)
            super
            @account = account

            add_field('posData', {'orderId' => order_id}.to_json)
            add_field('fullNotifications', true)
            add_field('transactionSpeed', 'high')
          end

          mapping :amount, 'price'
          mapping :order, 'orderID'
          mapping :currency, 'currency'

          mapping :customer, :first_name => 'buyerName',
                             :email      => 'buyerEmail',
                             :phone      => 'buyerPhone'

          mapping :billing_address, :city     => 'buyerCity',
                                    :address1 => 'buyerAddress1',
                                    :address2 => 'buyerAddress2',
                                    :state    => 'buyerState',
                                    :zip      => 'buyerZip',
                                    :country  => 'buyerCountry'

          mapping :notify_url, 'notificationURL'
          mapping :return_url, 'redirectURL'
          mapping :id, 'id'

          def form_method
            "GET"
          end

          def form_fields
            invoice = create_invoice

            {"id" => invoice['id']}
          end

          private

          def create_invoice
            uri = URI.parse(BitPay.invoicing_url)
            http = Net::HTTP.new(uri.host, uri.port)
            http.use_ssl = true

            request = Net::HTTP::Post.new(uri.request_uri)
            request.content_type = "application/json"
            request.body = @fields.to_json
            request.basic_auth @account, ''

            response = http.request(request)
            JSON.parse(response.body)
          rescue JSON::ParseError
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
activemerchant-1.42.2 lib/active_merchant/billing/integrations/bit_pay/helper.rb
activemerchant-1.42.1 lib/active_merchant/billing/integrations/bit_pay/helper.rb
activemerchant-1.42.0 lib/active_merchant/billing/integrations/bit_pay/helper.rb