【问题标题】:How to make the actual Stripe charge in Angular (stripeToken already known)?如何在 Angular 中进行实际的 Stripe 收费(stripeToken 已知)?
【发布时间】:2016-01-04 16:27:58
【问题描述】:

我正在使用Angular-Stripe-Checkout library 创建一个stripeToken,就像在这个example 中一样。一些亮点如下所示。

与许多 angular-stripe 库和示例一样,它仅显示如何创建 stripeToken。

但是,我不清楚在检索到 stripeToken 后如何实际向用户收费?

在 Stripe 上,他们有 instructions 关于如何使用 node.js 向用户收费。但我不清楚如何设置它并使其与 Angular 兼容。


代码亮点

html

<button ng-click="product.doCheckout()">Buy</button>

js

// note: StripeCheckout is injected in the controller
product.doCheckout = function(token, args) {

        // You should configure a handler when the view is loaded,
        // just as you would if you were using checkout.js directly.
        //
        // Unlike a vanilla Stripe Checkout handler, this one can be
        // reused as many times as you like.
        var handler = StripeCheckout.configure({
          name: "Custom Example",
          token: function(token, args) {
              console.log(token.id)
            //$log.debug("Got stripe token: " + token.id);
          }
        });

        var options = {
          description: "Ten dollahs!",
          amount: 10000
        };

        // The default handler API is enhanced by having open()
        // return a promise. This promise can be used in lieu of or
        // in addition to the token callback (or you can just ignore
        // it if you like the default API).
        //
        // The rejection callback doesn't work in IE6-7.
        handler.open(options)
          .then(function(result) {
            alert("Got Stripe token: " + result[0].id);
            console.log(result);

            var stripe = window.Stripe;
            var stripeToken = result[0].id;

            //
            // what next?

          },function() {
            alert("Stripe Checkout closed without making a sale :(");
          }
        );
    };

【问题讨论】:

    标签: javascript angularjs stripe-payments


    【解决方案1】:

    简短的回答是:不要这样做。

    长答案:您收到的这个条带令牌是一个交易 ID。它是公开的,因为它本身不构成任何风险。将其视为您要处理费用的卡的查找键。

    然而,要处理交易,您需要一个私有/秘密元素:您的密钥。为了在客户端处理它,您需要将此秘密泄露给客户端,这意味着该值对任何人都是可见的。

    我认为这样做并不明智,尤其是因为所讨论的密钥是您要进行条带化的 API 密钥。

    我还做了一个简短的检查,他们的 API 不会广播 CORS 标头,所以无论如何,您都无法使用某种后端来执行请求。

    创建费用的后端请求实际上非常简单。在实践中,HTTP 调用类似于:

    curl https://api.stripe.com/v1/charges \
      -u sk_test_BQokikJOvBiI2HlWgH4olfQ2: \
      -d amount=400 \
      -d currency=usd \
      -d source=tok_16t4Xt2eZvKYlo2CLvBSsmXD \
      -d description="Charge for test@example.com"
    
    • sk_test_BQokikJOvBiI2HlWgH4olfQ2 是 API 密钥
    • 您为此收取 400 美元
    • source 是您收到的条带令牌
    • 描述为支付描述

    如果您可以在任何服务器端环境中复制这样的调用,那您就很成功了。

    (这直接来自他们的 API 文档https://stripe.com/docs/api#create_charge

    【讨论】:

    • 那么有哪些选择呢?我如何称呼这个后端?
    • @JohnAndrews 添加了 API 调用的详细信息以在卡上创建费用。
    • 好吧,看起来很有趣。但是,我仍然不知道如何在后端应用它。如何在 HTTP 设置中使用 curl?
    猜你喜欢
    • 2021-06-03
    • 2020-03-07
    • 2015-11-08
    • 1970-01-01
    • 2015-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多