Class: SpookAndPay::Providers::Spreedly

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

Constant Summary collapse

FORM_FIELD_NAMES =

The map of generic field names to what is specifically required by Spreedly.

{
  :name             => "credit_card[full_name]",
  :number           => "credit_card[number]",
  :expiration_month => "credit_card[month]",
  :expiration_year  => "credit_card[year]",
  :cvv              => "credit_card[verification_value]"
}.freeze

Instance Attribute Summary collapse

Attributes inherited from Base

#config, #environment

Instance Method Summary collapse

Methods inherited from Base

#credit_via_credit_card, #supports_partial_refund?, #supports_refund?

Constructor Details

#initialize(env, config) ⇒ Spreedly

Generate a new instance of the Spreedly provider.

Parameters:

  • Hash

    config

  • config (Hash)

    a customizable set of options

Options Hash (config):

  • String (Object)

    :environment_key

  • String (Object)

    :access_secret

  • String (Object)

    :gateway_token

  • String (Object)

    :currency_code



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/spook_and_pay/providers/spreedly.rb', line 36

def initialize(env, config)
  @gateway_token = config[:gateway_token]
  @currency_code = config[:currency_code] || 'AUD'
  @spreedly = ::Spreedly::Environment.new(
    config[:environment_key],
    config[:access_secret],
    :currency_code => currency_code
  )

  super(env, config)
end

Instance Attribute Details

#currency_codeObject (readonly)

Currency code. Spreedly defaults to USD, but we default to AUD.



27
28
29
# File 'lib/spook_and_pay/providers/spreedly.rb', line 27

def currency_code
  @currency_code
end

#gateway_tokenObject (readonly)

The which refers to a specific gateway.



17
18
19
# File 'lib/spook_and_pay/providers/spreedly.rb', line 17

def gateway_token
  @gateway_token
end

#spreedlyObject (readonly)

An instance of the spreedly spreedly.



22
23
24
# File 'lib/spook_and_pay/providers/spreedly.rb', line 22

def spreedly
  @spreedly
end

Instance Method Details

#authorize_via_credit_card(id, amount) ⇒ Object



155
156
157
158
# File 'lib/spook_and_pay/providers/spreedly.rb', line 155

def authorize_via_credit_card(id, amount)
  result = spreedly.authorize_on_gateway(gateway_token, credit_card_id(id), (amount.to_f * 100).to_i)
  coerce_result(result)
end

#capture_transaction(id) ⇒ Object



102
103
104
105
# File 'lib/spook_and_pay/providers/spreedly.rb', line 102

def capture_transaction(id)
  result = spreedly.capture_transaction(transaction_id(id))
  coerce_result(result)
end

#confirm_payment_submission(query_string, opts) ⇒ Object

Confirms the submission of payment details to Spreedly Core.

Parameters:

  • String

    query_string

  • Hash

    opts

  • opts (Hash)

    a customizable set of options

Options Hash (opts):

  • :amount (String, Numeric)
  • :execute (:purchase, :authorize, :store)

Returns:

  • SpookAndPay::Result



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/spook_and_pay/providers/spreedly.rb', line 72

def confirm_payment_submission(query_string, opts)
  token = Rack::Utils.parse_nested_query(query_string)["token"]
  card = credit_card(token)

  if card.valid?
    case opts[:execute]
    when :authorize then card.authorize!(opts[:amount])
    when :purchase  then card.purchase!(opts[:amount])
    else                 SpookAndPay::Result.new(true, nil, :credit_card => card)
    end
  else
    SpookAndPay::Result.new(false, nil, :credit_card => card, :errors => extract_card_errors(card.raw))
  end
end

#credit_card(id) ⇒ Object



87
88
89
90
# File 'lib/spook_and_pay/providers/spreedly.rb', line 87

def credit_card(id)
  result = spreedly.find_payment_method(id)
  coerce_credit_card(result)
end

