Class: SpookAndPay::Providers::Braintree

Inherits:
Base
  • Object
show all
Defined in:
lib/spook_and_pay/providers/braintree.rb

Constant Summary collapse

FORM_FIELD_NAMES =
{
  :name             => "transaction[credit_card][cardholder_name]",
  :number           => "transaction[credit_card][number]",
  :expiration_month => "transaction[credit_card][expiration_month]",
  :expiration_year  => "transaction[credit_card][expiration_year]",
  :cvv              => "transaction[credit_card][cvv]"
}.freeze

Instance Attribute Summary collapse

Attributes inherited from Base

#config, #environment

Instance Method Summary collapse

Methods inherited from Base

#authorize_via_credit_card, #credit_via_credit_card, #delete_credit_card, #retain_credit_card, #supports_authorize?, #supports_capture?, #supports_credit?, #supports_delete?, #supports_partial_refund?, #supports_purchase?, #supports_refund?, #supports_void?

Constructor Details

#initialize(env, config) ⇒ Braintree

Returns a new instance of Braintree.

Parameters:

  • Hash

    config

  • config (Hash)

    a customizable set of options

Options Hash (config):

  • String (Object)

    :merchant_id

  • String (Object)

    :public_key

  • String (Object)

    :private_key



18
19
20
21
22
23
24
25
26
27
# File 'lib/spook_and_pay/providers/braintree.rb', line 18

def initialize(env, config)
  @adapter = SpookAndPay::Adapters::Braintree.new(
    env,
    config[:merchant_id],
    config[:public_key],
    config[:private_key]
  )

  super(env, config)
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



12
13
14
# File 'lib/spook_and_pay/providers/braintree.rb', line 12

def adapter
  @adapter
end

Instance Method Details

#capture_transaction(id) ⇒ Object



110
111
112
113
# File 'lib/spook_and_pay/providers/braintree.rb', line 110

def capture_transaction(id)
  result = adapter.capture(transaction_id(id))
  generate_result(result)
end

#confirm_payment_submission(query_string, opts = {}) ⇒ Object

Confirms the submission of payment details to the provider.

Parameters:

  • String

    query_string

Returns:

  • SpookAndPay::Result



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/spook_and_pay/providers/braintree.rb', line 67

def confirm_payment_submission(query_string, opts = {})
  result = adapter.confirm(query_string)

  case result
  when ::Braintree::SuccessfulResult
    SpookAndPay::Result.new(
      true,
      result,
      :credit_card => extract_credit_card(result.transaction.credit_card_details, true, false),
      :transaction => extract_transaction(result.transaction)
    )
  when ::Braintree::ErrorResult
    SpookAndPay::Result.new(
      false,
      result,
      :credit_card => extract_credit_card(result.params[:transaction][:credit_card], false, false),
      :transaction => extract_transaction(result.params[:transaction]),
      :errors => extract_errors(result)
    )
  end
end

#credit_card(id) ⇒ Object



89
90
91
92
# File 'lib/spook_and_pay/providers/braintree.rb', line 89

def credit_card(id)
  result = adapter.credit_card(id)
  extract_credit_card(result, true, false) if result
end

#credit_card_from_transaction(id) ⇒ Object



94
95
96
97
98
# File 'lib/spook_and_pay/providers/braintree.rb', line 94

def credit_card_from_transaction(id)
  _id = id.is_a?(String) ? id : id.id
  result = adapter.transaction(_id)
  extract_credit_card(result.credit_card_details, true, false)
end

#partially_refund_transaction(id, amount) ⇒ Object



120
121
122
123
# File 'lib/spook_and_pay/providers/braintree.rb', line 120

def partially_refund_transaction(id, amount)
  result = adapter.partially_refund(transaction_id(id), amount)
  generate_result(result)
end

#prepare_payment_submission(redirect_url, opts = {}) ⇒ Object

Braintree specific version of this method. Can be used to either authorize a payment — and capture it later — or submit a payment for settlement immediately. This is done via the type param.

Because Braintree accepts payment details and processes payment in a single step, this method must also be provided with an amount.

Parameters:

  • String

    redirect_url

  • Hash

    opts

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :vault (true, false)
  • :type (:purchase, :authorize)
  • :amount (String, Numeric)

Returns:

  • Hash



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/spook_and_pay/providers/braintree.rb', line 42

def prepare_payment_submission(redirect_url, opts = {})
  payload = {
    :transaction  => {:type => 'sale', :amount => opts[:amount]},
    :redirect_url => redirect_url
  }

  if opts[:vault]
    (payload[:transaction][:options] ||= {})[:store_in_vault] = true
  end

  if opts[:type] == :purchase
    (payload[:transaction][:options] ||= {})[:submit_for_settlement] = true
  end

  {
    :url            => adapter.transparent_redirect_url,
    :hidden_fields  => {:tr_data => adapter.transaction_data(payload)},
    :field_names    => self.class::FORM_FIELD_NAMES
  }
end

#purchase_via_credit_card(id, amount) ⇒ Object



100
101
102
103
# File 'lib/spook_and_pay/providers/braintree.rb', line 100

def purchase_via_credit_card(id, amount)
  result = adapter.credit_card_purchase(credit_card_id(id), amount)
  generate_result(result)
end

#refund_transaction(id) ⇒ Object



115
116
117
118
# File 'lib/spook_and_pay/providers/braintree.rb', line 115

def refund_transaction(id)
  result = adapter.refund(transaction_id(id))
  generate_result(result)
end

#transaction(id) ⇒ Object



105
106
107
108
# File 'lib/spook_and_pay/providers/braintree.rb', line 105

def transaction(id)
  result = adapter.transaction(id)
  extract_transaction(result) if result
end

#void_transaction(id) ⇒ Object



125
126
127
128
# File 'lib/spook_and_pay/providers/braintree.rb', line 125

def void_transaction(id)
  result = adapter.void(transaction_id(id))
  generate_result(result)
end