添加卡片来源在19.45.0版本中有效,在20.0.0及以上版本中失败。

huangapple go评论84阅读模式
英文:

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(&quot;cus_xxxxxxx&quot;);
	
	Map&lt;String, Object&gt; cardParam = new HashMap&lt;String, Object&gt;();
		cardParam.put(&quot;number&quot;, &quot;4242424242424242&quot;);
		cardParam.put(&quot;exp_month&quot;, &quot;11&quot;);
		cardParam.put(&quot;exp_year&quot;, &quot;2022&quot;);
		cardParam.put(&quot;cvc&quot;, &quot;123&quot;);
	
	//token
	Map&lt;String, Object&gt; tokenParam = new HashMap&lt;String, Object&gt;();
	tokenParam.put(&quot;card&quot;, cardParam);
	
	Token token = Token.create(tokenParam);
	
	//user token
	Map&lt;String, Object&gt; sourceParam = new HashMap&lt;String, Object&gt;();
	sourceParam.put(&quot;source&quot;, 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] &quot;sources&quot;

CustomerRetrieveParams params = CustomerRetrieveParams.builder()
            .addExpand(&quot;sources&quot;).build();
Customer stripeCustomer = Customer.retrieve(&quot;cus_xxxxxxx&quot;, 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] &quot;sources&quot; when retrieving the Customer in order to populate customer.getSources()

CustomerRetrieveParams params = CustomerRetrieveParams.builder()
        .addExpand(&quot;sources&quot;).build();
Customer stripeCustomer = Customer.retrieve(&quot;cus_xxxxxxx&quot;, 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

[1] https://stripe.com/docs/upgrades#2020-08-27

[2] https://stripe.com/docs/expand

huangapple
  • 本文由 发表于 2020年9月21日 19:27:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63991339.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定