如何在执行特定文件上的Python函数之前等待Stripe完成付款?

huangapple go评论64阅读模式
英文:

How can I wait for Stripe to complete payment before I execute a Python function on specific file?

问题

我正在使用Python + Flask构建一个应用程序,该应用程序接受用户的CSV文件并从API获取数据进行丰富。

我的目标:

  • 用户上传CSV文件 [已完成]
  • 设置并在Stripe支付页面上显示支付金额 [已完成]
  • 一旦用户付款,然后CSV文件将从API中获取数据(添加一列),并通过电子邮件发送给用户。 [丰富和发送电子邮件已完成。我只是不知道如何等待+匹配支付以确保正确的csv]

我的问题:

  • 如何确保在Stripe支付完成之前不对用户进行CSV文件的丰富/发送电子邮件?

我已经设置了一个Webhook。问题是,我不知道如何将用户上传的CSV文件与来自Stripe的实际payment_id进行匹配,以确保我发送给他们正确的文件。

我相信我只是对某些概念一头雾水,所以任何指导性的帮助都将不胜感激。

英文:

I am using Python + Flask to build an app that takes a user CSV file and enriches it with data from an API.

My objective:

  • User uploads CSV file [done]
  • A payment amount is set and presented on Stripe payment page [done]
  • Once user pays, then the CSV file is enriched with data from an API (a column is appended), and is emailed to the user. [enriching and emailing is done. I just don't know how to make it wait + match payment to correct csv]

My question:

  • How can I make sure that the CSV file is not enriched/emailed to the user until the Stripe payment is completed?

I have set up a webhook. The problem is, I don't know how to match up the CSV file the user uploaded with the actual payment_id from Stripe to make sure I send them the right file.

I'm sure I am just blanking on some concept here, so any directional help is appreciated.

答案1

得分: 1

如果您想要在执行Python函数之前等待Stripe完成付款,您需要实现一个监听Stripe中payment_intent.succeeded事件的Webhook。当触发此事件时,表示付款已成功完成,然后您可以执行Python函数。

以下是您需要采取的基本步骤概述:

  1. 在您的应用程序中实现一个Webhook端点,用于监听Stripe中的payment_intent.succeeded事件。

  2. 在Webhook端点中,当触发payment_intent.succeeded事件时,您可以调用您的Python函数。

  3. 在Stripe仪表板中配置Webhook端点,以将payment_intent.succeeded事件发送到您的应用程序。

以下是一个在Flask中的简单示例实现:

from flask import Flask, request

app = Flask(__name__)

@app.route("/webhook", methods=["POST"])
def webhook():
  # 从请求中检索事件数据
  event_json = request.get_json()

  # 检查事件是否为payment_intent.succeeded事件
  if event_json["type"] == "payment_intent.succeeded":
    # 在此处调用您的Python函数
    execute_python_function()

  return "success"

def execute_python_function():
  # 您的Python函数代码放在这里
  ...

if __name__ == "__main__":
  app.run(debug=True)

请注意,这只是一个基本示例,您需要根据您的应用程序的特定需求进行修改。您还需要确保您的Webhook端点是安全的,只能由Stripe访问。

英文:

If you want to wait for Stripe to complete a payment before executing a Python function, you'll need to implement a webhook that listens for the payment_intent.succeeded event in Stripe. When this event is triggered, it indicates that the payment has been completed successfully, and you can then execute your Python function.

Here's a basic outline of the steps you'll need to take:

  1. Implement a webhook endpoint in your application that listens for the payment_intent.succeeded event in Stripe.

  2. In the webhook endpoint, when the payment_intent.succeeded event is triggered, you can call your Python function.

  3. Configure the webhook endpoint in your Stripe dashboard to send the payment_intent.succeeded event to your application.

Here's a simple example implementation in Flask:

from flask import Flask, request

app = Flask(__name__)

@app.route("/webhook", methods=["POST"])
def webhook():
  # Retrieve the event data from the request
  event_json = request.get_json()

  # Check if the event is a payment_intent.succeeded event
  if event_json["type"] == "payment_intent.succeeded":
    # Call your Python function here
    execute_python_function()

  return "success"

def execute_python_function():
  # Your Python function code goes here
  ...

if __name__ == "__main__":
  app.run(debug=True)

Note that this is just a basic example, and you'll need to modify it to meet the specific needs of your application. You'll also need to ensure that your webhook endpoint is secured and can only be accessed by Stripe.

huangapple
  • 本文由 发表于 2023年2月10日 12:09:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75406858.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定