【问题标题】:How to tell if a card is the default payment source in Stripe?如何判断一张卡是否是 Stripe 的默认支付来源?
【发布时间】:2018-09-15 05:02:42
【问题描述】:

如何判断 Stripe 中的卡是否是 Java 中的默认支付选项?例如:

for ( ExternalAccount source : customer.getSources().getData() ){
    Card card = (Card) source;
    if ( card.???? // how do I know if it's the default payment option
}

或者,我可以查询默认付款来源吗?

我问的原因是因为我想向客户显示卡的最后 4 位数字,以向他们展示当前默认的付款方式。但是如果有多张卡,我怎么知道呢?

【问题讨论】:

    标签: java stripe-payments


    【解决方案1】:

    据我现在所知(查看文档等),无法通过检查一张卡来判断它是否是默认付款来源。但我错过了让您直接检索默认付款选项(如果有的话)的客户选项,例如

    ExternalAccount defaultSource = customer.getDefaultSourceObject();
    Card defaultCard = (Card) defaultSource; // if it's a card
    

    【讨论】:

      【解决方案2】:

      您可以通过比较fingerprint 属性来查看您是否已经有一张卡片存档。永远都是一样的。

      您还可以通过使用 Customer 对象上的 getDefaultSourceObject() 方法直接获取默认付款来源。

      https://github.com/stripe/stripe-java/blob/master/src/main/java/com/stripe/model/Customer.java#L92

      换句话说,将您拥有的fingerprint 与默认来源的fingerprint 进行比较,您就会得到答案。

      希望有帮助!

      【讨论】:

        【解决方案3】:

        使用最新的 Stripe SDK,我无法使用 customer.getDefaultSourceObject()。这是适合我的代码

            String defaultSource = customer.getDefaultSource();
            if (defaultSource != null) {
                List<ExternalAccount> sources = customer.getSources().getData();
                for (ExternalAccount source: sources) {
                    if (source instanceof Card && source.getId().equals(defaultSource)) {
                        Card card = (Card)source;
                        LOG.info("Last-4=" + card.getLast4()...);
                        return card;
                    }
                }
            }
        

        【讨论】:

        • 我可以知道“List sources = customer.getSources().getData();”中的客户是什么
        • 客户 customer = Customer.list(Collections.singletonMap("email", "alice@bob.com")).getData().get(0);
        猜你喜欢
        • 2021-02-14
        • 2014-09-02
        • 2021-01-08
        • 2016-04-27
        • 2012-01-29
        • 1970-01-01
        • 2021-09-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多