stripe / stripe-cli

A command-line tool for Stripe
https://stripe.com/docs/stripe-cli
Apache License 2.0
1.61k stars 374 forks source link

How to make Stripe CLI return more complete version of Payment Intent #826

Closed hashimaziz1 closed 2 years ago

hashimaziz1 commented 2 years ago

I'm testing Stripe webhooks locally using the Stripe CLI, and so far I've used it to return a PaymentIntent object that contains most of the data I need to capture and have also successfully persisted most of that data to my database, but I'm not able to get the postcode entered by the user in the Payment Element - billing_details and any objects under it give me undefined index errors when accessing them like so:

$transaction->postcode = $paymentIntent["billing_details"]["address"]["postal_code"];

Looking at the API documentation, it seems this is because billing_details is not available in a normal Payment Intent created by the create() method, but it does say:

When confirm=true is used during creation, it is equivalent to creating and confirming the PaymentIntent in the same call. You may use any parameters available in the confirm API when confirm=true is supplied.

How can I therefore make the Stripe CLI return a Payment Intent that uses the "confirm API" and therefore has access to more of the data about my transaction?

I've tried running both:

stripe trigger payment_intent.succeeded --add confirm=true
stripe trigger payment_intent.succeeded --override confirm=true

...but they both give more or less the same error:

panic: runtime error: index out of range [1] with length 1

goroutine 1 [running]:
github.com/stripe/stripe-cli/pkg/fixtures.buildRewrites({0xc00051f650, 0x1, 0x8ca15e}, 0x0)
        D:/a/stripe-cli/stripe-cli/pkg/fixtures/fixtures.go:377 +0x5ab
github.com/stripe/stripe-cli/pkg/fixtures.(*Fixture).Add(0xc0005a4160, {0xc00051f650, 0x1, 0x1})
        D:/a/stripe-cli/stripe-cli/pkg/fixtures/fixtures.go:168 +0x7e
github.com/stripe/stripe-cli/pkg/fixtures.NewFixtureFromFile({0x10e0de0, 0x16e1078}, {0xc00053dd80, 0x6b}
, {0x0, 0x0}, {0xede220, 0x16}, {0xef2396, 0x26}, ...)
        D:/a/stripe-cli/stripe-cli/pkg/fixtures/fixtures.go:103 +0x2d2
github.com/stripe/stripe-cli/pkg/fixtures.BuildFromFixtureFile({0x10e0de0, 0x16e1078}, {0xc00053dd80, 0xc
00053dd80}, {0x0, 0xc00021f8c0}, {0xede220, 0xa}, {0xef2396, 0x26}, ...)
        D:/a/stripe-cli/stripe-cli/pkg/fixtures/triggers.go:83 +0x108
github.com/stripe/stripe-cli/pkg/fixtures.Trigger({0x10cb1e0, 0xc0003fca20}, {0xc000030228, 0x1}, {0x0, 0
x0}, {0xede220, 0x16}, {0xc00053dd80, 0x6b}, ...)
        D:/a/stripe-cli/stripe-cli/pkg/fixtures/triggers.go:146 +0x2c5
github.com/stripe/stripe-cli/pkg/cmd.(*triggerCmd).runTriggerCmd(0xc00021e210, 0xc000240280, {0xc0003fcb7
0, 0x1, 0x0})
        D:/a/stripe-cli/stripe-cli/pkg/cmd/trigger.go:81 +0x205
github.com/spf13/cobra.(*Command).execute(0xc000240280, {0xc0003fcb40, 0x3, 0x3})
        C:/Users/runneradmin/go/pkg/mod/github.com/spf13/cobra@v1.2.1/command.go:856 +0x60e
github.com/spf13/cobra.(*Command).ExecuteC(0x167d940)
        C:/Users/runneradmin/go/pkg/mod/github.com/spf13/cobra@v1.2.1/command.go:974 +0x3bc
github.com/spf13/cobra.(*Command).Execute(...)
        C:/Users/runneradmin/go/pkg/mod/github.com/spf13/cobra@v1.2.1/command.go:902
github.com/spf13/cobra.(*Command).ExecuteContext(...)
        C:/Users/runneradmin/go/pkg/mod/github.com/spf13/cobra@v1.2.1/command.go:895
github.com/stripe/stripe-cli/pkg/cmd.Execute({0x10cb1e0, 0xc0003fc9f0})
        D:/a/stripe-cli/stripe-cli/pkg/cmd/root.go:82 +0x1b8
main.main()
        D:/a/stripe-cli/stripe-cli/cmd/stripe/main.go:27 +0x13b

And the same command with debugging enabled:

$ stripe trigger payment_intent.succeeded --add confirm=true --log-level "debug"

panic: runtime error: index out of range [1] with length 1

