Stripe Card id using token in Android - java

I am integrating Stripe Payment module in my application. In that I use this Stripe Library.
Now using this code I generating token. Using this token I need card id before payment.
How to get this card id'. ?
Here I show you response of Stripe.
When I enter Card Information about the card Like this :
Stripe_Token::create(array(
"card" => array(
"number" => "4242424242424242",
"exp_month" => 8,
"exp_year" => 2015,
"cvc" => "314"
)
));
After that Stripe give me this response :
{
"id": "tok_14WdJ02eZvKYlo2CyaZ49ZP7",
"livemode": false,
"created": 1409272314,
"used": false,
"object": "token",
"type": "card",
"card": {
"id": "card_14WdJ02eZvKYlo2C5nE5XjtP",
"object": "card",
"last4": "4242",
"brand": "Visa",
"funding": "credit",
"exp_month": 8,
"exp_year": 2015,
"fingerprint": "Xt5EWLLDS7FJjR1c",
"country": "US",
"name": null,
"address_line1": null,
"address_line2": null,
"address_city": null,
"address_state": null,
"address_zip": null,
"address_country": null,
"customer": null
}
}
Before payment and after token created
I need this card id :
"card": {
"id": "card_14WdJ02eZvKYlo2C5nE5XjtP",
}
Hope you get my question.

As you can see in the response from stripe, you can simple get your card id and all other fields from the token object you get in response as follows:
String card_id = token.getCard().getId(); // to get card id
String id = token.getId(); // to get this "id": "tok_14WdJ02eZvKYlo2CyaZ49ZP7"
boolean live_mode = token.getLivemode(); // to get livemode
String last_four = token.getCard().getLast4(); // to get last 4
where token is the response object.

After Generating token
Use this...
Customer.all(new HashMap<String, Object>());
Using this finally i got Cardid which i want.
Here is the total response of Customer.
{
"data": [
com.stripe.model.Customer JSON: {
"object": "customer",
"created": 1410001523,
"id": "cus_4j9JlwfZ5arO4M",
"livemode": false,
"description": null,
"email": "customer#example.com",
"delinquent": false,
"metadata": {
},
"subscriptions": {
"object": "list",
"total_count": 0,
"has_more": false,
"url": "/v1/customers/cus_4j9JlwfZ5arO4M/subscriptions",
"data": [
]
},
"discount": null,
"account_balance": 0,
"currency": "usd",
"cards": {
"object": "list",
"total_count": 1,
"has_more": false,
"url": "/v1/customers/cus_4j9JlwfZ5arO4M/cards",
"data": [
{
"id": "card_14Zh0M2eZvKYlo2CAl9gQ262",
"object": "card",
"last4": "4242",
"brand": "Visa",
"funding": "credit",
"exp_month": 12,
"exp_year": 2015,
"fingerprint": "Xt5EWLLDS7FJjR1c",
"country": "US",
"name": "akinci_yasin#hotmail.com",
"address_line1": null,
"address_line2": null,
"address_city": null,
"address_state": null,
"address_zip": null,
"address_country": null,
"cvc_check": null,
"address_line1_check": null,
"address_zip_check": null,
"customer": "cus_4j9JlwfZ5arO4M"
}
]
},
"default_card": "card_14Zh0M2eZvKYlo2CAl9gQ262"
},
#<com.stripe.model.Customer[...] ...>,
#<com.stripe.model.Customer[...] ...>
],
"has_more": false
}
Hope it may help for You.
Thanks

Using gson:
package gson.sample.one;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class gsonsample
{
public class card {
public String id;
public String object;
public String last4;
public String brand;
public String funding;
public int exp_month;
public int exp_year;
public String fingerprint;
public String country;
public String name;
public String address_line1;
public String address_line2;
public String address_city;
public String address_state;
public String address_zip;
public String address_country;
public String customer;
}
public class Response {
public String id;
public Boolean livemode;
public int created;
public String object;
public String type;
public card card;
}
public static void main(String [] args)
{
String json = "{ \"id\": \"tok_14WdJ02eZvKYlo2CyaZ49ZP7\", \"livemode\": false, \"created\": 1409272314, \"used\": false, \"object\": \"token\", \"type\": \"card\", \"card\": { \"id\": \"card_14WdJ02eZvKYlo2C5nE5XjtP\", \"object\": \"card\", \"last4\": \"4242\", \"brand\": \"Visa\", \"funding\": \"credit\", \"exp_month\": 8, \"exp_year\": 2015, \"fingerprint\": \"Xt5EWLLDS7FJjR1c\", \"country\": \"US\", \"name\": null, \"address_line1\": null, \"address_line2\": null, \"address_city\": null, \"address_state\": null, \"address_zip\": null, \"address_country\": null, \"customer\": null } }";
//
Gson gson = new GsonBuilder().create();
Response response = gson.fromJson(json, Response.class);
String id = response.card.id;
System.out.println(id);
}
}
Stripe also has these objects defined in the model definitions of their Java API and usage examples in their unit tests.

