0
votes

I am trying to follow the guide here ( https://stripe.com/docs/stripe-js/elements/payment-request-button ) to setup Apple Pay for the web and Stripe. The initial steps such as verification of domain and all the pre-setup is done but I am having an issue following the steps for the payment.

The Apple Pay Button is showing up in my Safari browser. When the button is clicked, I fire an event called Paymentmethode() i am facing this error while checking live.Either you do not have a card saved to your Wallet or the current domain (pwafe.devco.pk) is not registered for Apple Pay. Visit https://dashboard.stripe.com/account/apple_pay to register this domain. main.js:25. and button is hide I get lost after step 3 and not sure what to do. I am posting to my backend and on the backend, creating a payment intent and returning the client_secret

 paymentMethod() {
      // STEP 1 FROM GUIDE
      var stripe = Stripe("pk_test_YxSI6F4QeV0XCofSgabilbTu00ChOmJWJ0", {
        apiVersion: "2020-08-27",
        stripeAccount: "CONNECTED_STRIPE_ACCOUNT_ID",
      });
      // STEP 2 FROM GUIDE
      var paymentRequest = stripe.paymentRequest({
        country: "US",
        currency: "usd",
        total: {
          label: "Demo total",
          amount: 1099,
        },
        requestPayerName: true,
        requestPayerEmail: true,
      });

      // STEP 3 FROM GUIDE
      var elements = stripe.elements();
      var prButton = elements.create("paymentRequestButton", {
        paymentRequest: paymentRequest,
      });
      // console.log("before api call", paymentRequest);
      paymentRequest.canMakePayment().then(function (result) {
        // console.log("after api called" + result);
        if (result) {
          prButton.mount("#payment-request-button");
        } else {
          //prButton.mount('#payment-request-button');
          document.getElementById("payment-request-button").style.display =
            "none";
        }
      });
      // STEP 4 FROM GUIDE -- THIS RETURNS A CLIENT SECRET
      let clientSecret;
      axios
        .post("https://pwa.devco.pk/api/Create_PaymentIntent", {})
        .then((resp) => {
          // Assign this previously defined variable
          clientSecret = resp.client_secret;
        });
      paymentRequest.on("paymentmethod", function (ev) {
        // Confirm the PaymentIntent without handling potential next actions (yet).
        stripe
          .confirmCardPayment(
            clientSecret,
            {
              payment_method: ev.paymentMethod.id,
            },
            {
              handleActions: false,
            }
          )
          .then(function (confirmResult) {
            if (confirmResult.error) {
              // Report to the browser that the payment failed, prompting it to
              // re-show the payment interface, or show an error message and close
              // the payment interface.
              ev.complete("fail");
            } else {
              // Report to the browser that the confirmation was successful, prompting
              // it to close the browser payment method collection interface.
              ev.complete("success");
              // Check if the PaymentIntent requires any actions and if so let Stripe.js
              // handle the flow. If using an API version older than "2019-02-11" instead
              // instead check for: `paymentIntent.status === "requires_source_action"`.
              if (confirmResult.paymentIntent.status === "requires_action") {
                // Let Stripe.js handle the rest of the payment flow.
                stripe.confirmCardPayment(clientSecret).then(function (result) {
                  if (result.error) {
                    let data = {
                      msg: "An error occurred. Please try again.",
                    };
                    this.handleShowFlashMsg(data);
                    // The payment failed -- ask your customer for a new payment method.
                  } else {
                    this.handleShowOrderConfirmModal();
                    // The payment has succeeded.
                  }
                });
              } else {
                // The payment has succeeded.
              }
            }
          });
      });
      var paymentRequest = stripe.paymentRequest({
        country: "US",
        currency: "usd",
        total: {
          label: "Demo total",
          amount: 1099,
        },
        requestShipping: true,
        // `shippingOptions` is optional at this point:
        shippingOptions: [
          // The first shipping option in this list appears as the default
          // option in the browser payment interface.
          {
            id: "free-shipping",
            label: "Free shipping",
            detail: "Arrives in 5 to 7 days",
            amount: 0,
          },
        ],
      });
      paymentRequest.on("shippingaddresschange", function (ev) {
        if (ev.shippingAddress.country !== "US") {
          ev.updateWith({
            status: "invalid_shipping_address",
          });
        } else {
          // Perform server-side request to fetch shipping options
          fetch("/calculateShipping", {
            data: JSON.stringify({
              shippingAddress: ev.shippingAddress,
            }),
          })
            .then(function (response) {
              return response.json();
            })
            .then(function (result) {
              ev.updateWith({
                status: "success",
                shippingOptions: result.supportedShippingOptions,
              });
            });
        }
      });

      var stripe = Stripe("pk_test_YxSI6F4QeV0XCofSgabilbTu00ChOmJWJ0", {
        apiVersion: "2020-08-27",
        stripeAccount: "CONNECTED_STRIPE_ACCOUNT_ID",
      });
    },
1
"Visit dashboard.stripe.com/account/apple_pay to register this domain." Did you do this?ceejayoz
One of the main issues I see here is that there are 2 definitions of stripe object and also the payment request instance. Furthermore, I would consider changing the code so that is no button click that fires any events. Rather, canMakePayment() should be used to figure out if you should mount the button and then event handles like on paymentmethod and on shipping address changed on a singular instance of paymentRequest.v3nkman
Then I would also check that the domain is registered in live mode either via the API or via the Dashboard: stripe.com/docs/stripe-js/elements/…v3nkman

1 Answers

-1
votes

You should verify domain registration and add card into wallet.tha