Class: Parsbank::Zarinpal

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Gates

#default_config, logo, #redirect_loaders

Constructor Details

#initialize(args = {}) ⇒ Zarinpal

Returns a new instance of Zarinpal.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/parsbank/zarinpal/zarinpal.rb', line 8

def initialize(args = {})
  @mobile = args.fetch(:mobile, '')
  @email = args.fetch(:email, '')
  @amount = args.fetch(:amount)
  @description = args.fetch(:description, ' ')
  @callback_url = args.fetch(:callback_url,
                             (default_config(:callback_url) || Parsbank.configuration.callback_url))
  @merchant_id = args.fetch(:merchant_id, default_config(:merchant_id))
  @wsdl = create_rest_client
rescue KeyError => e
  raise ArgumentError, "Missing required argument: #{e.message}"
end

Instance Attribute Details

#amountObject

Returns the value of attribute amount.



5
6
7
# File 'lib/parsbank/zarinpal/zarinpal.rb', line 5

def amount
  @amount
end

#descriptionObject

Returns the value of attribute description.



5
6
7
# File 'lib/parsbank/zarinpal/zarinpal.rb', line 5

def description
  @description
end

#emailObject

Returns the value of attribute email.



5
6
7
# File 'lib/parsbank/zarinpal/zarinpal.rb', line 5

def email
  @email
end

#logoObject (readonly)

Returns the value of attribute logo.



6
7
8
# File 'lib/parsbank/zarinpal/zarinpal.rb', line 6

def 
  
end

#merchant_idObject

Returns the value of attribute merchant_id.



5
6
7
# File 'lib/parsbank/zarinpal/zarinpal.rb', line 5

def merchant_id
  @merchant_id
end

#mobileObject

Returns the value of attribute mobile.



5
6
7
# File 'lib/parsbank/zarinpal/zarinpal.rb', line 5

def mobile
  @mobile
end

#ref_idObject (readonly)

Returns the value of attribute ref_id.



6
7
8
# File 'lib/parsbank/zarinpal/zarinpal.rb', line 6

def ref_id
  @ref_id
end

#responseObject (readonly)

Returns the value of attribute response.



6
7
8
# File 'lib/parsbank/zarinpal/zarinpal.rb', line 6

def response
  @response
end

#statusObject (readonly)

Returns the value of attribute status.



6
7
8
# File 'lib/parsbank/zarinpal/zarinpal.rb', line 6

def status
  @status
end

#status_messageObject (readonly)

Returns the value of attribute status_message.



6
7
8
# File 'lib/parsbank/zarinpal/zarinpal.rb', line 6

def status_message
  @status_message
end

#wsdlObject

Returns the value of attribute wsdl.



5
6
7
# File 'lib/parsbank/zarinpal/zarinpal.rb', line 5

def wsdl
  @wsdl
end

Instance Method Details

#callObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/parsbank/zarinpal/zarinpal.rb', line 38

def call
  response =  @wsdl.call

  Rails.logger.info "Received response with status: #{response.status}, body: #{response.body.inspect}"

  if response.valid?
    validate(response.body)
  else
    @valid = false
    Rails.logger.error "POST request to #{BASE_URL}/#{endpoint} failed with status: #{response.status}, error: #{response.body.inspect}"
    raise "API request failed with status #{response.status}: #{response.body}"
  end
rescue Faraday::ConnectionFailed => e
  Rails.logger.error "Connection failed: #{e.message}"
  raise "Connection to API failed: #{e.message}"
rescue Faraday::TimeoutError => e
  Rails.logger.error "Request timed out: #{e.message}"
  raise "API request timed out: #{e.message}"
rescue StandardError => e
  Rails.logger.error "An error occurred: #{e.message}"
  raise "An unexpected error occurred: #{e.message}"

  JSON.parse response.body

  if response[:success]
    validate(response[:body])
  else
    { status: :nok, message: begin
      response[:error]
    rescue StandardError
      response
    end }
  end
end

#redirect_form(ref_id) ⇒ 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
# File 'lib/parsbank/zarinpal/zarinpal.rb', line 73

def redirect_form(ref_id)
  javascript_tag = "    <script type='text/javascript' charset='utf-8'>\n      function postRefId(refIdValue) {\n        var form = document.createElement('form');\n        form.setAttribute('method', 'POST');\n        form.setAttribute('action', 'https://www.zarinpal.com/pg/StartPay/' + refIdValue);\n        form.setAttribute('target', '_self');\n        var hiddenField = document.createElement('input');\n        hiddenField.setAttribute('name', 'RefId');\n        hiddenField.setAttribute('value', refIdValue);\n        form.appendChild(hiddenField);\n        document.body.appendChild(form);\n        form.submit();\n        document.body.removeChild(form);\n      }\n      postRefId('\#{ref_id}');\n    </script>\n  JS\n\n  redirect_loaders do\n    \"\#{javascript_tag}\#{I18n.t('actions.redirect_to_gate')}\"\n  end\nend\n"

#valid?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/parsbank/zarinpal/zarinpal.rb', line 30

def valid?
  @valid
end

#validate(response = nil) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/parsbank/zarinpal/zarinpal.rb', line 21

def validate(response = nil)
  @response = response[:payment_request_response] || response[:payment_verification_response] || response
  @ref_id = @response[:authority]
  @status = @response[:status].present? ? @response[:status] : 'FAILED'

  perform_validation
  self
end