Related

How to send array request in body parameter retrofit android

This is my Request which i need to send in #Post request with Data as a key. Please help me, I am stuck since 2 days.
[
{
"barcodeList": "abc",
"fieldboyId": "17",
"lattitude": "37.4219513",
"longitude": "-122.0841169",
"quantity": "2",
"refrenceId": "1",
"sampleId": null,
"sampleName": null,
"sampleType": null,
"type": "Barcode"
},
{
"barcodeList": "acd",
"fieldboyId": "17",
"lattitude": "37.4219513",
"longitude": "-122.0841169",
"quantity": "1",
"refrenceId": "1",
"sampleId": null,
"sampleName": null,
"sampleType": null,
"type": "Barcode"
}
]
You could make these classes:
public class RequestBody {
List<Data> data;
public class Data {
String barcodeList;
String fieldboyId;
String lattitude;
String longitude;
String quantity;
String refrenceId;
String sampleId;
String sampleName;
String sampleType;
String type;
}
}
and in the interface something like
#POST("/api/path")
Call<Something> myPostRequest(#Body requestBody: RequestBody);

How to parse a Json with a field containg several kind of objects

How to parse a Json with a field "data" which can contain several types of objets, or an object and a simple message (outside the object structure).
An Android app request information to its backend. In most cases, the response has the same structure and if the "data" field only contains one kind of object i can complete the process.
But if the "data" field contains several kinds of objects or one object with a external message, i can´t parse it.
Normal Response structure (I can parse these cases):
{
"success": true,
"code": "OK",
"error": null,
"data": {
"_type": "auth",
"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImlzcyI6Im9mZnRpbWUiLCJzdWIiOiJkZXZlbG9wZXIifQ==.eyJyb2xlIjpbImFwcCJdLCgfgdfgJhcgdfgdfAiOiJPRkZUSU1FIEFuZHJvaWQiLCJvd25lcdsgsghhjghkukI6Ik9zY2FyIn0=.7PiMEmX8M3ABs7P8KymuXvBpTL1gdgdfLRajFbwxP5IBARM=",
"refresh": null,
"logout": null
}
}
Response with an object "user" and a message (i have problems parsing it):
{
"success": true,
"code": "USER_ALREADY_EXISTS",
"error": null,
"data": {
"user": {
"_type": "user",
"id": 32,
"name": "Pascual22",
"email": "pascual22#juanedc.com",
"picture": null
},
"message": "This email is already registered, you will receive an email with the reset password instructions"
}
}
Response with several kinds of objects (i have problems parsing it):
{
"success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aU9pSjFjMlZ5SWl3aVpYaHdJam94TlRZd05UazJNelV3ZlE9PS5leUp5YjJ4bElqcGJJbUZ3Y0NJc0luVnpaWElpWFN3aWIzZHVaWElpT2pJMkxDSmtaWFpwWTJVaU9pSTVJbjA9Lkp4YmlkcUNuWk5DdDhleVJVYkxFOEhFSktlV2d3RmVqelwvOXJNSlBvS1MwPSJ9.syuKoi8XiHp/me/AJAydyXfRbXa63KVWqPSKS7ySGQc="
},
"user": {
"_type": "user",
"id": 26,
"name": "Sin Foto 2",
"email": "sf2#yopmail.com",
"picture": null
},
"device": {
"_type": "device",
"id": "9",
"category": "mobile",
"manufacturer": "Xiaomi",
"model": "Mi A1",
"os": "android",
"os_version": "9",
"sim_support": true,
"name": null,
"added": "2019-06-10 10:59:10"
}
}
}
My class response:
#JsonIgnoreProperties(ignoreUnknown = true)
public class Response {
#JsonProperty("success")
private String success;
#JsonProperty("code")
private String code;
#JsonProperty("error")
private String error;
#JsonProperty("data")
private Object data;
public String getSuccess() {return success;}
public void setSuccess(String success) {this.success = success;}
public String getCode() {return code;}
public void setCode(String code) {this.code = code;}
public String getError() {return error;}
public void setError(String error) {this.error = error;}
public Object getData() {return data;}
public void setData(Object data) {this.data = data;}
}
Initial parsing process (from Json to Response Object, using spring for android framework, ):
responseApi = restTemplate.exchange(uri, HttpMethod.POST, requestEntity, Response.class);
Parsing Process of data field to Objects (Example with a user object in "data"):
try {
User user = mapper.convertValue(responseApi.getBody().getData(), User.class);
}
catch (Exception e){
e.printStackTrace();
}
I tried to make a Data class with a generic object and a message atributes, but, it din´t work. I need to parse the response in all cases, how can i achieve it? Some suggestions about the class structure in order to make a correct automatic parsing process?

