简体   繁体   English

条纹订阅测试

[英]Stripe Subscription Testing

I'm trying to create a monthly subscription with stripe.我正在尝试使用条纹创建每月订阅。

I wrote a sample code like that我写了一个这样的示例代码

            if event_type == 'checkout.session.completed':
            # Payment is successful and the subscription is created.
            # You should provision the subscription and save the customer ID to your database.
            try:
                session = stripe.checkout.Session.retrieve(
                    event['data']['object'].id, expand=['line_items', 'subscription'])
                _current_period_end = session['subscription']['current_period_end']
                _current_period_start = session['subscription']['current_period_start']
                _product_id = session['subscription']['items']['data'][0]['plan']['product']
                _user_id = session['client_reference_id']
                _stripe_customer_id = session['customer']
                _subscription_id = session["subscription"]['id']
                '''
                do other things to update user package
                '''

            except Exception as e:
                '''
                error log
                '''
        elif event_type == 'invoice.paid':

                if THIS_IS_NOT_FIRST_TIME:
                    parse_event_data
                    '''
                    do other things to extend subscription
                    '''

I have some questions;我有一些问题;

  1. I parsed web hook result from a dict which is returned from stripe.checkout.Session.retrieve object.我从从 stripe.checkout.Session.retrieve object 返回的字典中解析了 web 钩子结果。 It seemed a little bit odd to me.我觉得有点奇怪。 What if stripe update his API response and use a different names for dict keys that i used?如果条带更新他的 API 响应并为我使用的 dict 键使用不同的名称怎么办? Is there another way to get these values such as with dot notation maybe (session.get.product_id)?是否有另一种方法来获取这些值,例如使用点表示法(session.get.product_id)?

  2. How can i understand that invoice.paid event not triggered for the first time of subscription?我如何理解第一次订阅未触发 invoice.paid 事件?

  3. I want to test renewal process of my monthly subscription.我想测试我的每月订阅的续订过程。 I used stripe trigger invoice.payment_succeeded but i need a real data for my test accounts (with a test customer, subscription, product etc...)我使用了条带触发器 invoice.payment_succeeded 但我需要我的测试帐户的真实数据(测试客户、订阅、产品等......)

  4. I can update my user package with using CHECKOUT_SESSION_ID from checkout success url ("success?session_id={CHECKOUT_SESSION_ID}). Should i do that or use checkout.session.completed web hook? I can update my user package with using CHECKOUT_SESSION_ID from checkout success url ("success?session_id={CHECKOUT_SESSION_ID}). Should i do that or use checkout.session.completed web hook?

  5. I have just returned HTTP 500 response to every request to my webhook URL to see if STRIPE show an error message to user in checkout page.我刚刚返回 HTTP 500 响应对我的 webhook URL 的每个请求,以查看 STRIPE 是否在结帐页面向用户显示错误消息。 However, STRIPE just created a successful subscription.但是,STRIPE 刚刚创建了一个成功的订阅。 In that case, STRIPE will take a payment from my customer, however even if i can not update my customer package on my database.在这种情况下,STRIPE 将从我的客户那里收取款项,但是即使我无法在我的数据库中更新我的客户 package。 What should i do to prevent this issue?我应该怎么做才能防止这个问题? Should i create a scheduled job to sync data between STRIPE and my db?我应该创建一个计划作业来在 STRIPE 和我的数据库之间同步数据吗?

You have many separate questions that would be better suited for Stripe's support team directly: https://support.stripe.com/contact/email您有许多单独的问题更适合直接向 Stripe 的支持团队提问: https://support.stripe.com/contact/email

Now I will try to touch on some of the questions you had starting with the first one.现在我将尝试谈谈您从第一个问题开始提出的一些问题。

When you call session = stripe.checkout.Session.retrieve(...) you get back a class that is a Checkout Session . When you call session = stripe.checkout.Session.retrieve(...) you get back a class that is a Checkout Session . All the properties of the class map to the properties covered in the API Reference for Session . class map 的所有属性到 API 参考Z71C7AE294B7ABD866B3FB295B3B中涵盖的属性。 This means you can do session.id which is cs_test_123 or session.created which is a timestamp of the creation date.这意味着您可以执行session.id这是cs_test_123session.created这是创建日期的时间戳。 It's not really different from accessing as a dictionary overall.这与整体上作为字典访问并没有什么不同。

You're also asking if those can change and Stripe explains their backwards compatibility policy in details in their docs here .您还询问这些是否可以更改,Stripe 在此处的文档中详细解释了他们的向后兼容性政策。 If they were to change a name from created to created_at , they would do it in a new API version for new integrations and it wouldn't impact your code unless you manually changed the API version for your account so that is safe.如果他们要将名称从created更改为created_at ,他们将在新的 API 版本中进行新的集成,并且除非您手动更改您帐户的 API 版本,否则不会影响您的代码,这样是安全的。

For the invoice.paid event, you want to look at the invoice's billing_reason property which would be subscription_create for the first invoice.对于invoice.paid事件,您要查看发票的billing_reason 属性,该属性将是第一张发票的subscription_create

You can test all of this easily in Test mode, create a session, start a subscription, etc. You can also simulate cycle changes.您可以在测试模式下轻松测试所有这些,创建 session,开始订阅等。您还可以模拟周期变化。

I hope this helps but chatting with their support team is your best bet since those are more integration questions and not coding questions.我希望这会有所帮助,但与他们的支持团队聊天是你最好的选择,因为这些是更多的集成问题,而不是编码问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM