PayPal how to pass the id to the client - php

I'm working on integrating PayPal to my site, however I've got to the point where it requests me to pass the id back to the client. And I'm not sure how to do this. Any help would be great.
This is the last JSON code I get from the PayPal servers. As I'm doing it from the server side code.
{
"id":"PAY-5VL741754E714161BLHUWCKY",
"intent":"sale",
"state":"created",
"payer":
{
"payment_method":"paypal"
},
"transactions":
[{
"amount":
{
"total":"4.00",
"currency":"USD",
"details":
{
"subtotal":"2.00",
"tax":"2.00",
"shipping":"1.00",
"shipping_discount":"-1.00"
}
},
"description":"The payment transaction description.",
"custom":"PlayerID",
"invoice_number":"merchant invoice",
"item_list":
{
"items":
[{
"name":"item 1",
"description":"item 1 description",
"price":"1.00",
"currency":"USD",
"tax":"1.00",
"quantity":1
},
{
"name":"item 2",
"description":"item 2 description",
"price":"1.00",
"currency":"USD",
"tax":"1.00",
"quantity":1
}]
},
"related_resources":
[]
}],
"create_time":"2017-10-20T02:36:27Z",
"links":
[{
"href":"h.t.t.p.s.:././.a.p.i...s.andbox.paypal.com/v1/payments/payment/PAY-5VL741754E714161BLHUWCKY",
"rel":"self",
"method":"GET"
},
{
"href":"h.t.t.p.s.:././.w.w.w...s.andbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-3U757031AX945661J",
"rel":"approval_url",
"method":"REDIRECT"
},
{
"href":"h.t.t.p.s:././.a.p.i...s.a.ndbox.paypal.com/v1/payments/payment/PAY-5VL741754E714161BLHUWCKY/execute",
"rel":"execute",
"method":"POST"
}
]}

You can use the json_decode() function (function docs) to decode the JSON information into a multi-dimensional array.
The following script will echo out the ID as required (provided $response is the JSON-encoded response from the PayPal transaction):
$data = json_decode($response, true);
echo $data['id'];

I worked it out, and its the following.
$response1 = json_decode($response);
$payment_id = $response1->id;
$payment_id = print_r($payment_id, true);
print ("{
\"id\": \"$payment_id\"
}");

Related

How to replace JSON key without losing its value and position using PHP

This is the JSON
{
"table-name": "Kiwi",
"created-on": "November 20, 2021",
"columns": {
"Info": {
"type": "longtext",
"extra": ""
},
"Status": {
"type": "droplist",
"extra": ""
},
"Task": {
"type": "text",
"extra": ""
}
},
"data": [
{
"Name": "Team Reports",
"Info": "Submitting marketing materials reports",
"Status": "Completed"
},
{
"Name": "Fabia HR",
"Info": "Brian asking for a report",
"Status": "Pending"
},
{
"Name": "Fabia",
"Info": "Meeting with CEO #cafe 9:00",
"Status": "Cancelled"
}
]
}
And I was trying to achieve to rename the "Info" into "Description" without losing its value and array position. I'm not very familiar with array_replace , that seems my code is not working. Please share some codes, it will be much appreciated.
Here my PHP code I tried
<?PHP
$jsn = file_get_contents('./test.json');
$arr = json_decode($jsn, true);
$newArray= [];
foreach($arr['data'] as $row){
$row['Description'] = $row['Info'];
array_push($newArray, $row);
}
//print_r($newArray);
array_replace($arr['data'],$newArray);
echo json_encode($arr, JSON_PRETTY_PRINT);
Thank you so much for your attention and advance help. It will really gonna save me some time. Noob here and I'm still learning things about PHP and JSON

How to get json data to an angular array variable