How to hide json object in recyclerview based on condition?

I have one json array in array list I need show one object as count and one as text view, while going to filter the object I'm not able to find count view and display view, I search for hash map but it does not work, my question i can i add two array list in one recyclerview adapter one for view and one for count in same view.
Edit: after researching about that I come to know I need to hide object if it is a match with id but I could not found to display and hide content base on condition in recyclerview any solution?
#Override
public void onBindViewHolder(#NonNull DiscussingAdapter.DiscussionView holder, int position) {
HashMap<String, List<Result>> hashMap = new HashMap<String, List<Result>>();
Result result = discussionsList.get(position);
//discussionsList.get(position);
List<Result> filterList = discussionsList.stream()
.filter(p -> p.getParentCommentID()==null).collect(Collectors.toList());
Log.d("filterList",filterList.toString());
for (int counter = 0; counter < filterList.size(); counter++) {
holder.tvHeader.setText(filterList.get(counter).getTitle());
holder.tvDetail.setText(Utils.html2text(filterList.get(counter).getComment()));
holder.tvViewReply.setText("Reply");
holder.tvUser.setText("By " + filterList.get(counter).getAuthor().getTitle());
String DateofReceipt = filterList.get(counter).getCreated();
String date = DateofReceipt.split("T", 0)[0];
String date_before = date;
String date_after = Utils.date(date_before);
holder.tvDate.setText(date_after);
}
Map<String, Integer> commentsCountMap = new HashMap<>();
for(Result res : discussionsList) {
String parentCommentId = res.getParentCommentID();
// If the Result has a parent comment
if(parentCommentId != null) {
// get the count for this parent comment (default to 0)
int nbCommentsForParent = commentsCountMap.getOrDefault(parentCommentId, 0);
// increment the count
nbCommentsForParent++;
// Update the Map with the new count
commentsCountMap.put(parentCommentId, nbCommentsForParent);
}
}
for(Map.Entry<String,Integer> cCount : commentsCountMap.entrySet()){
holder.tvViewReply.setText(String .valueOf(cCount.getValue())+" Reply");
}}
Json object generated is as below
{
"d": {
"results": [
{
"__metadata": {
"id": "e7433825-6771-4f5e-96c7-c4d2674d7764",
"uri": "",
"etag": "\"2\"",
"type": "SP.Data.InquiryDiscussionsListListItem"
},
"Author": {
"__metadata": {
"id": "f064775c-6161-4bdb-9f4d-8bc6a898d218",
"type": "SP.Data.UserInfoItem"
},
"Title": "Submitter1"
},
"Id": 1501,
"ID": 1501,
"Title": null,
"Created": "2019-06-06T04:15:17Z",
"ParentCommentID": "1439",
"Comment": "<div class=\"ExternalClass8C333A77B6564A53BED74CA1BA2D2A10\">
reply add for 009</div>",
"CommentType": null,
"CommentDocumentName": null,
"AppID": "1083",
"Role": "Customer"
},
{
"__metadata": {
"id": "e92f708f-93dc-4587-8c4f-5518ed24f360",
"uri": "",
"etag": "\"2\"",
"type": "SP.Data.InquiryDiscussionsListListItem"
},
"Author": {
"__metadata": {
"id": "209d2d9a-bb07-4064-aaa0-231ad881a80f",
"type": "SP.Data.UserInfoItem"
},
"Title": "Submitter1"
},
"Id": 1500,
"ID": 1500,
"Title": null,
"Created": "2019-06-06T04:14:55Z",
"ParentCommentID": "1439",
"Comment": "<div class=\"ExternalClass44B1A0BB4D314C57BEE20141BFF10491\">comment add for 009</div>",
"CommentType": null,
"CommentDocumentName": null,
"AppID": "1083",
"Role": "Customer"
},
{
"__metadata": {
"id": "ec112002-3132-4bc1-8f85-03fbd9fda11d",
"uri": "",
"etag": "\"2\"",
"type": "SP.Data.InquiryDiscussionsListListItem"
},
"Author": {
"__metadata": {
"id": "6e8ecb1d-4deb-4168-a8b4-a725abf8002a",
"type": "SP.Data.UserInfoItem"
},
"Title": "Sarada Devi Potti"
},
"Id": 1439,
"ID": 1439,
"Title": "Canceled",
"Created": "2019-06-03T09:32:34Z",
"ParentCommentID": null,
"Comment": "<div class=\"ExternalClass5E1BFEDC348C43719AD940E644E0E0B6\">sdaeadfasdf</div>",
"CommentType": "Public",
"CommentDocumentName": null,
"AppID": "1083",
"Role": "Budget Analyst"
}
]
}
}
In Above Json response if object is Comment I want to display content but if it is a reply I need to display count base on id match any solution?
You can achieve this in a simple way first pass both the list to your adapter
then return the total size
#Override
public int getItemCount() {
return mList.size() + mList1.size();
}
Now for onBindViewHolder for example you want to display mList first then
#Override
public void onBindViewHolder(#NonNull DiscussingAdapter.DiscussionView holder, int position) {
if(position < mList.size()){
// Load the data from mList
holder.tvName.setText(mList.get(position).getName())
}else{
//For getting Data from secondList
holder.tvName.setText(mList1.get(position-mList.getSize()).getName())
// Now you will get data from second List
}
}
I am not sure I understand you fully. However, In my opinion, you can create a model class and keep two variables there.
public class Demo {
private int count;
private int value;
public Demo(int count, int value) {
this.count = count;
this.value = value;
}
}
Then, you can create an ArrayList of that model class and pass it to RecyclerView adapter.
ArrayList<Demo> demos = new ArrayList<>();
Demo firstObject = new Demo(1, 10);
demos.add(firstObject);
Demo secondObject = new Demo(2, 11);
demos.add(secondObject);
Demo thirdObject = new Demo(3, 12);
demos.add(thirdObject);
Feel free to ask if I'm not clear or anything else.

