【问题标题】:Stripe - Add new card to existing customerStripe - 向现有客户添加新卡
【发布时间】:2015-05-08 20:55:45
【问题描述】:

我需要为现有客户添加一张卡。这是我所做的:

1。从用户提交中获取token

card_token = request.POST('stripeToken')

2。检索客户

customer =  stripe.Customer.retrieve('cus_xxxxxxxxxx')

3。向该客户添加卡

customer.Cards.create(card=card_token)

#3我遇到了麻烦,因为看起来客户没有方法卡,但我看到有人在别处做过。

我应该如何做到这一点?

【问题讨论】:

    标签: stripe-payments


    【解决方案1】:

    如果您使用的是2015-02-18 API 版本或更高版本,则cards 属性已更改为sources,正如您在changelog 中看到的那样

    Create Card API 上的文档现在显示以下代码:

    customer = stripe.Customer.retrieve('cus_xxxxxxxxxx')
    customer.sources.create(card=card_token)
    

    您可以在仪表板的 API 密钥 settings 中找到您的 API 版本,您还可以使用 Stripe-Version 标头将您的 API 请求强制为较旧的 API 版本,以便 cards 仍然可以按照Versioning 文档:

    stripe.api_version = '2015-01-26'
    

    【讨论】:

      【解决方案2】:

      2019 年更新:随着欧洲的强客户身份验证 (SCA) 要求,情况发生了一些变化;您现在可能希望使用 Setup Intents API 预先收集卡详细信息以供将来付款。

      此新 API 符合 PCI 和 SCA。你可以了解更多here
      或在 GitHub 上查看此示例代码:https://github.com/stripe-samples/saving-card-without-payment

      您现在也可以entirely with Checkout 这样做!

      【讨论】:

        【解决方案3】:

        示例(customerId - cus_xxxxxxxxxx):

            Stripe.apiKey = stripeApiKey;
        
            Customer customer = Customer.retrieve(customerId);
        
            Map<String, Object> cardParams = new HashMap<String, Object>();
            cardParams.put("number", "4242424242424242");
            cardParams.put("exp_month", "12");
            cardParams.put("exp_year", "2018");
            cardParams.put("cvc", "314");
        
            Map<String, Object> tokenParams = new HashMap<String, Object>();
            tokenParams.put("card", cardParams);
            Token cardToken = Token.create(tokenParams);
        
            Map<String, Object> sourceParams = new HashMap<String, Object>();
            sourceParams.put("source", cardToken.getId()); //?
            Card source = (Card) customer.getSources().create(sourceParams);
            logger.info("Card created: " + source.toString());
        

        【讨论】:

        • 注意:最好不要在服务器端代码中处理卡号或 cvc。这样做会将您的服务器置于 PCI 范围内,这是大多数开发人员想要避免的。与避免服务器端处理敏感卡数据相比,使用此代码的任何人在实现 PCI 合规性方面都将面临更多挑战/更昂贵的时间。
        猜你喜欢
        • 1970-01-01
        • 2017-04-20
        • 2020-08-14
        • 2019-03-03
        • 2017-03-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-04
        相关资源
        最近更新 更多