Class: PaymentMethod::Billet

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

Instance Method Summary collapse

Instance Method Details

#authorize(amount, source, *args) ⇒ ActiveMerchant::Billing::Response

Authorizes the payment saving the amount, document number (payment ID) on source object (Spree::Billet)

Returns:

  • (ActiveMerchant::Billing::Response)

Author:

  • Isabella Santos



37
38
39
40
41
42
43
44
# File 'app/models/spree/payment_method/billet.rb', line 37

def authorize(amount, source, *args)
  source.amount = amount.to_d / 100
  source.document_number = source.payment.id

  ret = source.save

  ActiveMerchant::Billing::Response.new(ret, '', {}, authorization: source.document_number)
end

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

Cancel the payment modifying the status of the source (Spree::Billet)

Returns:

  • (ActiveMerchant::Billing::Response)

Author:

  • Isabella Santos



90
91
92
93
94
95
96
97
98
# File 'app/models/spree/payment_method/billet.rb', line 90

def cancel(response_code)
  billet = Spree::Billet.find_by document_number: response_code
  billet.status = 'void'
  billet.save

  ActiveMerchant::Billing::Response.new(true, 'Billet Method: Successfully canceled', {}, authorization: response_code)
rescue
  ActiveMerchant::Billing::Response.new(false, 'Billet Method: Failed when try cancel', {}, {})
end

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

Captures the payment modifying the status and amount and saving the paid date of the source (Spree::Billet)

Returns:

  • (ActiveMerchant::Billing::Response)

Author:

  • Isabella Santos



54
55
56
57
58
59
60
61
62
63
64
# File 'app/models/spree/payment_method/billet.rb', line 54

def capture(amount, response_code, gateway_options)
  billet = Spree::Billet.find_by document_number: response_code
  billet.amount = amount.to_d / 100
  billet.paid_in = Date.today
  billet.status = 'paid'
  billet.save

  ActiveMerchant::Billing::Response.new(true, 'Billet Method: Successfully captured', {}, authorization: response_code)
rescue
  ActiveMerchant::Billing::Response.new(false, 'Billet Method: Failed when try capture', {}, {})
end

#payment_source_classObject



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

def payment_source_class
  Spree::Billet
end

#purchase(amount, source, *args) ⇒ ActiveMerchant::Billing::Response

Purchases the payment saving the amount, document number (payment ID) on source object (Spree::Billet) This method is called when th payment method is set to auto capture so, this will create the billet with status paid

Returns:

  • (ActiveMerchant::Billing::Response)

Author:

  • Isabella Santos



18
19
20
21
22
23
24
25
26
27
# File 'app/models/spree/payment_method/billet.rb', line 18

def purchase(amount, source, *args)
  source.amount = amount.to_d / 100
  source.document_number = source.payment.id
  source.status = 'paid'

  source.payment.response_code = source.document_number
  ret = source.save

  ActiveMerchant::Billing::Response.new(ret, '', {}, authorization: source.document_number)
end

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

Voids the payment modifying the status of the source (Spree::Billet)

Returns:

  • (ActiveMerchant::Billing::Response)

Author:

  • Isabella Santos



73
74
75
76
77
78
79
80
81
# File 'app/models/spree/payment_method/billet.rb', line 73

def void(response_code, gateway_options)
  billet = Spree::Billet.find_by document_number: response_code
  billet.status = 'void'
  billet.save

  ActiveMerchant::Billing::Response.new(true, 'Billet Method: Successfully voided', {}, authorization: response_code)
rescue
  ActiveMerchant::Billing::Response.new(false, 'Billet Method: Failed when try void', {}, {})
end