This question already has answers here:
php: loop through json array
(4 answers)
Closed 7 years ago.
I'm new to using PayPal API Express Checkout, i have a question.
How to convert/get the response value from paypal. here is my response string;
{ "id": "PAY-XXXXXXXXXX272783PKXTZ4NI", "create_time": "2015-09-03T01:11:17Z", "update_time": "2015-09-03T01:12:21Z", "state": "approved", "intent": "sale", "payer": { "payment_method": "paypal", "payer_info": { "email": "XXXXXXXXX-buyer#gmail.com", "first_name": "test", "last_name": "buyer", "payer_id": "PJ9LKYDVZXXXX", "shipping_address": { "line1": "1 Main St", "city": "San Jose", "state": "CA", "postal_code": "95131", "country_code": "US", "recipient_name": "test buyer" } } }, "transactions": [ { "amount": { "total": "700.00", "currency": "PHP", "details": { "subtotal": "590.00", "shipping": "110.00" } }, "description"...
I want to get it like this:
$ID = $response["id"];
$shipping_add_array = $response["shipping_address"];
echo $shipping_add_array["state"];
Thank you for your help. i have not been able to figure this out for a week
You are close - add json_decode():
$response = json_decode($paypal_response, true);
$ID = $response["id"];
To get shipping address, you will need to dig deeper:
$shipping_add_array = $response["payer"]["payer_info"]["shipping_address"];
echo $shipping_add_array["state"];
You could get JSON formatter to help you figure out the nested levels of the object (easier to visualize nested json data) - example plugin I found: (no affiliation with the developer)
chrome.google.com/webstore/detail/json-formatter/bcjindcccaagfpapjjmafapmmgkkhgoa?hl=en
Related
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 10 months ago.
I've a GET request in PHP that receives data in JSON as below.
{
"data": {
"id": "1cc58ad2-ccfd-4ede-a6a6-35809c81cb8a",
"type": "payment",
"attributes": {
"status": "Sent",
"created_at": "2022-05-02T08:57:50.171+03:00",
"amount": {
"currency": "KES",
"value": "500.0"
},
"transfer_batches": [{
"status": "Sent",
"amount": "500.0",
"disbursements": [{
"amount": "500.0",
"status": "Transferred",
"origination_time": "2022-05-02T08:57:50.171+03:00",
"transaction_reference": "1651471070",
"destination_type": "till",
"destination_reference": "3eccf0c1-ab6b-41a4-b51a-4e8f588105f1"
}]
}],
"metadata": {
"something": "TST-01",
"something_else": "Something else"
},
"_links": {
"callback_url": "http://portal.zarafu.com/payments",
"self": "https://sandbox.kopokopo.com/api/v1/payments/1cc58ad2-ccfd-4ede-a6a6-35809c81cb8a"
}
}
}
}
How can I filter the transaction_reference and status.
I have tried..
$jsonData = json_decode($response);
echo '<pre>';
echo $jsonData->status;
echo '</pre>';
You can't get the status like that. You have to use the format of your json. Exemple for the status :
echo $jsonData->data->attributes->status;
For transaction reference, it's :
echo $jsonData->data->attributes->transfer_batches->0->disbursements->0->transaction_reference;
I put '0' because it's arrays
I try to implement a paypal express checkout flow to a website.
The user must be able to adjust the order after they have logged in with paypal and choosed their payment option.
To do so, I create the order with paypal with the intent "AUTHORIZE" and user action "CONTINUE". It is created, the user is sent back to our website and I can fetch order and payer information with the (order) id created.
But then, when the payer is done checking and adjusting some final parameters and clicks the "buy now" button, I want to send the update order call as referenced here:
https://developer.paypal.com/api/orders/v2/#orders_patch
I create the payload in exactly the same way as for order creation, but this time with intent "CAPTURE" and user_action="PAY_NOW". I PATCH it to the correct path, but I keep getting the failure message "MALFORMED_REQUEST_JSON" .
Hoewever, the JSON itself is a valid JSON, no errors thrown in creation. Here is a sample:
{
"intent": "CAPTURE",
"application_context": {
"landing_page": "NO_PREFERENCE",
"shipping_preference": "SET_PROVIDED_ADDRESS",
"user_action": "PAY_NOW"
},
"purchase_units": [{
"reference_id": "2289256",
"description": "Your order at site",
"custom_id": "order id 2289256",
"soft_descriptor": "site name",
"invoice_id": "2289256",
"amount": {
"currency_code": "EUR",
"value": 59.98,
"breakdown": {
"item_total": {
"currency_code": "EUR",
"value": 50.41
},
"shipping": {
"currency_code": "EUR",
"value": 0
},
"discount": {
"currency_code": "EUR",
"value": 0
},
"tax_total": {
"currency_code": "EUR",
"value": 9.57
}
}
},
"items": [{
"name": "Product 1",
"description": "Product 1 Description",
"sku": "1019879",
"unit_amount": {
"currency_code": "EUR",
"value": 16.8
},
"tax": {
"currency_code": "EUR",
"value": 3.19
},
"quantity": "1",
"category": "PHYSICAL_GOODS"
}, {
"name": "Product 2",
"description": "Product 2 Description",
"sku": "1024593",
"unit_amount": {
"currency_code": "EUR",
"value": 33.61
},
"tax": {
"currency_code": "EUR",
"value": 6.38
},
"quantity": "1",
"category": "PHYSICAL_GOODS"
}],
"shipping": {
"name": {
"full_name": "John Doe"
},
"address": {
"address_line_1": "Badensche Str. 24",
"address_line_2": "",
"admin_area_2": "Berlin(Berlin)",
"postal_code": "10715",
"country_code": "DE"
}
}
}]
}
Here is paypal´s response
{
name: "INVALID_REQUEST",
message: "Request is not well-formed, syntactically incorrect, or violates schema.",
debug_id: "c315ce9eb90b4",
details: [{
field: "/",
location: "body",
issue: "MALFORMED_REQUEST_JSON",
description: "The request JSON is not well formed.",
}],
links: [{
href: "https://developer.paypal.com/docs/api/orders/v2/#error-MALFORMED_REQUEST_JSON",
rel: "information_link",
encType: "application/json",
}],
}
I just cannot figure out the problem. I tried to remove the whole application_context, purchase_units, intent and see if there is a problem within any of these parameters. Nothing changed. What exactly is wrong with this call?
That's not how a patch operation works. A patch operation's JSON payload must look something like the example in the API reference:
'[
{
"op": "replace",
"path": "/purchase_units/#reference_id==\'default\'/shipping/address",
"value": {
"address_line_1": "123 Townsend St",
"address_line_2": "Floor 6",
"admin_area_2": "San Francisco",
"admin_area_1": "CA",
"postal_code": "94107",
"country_code": "US"
}
}
]'
However, based on the information provided it's unclear why you are attempting to use a PATCH, since you do not list any relevant fields for a patch.
Intent authorize and intent capture are for creating an order. Use one or the other, never both for the same transaction. To capture an order that was created with intent capture, use a capture API call. To authorize an order that was created with intent authorize, use an authorize API call. The relevant API endpoints for either will be in the API response when you create the order, or you can read the documentation.
Since it appears your intended result is a captured payment (rather than an authorization), intent authorize is not relevant to your use case.
The task is to fetch json user data from an api and store it in array. Then be able to make changes to it different routes.
Here is a sample of the data from the api, they are 10 in total and in an array.
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere#april.biz",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
},
"phone": "1-770-736-8031 x56442",
"website": "hildegard.org",
"company": {
"name": "Romaguera-Crona",
"catchPhrase": "Multi-layered client-server neural-net",
"bs": "harness real-time e-markets"
}
},
{
"id": 2,
"name": "Ervin Howell",
"username": "Antonette",
"email": "Shanna#melissa.tv",
"address": {
"street": "Victor Plains",
"suite": "Suite 879",
"city": "Wisokyburgh",
"zipcode": "90566-7771",
"geo": {
"lat": "-43.9509",
"lng": "-34.4618"
}
},
"phone": "010-692-6593 x09125",
"website": "anastasia.net",
"company": {
"name": "Deckow-Crist",
"catchPhrase": "Proactive didactic contingency",
"bs": "synergize scalable supply-chains"
}
},
i fetched the data using this:
$jsonurl = "http://jsonplaceholder.typicode.com/users";
$json = file_get_contents($jsonurl);
global $userData;
$userData = json_decode($json);
I displayed the data in a /users route with this
Route::get('/users', function(){
global $userData;
echo json_encode($userData);
});
I was told to get set a /delete route that deletes the last object from the array.
Route::get('/delete', function(){
global $userData;
$lastObjIndex = count($userData) -1;
array_splice($userData, $lastObjIndex,1);
//print_r('last user deleted');
echo json_encode($userData);
$userData = $userData;
});
The problem is that the delete route does not only works once(it does not delete subsequent last objects and returns the same thing when the route is accessed again) and does not show any change in when i access the users route. The data isn't stored in the server.
I feel there is something i don't know about that could help with this. Any suggestion would be appreciated.
Bellow JSON contain my fan page info in the json format.I tried getJson in the jquery but it didn't work for me
{
"about": " One who does something \n\n",
"description": "Doers Web development\nDoers Software Development\nDoers Graphics design\nDoers Hardware & Networking\nDoers Mobile Application Development",
"is_published": true,
"location": {
"street": "",
"city": "Colombo",
"country": "Sri Lanka",
"zip": ""
},
"phone": "+94773633412",
"talking_about_count": 686,
"username": "DoersIncorporation",
"website": "http://www.doers.lk",
"were_here_count": 0,
"category": "Computers/technology",
"id": "397319800348866",
"name": "Doers Inc",
"link": "https://www.facebook.com/DoersIncorporation",
"likes": 767,
"cover": {
"cover_id": 398352273578952,
"source": "http://sphotos-f.ak.fbcdn.net/hphotos-ak-snc7/s720x720/484190_398352273578952_36222530_n.png",
"offset_y": 0
}
}
What i want to know is how to parse cover image from it using php and jquery ???
Save the JSON to a variable:
var x = theJSON
x.cover.source
will return the url
Fiddle
I'm new to JSON and really struggling with this. I've read countless other posts and web pages but can't seem to figure it out.
I'm using PHP to output JSON (from data from the database) with this code:
header('Content-type: application/json');
echo json_encode($data);
Here is the JSON:
{
"x0": {
"id": "1",
"name": "Rob",
"online": "1",
"gender": "m",
"age": "29",
"height": "5'8''",
"build": "Average",
"ethnicity": "White",
"description": "Art geek person",
"looking_for": "Anything",
"image": "4fs5d43f5s4d3f544sdf.jpg",
"last_active": "29-06-11-1810",
"town": "Manchester",
"country": "UK",
"distance": 0.050973560712308
},
"x1": {
"id": "2",
"name": "Dave",
"online": "1",
"gender": "m",
"age": "29",
"height": "5'8''",
"build": "Average",
"ethnicity": "White",
"description": "Art geek person",
"looking_for": "Anything",
"image": "4fs5d43f5s4d3f544sdf.jpg",
"last_active": "29-06-11-1810",
"town": "Manchester",
"country": "UK",
"distance": 0.050973560712308
}
}
I think the problem I'm having is that the JSON is nested(might be wrong there)?.
This is the JQuery:
function fetchProfiles() {
var url='http://url.com/here';
var i = 0;
var handle = 'x'.i;
$.getJSON(url,function(json){
$.each(json.results,function(i,profile){
$("#profiles").append('<p><img src="'+profile.handle.image+'" widt="48" height="48" />'+profile.handle.name+'</p>');
i++;
});
});
}
Any ideas or suggestions appreciated!
Thanks!
i think that the problem is that you call $.each on json.results (if the json is exactly what you showed us).
you sholud do:
$.each(json,function(i,profile){
$("#profiles").append('<p><img src="'+profile.image+'" widt="48" height="48" />'+profile.name+'</p>');
});
look at the fiddle here: http://jsfiddle.net/ENcVd/1/ (it alerst the image property of you json object)