Class: PaymentMethod::Komerci

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

Instance Method Summary collapse

Instance Method Details

#authorize(_amount, source, gateway_options) ⇒ ActiveMerchant::Billing::Response

Authorizes the payment to Cielo

Returns:

  • (ActiveMerchant::Billing::Response)

Author:

  • Isabella Santos



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/models/spree/payment_method/komerci.rb', line 42

def authorize(_amount, source, gateway_options)
  if gateway_options[:portions].nil?
    return ActiveMerchant::Billing::Response.new(false, Spree.t('komerci.messages.invalid_portions'), {}, {})
  end

  params = prepare_authorize_params source, gateway_options
  params[:transacao] = '73'

  if gateway_options[:portions] == 1
    params[:parcelas] = '00'
  else
    params[:parcelas] = gateway_options[:portions]
  end

  response = Spree::KomerciConfig.authorize params

  verify_authorize_response(source, gateway_options, response, 'authorize')
end

#cancel(response_code) ⇒ ActiveMerchant::Billing::Response

Cancel the payment

Returns:

  • (ActiveMerchant::Billing::Response)

Author:

  • Isabella Santos



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'app/models/spree/payment_method/komerci.rb', line 140

def cancel(response_code)
  transaction = Spree::KomerciTransaction.find_by order_number: response_code

  params = {
      total: transaction.total,
      filiacao: Spree::KomerciConfig[:afiliation_key],
      numcv: transaction.order_number,
      numautor: transaction.authorization_number,
      usr: Spree::KomerciConfig[:komerci_user],
      pwd: Spree::KomerciConfig[:komerci_password],
      concentrador: nil
  }

  response = Spree::KomerciConfig.void_transaction params

  if response[:success] == true
    ActiveMerchant::Billing::Response.new(true, Spree.t('komerci.messages.cancel_success'), {}, authorization: response_code)
  else
    # Retorna como cancelado, mas avisando que o cancelamento deve ser feito no Portal
    ActiveMerchant::Billing::Response.new(true, Spree.t('komerci.messages.cancel_error_date'), {}, authorization: response_code)
  end
end

#capture(amount, response_code, _gateway_options) ⇒ ActiveMerchant::Billing::Response

Captures the payment

Returns:

  • (ActiveMerchant::Billing::Response)

Author:

  • Isabella Santos



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/models/spree/payment_method/komerci.rb', line 67

def capture(amount, response_code, _gateway_options)
  total = sprintf('%0.2f', amount / 100)
  transaction = Spree::KomerciTransaction.find_by order_number: response_code

  if transaction.payment.portions == 1
    transaction_option = '04'
    portions = '00'
  else
    transaction_option = Spree::KomerciConfig[:portions_type]
    portions = transaction.payment.portions
  end

  params = {
      filiacao: Spree::KomerciConfig[:afiliation_key],
      total: total,
      transorig: transaction_option,
      parcelas: portions,
      data: transaction.created_at.strftime('%Y%m%d'),
      numautor: transaction.authorization_number,
      numcv: transaction.order_number,
      usr: Spree::KomerciConfig[:komerci_user],
      pwd: Spree::KomerciConfig[:komerci_password],
      distribuidor: nil,
      concentrador: nil
  }

  response = Spree::KomerciConfig.conf_authorize params

  if response[:success] == true
    ActiveMerchant::Billing::Response.new(true, Spree.t('komerci.messages.capture_success'), {}, authorization: response_code)
  else
    message = response[:data][:error_number] ? "#{response[:data][:error_number]}: " : ''
    message << response[:data][:error_message]
    ActiveMerchant::Billing::Response.new(false, message, {}, authorization: response_code)
  end
end

#payment_source_classObject



4
5
6
# File 'app/models/spree/payment_method/komerci.rb', line 4

def payment_source_class
  Spree::CreditCard
end

#purchase(_amount, source, gateway_options) ⇒ ActiveMerchant::Billing::Response

Purchases the payment to Komerci

Returns:

  • (ActiveMerchant::Billing::Response)

Author:

  • Isabella Santos



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/models/spree/payment_method/komerci.rb', line 14

def purchase(_amount, source, gateway_options)
  if gateway_options[:portions].nil?
    return ActiveMerchant::Billing::Response.new(false, Spree.t('komerci.messages.invalid_portions'), {}, {})
  end

  if gateway_options[:portions] == 1
    transaction = '04'
    portions = '00'
  else
    transaction = Spree::KomerciConfig[:portions_type]
    portions = gateway_options[:portions]
  end

  params = prepare_authorize_params source, gateway_options
  params[:transacao] = transaction
  params[:parcelas] = portions

  response = Spree::KomerciConfig.authorize params

  verify_authorize_response(source, gateway_options, response, 'purchase')
end

#void(response_code, _gateway_options) ⇒ ActiveMerchant::Billing::Response

Voids the payment

Returns:

  • (ActiveMerchant::Billing::Response)

Author:

  • Isabella Santos



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'app/models/spree/payment_method/komerci.rb', line 110

def void(response_code, _gateway_options)
  transaction = Spree::KomerciTransaction.find_by order_number: response_code

  params = {
      total: transaction.total,
      filiacao: Spree::KomerciConfig[:afiliation_key],
      numcv: transaction.order_number,
      numautor: transaction.authorization_number,
      usr: Spree::KomerciConfig[:komerci_user],
      pwd: Spree::KomerciConfig[:komerci_password],
      concentrador: nil
  }

  response = Spree::KomerciConfig.void_transaction params

  if response[:success] == true
    ActiveMerchant::Billing::Response.new(true, Spree.t('komerci.messages.void_success'), {}, authorization: response_code)
  else
    message = response[:data][:error_number] ? "#{response[:data][:error_number]}: " : ''
    message << response[:data][:error_message]
    ActiveMerchant::Billing::Response.new(false, message, {}, authorization: response_code)
  end
end