在 Android 中使用令牌的 Stripe Card id

Posted

技术标签:

【中文标题】在 Android 中使用令牌的 Stripe Card id【英文标题】:Stripe Card id using token in Android 【发布时间】:2014-10-28 16:48:39 【问题描述】:

我正在我的应用程序中集成 Stripe Payment 模块。我使用这个Stripe Library. 现在使用此代码生成令牌。使用这个令牌我需要 card id before payment

How to get this card id'. ?

在这里,我向您展示 Stripe 的回应。

当我输入关于卡片的卡片信息时:

Stripe_Token::create(array(
"card" => array(
"number" => "4242424242424242",
"exp_month" => 8,
"exp_year" => 2015,
"cvc" => "314"
)
));

Stripe 之后给我这个回复:


"id": "tok_14WdJ02eZvKYlo2CyaZ49ZP7",
"livemode": false,
"created": 1409272314,
"used": false,
"object": "token",
"type": "card",
"card": 
"id": "card_14WdJ02eZvKYlo2C5nE5XjtP",
"object": "card",
"last4": "4242",
"brand": "Visa",
"funding": "credit",
"exp_month": 8,
"exp_year": 2015,
"fingerprint": "Xt5EWLLDS7FJjR1c",
"country": "US",
"name": null,
"address_line1": null,
"address_line2": null,
"address_city": null,
"address_state": null,
"address_zip": null,
"address_country": null,
"customer": null


付款前和创建令牌后 我需要这张卡号:

"card": 
"id": "card_14WdJ02eZvKYlo2C5nE5XjtP",

希望你能得到我的问题。

【问题讨论】:

这是用 Java 写的吧? 你可以这么说,但我在android中使用@MattR @strider,我也在做同样的事情,我有一个疑问,在从客户端或服务器端获得付款的令牌之后 你可以在两边都做......如果你使用卡ID,那么你可以在服务器端付款,如果你使用令牌,那么付款是从你身边完成的......@Androidcuckoo 【参考方案1】:

正如您在 stripe 的响应中看到的那样,您可以简单地从响应中获得的令牌对象中获取您的卡 ID 和所有其他字段,如下所示:

String card_id = token.getCard().getId(); // to get card id
String id = token.getId(); // to get this "id": "tok_14WdJ02eZvKYlo2CyaZ49ZP7"
boolean live_mode = token.getLivemode(); // to get livemode
String last_four = token.getCard().getLast4(); // to get last 4

其中 token 是响应对象。

【讨论】:

这个答案解决了我的问题。非常感谢@Nishant Sharma 我无法从响应中获取令牌对象。【参考方案2】:

生成令牌后 用这个...

Customer.all(new HashMap<String, Object>());

最终我得到了我想要的 Cardid。

这是客户的全部回复。


  "data": [
    com.stripe.model.Customer JSON: 
      "object": "customer",
      "created": 1410001523,
      "id": "cus_4j9JlwfZ5arO4M",
      "livemode": false,
      "description": null,
      "email": "customer@example.com",
      "delinquent": false,
      "metadata": 
      ,
      "subscriptions": 
        "object": "list",
        "total_count": 0,
        "has_more": false,
        "url": "/v1/customers/cus_4j9JlwfZ5arO4M/subscriptions",
        "data": [

        ]
      ,
      "discount": null,
      "account_balance": 0,
      "currency": "usd",
      "cards": 
        "object": "list",
        "total_count": 1,
        "has_more": false,
        "url": "/v1/customers/cus_4j9JlwfZ5arO4M/cards",
        "data": [
          
            "id": "card_14Zh0M2eZvKYlo2CAl9gQ262",
            "object": "card",
            "last4": "4242",
            "brand": "Visa",
            "funding": "credit",
            "exp_month": 12,
            "exp_year": 2015,
            "fingerprint": "Xt5EWLLDS7FJjR1c",
            "country": "US",
            "name": "akinci_yasin@hotmail.com",
            "address_line1": null,
            "address_line2": null,
            "address_city": null,
            "address_state": null,
            "address_zip": null,
            "address_country": null,
            "cvc_check": null,
            "address_line1_check": null,
            "address_zip_check": null,
            "customer": "cus_4j9JlwfZ5arO4M"
          
        ]
      ,
      "default_card": "card_14Zh0M2eZvKYlo2CAl9gQ262"
    ,
    #<com.stripe.model.Customer[...] ...>,
    #<com.stripe.model.Customer[...] ...>
  ],
  "has_more": false

希望对您有所帮助。 谢谢

【讨论】:

【参考方案3】:

Using gson:

package gson.sample.one;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class gsonsample
 
    public class card 
        public String id;
        public String object;
        public String last4;
        public String brand;
        public String funding;
        public int exp_month;
        public int exp_year;
        public String fingerprint;
        public String country;
        public String name;
        public String address_line1;
        public String address_line2;
        public String address_city;
        public String address_state;
        public String address_zip;
        public String address_country;
        public String customer;
    

    public class Response 
        public String id;
        public Boolean livemode;
        public int created;
        public String object;
        public String type;
        public card card;
    

    public static void main(String [] args)
    
        String json = " \"id\": \"tok_14WdJ02eZvKYlo2CyaZ49ZP7\", \"livemode\": false, \"created\": 1409272314, \"used\": false, \"object\": \"token\", \"type\": \"card\", \"card\":  \"id\": \"card_14WdJ02eZvKYlo2C5nE5XjtP\", \"object\": \"card\", \"last4\": \"4242\", \"brand\": \"Visa\", \"funding\": \"credit\", \"exp_month\": 8, \"exp_year\": 2015, \"fingerprint\": \"Xt5EWLLDS7FJjR1c\", \"country\": \"US\", \"name\": null, \"address_line1\": null, \"address_line2\": null, \"address_city\": null, \"address_state\": null, \"address_zip\": null, \"address_country\": null, \"customer\": null  "; 
        // 
        Gson gson = new GsonBuilder().create();
        Response response = gson.fromJson(json, Response.class);

        String id = response.card.id;

        System.out.println(id);
    

Stripe 还在其Java API 的模型定义中定义了这些对象,并在其unit tests 中定义了使用示例。

【讨论】:

以上是关于在 Android 中使用令牌的 Stripe Card id的主要内容,如果未能解决你的问题,请参考以下文章

条带支付中没有此类令牌错误

将 csrf 令牌传递给 Stripe

没有这样的令牌“some_random_token”在 rails 中发现 Stripe 错误

从 Firebase Cloud Function 连接到 Stripe 时出错

使用 Stripe on Rails 免费订阅 - 无需卡即可获得令牌

如何在条带支付网关中生成令牌