GoogleCloudPlatform/microservices-demo · error · UnacceptedCreditCard

UnacceptedCreditCard

Error message

UnacceptedCreditCard

What it means

UnacceptedCreditCard is thrown by charge() in src/paymentservice/charge.js when the card number is structurally valid but its detected type (from simple-card-validator) is not 'visa' or 'mastercard' — e.g. AMEX, Diners Club, Discover. The service deliberately accepts only VISA and MasterCard. It is a CreditCardError with code 400 (Invalid Argument) and the message includes the rejected card type.

Source

Thrown at src/paymentservice/charge.js:74

 * Verifies the credit card number and (pretend) charges the card.
 *
 * @param {*} request
 * @return transaction_id - a random uuid.
 */
module.exports = function charge (request) {
  const { amount, credit_card: creditCard } = request;
  const cardNumber = creditCard.credit_card_number;
  const cardInfo = cardValidator(cardNumber);
  const {
    card_type: cardType,
    valid
  } = cardInfo.getCardDetails();

  if (!valid) { throw new InvalidCreditCard(); }

  // Only VISA and mastercard is accepted, other card types (AMEX, dinersclub) will
  // throw UnacceptedCreditCard error.
  if (!(cardType === 'visa' || cardType === 'mastercard')) { throw new UnacceptedCreditCard(cardType); }

  // Also validate expiration is > today.
  const currentMonth = new Date().getMonth() + 1;
  const currentYear = new Date().getFullYear();
  const { credit_card_expiration_year: year, credit_card_expiration_month: month } = creditCard;
  if ((currentYear * 12 + currentMonth) > (year * 12 + month)) { throw new ExpiredCreditCard(cardNumber.replace('-', ''), month, year); }

  logger.info(`Transaction processed: ${cardType} ending ${cardNumber.substr(-4)} \
    Amount: ${amount.currency_code}${amount.units}.${amount.nanos}`);

  return { transaction_id: uuidv4() };
};

View on GitHub (pinned to 72ba613a05)

Solutions

  1. Use a VISA or MasterCard test number instead, e.g. '4012-8888-8888-1881' or '5555-5555-5555-4444'
  2. If AMEX/other networks should be supported, edit charge.js to add the accepted card types to the check on line 74
  3. Reject or reroute non-VISA/MasterCard cards in the frontend before calling Charge to give a friendlier error
  4. Check card_type returned by simple-card-validator to confirm what type your number is being detected as

Example fix

// before
if (!(cardType === 'visa' || cardType === 'mastercard')) { throw new UnacceptedCreditCard(cardType); }
// after
const accepted = ['visa', 'mastercard', 'american_express'];
if (!accepted.includes(cardType)) { throw new UnacceptedCreditCard(cardType); }
Defensive patterns

Strategy: validation

Validate before calling

const cardValidator = require('simple-card-validator');
function isAcceptedCard(cardNumber) {
  const { card_type: cardType, valid } = cardValidator(cardNumber).getCardDetails();
  return valid && (cardType === 'visa' || cardType === 'mastercard');
}
if (!isAcceptedCard(cardNumber)) {
  return res.status(400).json({ error: 'Only VISA and MasterCard are accepted' });
}

Type guard

function isAcceptedCardType(cardType) {
  return cardType === 'visa' || cardType === 'mastercard';
}

Try / catch

try {
  const result = await paymentClient.Charge(chargeRequest);
} catch (err) {
  if (err.message === 'UnacceptedCreditCard' || /cannot process .* credit cards/i.test(err.message)) {
    return res.status(400).json({ error: err.message, hint: 'Use a VISA or MasterCard.' });
  }
  throw err;
}

Prevention

When it happens

Trigger: Passing a ChargeRequest whose credit_card_number is a valid but unsupported card: any AMEX number (starting 34/37), Diners Club (30/36/38), Discover (6011/65), JCB, etc.

Common situations: Users entering corporate AMEX cards at checkout; test suites reusing AMEX test numbers; integrating this demo service into an app that expects all major card networks to be supported.

Related errors


AI-assisted analysis of GoogleCloudPlatform/microservices-demo@72ba613a05 (2026-09-02). Data as JSON: /api/errors/7fe37ab6798f3e6f. Report an issue: GitHub.