Class: PayCertify::ThreeDS::Form

Inherits:
Object
  • Object
show all
Defined in:
lib/paycertify/three_ds/form.rb

Defined Under Namespace

Classes: UnauthenticatedPaymentError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(authentication) ⇒ Form

Returns a new instance of Form.



9
10
11
12
13
# File 'lib/paycertify/three_ds/form.rb', line 9

def initialize(authentication)
  check_authentication!(authentication)

  self.authentication = authentication
end

Instance Attribute Details

#authenticationObject

Returns the value of attribute authentication.



7
8
9
# File 'lib/paycertify/three_ds/form.rb', line 7

def authentication
  @authentication
end

#settingsObject

Returns the value of attribute settings.



7
8
9
# File 'lib/paycertify/three_ds/form.rb', line 7

def settings
  @settings
end

Instance Method Details

#acs_urlObject



15
16
17
# File 'lib/paycertify/three_ds/form.rb', line 15

def acs_url
  @acs_url ||= authentication['AcsUrl']
end

#frictionlessObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
103
104
105
106
107
108
109
110
111
# File 'lib/paycertify/three_ds/form.rb', line 50

def frictionless
  html = <<-HTML.squish
    <style> #frame { display: none; } </style>
    <iframe id="frame" src="about:blank"></iframe>
    <form id="callback-form" method="POST" action="#{term_url}">
      <input type="hidden" name="_frictionless_3ds_callback" value="1"/>
  HTML

  settings.each do |key, value|
    html << <<-HTML.squish
      <input type="hidden" name="#{key}" value="#{value}"/>
    HTML
  end

  html << <<-HTML.squish
    </form>

    <script>
      (function(){
        var frame = document.getElementById('frame');
        var form = document.getElementById('callback-form');
        var interval = 500;
        var timeout = interval * 15;

        frame.contentDocument.write('#{form}');
        frame.contentDocument.form3ds.submit();

        var interval = setInterval(function() {
          try {
            var frameContent = frame.contentDocument;
            var frameDoc = frameContent.documentElement;

            var text = frameContent.body.innerHTML || frameDoc.textContent || frameDoc.innerText;
            var json = JSON.parse(text);

            var input;

            for(key in json) {
              input = document.createElement('input');
              input.type = 'hidden';
              input.name = key;
              input.value = json[key];

              form.appendChild(input);
            };

            clearInterval(interval);
            form.submit();
          } catch(e) {
            return false;
          };
        }, interval);

        setTimeout(function() {
          form.submit();
        }, timeout);
      })();
    </script>
  HTML

  html
end

#mdObject



23
24
25
# File 'lib/paycertify/three_ds/form.rb', line 23

def md
  @md ||= authentication['MD']
end

#pareqObject



19
20
21
# File 'lib/paycertify/three_ds/form.rb', line 19

def pareq
  @pareq ||= authentication['PaReq']
end

#render_html_for(settings, type) ⇒ Object



31
32
33
34
35
36
# File 'lib/paycertify/three_ds/form.rb', line 31

def render_html_for(settings, type)
  self.settings = settings
  send(type)
rescue NoMethodError
  raise UndefinedTypeError, 'Type is not supported: '+ type
end

#strictObject



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/paycertify/three_ds/form.rb', line 38

def strict
  <<-HTML.squish
    #{form}

    <script>
      window.onload = function() {
        document.form3ds.submit();
      }
    </script>
  HTML
end

#term_urlObject



27
28
29
# File 'lib/paycertify/three_ds/form.rb', line 27

def term_url
  @term_url ||= authentication['TermUrl']
end