如何使用httr创建条纹会话

时间:2019-07-09 15:51:51

标签: r curl shiny stripe-payments httr

我正在尝试创建一个闪亮的条纹会话以启动付款过程。 (Stripe docs

我有这个curl发布请求:

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

使用httr在R中尝试类似的命令(包括对line_items的预订)总是会引发错误。
到目前为止,我已经尝试过:

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"
)

哪个退出错误

Error in vapply(elements, encode, character(1)) : 
  values must be length 1,
 but FUN(X[[6]]) result is length 2

该问题似乎是由于参数card和subscription_data引起的。至少subscription_data必须为字典类型。 httr如何处理?

1 个答案:

答案 0 :(得分:0)

我通过将主体参数设置为尽可能接近原始curl命令来解决了这个问题

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"
)