I am trying to get a json result to an array object .I am echoing a json data in php. like this.
<?php
header('Access-Control-Allow-Origin: *');
header('Content-type:application/json;charset=utf-8');
$arr= '[{
"id": "1",
"date": "2020-03-21",
"status": "present",
"studentid": "1"
},
{
"id": "2",
"date": "2020-03-24",
"status": "present",
"studentid": "1"
},
{
"id": "3",
"date": "2020-03-25",
"status": "absent",
"studentid": "1"
},
{
"id": "4",
"date": "2020-03-26",
"status": "absent",
"studentid": "1"
}
]';
echo $arr;
?>
~
How to get absentees using angular in an array.
Angular part i tried didnt work
this.http.post("http://localhost/android/Api.php?apicall=getattendance", JSON.stringify(this.postData),options)
.subscribe( (data) => {
this.setUsersArray(data);
console.log(data + "URL DATA"+JSON.stringify(this.postData));
}
);
=====================================================================
setUsersArray(data){
if (data instanceof Array) {
{
this.date_present = data.map(function (ele) {
if(ele.status==='present')
{
return ele.date;
}
});
this.date_absent = data.map(function (ele) {
if(ele.status==='absent')
return ele.date;
});
}
}
I am getting date_absent and date_present as null.Why am i getting this as null.Please help me. I am new to angular.
try for loop in typescript
setUsersArray(data:any){
for(let item of data){
console.log(item);
if(item.status==='present') {
this.present.push(item.date);
}
if(item.status==='absent') {
this.absent.push(item.date);
}
}
Trying to this url
$characters = json_decode($data, true); // decode the JSON feed and make an associative array
https://www.taniarascia.com/how-to-use-json-data-with-php-or-javascript/

JSON Post PHP (TypeForm)

I have never used JSON before so apologies if this is a simple request.
I have a webhook setup that sends me a JSON Post (Example Below) - I want to extract the two answers from this "text":"250252" & {"label":"CE"}
{
"event_id": "1",
"event_type": "form_response",
"form_response": {
"form_id": "VpWTMQ",
"token": "1",
"submitted_at": "2018-05-22T14:11:56Z",
"definition": {
"id": "VpWTMQ",
"title": "SS - Skill Change",
"fields": [
{
"id": "kUbaN0JdLDz8",
"title": "Please enter your ID",
"type": "short_text",
"ref": "9ac66945-899b-448d-859f-70562310ee5d",
"allow_multiple_selections": false,
"allow_other_choice": false
},
{
"id": "JQD4ksDpjlln",
"title": "Please select the skill required",
"type": "multiple_choice",
"ref": "a24e6b58-f388-4ea9-9853-75f69e5ca337",
"allow_multiple_selections": false,
"allow_other_choice": false
}
]
},
"answers": [
{
"type": "text",
"text": "250252",
"field": {
"id": "kUbaN0JdLDz8",
"type": "short_text"
}
},
{
"type": "choice",
"choice": {
"label": "CE"
},
"field": {
"id": "JQD4ksDpjlln",
"type": "multiple_choice"
}
}
]
}
}
I have this currently in my PHP file:
$data = json_decode(file_get_contents('php://input'));
$ID = $data->{"text"};
$Skill = $data->{"label"};
This does not work and all I get is null - Any help would really be appreciated, Thank You.
You need to look at the JSON object you're receiving to know the structure of the object you're receiving after using json_decode, what you're trying to get is in $data->form_response->answers, So you can have a variable for easier access:
$answers = $data->form_response->answers;
remember $answers is an array
So to achieve what you're trying to get, you can do:
$data = json_decode(file_get_contents('php://input'));
$answers = $data->form_response->answers;
$ID = $answers[0]->text;
$Skill = $answers[1]->choice->label;

Gmail API return chats with "SENT" label

Using the Gmail API call
https://www.googleapis.com/gmail/v1/users/userId/messages/id
return chats with label SENT
I have used the following code for getting the message content
$url = "https://www.googleapis.com/gmail/v1/users/$useremail/messages/$messageid?format=full";
$response = $gmailInstance->performHttpRequest($gmailAuthObj, $useremail, $url);
$responseBody = json_decode($response->getResponseBody(), TRUE);
I got the following response for the above API call
{
"id": msgid,
"threadId": threadid,
"labelIds": [
"CHAT",
"SENT"
],
"snippet": "sometext",
"historyId": 7 digit numeric,
"internalDate": "1371160444361",
"payload": {
"partId": "",
"mimeType": "text/html",
"filename": "",
"headers": [{
"name": "From",
"value": "Test User \u003ctuser#tdom.com\u003e"
}],
"body": {
"size": 20,
"data": somerandomstring
}
},
"sizeEstimate": 100
}
Does anybody know why the API response return chat message with SENT label in it? Is there a way to avoid this (SENT label from chat response)?

Decode json in php and create variables

I a trying to decode a json callback.
The json code is posted to callback.php - Here is an example of the json:
{
"order": {
"id": "5RTQNACF",
"created_at": "2012-12-09T21:23:41-08:00",
"status": "completed",
"total_btc": {
"cents": 100000000,
"currency_iso": "BTC"
},
"total_native": {
"cents": 1253,
"currency_iso": "USD"
},
"custom": "order1234",
"receive_address": "1NhwPYPgoPwr5hynRAsto5ZgEcw1LzM3My",
"button": {
"type": "buy_now",
"name": "Alpaca Socks",
"description": "The ultimate in lightweight footwear",
"id": "5d37a3b61914d6d0ad15b5135d80c19f"
},
"transaction": {
"id": "514f18b7a5ea3d630a00000f",
"hash": "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b",
"confirmations": 0
},
"customer": {
"email": "coinbase#example.com",
"shipping_address": [
"John Smith",
"123 Main St.",
"Springfield, OR 97477",
"United States"
]
}
}
}
I can echo the json and get the following response:
{"order""id":null,"created_at":null,"status":"completed","total_btc":{"cents":100000000,"currency_iso":"BTC"},"total_native":{"cents":83433,"currency_iso":"USD"},"custom":"123456789","receive_address":"1A2qsxGHo9KjtWBTnAopTwUiBQf2w6yRNr","button":{"type":"buy_now","name":"Test Item","description":null,"id":null},"transaction":{"id":"52d064b59eeb59985e00002c","hash":"4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b","confirmations":0}}}
However if I try to decode the json using the following:
$array = json_decode($jsonString, true);
echo $array;
I get the following response: "200 Array"
I want to be able turn each json parameter in to a php variable.
You can access the variables within $array, for example by doing:
echo $array['custom']; // prints out "order1234"
You don't want to extract the variables directly into the local lexical scope of your program as that would create security concerns. Just use the data as indicated in the snippet above.

Categories