Guest User

Untitled

a guest
Sep 14th, 2019
451
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const recurly_form_verify = document.querySelector( '#pm-recurly-form-3ds' );
  2. const recurly_active_verify = ( window.recurly && recurly_key && recurly_form_verify && errors ) ? true : false;
  3.  
  4. document.addEventListener( 'DOMContentLoaded', function() {
  5.     if ( ! recurly_active_verify ) {
  6.         console.log( 'Recurly verify not active' );
  7.         return false;
  8.     }
  9.       // We expect this page to load with the following parameter pattern
  10.       // `/3d-secure.html#token_id=xxx&action_token_id`
  11.       // This is a simple parser for that pattern. You may use window.URLSearchParams
  12.       // instead if IE11 support is not needed
  13.       var hashParams = location.hash.substr(1).split('&').reduce(function (acc, i) {
  14.         var [k,v] = i.split('=');
  15.         acc[k]=v;
  16.         return acc;
  17.       }, {});
  18.       // Configure Recurly.js
  19.       recurly.configure(recurly_key);
  20.       // In order to remain backend agnostic, this example passes the Recurly.js credit card token
  21.       // to this page via the URL hash. Here we add it to the form so that it will be passed
  22.       // to our backend
  23.       document.querySelector('#recurly-token').setAttribute('value',hashParams.token_id);
  24.      
  25.       // Now we construct the 3-D Secure authentication flow
  26.  
  27.         var risk = recurly.Risk();
  28.         var threeDSecure = risk.ThreeDSecure({ actionTokenId: hashParams.action_token_id });
  29.         var container = document.querySelector('#container-3ds-authentication');
  30.  
  31.  
  32.         // Handle errors that occur during 3-D Secure authentication
  33.         threeDSecure.on('error', error);
  34.         // 'token' is called when your user completes the 3-D Secure authentication flow
  35.         threeDSecure.on('token', function (token) {
  36.             console.log(token);
  37.           // place the result token in your form so that it will be submitted
  38.           // when the customer re-submits
  39.           document.querySelector('#three-d-secure-token').setAttribute('value',token.id);
  40.           // Hide the container once we have a token
  41.           container.style.display = "none";
  42.           // Show the loading message
  43.           document.querySelector('.three-d-secure-submitting-messagge').style.display = "block";
  44.           // submit the form automatically
  45.           recurly_form_verify.submit();
  46.         });
  47.  
  48.         // Attach our 3D Secure session to the container
  49.         threeDSecure.attach(container);
  50.         // Show the container
  51.    
  52.         container.style.display = "block";
  53.       // runs some simple animations for the page
  54.       document.querySelector('body').classList.add( 'show' );
  55.    
  56.       // A simple error handling function to expose errors to the customer
  57.       function error (err) {
  58.           console.log(err);
  59.         if (err.fields) {
  60.             document.querySelector('#errors').text('The following fields appear to be invalid: ' + err.fields.join(', '));
  61.             loop( err.fields, function (i, field ) {
  62.                 document.querySelector( '[data-recurly=' + field + ']' ).classList.add( 'error' );
  63.             } );
  64.         //   $.each(err.fields, function (i, field) {
  65.         //     document.querySelector('[data-recurly=' + field + ']').classList.add( 'error' );
  66.         //   });
  67.         } else if (err.message) {
  68.             document.querySelector('#errors').innerHTML = '<p>'+err.message+'</p>';
  69.         }
  70.         document.querySelector('button').disabled = false;
  71.       }
  72. } );
Advertisement
Add Comment
Please, Sign In to add comment