Commit 2731e509 authored by David Valdez's avatar David Valdez Committed by Benjamin Melançon
Browse files

Issue #3020066 by gnuget, mlncn, cedewey: Make a second transaction attempt if...

Issue #3020066 by gnuget, mlncn, cedewey: Make a second transaction attempt if Stripe returns a 500 api_error response
parent 44331882
Loading
Loading
Loading
Loading
+24 −1
Original line number Diff line number Diff line
@@ -84,11 +84,34 @@
        if (address_zip) {
          extraDetails.address_zip = address_zip.value;
        }
        stripe.createToken(card, extraDetails).then(handleResponse);
        createToken(stripe, card, extraDetails, 3);
      }
    });
  }

  /**
   * Try to create the token "n" times before to fail.
   */
  function createToken(stripe, card, extraDetails, n) {
    return stripe.createToken(card, extraDetails)
      .then(handleResponse)
      .catch(function (error) {
        if (n === 1) {
          // Execute handleResponse with an error.
          var result = {
            token: null,
            error: {
              message: 'There was a problem contacting the payment gateway please try again later.'
            }
          };
          handleResponse(result);
          logProblem('Stripe is not available');
          throw error;
        }
        createToken(stripe, card, extraDetails, n - 1);
      });
  };

  function handleResponse(result) {
    var successElement = document.querySelector('#stripe-card-success');
    var errorElement = document.querySelector('#stripe-card-errors');