how to make count similar id object in list and display in recyclerview?

I have a JSON array, in that I need to make match count using ParentCommentID and parse data into recyclerview using text view while counting i get number count only one position and same for other item also, any help.
{
"d": {
"results": [
{
"__metadata": {
"id": "e7433825-6771-4f5e-96c7-c4d2674d7764",
"uri": "",
"etag": "\"2\"",
"type": "SP.Data.InquiryDiscussionsListListItem"
},
"Author": {
"__metadata": {
"id": "f064775c-6161-4bdb-9f4d-8bc6a898d218",
"type": "SP.Data.UserInfoItem"
},
"Title": "Submitter1"
},
"Id": 1501,
"ID": 1501,
"Title": null,
"Created": "2019-06-06T04:15:17Z",
"ParentCommentID": "1439",
"Comment": "<div class=\"ExternalClass8C333A77B6564A53BED74CA1BA2D2A10\">
reply add for 009</div>",
"CommentType": null,
"CommentDocumentName": null,
"AppID": "1083",
"Role": "Customer"
},
{
"__metadata": {
"id": "e92f708f-93dc-4587-8c4f-5518ed24f360",
"uri": "",
"etag": "\"2\"",
"type": "SP.Data.InquiryDiscussionsListListItem"
},
"Author": {
"__metadata": {
"id": "209d2d9a-bb07-4064-aaa0-231ad881a80f",
"type": "SP.Data.UserInfoItem"
},
"Title": "Submitter1"
},
"Id": 1500,
"ID": 1500,
"Title": null,
"Created": "2019-06-06T04:14:55Z",
"ParentCommentID": "1439",
"Comment": "<div class=\"ExternalClass44B1A0BB4D314C57BEE20141BFF10491\">comment add for 009</div>",
"CommentType": null,
"CommentDocumentName": null,
"AppID": "1083",
"Role": "Customer"
},
{
"__metadata": {
"id": "ec112002-3132-4bc1-8f85-03fbd9fda11d",
"uri": "",
"etag": "\"2\"",
"type": "SP.Data.InquiryDiscussionsListListItem"
},
"Author": {
"__metadata": {
"id": "6e8ecb1d-4deb-4168-a8b4-a725abf8002a",
"type": "SP.Data.UserInfoItem"
},
"Title": "Sarada Devi Potti"
},
"Id": 1439,
"ID": 1439,
"Title": "Canceled",
"Created": "2019-06-03T09:32:34Z",
"ParentCommentID": null,
"Comment": "<div class=\"ExternalClass5E1BFEDC348C43719AD940E644E0E0B6\">sdaeadfasdf</div>",
"CommentType": "Public",
"CommentDocumentName": null,
"AppID": "1083",
"Role": "Budget Analyst"
}
]
}
}
My code is onbind method:
Map<String, Integer> commentsCountMap = new HashMap<>();
for(Result res : discussionsList) {
String parentCommentId = res.getParentCommentID();
// If the MyDocsResult has a parent comment
if(parentCommentId != null) {
// get the count for this parent comment (default to 0)
int nbCommentsForParent = commentsCountMap.getOrDefault(parentCommentId, 0);
// increment the count
nbCommentsForParent++;
// Update the Map with the new count
commentsCountMap.put(parentCommentId, nbCommentsForParent);
System.out.println(commentsCountMap);
Log.d("Count:",commentsCountMap.toString());
}
}
for example id 1439, having two ParentCommentID, so in that case for id 1439 there should be a count of two comments, I tried to use for in my
recyclerview adapter but its dose not work.
I think you need to run through the list of Results and when when you find a Result that has a parent comment, then you go in the map, get the parent comment, increment its count by one and stick it back in the Map:
Assuming your Result class is something like this:
class Result {
private String id;
private String parentCommentID;
public Result(String id, String parentCommentID) {
this.id = id;
this.parentCommentID = parentCommentID;
}
// GETTERS/SETTERS
}
And you have a discussion list with 3 Results
discussionsList = Arrays.asList(
new Result("1439", null),
new Result("1500", "1439"),
new Result("1801", "1439")
);
Like your case, 1439 has no parent and 1500 and 1501 both have 1439 as parent
Then you can do this kind of thing:
Map<String, Integer> commentsCountMap = new HashMap<>();
// Loop through the discussion Map
for(Result res : discussionsList) {
String parentCommentId = res.getParentCommentID();
// If the Result has a parent comment
if(parentCommentId != null) {
// get the count for this parent comment (default to 0)
int nbCommentsForParent = commentsCountMap.getOrDefault(parentCommentId, 0);
// increment the count
nbCommentsForParent++;
// Update the Map with the new count
commentsCountMap.put(parentCommentId, nbCommentsForParent);
}
}
System.out.println(commentsCountMap);
This outputs
{1439=2}

