【问题标题】:Stripe charge create amount to be dynamic条带电荷创造量动态
【发布时间】:2015-11-13 06:45:58
【问题描述】:

我正在尝试实施条带支付,我知道条带要求您设置两次金额。一次在结帐时,另一次在服务器端。

我使用 web2py 作为我的框架。

所以我的问题是如何让它们匹配?

我通过 JS 使服务器端动态化,但我正在努力让服务器端拥有相同的数量。

# Set your secret key: remember to change this to your live secret key in production
# See your keys here https://dashboard.stripe.com/account/apikeys
stripe.api_key = "sk_test_BQokikJOvBiI2HlWgH4olfQ2"

# Get the credit card details submitted by the form
token = request.POST['stripeToken']

# Create the charge on Stripe's servers - this will charge the user's card
try:
  charge = stripe.Charge.create(
      amount=1000, # how to make this portion match the check out amount
      currency="usd",
      source=token,
      description="Example charge"
  )
except stripe.error.CardError, e:
  # The card has been declined
  pass

是否可以获取更多信息?

【问题讨论】:

  • 您将令牌传递给您的服务器以通过 POST 变量完成结帐。为什么不通过金额呢?
  • 我尝试用谷歌搜索,但我不知道如何使用 json/jquery 来做到这一点。但是很好
  • 你是如何传递stripeToken 变量的?不管你怎么做,对金额做同样的事情。
  • stripeToken 变量是使用 stripe 在网站上提供的 api 传递的。所以我不太确定这是怎么发生的。

标签: stripe-payments web2py


【解决方案1】:

data-amountdata-currency Checkout configuration options 仅用于显示目的。它们与实际费用的金额和货币无关。

要让您的用户自己指定金额,您可以在表单中添加一个amount 字段,该字段将与“正常”结帐parametersstripeTokenstripeEmail 等)一起发送.

这里有一个简单的 JSFiddle 来说明:https://jsfiddle.net/ywain/g2ufa8xr/

服务器端,您需要做的就是从 POST 参数中获取金额:

try:
  charge = stripe.Charge.create(
    amount=request.POST['amount']
    # ...

当然,在真实场景中,您应该在客户端和服务器端验证 amount 字段。至少,您要确保它是:

【讨论】:

  • 另外,注意在web2py中,提交的表单值在request.post_vars,所以应该是request.post_vars.amount
猜你喜欢
  • 2019-04-03
  • 1970-01-01
  • 2019-03-09
  • 1970-01-01
  • 2015-08-10
  • 2016-02-04
  • 1970-01-01
  • 2020-12-04
  • 2016-11-15
相关资源
最近更新 更多