Stripe API Update Customer Credit Card Details - Java - 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);

Related

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);

Java Stripe API

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);

dynamodb: how to filer all items which do not have a certain attribute?

I have a table of users with a primary hash key of userId. each user may/may not have a string attribute called "environment".
I would like to get all the users which have "environment"="xyz" or which do not have the "environment" attribute.
The following code will filter those users with environment=xyz, but how do I filter those items with no environment at all? Dynamo API will not allow to filter on an empty String.
AmazonDynamoDBClient client = DbClientManager.getDynamoDbClient();
ArrayList<AttributeValue> avList = new ArrayList<AttributeValue>();
avList.add(new AttributeValue().withS("xyz"));
Condition scanFilterCondition = new Condition()
.withComparisonOperator(ComparisonOperator.EQ.toString())
.withAttributeValueList(avList);
Map<String, Condition> conditions = new HashMap<>();
conditions.put("environment", scanFilterCondition);
ScanRequest scanRequest = new ScanRequest()
.withTableName("users")
.withAttributesToGet(
"userId",
"environment");
.withScanFilter(conditions);
ScanResult result = client.scan(scanRequest);
For now I just dropped the scan filter, and I do the filtering client-side. Bit is there any way to do it server side?
Thanks,
Aliza
Hope I'm not too late.
I've found useful function which you could use in the query. I did not check with ScanRequest but with QueryRequest works as charm.
QueryRequest queryRequest = new QueryRequest()
.withTableName("YouTableName")
queryRequest.setFilterExpression(" attribute_not_exists(yourAttributeName) ")
queryRequest.setExpressionAttributeValues(expressionAttributeValues)
queryRequest.setExclusiveStartKey(ifYouHave)
queryRequest.setSelect('ALL_ATTRIBUTES')
queryRequest.setExpressionAttributeNames(youNames)
attribute_not_exists(yourAttributeName) works with ":aws-sdk:1.11.11"
also you could use attribute_exists(yourAttributeName)
You need to use the NULL ComparisonOperator.
Check out this link: http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Condition.html
NOT_NULL : The attribute exists.
NULL : The attribute does not exist.
Does this work for you?
I my case also, attribute_not_exists(attribute) worked. You can refer to this question:- How to check whether an attribute is not present in dynamoDB filter expression
for more details.

Spring Social Facebook - publish/post API details

I have been looking into Spring Social Facebook's publish(objectId, connectionName, data) API, but am not sure of the usage of this API (sadly, due to lack of javadocs!). Can someone point me to a comprehensive sample usage of the API, please?
What I am looking to do is publish a story on a user's wall, similar to the below snapshot:
How should the publish() API be used to do the same? Any help is highly appreciated!
Also, I need my post to have additional actions (apart from Like, Comment).
The link given by you already having a lot documentation for method.
Find one example with flow of publish(objectId, connectionName, data) here
Also see many examples for at github-SpringSource for additional actions including publish(objectId, connectionName, data).
Update:
You might get some help from this method:
public void postToWall(String message, FacebookLink link) {
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
map.set("link", link.getLink());
map.set("name", link.getName());
map.set("caption", link.getCaption());
map.set("description", link.getDescription());
map.set("message", message);
publish(CURRENT_USER, FEED, map);
}
Here's what I could finally figure out:
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
map.set("link", linkUrlString);
map.set("name", "Link Heading");
map.set("caption", "Link Caption");
map.set("description", "Loooooo....ng description here");
map.set("message", "hello world");
// THE BELOW LINES ARE THE CRITICAL PART I WAS LOOKING AT!
map.set("picture", "http://www.imageRepo.com/resources/test.png"); // the image on the left
map.set("actions", "{'name':'myAction', 'link':'http://www.bla.com/action'}"); // custom actions as JSON string
publish(userIdToPostTo, "feed", map);
Like above answer but I use post for my solution. See this:
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
map1.set("link", "https://www.facebook.com/profile.php?id=100006216492034");
map1.set("name", "Project Test Post to Group");
map1.set("caption", "Please ignore this Post");
map1.set("description", "YOLO here is my discription, Please ignore this post");
facebook.post("userId or GroupID", "feed", map);

Categories