Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stripe CLI for triggering invoice.payment_failed fails when passing subscription #1249

Closed
TheLox95 opened this issue Sep 21, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@TheLox95
Copy link

The more information we have the easier it is for us to help. Feel free to remove any sections that might not apply

Issue

When triggering the invoice.payment_failed and passing a subscription the request fails with this error

stripe  trigger \
invoice.payment_failed \
--override \
invoice:customer=cus_123 \
--override \
invoice:subscription=sub_123 \
--override \
payment_method:customer=cus_123 \
--override \
invoiceitem:customer=cus_123
Trigger failed: Request failed, status=400, body={
  "error": {
    "message": "You may only specify one of these parameters: pending_invoice_items_behavior, subscription.",
    "param": "pending_invoice_items_behavior",
    "request_log_url": "https://dashboard.stripe.com/test/logs/req_oNl6qwS50AWtB0?t=1726899665",
    "type": "invalid_request_error"
  }
}

I already tried editing the value for pending_invoice_items_behavior but does not work

Expected Behavior

To call the webhook for this event and include the subscription ID in the request body

Steps to reproduce

This code generates the CLI command showed above

import Stripe from "stripe";

const stripe = new Stripe(
  "sk_test_key"
);

const name = "Joe doe";
const email = "[email protected]";

const paymentMethod = await stripe.paymentMethods.create({
  type: "card",
  card: { token: "tok_visa" },
});

const customer = await stripe.customers.create({
  name,
  email,
});

const attach = await stripe.paymentMethods.attach(paymentMethod, {
  customer: customer.id,
});

await stripe.customers.update(customer.id, {
  invoice_settings: {
    default_payment_method: attach.id,
  },
});

const price = await stripe.prices.create({
  currency: "gbp",
  unit_amount: 1000,
  product_data: {
    name: "Gold Plan",
  },
});

await stripe.invoiceItems.create({
  customer: stripeData.customer.id,
  price: price.id,
});

const testSub = await stripe.subscriptions.create({
  customer: stripeData.customer.id,
  items: [
    {
      price: priceForSub.id,
    },
  ],
});

const cliParams = [
  "trigger",
  "invoice.payment_failed",
  `--override`,
  `invoice:customer=${customer.id}`,
  `--override`,
  `invoice:subscription=${testSub.id}`,
  `--override`,
  `payment_method:customer=${customer.id}`,
  `--override`,
  `invoiceitem:customer=${customer.id}`,
];
console.log("stripe ", cliParams.join(" \\\n"));

Environment

Operating System: elementary OS 6.1 Jólnir
Kernel: Linux 5.15.0-91-generic
Architecture: x86-64

@TheLox95 TheLox95 added the bug Something isn't working label Sep 21, 2024
@jeffl8n
Copy link

jeffl8n commented Sep 21, 2024

This is happening because pending_invoice_items_behavior is set to include by default, so the trigger will work without having to pass a subscription override.

If you want to pass a subscription override, you will need to add the switch --remove invoice:pending_invoice_items_behavior to remove the default parameter from being sent by the cli.

For example:

stripe trigger \
invoice.payment_failed \
--override \
invoice:customer=cus_123 \
--override \
invoice:subscription=sub_123 \
--override \
payment_method:customer=cus_123 \
--override \
invoiceitem:customer=cus_123 \
--remove \
invoice:pending_invoice_items_behavior

@TheLox95
Copy link
Author

@jeffl8n Thanks that fix the error

This is a full working example

import Stripe from "stripe";
const stripe = new Stripe(
    "sk_test_XXX"
);

const name = "Jhon Doe";
const email = "[email protected]";

const paymentMethod = await stripe.paymentMethods.create({
    type: "card",
    card: {
        token: "tok_visa"
    },
});

const customer = await stripe.customers.create({
    name,
    email,
});

await stripe.paymentMethods.attach(paymentMethod.id, {
    customer: customer.id,
});

await stripe.customers.update(customer.id, {
    invoice_settings: {
        default_payment_method: paymentMethod.id,
    },
});

const price = await stripe.prices.create({
    currency: "gbp",
    unit_amount: 1000,
    product_data: {
        name: "Gold Plan",
    },
    recurring: {
        interval: "day",
    },
});

const priceForSub = await stripe.prices.create({
    currency: "gbp",
    unit_amount: 1000,
    product_data: {
        name: "Gold Plan",
    },
    recurring: {
        interval: "day",
    },
});

const testSub = await stripe.subscriptions.create({
    customer: customer.id,
    items: [{
        price: priceForSub.id,
    }, ],
});

await stripe.subscriptionItems.create({
    subscription: testSub.id,
    price: price.id,
});

const cliParams = [
    "trigger",
    "invoice.payment_failed",
    `--override`,
    `invoice:customer=${customer.id}`,
    `--override`,
    `invoice:subscription=${testSub.id}`,
    `--override`,
    `payment_method:customer=${customer.id}`,
    `--override`,
    `invoiceitem:customer=${customer.id}`,
    `--remove`,
    `invoice:pending_invoice_items_behavior`,
];
console.log("stripe ", cliParams.join(" \\\n"));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants