Class: Payment::Gateways::Echeck

Inherits:
BasePaymentGateway
  • Object
show all
Defined in:
app/services/payment/gateways/echeck.rb

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Constructor Details

#initialize(payment, _delivery = nil) ⇒ Echeck

Returns a new instance of Echeck.



8
9
10
# File 'app/services/payment/gateways/echeck.rb', line 8

def initialize(payment, _delivery = nil)
  @payment = payment
end

Instance Method Details

#authorizeObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/services/payment/gateways/echeck.rb', line 12

def authorize
  @payment.reference = "XXXXX#{@payment..to_s.last(4)}"
  @payment.card_type = 'check'

  tx = OrderTransaction.new(
    action: 'authorization',
    amount: (@payment.amount * 100).to_i,
    success: true,
    reference: @payment.reference,
    message: 'Manual eCheck - pending Forte processing',
    params: { manual: true, account_type: @payment. },
    test: !Rails.env.production?
  )
  @payment.transactions.push(tx)
  @payment.payment_authorized!
  @payment.save!

  InternalMailer.forte_payment_pending(@payment).deliver_later

  Result.new(success: true, message: 'eCheck payment authorized (manual processing)')
end

#capture(capture_amount, options = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/services/payment/gateways/echeck.rb', line 34

def capture(capture_amount, options = {})
  tx = OrderTransaction.new(
    action: 'capture',
    amount: (capture_amount * 100).to_i,
    success: true,
    reference: @payment.authorization_code || @payment.reference,
    message: 'Manual eCheck capture',
    params: { manual: true },
    test: !Rails.env.production?
  )
  @payment.transactions.push(tx)
  @payment.payment_captured!

  Rails.logger.info("#{Time.current}: eCheck capture of Payment ID #{@payment.id} Amount #{capture_amount}")

  Result.new(success: true, message: 'ok')
end

#create_receipt(invoice, amount, balance) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'app/services/payment/gateways/echeck.rb', line 119

def create_receipt(invoice, amount, balance)
  new_receipt = @payment.receipts.new(
    company: invoice.company,
    customer: invoice.customer,
    category: 'Echeck',
    amount: amount,
    reference: @payment.reference,
    currency: invoice.currency,
    payment: @payment,
    bank_account: invoice.company.,
    gl_date: Date.current,
    receipt_date: Date.current
  )
  new_receipt.receipt_details << ReceiptDetail.new(
    category: 'Invoice', invoice: invoice, amount: amount, gl_date: Date.current
  )
  begin
    new_receipt.save!
    Rails.logger.info("#{Time.current}: Created new receipt id: #{new_receipt.id}")
    if @payment.amount > balance
      Mailer.admin_notification(
        "Echeck amount already captured greater than balance on invoice id: #{invoice.id}, ref: #{invoice.reference_number}",
        "Amount already captured for eCheck payment id: #{@payment.id} was greater than order balance. eCheck amount: #{@payment.amount}, Invoice balance: #{balance}. Inform accounting who should process a refund."
      ).deliver
    end
    success = true
  rescue StandardError => exc
    report_exception exc, payment_id: @payment.id, message: "Unable to create new receipt for echeck"
    success = false
  end
  Result.new(success: success, receipt: new_receipt)
end

#refund(refund_amount, credit_memo) ⇒ Object



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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'app/services/payment/gateways/echeck.rb', line 73

def refund(refund_amount, credit_memo)
  tx = OrderTransaction.new(
    action: 'refund',
    amount: (refund_amount * 100).to_i,
    success: true,
    reference: @payment.authorization_code || @payment.reference,
    message: 'Manual eCheck refund',
    params: { manual: true },
    test: !Rails.env.production?
  )
  @payment.transactions.push(tx)
  @payment.payment_refunded!

  Rails.logger.info("#{Time.current}: eCheck Refund of Payment ID #{@payment.id} Amount #{refund_amount}")

  new_receipt = Receipt.new(
    company: credit_memo.company,
    customer: credit_memo.customer,
    category: 'Echeck',
    amount: -refund_amount,
    reference: @payment.reference,
    card_type: @payment.card_type,
    currency: @payment.currency,
    payment: @payment,
    bank_account: @payment.invoice.company.,
    gl_date: Date.current,
    receipt_date: Date.current
  )
  new_receipt.receipt_details << ReceiptDetail.new(
    category: 'Credit Memo', credit_memo: credit_memo, amount: -refund_amount, gl_date: Date.current
  )

  receipt_ok = false
  begin
    new_receipt.save!
    receipt_ok = true
    Rails.logger.info("#{Time.current}: Created new receipt id: #{new_receipt.id}")
  rescue StandardError => exc
    report_exception exc, payment_id: @payment.id, message: "Payment refunded but no receipt"
  end

  InternalMailer.forte_payment_pending(@payment).deliver_later

  Result.new(success: receipt_ok, receipt: new_receipt)
end

#void(report_fraud = false) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/services/payment/gateways/echeck.rb', line 52

def void(report_fraud = false)
  return Result.new(success: false) unless @payment.authorized?

  tx = OrderTransaction.new(
    action: 'void',
    success: true,
    reference: @payment.authorization_code || @payment.reference,
    message: 'Manual eCheck void',
    params: { manual: true },
    test: !Rails.env.production?
  )
  @payment.transactions.push(tx)
  @payment.payment_voided!

  Rails.logger.info("#{Time.current}: eCheck Void of Payment ID #{@payment.id}")

  InternalMailer.forte_payment_pending(@payment).deliver_later

  Result.new(success: true, message: 'voided')
end