I'm trying to create a stripe session in shiny to inititate a payment process. (Stripe docs)
I have this curl post request:
curl https://api.stripe.com/v1/checkout/sessions \
-u sk_test_XXX: \
-d success_url="https://example.com/success" \
-d cancel_url="https://example.com/cancel" \
-d payment_method_types[]=card \
-d line_items[][name]=T-shirt
Trying a similar command (incl subscriptions for line_items) in R with httr always throws an error.
So far, I tried this:
httr::POST(
url = "https://api.stripe.com/v1/checkout/sessions",
config = httr::add_headers(
Authorization = paste("Bearer", Sys.getenv("stripe_key"))
),
body = list(
success_url = "https://example.com/success",
cancel_url = "https://example.com/cancel",
payment_method_types = "card",
subscription_data = list(
items = list(plan = "plan_XX"),
trial_period_days = 7
)
),
encode = "form"
)
Which exits with error
Error in vapply(elements, encode, character(1)) :
values must be length 1,
but FUN(X[[6]]) result is length 2
The issue seems to be due to parameters card and subscription_data. At least subscription_data needs to be of type dictionary. How to handle this with httr?
I solved the issue by setting the body parameters as close as possible to the original curl command
httr::POST(
url = "https://api.stripe.com/v1/checkout/sessions",
config = httr::add_headers(
Authorization = paste("Bearer", Sys.getenv("stripe_key"))
),
body = list(
`success_url` = "https://example.com/success",
`cancel_url` = "https://example.com/cancel",
`payment_method_types[]` = "card",
`subscription_data[items][][plan` = "plan_XX"
),
encode = "form"
)