Stripe错误,可以在localhost上运行,但不能在服务器上运行

时间:2020-08-04 21:58:41

标签: javascript shell stripe-payments

我目前正在开发对条带化服务的订阅。多亏了composer start命令,一切都在localhost:4242上运行良好。

但是当我将文件上传到我的网站时,它不起作用。我在论坛上查看了一下,发现问题可能出在SSL证书上,因此我在网站的所有页面上都添加了.httacess https。

因此,要找到问题,我进入控制台,看到一个错误,但是我不知道它是否从那里来,也不知道如何解决。

错误图片:

Image of the error

这就是脚本:

var stripe, customer, setupIntent, plan;
var signupForm = document.getElementById("signup-form");
var paymentForm = document.getElementById("payment-form");

var stripeElements = function(publishableKey) {
  stripe = Stripe(publishableKey, { betas: ["au_bank_account_beta_2"] });
  var elements = stripe.elements();

  // Card Element styles
  var style = {
    base: {
      fontSize: "16px",
      color: "#32325d",
      fontFamily:
        "-apple-system, BlinkMacSystemFont, Segoe UI, Roboto, sans-serif",
      fontSmoothing: "antialiased",
      "::placeholder": {
        color: "rgba(0,0,0,0.4)"
      }
    }
  };

  var card = elements.create("card", { style: style });

  card.mount("#card-element");

  // AU BECS Debit Element
  var auBankAccount = elements.create("auBankAccount", {
    style: style
  });

  // Add an instance of the auBankAccount Element into the `auBankAccount-element` <div>.
  auBankAccount.mount("#auBankAccount-element");

  for (element of [card, auBankAccount]) {
    // Element focus ring
    element.on("focus", function() {
      var el = document.getElementById(`${element._componentName}-element`);
      el.classList.add("focused");
    });

    element.on("blur", function() {
      var el = document.getElementById(`${element._componentName}-element`);
      el.classList.remove("focused");
    });

    element.on("change", function(event) {
      var displayError = document.getElementById("error-message");
      if (event.error) {
        displayError.textContent = event.error.message;
      } else {
        displayError.textContent = "";
      }
    });
  }

  signupForm.addEventListener("submit", function(evt) {
    evt.preventDefault();

    document.getElementById("signup-view").classList.add("hidden");
    document.getElementById("payment-view").classList.remove("hidden");

    // Create customer
    createCustomer().then(result => {
      customer = result.customer;
      setupIntent = result.setupIntent;

      paymentForm.addEventListener("submit", function(evt) {
        evt.preventDefault();
        changeLoadingState(true);

        // Set up payment method for recurring usage
        var payment = paymentForm.querySelector("input[name=payment]:checked")
          .value;
        setupPaymentMethod(setupIntent.client_secret, payment, {
          card: card,
          au_becs_debit: auBankAccount
        });
      });
    });
  });
};

var setupPaymentMethod = function(setupIntentSecret, paymentMethod, element) {
  var billingName = document.querySelector("#name").value;
  var billingEmail = document.querySelector("#email").value;
  var prenom = document.querySelector("#prenom").value;
  var nom = document.querySelector("#nom").value;


  switch (paymentMethod) {
    case "card":
      stripe
        .confirmCardSetup(setupIntentSecret, {
          payment_method: {
            card: element[paymentMethod],
            billing_details: {
              name: billingName,
              email: billingEmail
            }
          }
        })
        .then(handleResult);
      break;
    case "au_becs_debit":
      stripe
        .confirmAuBecsDebitSetup(setupIntentSecret, {
          payment_method: {
            au_becs_debit: element[paymentMethod],
            billing_details: {
              name: billingName,
              email: billingEmail
            }
          }
        })
        .then(handleResult);
      break;
    default:
      console.warn("Unhandled Payment Method!");
      break;
  }

  function handleResult(result) {
    if (result.error) {
      showCardError(result.error);
    } else {
      // Create the subscription
      createSubscription(customer.id, result.setupIntent.payment_method);
    }
  }
};

function createCustomer() {
  var billingName = document.querySelector("#name").value;
  var billingEmail = document.querySelector("#email").value;
  var prenom = document.querySelector("#prenom").value;
  var nom = document.querySelector("#nom").value;

  return fetch("/create-customer", {
    method: "post",
    headers: {
      "Content-Type": "application/json"
    },
    body: JSON.stringify({
      name: billingName,
      email: billingEmail
    })
  })
    .then(response => {
      return response.json();
    })
    .then(result => {
      return result;
    });
}

