Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在django/python中创建条带电荷的正确方法_Python_Django_Stripe Payments - Fatal编程技术网

在django/python中创建条带电荷的正确方法

在django/python中创建条带电荷的正确方法,python,django,stripe-payments,Python,Django,Stripe Payments,我目前正在将stripe集成到django项目中。我遵循了使用代码示例的教程,但我不确定它们是否已准备好生产。这是应用程序中最微妙的部分,我想确保它做得正确。目前,我有一些类似的东西 if new_order_form.is_valid(): new_order = new_order_form.save(commit=False) new_order.total = 10 new_order.order_number = generate_order_number(8)

我目前正在将stripe集成到django项目中。我遵循了使用代码示例的教程,但我不确定它们是否已准备好生产。这是应用程序中最微妙的部分,我想确保它做得正确。目前,我有一些类似的东西

if new_order_form.is_valid():
    new_order = new_order_form.save(commit=False)
    new_order.total = 10
    new_order.order_number = generate_order_number(8)
    try:
      charge = stripe.Charge.create(
          amount=1000, # new_order.total * 100
          currency="usd",
          source=token,
          description="Example charge"
      )
      new_order.charge_id = charge.id
    except stripe.error.CardError, e:
      # The card has been declined
      pass

    new_order.save()
    return HttpResponseRedirect('/thanks/')
return redirect(request, 'new_order.html' context)
编辑: 我关注的是:

  • 有没有可能会出现一种情况,即信用卡被收取两次费用
  • 是否存在创建和保存订单的场景
    但指控没有成功
  • 应在何处创建和保存订单

你可以考虑使用或使用他们的代码作为参考。

< P>这是我们在我们的项目中使用的,希望它有帮助!p>
try:
    charge = stripe.Charge.create(
      amount={{amount}}, 
      currency="usd",
      customer={{customer}},
      description={{description}},
      metadata={{this.id}}
  )
except stripe.error.CardError as e:
    # Problem with the card
    pass
except stripe.error.RateLimitError as e:
    # Too many requests made to the API too quickly
    pass
except stripe.error.InvalidRequestError as e:
    # Invalid parameters were supplied to Stripe API
    pass
except stripe.error.AuthenticationError as e:
    # Authentication Error: Authentication with Stripe API failed (maybe you changed API keys recently)
    pass
except stripe.error.APIConnectionError as e:
    # Network communication with Stripe failed
    pass
except stripe.error.StripeError as e:
    # Stripe Error
    pass
else:
    #success

您使用此代码遇到的问题是什么?运行时是否有错误?我仍在测试不同的场景,尚未遇到任何问题。您的帖子中没有问题,因此无法真正回答。要得到具体的答案,请提出具体的问题。使用提供的代码很难告诉您希望反馈什么,因为有硬编码的值,并且没有解释它应该做什么,或者您试图处理什么情况。我理解这一点。我编辑了这篇文章以添加更多信息。这比仅仅使用stripe教程中的代码片段有什么优势。在包含了上面的代码片段之后,他们基本上写了:“就是这样!如果创建费用请求成功,并且没有抛出错误,那么该卡已成功收费。您将在七天内收到您的钱。”我将查看他们的代码以供参考。谢谢