BivorAdrito
BivorAdrito

Reputation: 84

How to do negative testing in Stripe using Apple Pay and Google Pay?

I have implemented Apple Pay and Google Pay in Stripe's Payment Request Button

The implementation is working perfectly. However, I need to do some negative testing using some cards that will show an error while doing a payment flow. For example,

  1. To test generic decline, 4000000000000002
  2. To test Lost card decline, 4000000000009987

How can I test these negative scenarios using Apple Pay and Google Pay in Stripe's Payment Request?

So far I have tried this code,

useEffect(() => {
    if (!stripe || !elements) {
      console.log("NO STRIPE", stripe, elements)
      return;
    }
   
    if (stripe && !paymentRequest) {
      const pr = stripe.paymentRequest({
        country: 'US',
        currency: 'usd',
        total: {
          label: 'Total Amount',
          amount: 2000,
        },
      });
  
      pr.canMakePayment().then(result => {
        if (!result?.applePay && !result?.googlePay && !result?.lin) {
          return
        }

        if (result) {
          setPaymentRequest(pr);
        } else {
        }
      }).catch(e => {
        console.log("error", e );
      });

    }
  }, [stripe]);

I have also gone through Stripe Testing Doc. But have not found any solution to this.

Upvotes: 2

Views: 926

Answers (1)

karllekko
karllekko

Reputation: 7198

You mostly can't do this since you can't add those test cards to Google/Apple wallets. Generally you would stub it. So in the on.("paymentmethod") event , change your code to ignore the actual payment method pm_xxx from the event, and instead pass the string pm_card_visa_chargeDeclined into the next part of your code.

One option that might work is to add those testcards as 'autofill' cards in chrome://settings/autofill and then try viewing the page using the PaymentRequestButton in an incognito Chrome window.

Upvotes: 2

Related Questions