function createSubscription(customerId, paymentMethodId) {
  return fetch("/subscription", {
    method: "post",
    headers: {
      "Content-type": "application/json"
    },
    body: JSON.stringify({
      customerId: customerId,
      paymentMethodId: paymentMethodId
    })
  })
    .then(function(response) {
      return response.json();
    })
    .then(function(subscription) {
      orderComplete(subscription);
    });
}

function showCardError(error) {
  changeLoadingState(false);
  // The card was declined (i.e. insufficient funds, card has expired, etc)
  var errorMsg = document.querySelector(".sr-field-error");
  errorMsg.textContent = error.message;
  setTimeout(function() {
    errorMsg.textContent = "";
  }, 8000);
}

function showPriceDetails(plan) {
  // Format price details and detect zero decimal currencies
  var amount = plan.amount;
  var numberFormat = new Intl.NumberFormat("en-US", {
    style: "currency",
    currency: plan.currency,
    currencyDisplay: "symbol"
  });
  var parts = numberFormat.formatToParts(amount);
  var zeroDecimalCurrency = true;
  for (var part of parts) {
    if (part.type === "decimal") {
      zeroDecimalCurrency = false;
    }
  }
  amount = zeroDecimalCurrency ? amount : amount / 100;
  var formattedAmount = numberFormat.format(amount);

  document.querySelector(
    ".order-amount"
  ).innerText = `${formattedAmount} per ${plan.interval}`;
}

function getConfig() {
  return fetch("/config", {
    method: "get",
    headers: {
      "Content-Type": "application/json"
    }
  })
    .then(function(response) {
      return response.json();
    })
    .then(function(response) {
      // Set up UI based on plan details
      showPriceDetails(response.plan);
      // TODO: show payment methods based on currency
      // Set up Stripe Elements
      stripeElements(response.publishableKey);
    });
}

getConfig();

/* ------- Post-payment helpers ------- */

/* Shows a success / error message when the payment is complete */




var orderComplete = function(subscription) {
  changeLoadingState(false);
  var subscriptionJson = JSON.stringify(subscription, null, 2);
  document.querySelectorAll(".sr-form").forEach(function(view) {
    view.classList.add("hidden");
  });
  document.querySelectorAll(".completed-view").forEach(function(view) {
    view.classList.remove("hidden");
  });
  document.querySelector(".order-status").textContent = subscription.status;
  var state = subscription.status; //renvoie active si paiement réussi

  var societe = document.querySelector("#name").value;
  var mail = document.querySelector("#email").value;
  var prenomPersonne = document.querySelector("#prenom").value;
  var nomPersonne = document.querySelector("#nom").value;


  if (state != "active"){
    //le paiement error
    alert("une erreur c'est produite !");
    //renvoie vers une page d'erreur
  }else{
    //le paiement est éffectué
    sessionStorage.setItem("societe",societe)
    sessionStorage.setItem("mail",mail)
    sessionStorage.setItem("prenom",prenomPersonne)
    sessionStorage.setItem("nom",nomPersonne)
    window.location="page2.php";
  }
};
//var billingName = document.querySelector("#name").value;
//var billingEmail = document.querySelector("#email").value;
//var prenom = document.querySelector("#prenom").value;
//var nom = document.querySelector("#nom").value;

// Show a spinner on subscription submission
var changeLoadingState = function(isLoading) {
  if (isLoading) {
    document.querySelector("#payment-form button").disabled = true;
    document.querySelector("#spinner").classList.add("loading");
    document.querySelector("#button-text").classList.add("hidden");
  } else {
    document.querySelector("#payment-form button").disabled = false;
    document.querySelector("#spinner").classList.remove("loading");
    document.querySelector("#button-text").classList.remove("hidden");
  }
};

var showPaymentMethods = function() {
  // Listen to changes to the payment method selector.
  for (let input of document.querySelectorAll("input[name=payment]")) {
    input.addEventListener("change", event => {
      event.preventDefault();
      var payment = paymentForm.querySelector("input[name=payment]:checked")
        .value;

      // Show the relevant details, whether it's an extra element or extra information for the user.
      paymentForm
        .querySelector(".payment-info.card")
        .classList.toggle("visible", payment === "card");
      paymentForm
        .querySelector(".payment-info.au_becs_debit")
        .classList.toggle("visible", payment === "au_becs_debit");
    });
  }
};
showPaymentMethods();

你能帮我吗?

非常感谢。 祝你今天愉快。 真诚的 JLair

0 个答案:

没有答案