Java Stripe API - java

I'm trying to bind a token as a customer card. So, this token has been created by Stripe.js. So, then I send this code on my backend service and I'm trying to set it up into a customer's cards:
Card card = Token.retrieve(id).getCard();
So, once I've got this token as a card, I'm trying it:
Customer.retrieve(this.customer).getSources().create(card.getMetadata())
Nevertheless, I'm getting this compilation error:
The method create(Map) in the type ExternalAccountCollection is not applicable for the arguments (Map)
Any ideas?

You can find the documentation for adding a card to an existing customer object here: https://stripe.com/docs/api/java#create_card.
The correct code would be:
Customer customer = Customer.retrieve(this.customer);
Map<String, Object> params = new HashMap<String, Object>();
params.put("source", id);
Card card = customer.getSources().create(params);

First, you load stripe.js and open stripe payment popup and submit, after stripe return token like(tok_kgkjfdskjfd) the this token pass as source into below api and stripe will automatically save the your card object.
Customer c = Customer.retrieve(this.customer);
Map<String, Object> params = new HashMap<String, Object>();
params.put("source", above_token);
Card card = customer.getSources().create(params);

Related

DocuSign's Java SDK: Add 'Recipient Company Name' to Signer/Recipient

DocuSigns' report section includes tables containing the column name Recipient Company Name
I had a look through all DocuSign models inside the SDK, but I couldn't find any way to fill this column. Is there a way to do so using the SDK?
This can only be filled if the recipient is either a :
Saved contact in your DocuSign account.
Has their own DocuSign account.
If your recipient is just a random email/name you added to a one-time envelope - there's no way to enter the company information.
You can update contacts in your account and add the company name, see this article I wrote about how to do that in 6 languages including Java:
(note the "Organization" field which is the compan)
// You will need to obtain an access token using your chosen authentication flow
Configuration config = new Configuration(new ApiClient(basePath));
config.addDefaultHeader("Authorization", "Bearer " + accessToken);
UsersApi usersApi = new UsersApi(config);
UserProfile userProfile = new UserProfile();
Contact contact = new Contact();
contact.setName("Inbar Gazit");
contact.setEmails(new java.util.ArrayList<String>());
contact.getEmails().add("inbar.gazit#docusign.com");
contact.setOrganization("DocuSign");
ContactPhoneNumber contactPhoneNumber = new ContactPhoneNumber();
contactPhoneNumber.setPhoneNumber("212-555-1234");
contactPhoneNumber.setPhoneType("mobile");
contact.setContactPhoneNumbers(new java.util.ArrayList<ContactPhoneNumber>());
contact.getContactPhoneNumbers().add(contactPhoneNumber);
ContactModRequest contactModificationRequest = new ContactModRequest();
contactModificationRequest.setContactList(new java.util.ArrayList<Contact>());
contactModificationRequest.getContactList().add(contact);
usersApi.postContacts(accountId, contactModificationRequest);

Adding card source works in 19.45.0, fails in 20.0.0 and above

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
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
[1] https://stripe.com/docs/upgrades#2020-08-27
[2] https://stripe.com/docs/expand

Stripe: Add default card and do not override old card

I am working with Stripe Customers, Subscription and Cards.
Now, I have a scenario where, Customer can have multiple cards.
Now, Customer adds a new card. And I have to mark that new added card as default_source.
So what I am doing is
Map<String, Object> params = new HashMap<String, Object>();
params.put("source", token.getId());
Customer customer = Customer.retrieve(user.getStripeId());
customerId = customer.getId();
Customer updatedCustomer = customer.update(params);
This piece of code is updating a customer and marking the current card as default_source which is as expected.
But if the Customer already has card then it overrides the old Card with new one. So the old card is deleted from that Customer.
Now what I want is, If Customer already have the card then I want to mark that card as secondary and then add new card and mark it default_source.
So how can i do that?
You'll want https://stripe.com/docs/api/sources/attach?lang=java and https://stripe.com/docs/api/customers/update?lang=java#update_customer-default_source :
Customer customer = Customer.retrieve(user.getStripeId());
// add a new payment source to the customer
Map<String, Object> params = new HashMap<String, Object>();
params.put("source", token.getId());
Source source = customer.getSources().create(params);
// make it the default
Map<String, Object> updateParams = new HashMap<String, Object>();
updateParams.put("default_source", source.getId());
customer.update(updateParams);

What is the best way to pass multiply parameters as one http query parameter?

I have java web application (servlet) that does user authentication using SalesForce Server OAuth Authentication Flow. This OAuth Authentication provides "state" query parameter to pass any data on callback. I have a bunch of parameters that I want to pass through this "state" query param. What is the best way to do it? In java in particularly?
Or in other words, what is the best way to pass an array or map as a single http query parameter?
You can put all in json or xml format or any other format and then encode in base64 as a one large string. Take care that params can impose some hard limit on some browser/web server.
So, I have done it this way. Thank you guys! Here are some code snippets to illustrate how it works for me:
// forming state query parameter
Map<String, String> stateMap = new HashMap<String, String>();
stateMap.put("1", "111");
stateMap.put("2", "222");
stateMap.put("3", "333");
JSONObject jsonObject = new JSONObject(stateMap);
String stateJSON = jsonObject.toString();
System.out.println("stateJSON: " + stateJSON);
String stateQueryParam = Base64.encodeBase64String(stateJSON.getBytes());
System.out.println("stateQueryParam: " + stateQueryParam);
// getting map from state query param
ObjectMapper objectMapper = new ObjectMapper();
stateMap = objectMapper.readValue(Base64.decodeBase64(stateQueryParam.getBytes()), LinkedHashMap.class);
System.out.println("stateMap: " + stateMap);
Here is output:
stateJSON: {"1":"111","2":"222","3":"333"}
stateQueryParam: eyIxIjoiMTExIiwiMiI6IjIyMiIsIjMiOiIzMzMifQ==
stateMap: {1=111, 2=222, 3=333}

Stripe API Update Customer Credit Card Details - Java

Cannot figure out for the life of me how to do this. I've tested the following which isn't working;
String stripeCustomerID = "123";
Customer cu = Customer.retrieve(stripeCustomerID);
cu.setDefaultSource(token);
Map<String, Object> updateParams = new HashMap<String, Object>();
updateParams.put("default_source", token);
enter code here`cu.update(updateParams);
This is the only place the Stripe API documentation hasn't had the answer.
Has anyone implemented this previously?
Regards,
Michael
default_source is expecting a card id not a token id. Thus you either need to:
1) Add the card to the customer and then update the default_source property
or
2) You can set the source property of the customer to the token. By setting source you will add the new card, delete the old default_source, and then set the new one as the default, all in the same API call.
Answer thanks to Matthew;
Customer cu = Customer.retrieve(stripeCustomerID);
Map<String, Object> updateParams = new HashMap<String, Object>();
updateParams.put("source", token);
cu.update(updateParams);

Categories