How to converted dataTable from request Spring Boot

i'm learning programming and want use DataTables and i have a problem. I don't know how to converted records...
I have code in spring boot
#RequestMapping(path="/seriess", method=RequestMethod.GET)
public Page<SeriesDao> showSeries(#RequestParam(defaultValue="0") int page)
{
Page<SeriesDao> sss = seriesRepository.findAll(new PageRequest(page, 20));
return sss;
}
#RequestMapping("/showSeries")
public ModelAndView model(){
ModelAndView model = new ModelAndView("showSeries");
return model;
}
Next I have json when i go to localhost:8080/seriess, i didn't copy all result (20)
{"content":[{"id":41,"name":"Average Weekly Earnings of All Employees: Total Private in Corpus Christi, TX (MSA)","file":"SMU48185800500000011.csv","cassid":"1d2e556b-031e-4c6f-aec4-981c4e907324","categoryid":3,"datefrom":"2006-12-31","dateto":"2016-09-30","frequency":5,"markers":null,"unit":"$ per Week","feed":"Macroeconomic_And_Major_Markets","createdate":1476567529000,"changedate":1483919401000}........and next 19 records ]
"last":false,"totalPages":25,"totalElements":488,"size":20,"number":0,"sort":null,"first":true,"numberOfElements":20}
This is my java script code. I have to correct convert this data from url, but i don't know how...
$(document).ready( function () {
$('#dataTable').DataTable({
"processing": true,
"serverSide": true,
"ajax": {
"url": "seriess",
"dataSrc" : ""
}
},
"columns": [
{ "data": "id"},
{ "data": "name" },
{ "data": "file" },
{ "data": "cassid" },
{ "data": "categoryid" },
{ "data": "datefrom" },
{ "data": "dateto" },
{ "data": "frequency" },
{ "data": "markers" },
{ "data": "unit" },
{ "data": "feed" },
{ "data": "createdate" },
{ "data": "changedate" }
]
})});
Change
"ajax": {
"url": "seriess",
"dataSrc" : ""
}
}
to
"ajax": {
"url": "/seriess",
"dataSrc" : "content"
}
}

Categories