#andyl-java-paymentsource

1 messages · Page 1 of 1 (latest)

fiery geyserBOT
golden yacht
#

andyl-java-paymentsource

#

👋 @dreamy jasper there are a lot of different threads to pull at but first: something must have changed. Either you change your stripe-java SDK version or something else

#

We cannot use payment intents here, because the tokenized source comes from Reserve with Google.
I'm not sure I get what that means. You mentioned a tok_123 which is a card Token. That will allow you to save/attach it to a Customer and get a Card object back card_123 and those work totally fine with PaymentIntents and also work as a PaymentMethod if you use https://stripe.com/docs/api/payment_methods/list for example

dreamy jasper
#

mmm no. the sdk has been the same for two years, and so our code. this problem only started recently

#

Anyway can you provide instructions on how to store a tok_.... tokenized card into a customer's profile

golden yacht
#

There are many ways to do this, so unfortunately I need you to provide a lot more context

dreamy jasper
#

ok let's narrow it down. I have a Custoer object. I have a tok_.... token. How do I attach it to the Customer and get a Card object back

golden yacht
dreamy jasper
#

Ok. That is exactly what our code is doing. Looking at the Java sample code in that link:

#

the last line is where we get the problem. customer.getPaymentSources() returns null

#

This is a customer that was just created by calling com.stripe.model.Customer.create(customerCreateParams, requestOptions);

golden yacht
#

I deleted your message you just leaked your test API key (you should roll it)

#

You were also retrieving a Customer but passing card_123 as an id that doesn't make sense

#

and ugh, we just changed our API ref and I think the code example is incorrect

#

So you have to explicitly expand that property when you retrieve the Customer first

dreamy jasper
#

ok but this is a newly created customer. not retrieved

golden yacht
#

not really, at least that's not what the code you shared did

#

If you just want to create a brand new Customer and attach a card at the same time, nothing has changed there either, you pass source: 'tok_123' on Customer creation and it works

fiery geyserBOT
dreamy jasper
#

the code I shared is the example I copied from the documentation you pointed to.

#

Just to show you we're trying to store the source according to your docs.

golden yacht
#

Sure and that code is to create a Card on an existing Customer object but now you said otherwise

dreamy jasper
#

our real code creates a customer

golden yacht
#

okay, can you share your exact/real code

dreamy jasper
#

CustomerCreateParams.Builder builder = CustomerCreateParams.builder();
builder.setName(...);
CustomerCreateParams customerCreateParams = builder.build();
stripeCustomer = com.stripe.model.Customer.create(customerCreateParams, requestOptions);
PaymentSourceCollection sources = stripeCustomer.getSources();
// sources IS NULL

golden yacht
#

Is this your real code though? Nowhere are you setting the Token id on creation but you said you would

#

@karmic ocean is taking over for me but basically the problem is what I said earlier: sources is not returned by default (and hasn't been for 3.5 years) so if you want it in the response you have to explicitly include it first using our Expand feature https://stripe.com/docs/expand

#

So in your params builder you have to add .addExpand("sources") and then it will work

dreamy jasper
#

how do I expand sources if this is NOT a retrieved customer?

#

the sample code in your site says that to add a source you call PaymentSource paymentSource = customer.getPaymentSources().create(params);

karmic ocean
#

Hi @dreamy jasper I'm taking over this thread, give a sec to go through the previous chat

dreamy jasper
#

params is where we add the token. But it does not matter. because the sources are already null.

#

we never get to call that create() with the tok_ parameters

golden yacht
#

Not really, that's why I asked for your exact code

dreamy jasper
#

ok. but I don't see the point since the error is before we create the source token parameters. But here you go anyway:

#

CustomerCreateParams.Builder builder = CustomerCreateParams.builder();
builder.setName(...);
CustomerCreateParams customerCreateParams = builder.build();
stripeCustomer = com.stripe.model.Customer.create(customerCreateParams, requestOptions);
PaymentSourceCollection sources = stripeCustomer.getSources();
// sources IS NULL
Map<String, Object> params;
params = new HashMap<>();
String stripeCardToken = creditCardDetails.getCardToken();
params.put("source", stripeCardToken);
Card card = (Card) sources.create(params, requestOptions);

karmic ocean
#

What error did you get?

golden yacht
#

I got it

dreamy jasper
#

so we get a nullpointerexception at the last line

golden yacht
#
  CustomerCreateParams.builder()
    .setName("John Doe")
    .setSource("tok_123456")
    .addExpand("sources") // Forces sources to be returned
    .build();

Customer stripeCustomer = Customer.create(params);
// This will have the new Card object that is passed on Customer creation
PaymentSourceCollection sources = stripeCustomer.getSources();```
#

this is the real code you should use. That does the Customer creation and card attachment in one call

#

But the important bit is addExpand("sources"). If you want to separate the Customer creation from the Card attachment, which is fine, then you have to pass this on the Customer creation so that our API responds with the sources property (a sub-list) so that your next part of the code, that relies on getSources() to get a real PaymentSourceCollection will work (instead of getting null)

#

So taking your real code and fixing it you want CustomerCreateParams.Builder builder = CustomerCreateParams.builder(); builder.setName(...); builder.addExpand("sources"); CustomerCreateParams customerCreateParams = builder.build(); stripeCustomer = com.stripe.model.Customer.create(customerCreateParams, requestOptions); PaymentSourceCollection sources = stripeCustomer.getSources(); // sources now works Map<String, Object> params; params = new HashMap<>(); String stripeCardToken = creditCardDetails.getCardToken(); params.put("source", stripeCardToken); Card card = (Card) sources.create(params, requestOptions);

dreamy jasper
#

ok.
"builder.addExpand("sources");"
was the missing bit. but it is also missing from yoru sample code...

golden yacht
#

let me know if anything is unclear (I have to run soon but not many people on my team know about those legacy things)

#

yep agreed

#

we just rebuilt our API reference and changed the way code examples are generated and sadly missed that one

#

it's a really weird edge-case only in java because of legacy reasons and since this overall integration path is more than discouraged at this point we haven't really tried to change it