【问题标题】:Adding a Stripe subscription through API通过 API 添加 Stripe 订阅
【发布时间】:2018-10-20 23:17:35
【问题描述】:

我正在尝试自动创建客户并添加订阅。我可以通过 Zapier 创建客户,但我真的不知道如何使用 Python 或 Javascript 添加订阅或发票。我可以将我需要的所有数据传递到计划 ID 和发票金额等变量中。有人有这方面的经验吗?

【问题讨论】:

  • 我建议阅读 Stripe 的详细文档 stripe.com/docs/subscriptions/quickstart 以更好地了解所有这些工作原理,或者使用第三方 stripe.com/works-with/categories/recurring-payments
  • 感谢您的建议。我已经做到了这一点,但不明白我遇到的语法错误。 api.stripe.com import stripe stripe.api_key = "sk_test_kLyQtpKGLBRmorVmYCToOdai" stripe.InvoiceItem.create{ customer=input['custid'], amount=input['opponboarding'], currency="usd", description="Onboarding Fee" } 订阅=stripe.Subscription.create{ customer=input['custid'], items=[{'plan':input['oppplan']}], } 错误:Traceback(最近一次调用最后):SyntaxError:无效语法(用户代码.py,第 8 行)

标签: javascript python stripe-payments zapier stripe.net


【解决方案1】:

如果您有customer_id 和plan_id,则可以创建订阅请求。如果您想使用 Zapier 执行此操作,可以使用 Code (Javascript) 应用程序中的 fetch 库发出 POST 请求。

在输入数据中传递customer_id 和plan_id。

在 Base64 中对 Stripe 的密钥进行编码。您可以使用this site 对您的密钥进行编码。替换下面代码中的encodedAPiKey 并将其粘贴到Zapier 中。

当此代码步骤运行时,它将为客户创建订阅。

const url = 'https://api.stripe.com/v1/subscriptions';

//Replace with Base64 encoded secret key from Stripe.
const encodedApiKey = "c2tLsfdGVzdF9aYlFNVjdBSzE3Tm1sTVdMVjkwVWdWTz";

const headers = {
    'Authorization': `Basic ${encodedApiKey}`,
    'Content-Type': 'application/x-www-form-urlencoded'
};

fetch(`${url}?customer=${inputData.customer_id}&items[0][plan]=${inputData.plan_id}`, {method:'POST',
        headers: headers
       })
.then(res => res.json())
.then(json => {
  console.log(json);
  callback(null, json);
})
.catch(callback);

您可以在 Stripe here 上找到相关文档。

注意:如果此代码被多次调用,它将为客户创建多个订阅。您可能需要添加一些代码来处理该问题,或者以一种不会调用此步骤两次的方式设置您的 Zaps。

希望有帮助!

【讨论】:

    猜你喜欢
    • 2017-12-30
    • 2016-06-12
    • 2017-07-03
    • 2017-08-20
    • 2020-06-06
    • 2021-03-11
    • 2016-02-12
    • 2021-09-24
    • 2021-12-17
    相关资源
    最近更新 更多