Class: Parsbank::TransactionRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/parsbank/transaction_request.rb

Class Method Summary collapse

Class Method Details

.create(args = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/parsbank/transaction_request.rb', line 3

def self.create(args = {})
  bank = fetch_bank(args)
  validate_bank(bank)

  description = args.fetch(:description)
  callback_url = generate_callback_url(bank, description)

  crypto_amount, fiat_amount, real_amount = fetch_amounts(args)
  validate_amounts(bank, crypto_amount, fiat_amount, real_amount)

  transaction = initialize_transaction(args, description, fiat_amount, crypto_amount, bank, callback_url)
  process_gateway(bank, transaction, description, callback_url, crypto_amount, fiat_amount, args)

  { transaction: transaction, html_form: @result }
end

.fetch_amounts(args) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/parsbank/transaction_request.rb', line 33

def self.fetch_amounts(args)
  [
    args.fetch(:crypto_amount, nil),
    args.fetch(:fiat_amount, nil),
    args.fetch(:real_amount, nil)
  ]
end

.fetch_bank(args) ⇒ Object



19
20
21
# File 'lib/parsbank/transaction_request.rb', line 19

def self.fetch_bank(args)
  args.fetch(:bank, Parsbank.available_gateways_list.keys.sample)
end

.generate_callback_url(bank, _description) ⇒ Object



28
29
30
31
# File 'lib/parsbank/transaction_request.rb', line 28

def self.generate_callback_url(bank, _description)
  selected_bank = Parsbank.available_gateways_list[bank]
  "#{selected_bank['callback_url'] || Parsbank.configuration.callback_url}&bank_name=#{bank}"
end

.initialize_transaction(args, description, fiat_amount, crypto_amount, bank, callback_url) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/parsbank/transaction_request.rb', line 55

def self.initialize_transaction(args, description, fiat_amount, crypto_amount, bank, callback_url)
  model_class = Parsbank.configuration.model || 'Transaction'
  @transaction= Object.const_get(model_class).new(
    description: description,
    amount: fiat_amount || crypto_amount,
    gateway: bank,
    callback_url: callback_url,
    status: 'start',
    user_id: args.fetch(:user_id, nil),
    cart_id: args.fetch(:cart_id, nil),
    local_id: args.fetch(:local_id, nil),
    ip: args.fetch(:ip, nil)
  )
  @transaction.save

  @transaction
end

.process_bscbitcoin(transaction, description, crypto_amount, args) ⇒ Object



125
126
127
128
129
130
131
132
# File 'lib/parsbank/transaction_request.rb', line 125

def self.process_bscbitcoin(transaction, description, crypto_amount, args)
  bscbitcoin = Parsbank::BscBitcoin.new(
    additional_data: description
  )
  convert_real_amount_to_assets if crypto_amount.nil? && args.key?(:real_amount)
  @result = bscbitcoin.generate_payment_address(amount: crypto_amount)
  transaction.update!(gateway_response: @result, unit: 'bitcoin')
end

.process_gateway(bank, transaction, description, callback_url, crypto_amount, fiat_amount, args) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/parsbank/transaction_request.rb', line 73

def self.process_gateway(bank, transaction, description, callback_url, crypto_amount, fiat_amount, args)
  case bank
  when 'mellat'
    process_mellat(transaction, description, callback_url, fiat_amount)
  when 'zarinpal'
    process_zarinpal(transaction, description, callback_url, fiat_amount)
  when 'zibal'
    process_zibal(description, callback_url, fiat_amount)
  when 'bscbitcoin'
    process_bscbitcoin(transaction, description, crypto_amount, args)
  else
    raise "Unsupported gateway: #{bank}"
  end
end

.process_mellat(transaction, description, callback_url, fiat_amount) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/parsbank/transaction_request.rb', line 88

def self.process_mellat(transaction, description, callback_url, fiat_amount)
  mellat = Parsbank::Mellat.new(
    amount: fiat_amount,
    additional_data: description,
    callback_url: callback_url,
    orderId: transaction.id
  )
  mellat.call
  transaction.update!(gateway_response: mellat.response, unit: 'irr')
  @result = mellat.redirect_form
end

.process_zarinpal(transaction, description, callback_url, fiat_amount) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/parsbank/transaction_request.rb', line 100

def self.process_zarinpal(transaction, description, callback_url, fiat_amount)
  zarinpal = Parsbank::Zarinpal.new(
    amount: fiat_amount,
    additional_data: description,
    callback_url: callback_url
  )
  zarinpal.call
  transaction.update!(
    gateway_response: zarinpal.response,
    track_id: zarinpal.ref_id,
    unit: 'irt'
  )
  @result = zarinpal.redirect_form
end

.process_zibal(description, callback_url, fiat_amount) ⇒ Object



115
116
117
118
119
120
121
122
123
# File 'lib/parsbank/transaction_request.rb', line 115

def self.process_zibal(description, callback_url, fiat_amount)
  zibal = Parsbank::Zibal.new(
    amount: fiat_amount,
    additional_data: description,
    callback_url: callback_url
  )
  zibal.call
  @result = zibal.redirect_form
end

.validate_amounts(bank, crypto_amount, fiat_amount, real_amount) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/parsbank/transaction_request.rb', line 41

def self.validate_amounts(bank, crypto_amount, fiat_amount, real_amount)
  raise 'Amount fields are empty: crypto_amount OR fiat_amount OR real_amount' if [crypto_amount, fiat_amount,
                                                                                   real_amount].all?(&:nil?)

  tags = Parsbank.supported_psp[bank]['tags']
  if tags.include?('crypto') && crypto_amount.nil? && real_amount.nil?
    raise "#{bank} needs crypto_amount or real_amount"
  end

  return unless tags.include?('rial') && fiat_amount.nil? && real_amount.nil?

  raise "#{bank} needs fiat_amount or real_amount"
end

.validate_bank(bank) ⇒ Object



23
24
25
26
# File 'lib/parsbank/transaction_request.rb', line 23

def self.validate_bank(bank)
  selected_bank = Parsbank.available_gateways_list[bank]
  raise "Bank not enabled or not exists in #{Parsbank.configuration.secrets_path}: #{bank}" unless selected_bank
end