If we are using Shopify Payments in test mode, it doesn’t allow you to put in an invalid credit card number from https://help.shopify.com/en/manual/payments/shopify-payments/testing-shopify-payments#simulate-failed-transactions. When you try to checkout, it fails. If you try to update your payment method to one of those, it just says ‘Failed to update payment method’. So this means the payment method on a subscription is always valid in test mode, since we can’t add non valid ones.
Currently there isn’t a way to test a card that succeeds initially and then fails for the reasons that you mentioned. The best way right now is to look at the error code[1] that will be returned when a billing attempt fails.
Hey @matart , I understand we can look at the error code, but the issue is I cannot actually make sure a transaction fails so I can test my flow properly with retries. Is there any plans to allow us to enter invalid card numbers?
I ended up finding a way to make this work by checking out a subscription with the normal test card, and then using the sendCustomerPaymentMethodUpdateEmail mutation to receive and email to update my card.
I then updated the customer’s payment method to Stripe’s “Decline after attaching” card:
So the method mentioned by @ChrisF2132 may have worked in the past, but as of July 2022, it just approved all transactions, defeating the purpose. The only way that I’ve found to make it work, while awkward and clunky, but gets the desired result is listed below. And I know it’s been a while since this question was asked, but Shopify still doesn’t think it’s important to be able to test our apps, you know, like apps that are responsible for charging customers real money. Who needs testing, Nah! Your code looks good enough.
I found this about a year later so I’m sure others will be able to use this method as well going forward.
Checkout in Test Mode with a card that gets successfully charged.
Disable Test Mode in settings (I’m using the Shopify payment gateway as I believe it’s required to test subscription apps).
Then trigger the sendCustomerPaymentMethodUpdateEmail mutation and update the card to a real CC number (I just used my bank’s Debit Card. And because we are no longer in Test Mode, it will allow this change to occur).
Go back into your development stores settings and re-enable Test Mode. This is super important so you do not charge your own card. You could always refund, but it’s best to not have to.
Have your app perform a billing attempt on the customer who you just updated the card for. My internal logic queries my dB and looks for any entities with their nextBillingDate set to today, and pulls their subscription contract Id into the
subscriptionBillingAttemptCreate mutation.
6. This should run the billing attempt against the real card for that user and spit back an error_code of payment_method_incompatible_with_gateway_configand an error_message of Payment method cannot be used with the current payment gateway test mode configuration triggering a subscription_billing_attempts_failure webhook.
Voilà!
NOTE Use your own card at your own risk. This is not financial advise ?. Please, for the love of all that is right, remove your real card after you’re finished testing and put back one of Shopify’s spoof cards. Or, just in case someone from Shopify sees this, just add a test card that is able to be added, but fails on billing attempts, like Stripe does. Then this whole thing would go away. Thanks!
I’ve tried @ChrisF2132 's method and can confirm it is no longer working as of now (27 Jan 2023). I am not comfortable with using my own credit card like @devsmk suggested. I finally figured out a way:
Check out a subscription product using a Stripe test card, remember what type of card you are using for this checkout, ie. is it a Mastercard or Visa or Amex, etc.
Then go onto Stripe Admin → Settings (bottom left) → Payments → disable the type of card you used earlier → Save.
Configure a something to listen to the Webhook of type SUBSCRIPTION_BILLING_ATTEMPTS_FAILURE
Then trigger a billingAttempt from your app, you should see a Failed billing attempt comes through. I used Google pub/sub, my webhook payload look like below:
{
“id”: 454765668,
“admin_graphql_api_id”: “gid://shopify/SubscriptionBillingAttempt/111823452346”,
“idempotency_key”: “whatever-unique-key”,
“order_id”: null,
“admin_graphql_api_order_id”: null,
“subscription_contract_id”:2353425623,
“admin_graphql_api_subscription_contract_id”: “gid://shopify/SubscriptionContract/2353425623”,
“ready”: true,
“error_message”: “Credit card brand is not accepted for payment”,
“error_code”: “unexpected_error”
}
This is a full list of error codes for a billingAttempt https://shopify.dev/api/admin-graphql/2023-01/enums/subscriptionbillingattempterrorcode , maybe you can simulate more scenarios such as Invalid Address, PAYMENT_METHOD_INCOMPATIBLE_WITH_GATEWAY_CONFIG, etc.
In order to test without using a real credit card, I’ve found a workaround using Stripe in Test mode throughout the process.
Firstly, I initiate the subscription as usual, employing a test VISA credit card number (4242.4242.4242.4242) as outlined in the documentation.
Then, prior to triggering the subscriptionBillingAttemptCreate, I navigate to the customer’s page. Here, I manually update their payment method through the ‘Replace card’ process. Choosing a card that simulates successful transactions, I select a card type not accepted by my Stripe setup. For example, I choose a Diners Club card number (3056.9309.0259.04) for replacement.
Finally, when I execute the subscriptionBillingAttemptCreate, it successfully triggers the webhook SUBSCRIPTION_BILLING_ATTEMPTS_FAILURE, allowing me to test a failed transaction scenario without the need for a real card.