英文:
Adding card source works in 19.45.0, fails in 20.0.0 and above
问题
我正在使用Stripe-Java,并尝试将卡片添加到客户端。
我的代码如下:
Customer stripeCustomer = Customer.retrieve("cus_xxxxxxx");
Map<String, Object> cardParam = new HashMap<String, Object>();
cardParam.put("number", "4242424242424242");
cardParam.put("exp_month", "11");
cardParam.put("exp_year", "2022");
cardParam.put("cvc", "123");
//token
Map<String, Object> tokenParam = new HashMap<String, Object>();
tokenParam.put("card", cardParam);
Token token = Token.create(tokenParam);
//user token
Map<String, Object> sourceParam = new HashMap<String, Object>();
sourceParam.put("source", token.getId());
//add to customer
stripeCustomer.getSources().create(sourceParam);
这段代码在Stripe-Java版本19.45.0上成功运行,但在20.0.0或以上版本上不起作用。添加卡片的方法是否发生了变化?
会抛出空指针异常。
谢谢!
英文:
I am playing with Stripe-Java and I'm trying to add a card to a customer.
My code looks like this:
Customer stripeCustomer = Customer.retrieve("cus_xxxxxxx");
Map<String, Object> cardParam = new HashMap<String, Object>();
cardParam.put("number", "4242424242424242");
cardParam.put("exp_month", "11");
cardParam.put("exp_year", "2022");
cardParam.put("cvc", "123");
//token
Map<String, Object> tokenParam = new HashMap<String, Object>();
tokenParam.put("card", cardParam);
Token token = Token.create(tokenParam);
//user token
Map<String, Object> sourceParam = new HashMap<String, Object>();
sourceParam.put("source", token.getId());
//add to customer
stripeCustomer.getSources().create(sourceParam);
This works successfully on Stripe-Java version 19.45.0 but not on 20.0.0 or any versions above. Has the method to add a card changed?
A nullpointer exception is thrown
Thanks
答案1
得分: 4
stripeCustomer.getSources()
在库的 v20.0.0 及以上版本中将为 null,因为它固定在 API 版本 2020-08-27,其中默认删除了 customer.sources
。[0] [1]
> Customers 上的 sources 属性不再默认包含在内。
> 你可以扩展列表,但出于性能原因,我们建议仅在必要时这样做。
在检索客户以填充 customer.getSources()
时,您需要显式展开 [2] "sources"
。
CustomerRetrieveParams params = CustomerRetrieveParams.builder()
.addExpand("sources").build();
Customer stripeCustomer = Customer.retrieve("cus_xxxxxxx", params, null);
另外,您的代码使用了传统的 Token API,并且从服务器传递原始的卡片详细信息,这会使您处于 PCI 范围内,您应该查阅推荐的集成路径:https://stripe.com/docs/payments/accept-a-payment
[0] https://github.com/stripe/stripe-java/blob/master/CHANGELOG.md#2000---2020-08-31
[1] https://stripe.com/docs/upgrades#2020-08-27
[2] https://stripe.com/docs/expand
英文:
This : stripeCustomer.getSources()
will be null in v20.0.0 and above of the library because it pins to API version 2020-08-27 where customer.sources
was removed by default. [0] [1]
> The sources property on Customers is no longer included by default.
> You can expand the list but for performance reasons we recommended
> against doing so unless needed.
You would need to explicitly expand [2] "sources"
when retrieving the Customer in order to populate customer.getSources()
CustomerRetrieveParams params = CustomerRetrieveParams.builder()
.addExpand("sources").build();
Customer stripeCustomer = Customer.retrieve("cus_xxxxxxx", params, null);
Also, your code uses the legacy Token API, and is passing raw card details from your server that puts you in PCI scope, you should look into the recommended integration paths : https://stripe.com/docs/payments/accept-a-payment
[0] https://github.com/stripe/stripe-java/blob/master/CHANGELOG.md#2000---2020-08-31
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论