goroutine 1 [running]:
github.com/stripe/stripe-cli/pkg/fixtures.buildRewrites({0xc0003d0d40, 0x1, 0x8ca15e}, 0x0)
        D:/a/stripe-cli/stripe-cli/pkg/fixtures/fixtures.go:377 +0x5ab
github.com/stripe/stripe-cli/pkg/fixtures.(*Fixture).Add(0xc00022fb80, {0xc0003d0d40, 0x1, 0x1})
        D:/a/stripe-cli/stripe-cli/pkg/fixtures/fixtures.go:168 +0x7e
github.com/stripe/stripe-cli/pkg/fixtures.NewFixtureFromFile({0x10e0de0, 0x16e1078}, {0xc000138980, 0x6b}
, {0x0, 0x0}, {0xede220, 0x16}, {0xef2396, 0x26}, ...)
        D:/a/stripe-cli/stripe-cli/pkg/fixtures/fixtures.go:103 +0x2d2
github.com/stripe/stripe-cli/pkg/fixtures.BuildFromFixtureFile({0x10e0de0, 0x16e1078}, {0xc000138980, 0xc
000138980}, {0x0, 0xc00022f970}, {0xede220, 0xa}, {0xef2396, 0x26}, ...)
        D:/a/stripe-cli/stripe-cli/pkg/fixtures/triggers.go:83 +0x108
github.com/stripe/stripe-cli/pkg/fixtures.Trigger({0x10cb1e0, 0xc0000aae40}, {0xc000030228, 0x0}, {0x0, 0
x0}, {0xede220, 0x16}, {0xc000138980, 0x6b}, ...)
        D:/a/stripe-cli/stripe-cli/pkg/fixtures/triggers.go:146 +0x2c5
github.com/stripe/stripe-cli/pkg/cmd.(*triggerCmd).runTriggerCmd(0xc00022e160, 0xc000242280, {0xc000572fa
0, 0x1, 0x0})
        D:/a/stripe-cli/stripe-cli/pkg/cmd/trigger.go:81 +0x205
github.com/spf13/cobra.(*Command).execute(0xc000242280, {0xc000572f50, 0x5, 0x5})
        C:/Users/runneradmin/go/pkg/mod/github.com/spf13/cobra@v1.2.1/command.go:856 +0x60e
github.com/spf13/cobra.(*Command).ExecuteC(0x167d940)
        C:/Users/runneradmin/go/pkg/mod/github.com/spf13/cobra@v1.2.1/command.go:974 +0x3bc
github.com/spf13/cobra.(*Command).Execute(...)
        C:/Users/runneradmin/go/pkg/mod/github.com/spf13/cobra@v1.2.1/command.go:902
github.com/spf13/cobra.(*Command).ExecuteContext(...)
        C:/Users/runneradmin/go/pkg/mod/github.com/spf13/cobra@v1.2.1/command.go:895
github.com/stripe/stripe-cli/pkg/cmd.Execute({0x10cb1e0, 0xc0000aae10})
        D:/a/stripe-cli/stripe-cli/pkg/cmd/root.go:82 +0x1b8
main.main()
        D:/a/stripe-cli/stripe-cli/cmd/stripe/main.go:27 +0x13b

I'm using the Windows binary on Cygwin, which is an emulated UNIX-like environment running on top of Windows, but the rest of the binary has functioned fine as most .exes are expected to, so I don't think it's an environment issue.

ianjabour-stripe commented 2 years ago

👋 @Kaos-Industries , thanks for filing this issue. The reason why you are getting that failure is because the --override / --add / --remove flags require you provide the name of the fixture as well.

In case you didn't know, stripe trigger actually uses the stripe fixtures command under the hood by simply selecting a predefined fixture file (You can find the one for payment_intent.succeeded here).

Being that the fixture name is payment_intent, you'd run it like so:

stripe trigger payment_intent.succeeded --add payment_intent:confirm=true

Side note:

If you want more flexibility on the parameters being set or API calls being made I'd recommend using stripe fixtures, i.e create a file like so:

// payment_intent.succeeded.json

{
  "_meta": {
    "template_version": 0
  },
  "fixtures": [
    {
      "name": "payment_intent",
      "path": "/v1/payment_intents",
      "method": "post",
      "params": {
        "amount": 2000,
        "confirm": "true",
        "currency": "usd",
        "description": "(created by Stripe CLI)",
        "payment_method": "pm_card_visa",
        "payment_method_types": ["card"],
        "shipping": {
          "name": "Jenny Rosen",
          "address": {
            "line1": "510 Townsend St",
            "postal_code": "94103",
            "city": "San Francisco",
            "state": "CA",
            "country": "US"
          }
        }
      }
    }
  ]
}

Then to run it:

stripe fixtures payment_intent.succeeded.json

The stripe fixtures command also supports the --override / --add / --remove flags as well if you need them.

ianjabour-stripe commented 2 years ago

Showing a runtime error in this case is not a great experience either so we'll go ahead and fix that to be a clearer error message as well 👍