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

时间:2015-07-31 12:49:38

标签: python django stripe-payments

我目前正在将条带集成到django项目中。我使用代码示例跟踪stripe website上的教程,但我不确定它们是否已准备就绪。这是应用程序中最精细的部分,我想确保它正确完成。目前,我有类似的东西。

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)

修改 我担心的是:

  • 是否会出现卡被充电两次的情况?
  • 是否会出现创建和保存订单的情况 但收费不成功?
  • 创建和保存订单应该在哪里?

2 个答案:

答案 0 :(得分:4)

这是我们在项目中使用的内容,希望它有所帮助!!

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

答案 1 :(得分:0)

您可以考虑使用django stripe payments,或使用其代码作为参考。