【问题标题】:How to manually complete the stripe checkout session from backend for testing?如何从后端手动完成条带结帐会话以进行测试?
【发布时间】:2021-10-05 19:20:12
【问题描述】:

我正在编写关于计费系统如何对 Stripe 中的各种事件做出反应的测试。在我们的后端测试中,有一种运行计费测试的模式,以便它与 Stripe API 对话,而不是使用固定装置。它会自动将来自 Stripe 的响应保存为固定装置。这意味着您只需要在 Stripe API 或计费系统发生更改时以夹具生成模式运行测试。

当然,棘手的部分是查看系统如何对来自 Stripe 的 webhook 事件做出反应。我发现的解决方法是使用条带事件 API 端点来获取最近的事件并将其发布到 webhook 端点。您可以使这项工作适用于几乎所有重要事件。例如,将有效的 PaymentMethod 附加到 PaymentIntent 将导致 payment_intent.succeeded 事件。您可以使用 stripe.Event.list 获取该事件并将该事件发送到计费系统的 webhook 以模拟该事件。

我正在生成 toruble 的事件是 checkout.session.completed 事件。因为看起来您只能在通过 Stripe 结帐后才能生成事件,这在后端是不可能的。当然,你可以像这样创建一个假事件

 [checkout_setup_intent] = stripe.SetupIntent.list(limit=1)
        stripe_setup_intent = stripe.SetupIntent.create(
            payment_method=payment_method.id,
            confirm=True,
            payment_method_types=checkout_setup_intent.payment_method_types,
            customer=checkout_setup_intent.customer,
            metadata=checkout_setup_intent.metadata,
            usage=checkout_setup_intent.usage,
        )
        [stripe_session] = stripe.checkout.Session.list(limit=1)
        stripe_session_dict = stripe_session.to_dict_recursive()
        stripe_session_dict["setup_intent"] = stripe_setup_intent.id

        event_payload = {
            "object": "event",
            "data": {"object": stripe_session_dict},
            "type": "checkout.session.completed",
        }

但这并不理想。有没有办法用真实数据触发checkout.session.completed事件而不需要Web ui?

【问题讨论】:

    标签: stripe-payments


    【解决方案1】:

    你可以用Stripe CLI:

    stripe trigger checkout.sessions.completed

    【讨论】:

    • 这会触发带有示例数据的 webhook 对吧?我正在寻找带有真实数据而非样本数据的checkout.sessions.completed 事件。也就是说,事件的 setup_intent 应该有后端测试在创建会话时设置的元数据。
    【解决方案2】:

    我的解决方案是使用 e2e 测试(使用 cypress)完成支付,并从我的集成测试代码中调用它。

    这需要大约 10 秒才能完成。

    NUnit 测试:

    var paymentUrl = "https://checkout.stripe.com/pay/cs_test_a1nqdPnsxtq.......";
            Process process = new Process();
            process.StartInfo.WorkingDirectory = @"C:\PathToCypress";
            process.StartInfo.FileName = "cmd.exe";
            process.StartInfo.Arguments = $"/c npx cypress run --env url={paymentUrl}";
            process.StartInfo.UseShellExecute = true;
            process.Start();
            process.WaitForExit();
    

    赛普拉斯脚本:

    describe('Stripe Billing', () => {
        it('Completes a checkout session', () => {
            cy.visit(Cypress.env('url'));
    
            // Enter billing details
            cy.get('#cardNumber').type('4242424242424242');
            cy.get('#cardExpiry').type('3333');
            cy.get('#cardCvc').type('3333');
            cy.get('#billingName').type('TestName');
            cy.get('.SubmitButton').click();
    
            // Wait for payment to succeed
            cy.server().route('GET', 'https://api.stripe.com/v1/checkout/sessions/completed_webhook_delivered/*').as('completed'); // define request
    
            cy.wait('@completed', { timeout: 60000 });
        })
    })
    

    【讨论】:

      猜你喜欢
      • 2021-10-27
      • 2020-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-25
      • 2021-09-16
      • 2018-06-26
      相关资源
      最近更新 更多