#credit_card_from_transaction(id) ⇒ Object



92
93
94
95
# File 'lib/spook_and_pay/providers/spreedly.rb', line 92

def credit_card_from_transaction(id)
  result = spreedly.find_transaction(transaction_id(id))
  coerce_credit_card(result.payment_method)
end

#delete_credit_card(id) ⇒ Object



165
166
167
168
# File 'lib/spook_and_pay/providers/spreedly.rb', line 165

def delete_credit_card(id)
  result = spreedly.redact_payment_method(credit_card_id(id))
  coerce_result(result)
end

#partially_refund_transaction(id, amount) ⇒ Object



112
113
114
115
# File 'lib/spook_and_pay/providers/spreedly.rb', line 112

def partially_refund_transaction(id, amount)
  result = spreedly.refund_transaction(transaction_id(id), :amount => (amount.to_f * 100).to_i)
  coerce_result(result)
end

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

Returns Hash.

Parameters:

  • String

    redirect_url

  • Hash

    opts

Returns:

  • Hash



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/spook_and_pay/providers/spreedly.rb', line 51

def prepare_payment_submission(redirect_url, opts = {})
  config = {
    :url            => spreedly.transparent_redirect_form_action,
    :field_names    => self.class::FORM_FIELD_NAMES,
    :hidden_fields  => {:redirect_url => redirect_url, :environment_key => spreedly.key}
  }

  if opts[:token]
    config[:hidden_fields][:payment_method_token] = opts[:token]
  end

  config
end

#purchase_via_credit_card(id, amount) ⇒ Object



160
161
162
163
# File 'lib/spook_and_pay/providers/spreedly.rb', line 160

def purchase_via_credit_card(id, amount)
  result = spreedly.purchase_on_gateway(gateway_token, credit_card_id(id), (amount.to_f * 100).to_i)
  coerce_result(result)
end

#refund_transaction(id) ⇒ Object



107
108
109
110
# File 'lib/spook_and_pay/providers/spreedly.rb', line 107

def refund_transaction(id)
  result = spreedly.refund_transaction(transaction_id(id))
  coerce_result(result)
end

#retain_credit_card(id) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Calls the retain action on the provider’s vault.

Parameters:

Returns:

  • SpookAndPay::Result



175
176
177
178
# File 'lib/spook_and_pay/providers/spreedly.rb', line 175

def retain_credit_card(id)
  result = spreedly.retain_payment_method(credit_card_id(id))
  coerce_result(result)
end

#supports_authorize?Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/spook_and_pay/providers/spreedly.rb', line 145

def supports_authorize?
  check_support_for('supports_authorize')
end

#supports_capture?Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/spook_and_pay/providers/spreedly.rb', line 141

def supports_capture?
  check_support_for('supports_capture')
end

#supports_credit?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/spook_and_pay/providers/spreedly.rb', line 137

def supports_credit?
  false
end

#supports_delete?Boolean

Returns:

  • (Boolean)


149
150
151
152
153
# File 'lib/spook_and_pay/providers/spreedly.rb', line 149

def supports_delete?
  # This does not check the gateway, since the redaction is specific to
  # Spreedly's store, not the gateway.
  true
end

#supports_purchase?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/spook_and_pay/providers/spreedly.rb', line 129

def supports_purchase?
  check_support_for('supports_purchase')
end

#supports_void?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/spook_and_pay/providers/spreedly.rb', line 133

def supports_void?
  check_support_for('supports_void')
end

#transaction(id) ⇒ Object



97
98
99
100
# File 'lib/spook_and_pay/providers/spreedly.rb', line 97

def transaction(id)
  result = spreedly.find_transaction(id)
  coerce_transaction(result)
end

#void_transaction(id) ⇒ Object



117
118
119
120
# File 'lib/spook_and_pay/providers/spreedly.rb', line 117

def void_transaction(id)
  result = spreedly.void_transaction(transaction_id(id))
  coerce_result(result)
end