Class: PaymentMethod::Webpay

Inherits:
PaymentMethod
  • Object
show all
Defined in:
app/models/spree/payment_method/webpay.rb

Overview

not precise notation, but required for Rails convention

Constant Summary collapse

CVC_CODE_TRANSLATOR =

Payment related methods ================ Options are ignored unless they are mentioned in parameters list.

{
  'pass' => 'M',
  'fail' => 'N',
  'unchecked' => 'P'
}

Instance Method Summary collapse

Instance Method Details

#authorize(money, paysource, options = {}) ⇒ Object

Performs an authorization, which reserves the funds on the customer’s credit card, but does not charge the card.

Parameters

  • money – The amount to be authorized as an Integer value in cents.

  • paysource – The CreditCard or Check details for the transaction.



68
69
70
# File 'app/models/spree/payment_method/webpay.rb', line 68

def authorize(money, paysource, options = {})
  create_charge(money, paysource, false)
end

#capture(money, authorization, options = {}) ⇒ Object

Captures the funds from an authorized transaction.

Parameters

  • money – The amount to be captured as an Integer value in cents.

  • authorization – The authorization returned from the previous authorize request.



88
89
90
# File 'app/models/spree/payment_method/webpay.rb', line 88

def capture(money, authorization, options = {})
  wrap_in_active_merchant_response { client.charge.capture(id: authorization, amount: money) }
end

#create_profile(payment) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'app/models/spree/payment_method/webpay.rb', line 121

def create_profile(payment)
  return if payment.source.gateway_customer_profile_id.present?

  begin
    customer = client.customer.create(
      email: payment.order.email,
      description: payment.order.name,
      card: payment.source.gateway_payment_profile_id,
      )
    payment.source.update_attributes!({
        gateway_customer_profile_id: customer.id,
        gateway_payment_profile_id: nil
      })
  rescue WebPay::ApiError => e
    payment.send(:gateway_error, e.respond_to?(:data) ? e.data.error.message : e.message)
  end
end

#credit(money, source, identification, options = {}) ⇒ Object



117
118
119
# File 'app/models/spree/payment_method/webpay.rb', line 117

def credit(money, source, identification, options = {})
  refund(money, source, identification, options)
end

#method_typeObject



14
15
16
# File 'app/models/spree/payment_method/webpay.rb', line 14

def method_type
  'webpay'
end

#payment_profiles_supported?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'app/models/spree/payment_method/webpay.rb', line 18

def payment_profiles_supported?
  true
end

#payment_source_classObject

Meta ================



10
11
12
# File 'app/models/spree/payment_method/webpay.rb', line 10

def payment_source_class
  CreditCard
end

#purchase(money, paysource, options = {}) ⇒ Object

Perform a purchase, which is essentially an authorization and capture in a single operation.

Parameters

  • money – The amount to be purchased as an Integer value in cents.

  • paysource – The CreditCard or Check details for the transaction.



78
79
80
# File 'app/models/spree/payment_method/webpay.rb', line 78

def purchase(money, paysource, options = {})
  create_charge(money, paysource, true)
end

#refund(money, _source, identification, options = {}) ⇒ Object

Refund a transaction.

This transaction indicates to the gateway that money should flow from the merchant to the customer.

Parameters

  • money – The amount to be credited to the customer as an Integer value.

  • identification – The ID of the original transaction against which the refund is being issued.



110
111
112
113
114
115
# File 'app/models/spree/payment_method/webpay.rb', line 110

def refund(money, _source, identification, options = {})
  wrap_in_active_merchant_response do
    charge = client.charge.retrieve(identification)
    client.charge.refund(id: identification, amount: charge.amount.to_i - charge.amount_refunded.to_i - money)
  end
end

#reusable_sources(order) ⇒ Object

Copied from spree-core gateway.rb



27
28
29
30
31
32
33
34
35
36
37
# File 'app/models/spree/payment_method/webpay.rb', line 27

def reusable_sources(order)
  if order.completed?
    sources_by_order order
  else
    if order.user_id
      self.credit_cards.where(user_id: order.user_id).with_payment_profile
    else
      []
    end
  end
end

#source_required?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'app/models/spree/payment_method/webpay.rb', line 22

def source_required?
  true
end

#supports?(source) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/models/spree/payment_method/webpay.rb', line 39

def supports?(source)
  @account_info ||= client..retrieve
  brand_name =
    case source.brand
    when 'visa' then 'Visa'
    when 'master' then 'MasterCard'
    when 'diners_club' then 'Diners Club'
    when 'american_express' then 'American Express'
    when 'discover' then 'Discover'
    when 'jcb' then 'JCB'
    end
  @account_info.card_types_supported.include?(brand_name)
end

#void(authorization, _source, options = {}) ⇒ Object

Void a previous transaction

Parameters

  • authorization - The authorization returned from the previous authorize request.



97
98
99
# File 'app/models/spree/payment_method/webpay.rb', line 97

def void(authorization, _source, options = {})
  wrap_in_active_merchant_response { client.charge.refund(authorization) }
end