Get values from Paypal payment-intent multidimensional array - php

I want to implement the serverside-sdk from Paypal to my website. Everything works fine so far.
After the payment-intent got send to Paypal, I receive an array with information about the payment from Paypal.
If I print_r the object I got back it looks like this in my browser:
PayPal\Api\Payment Object (
[_propMap:PayPal\Common\PayPalModel:private] => Array ( [intent] => sale [payer] => PayPal\Api\Payer Object (
[_propMap:PayPal\Common\PayPalModel:private] => Array ( [payment_method] => paypal ) ) [transactions] => Array ( [0] => PayPal\Api\Transaction Object (
[_propMap:PayPal\Common\PayPalModel:private] => Array ( [amount] => PayPal\Api\Amount Object (
[_propMap:PayPal\Common\PayPalModel:private] => Array ( [total] => 0.50 [currency] => EUR ) ) [related_resources] => Array ( ) ) ) ) [redirect_urls] => PayPal\Api\RedirectUrls Object ( [_propMap:PayPal\Common\PayPalModel:private] => Array ( [return_url] => https://www.fabiansquinobal.com/addjob_submitted.php [cancel_url] => https://example.com/your_cancel_url.html ) ) [id] => PAYID-LSSMWWI9WR61061C3673442W [state] => created [create_time] => 2019-04-03T15:03:52Z [links] => Array ( [0] => PayPal\Api\Links Object (
[_propMap:PayPal\Common\PayPalModel:private] => Array ( [href] => https://api.sandbox.paypal.com/v1/payments/payment/PAYID-LSSMWWI9WR61061C3673442W [rel] => self [method] => GET ) ) [1] => PayPal\Api\Links Object (
[_propMap:PayPal\Common\PayPalModel:private] => Array ( [href] => https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-0RG36824VV460380F [rel] => approval_url [method] => REDIRECT ) ) [2] => PayPal\Api\Links Object (
[_propMap:PayPal\Common\PayPalModel:private] => Array ( [href] => https://api.sandbox.paypal.com/v1/payments/payment/PAYID-LSSMWWI9WR61061C3673442W/execute [rel] => execute [method] => POST ) ) ) )
)
I am completly overwhelmed with that. I would need to know how I can get to the "[id] => PAYID-LSSMWWI9WR61061C3673442W" or generally extract information out of this "thing".
Regards
EDIT:
The var_export of the array looks like this:
PayPal\Api\Payment::__set_state(
array( '_propMap' => array ( 'intent' => 'sale', 'payer' => PayPal\Api\Payer::__set_state(
array( '_propMap' => array ( 'payment_method' => 'paypal', ), )), 'transactions' => array ( 0 => PayPal\Api\Transaction::__set_state(
array( '_propMap' => array ( 'amount' => PayPal\Api\Amount::__set_state(
array( '_propMap' => array ( 'total' => '15.00', 'currency' => 'EUR', ), )), 'related_resources' => array ( ), ), )), ), 'redirect_urls' => PayPal\Api\RedirectUrls::__set_state(array( '_propMap' => array ( 'return_url' => 'https://www.fabiansquinobal.com/addjob_submitted.php', 'cancel_url' => 'https://example.com/your_cancel_url.html', ), )), 'id' => 'PAYID-LSSNC7I1CX40811F59322531', 'state' => 'created', 'create_time' => '2019-04-03T15:30:05Z', 'links' => array ( 0 => PayPal\Api\Links::__set_state(
array( '_propMap' => array ( 'href' => 'https://api.sandbox.paypal.com/v1/payments/payment/PAYID-LSSNC7I1CX40811F59322531', 'rel' => 'self', 'method' => 'GET', ), )), 1 => PayPal\Api\Links::__set_state(
array( '_propMap' => array ( 'href' => 'https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-98A35361RF153213R', 'rel' => 'approval_url', 'method' => 'REDIRECT', ), )), 2 => PayPal\Api\Links::__set_state(
array( '_propMap' => array ( 'href' => 'https://api.sandbox.paypal.com/v1/payments/payment/PAYID-LSSNC7I1CX40811F59322531/execute', 'rel' => 'execute', 'method' => 'POST', ), )), ), ), )
)
An echo of the object looks like this:
{ "intent": "sale",
"payer": { "payment_method": "paypal" },
"transactions": [ { "amount": { "total": "0.50", "currency": "EUR" },
"related_resources": [] } ],
"redirect_urls": { "return_url": "https://www.fabiansquinobal.com/addjob_submitted.php", "cancel_url": "https://example.com/your_cancel_url.html" },
"id": "PAYID-LSSNFSY6AJ60558N2288040L",
"state": "created", "create_time": "2019-04-03T15:35:38Z",
"links": [ { "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAYID-LSSNFSY6AJ60558N2288040L",
"rel": "self", "method": "GET" },
{ "href": "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-5KK55362XB209301D",
"rel": "approval_url", "method": "REDIRECT" },
{ "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAYID-LSSNFSY6AJ60558N2288040L/execute",
"rel": "execute", "method": "POST" } ]
}

Either decode the string to an array and use:
$arr = json_decode($json, true);
echo $arr['id'];
Or if you want it as an object:
$arr = json_decode($json);
echo $arr->id;
https://3v4l.org/bF2t9

Related

Create PHP stdClass object from array

I am trying to create this format of information/Array in PHP
[OverrideEmailSettings] => stdClass Object (
[email#domain.com] => stdClass Object (
[Reports] => Array (
[0] => stdClass Object (
[ReportType] => 1
[SummaryFrequency] => Array (
[0] => stdClass Object (
[FrequencyType] => 8011
[SecondsPast] => 32400
)
)
[Filter] => stdClass Object (
[ClauseType] => and
[RuleField] =>
[RuleOperator] =>
[RuleValue] =>
[ClauseChildren] => Array (
[0] => stdClass Object (
[ClauseType] =>
[RuleField] => BackupJobDetail.TimeSinceStarted
[RuleOperator] => int_lte
[RuleValue] => 86400
)
)
)
)
)
)
)
Here is my code - $line = the equivelent to email#domain.com
$x = array(
'Reports' => array(
'ReportType' => '1',
'SummaryFrequency' => array(
'FrequencyType' => '8011',
'SecondsPast' => '32400',
),
'Filter' => array(
'ClauseType' => 'or',
'RuleField' => '',
'RuleOperator' => '',
'RuleValue' => '',
'ClauseChildren' => array(
'ClauseType' => '',
'RuleField' => 'BackupJobDetail.TimeSinceStarted',
'RuleOperator' => 'int_lte',
'RuleValue' => '86400',
),
),
),
);
$account_get_user_profile->OverrideEmailSettings->$line = $x;
But I think I have formatted it incorrectly.
You need to cast your arrays to stdClass like:
<?php
$x = [
"Reports" => [
(object) [
"ReportType" => "1",
"SummaryFrequency" => [
(object) [
"FrequencyType" => "8011",
"SecondsPast" => "32400",
],
],
"Filter" => (object) [
"ClauseType" => "or",
"RuleField" => "",
"RuleOperator" => "",
"RuleValue" => "",
"ClauseChildren" => [
(object) [
"ClauseType" => "",
"RuleField" => "BackupJobDetail.TimeSinceStarted",
"RuleOperator" => "int_lte",
"RuleValue" => "86400",
],
],
],
],
],
];
print_r($x);
Look PHP online code

push array into array in specific format

I have this array structure named $params
Array
(
[PLANT_RA] => Array
(
[0] => Array
(
[SIGN] => I
[OPTION] => EQ
[LOW] => 6104
[HIGH] =>
)
)
[STGE_LOC_RA] => Array
(
[0] => Array
(
[SIGN] => I
[OPTION] => EQ
[LOW] => 5700
[HIGH] =>
)
)
[BATCH_RA] => Array
(
[0] => Array
(
[SIGN] => I
[OPTION] => NE
[LOW] =>
[HIGH] =>
)
)
)
which is created:
$parms2 = array(
'PLANT_RA' => array(array(
'SIGN' => "I",
'OPTION' => getOption($_GET['PLANT_RA-low'], $_GET['PLANT_RA-high']),
'LOW' => $_GET['PLANT_RA-low'],
'HIGH' => $_GET['PLANT_RA-high']
)),
'STGE_LOC_RA' => array(array(
'SIGN' => "I",
'OPTION' => getOption($_GET['STGE_LOC_RA-low'], $_GET['STGE_LOC_RA-high']),
'LOW' => $_GET['STGE_LOC_RA-low'],
'HIGH' => $_GET['STGE_LOC_RA-high']
)),
'BATCH_RA' => array(array(
'SIGN' => "I",
'OPTION' => getOption($_GET['BATCH_RA-low'], $_GET['BATCH_RA-high']),
'LOW' => $_GET['BATCH_RA-low'],
'HIGH' => $_GET['BATCH_RA-high']
))
)
However, now for my purposes I need to change it and use it via array_push(),
so I can declare empty array:
$parms2 = array();
and now when I push it in:
if (isset($_GET['PLANT_RA-low'])) {
$storage_array = array('PLANT_RA' => array(
'SIGN' => "I",
'OPTION' => getOption($_GET['PLANT_RA-low'], $_GET['PLANT_RA-high']),
'LOW' => $_GET['PLANT_RA-low'],
'HIGH' => $_GET['PLANT_RA-high']
));
array_push($parms, $storage_array);
}
if (isset($_GET['STGE_LOC_RA-low'])) {
$storage_array = array('STGE_LOC_RA' => array(
'SIGN' => "I",
'OPTION' => getOption($_GET['STGE_LOC_RA-low'], $_GET['STGE_LOC_RA-high']),
'LOW' => $_GET['STGE_LOC_RA-low'],
'HIGH' => $_GET['STGE_LOC_RA-high']
));
array_push($parms, $storage_array);
}
if (isset($_GET['BATCH_RA-low'])) {
$batch_array = array('BATCH_RA' => array(
'SIGN' => "I",
'OPTION' => getOption($_GET['BATCH_RA-low'], $_GET['BATCH_RA-high']),
'LOW' => $_GET['BATCH_RA-low'],
'HIGH' => $_GET['BATCH_RA-high']
));
array_push($parms, $batch_array);
}
then it has not correct format which is refused by SAPNWRFC:
Array
(
[0] => Array
(
[PLANT_RA] => Array
(
[SIGN] => I
[OPTION] => EQ
[LOW] => 6104
[HIGH] =>
)
)
[1] => Array
(
[STGE_LOC_RA] => Array
(
[SIGN] => I
[OPTION] => EQ
[LOW] => 5700
[HIGH] =>
)
)
[2] => Array
(
[BATCH_RA] => Array
(
[SIGN] => I
[OPTION] => NE
[LOW] =>
[HIGH] =>
)
)
)
can you please guide me how do I push (in what format) so I have the EXACT same output of the $params array at the end?
In the end all this code can be reduced to:
$parms = [];
$keys = ['PLANT_RA', 'STGE_LOC_RA', 'BATCH_RA'];
foreach ($keys as $key) {
if (isset($_GET[$key . '-low'])) {
$parms[$key] = [
[
'SIGN' => "I",
'OPTION' => getOption($_GET[$key . '-low'], $_GET[$key . '-high']),
'LOW' => $_GET[$key . '-low'],
'HIGH' => $_GET[$key . '-high']
]
];
}
}

access single value of php object array

I call an api and the response i get is
Array (
[meta] => Array (
[code] => 200
[type] => Success
[message] => Success
)
[data] => Array (
[items] => Array (
[0] => Array (
[id] => b4a235596fd9550dfb69f181f4db007f
[tracking_number] => 2649884668232181
[carrier_code] => hermes
[order_create_time] =>
[destination_code] =>
[status] => delivered
[track_update] =>
[original_country] =>
[itemTimeLength] => 7
[stayTimeLength] => 74
[service_code] =>
[packageStatus] =>
[substatus] =>
[last_mile_tracking_supported] =>
[origin_info] => Array (
[ItemReceived] => 2019-04-09 09:29
[ItemDispatched] =>
[DepartfromAirport] =>
[ArrivalfromAbroad] =>
[CustomsClearance] =>
[DestinationArrived] =>
[weblink] => https://www.hermesworld.com/en/
[phone] =>
[carrier_code] => hermes
[trackinfo] => Array (
[0] => Array (
[Date] => 2019-04-15 11:51
[StatusDescription] => Posted through letterbox
[Details] =>
[checkpoint_status] => delivered
)
[1] => Array (
[Date] => 2019-04-15 09:45
[StatusDescription] => Delivery will be attempted between 10:00 and 14:00 today
[Details] =>
[checkpoint_status] => transit
)
[2] => Array (
[Date] => 2019-04-15 06:39
[StatusDescription] => On its way to the courier
[Details] =>
[checkpoint_status] => transit
)
[3] => Array (
[Date] => 2019-04-14 22:33
[StatusDescription] => At the customers local depot
[Details] =>
[checkpoint_status] => transit
)
.....
)
)
[destination_info] =>
[lastEvent] => Posted through letterbox,2019-04-15 11:51
[lastUpdateTime] => 2019-04-15 11:51
)
)
)
) 1
I would like to access the value of checkpoint_status but im not getting there using the following ways:
response in variable
$track = print_r($track);
1. $track['checkpoint_status']
2. $track[0] // just to see if it returns anything, but no result
3. $track[1] // just to see if it returns anything, but no result
4. array_column($track, 'checkpoint_status'); // returns nothing
You need to loop, to get all checkpoint_status
foreach($array['data']['items'] as $item){
foreach($item['origin_info']['trackinfo'] as $trackinfo){
echo $trackinfo['checkpoint_status'].PHP_EOL;
}
}
Sample output:-https://3v4l.org/IM7I1
You can use array_walk_recursive
$r = [];
array_walk_recursive($a, function($v, $k) use(&$r){
($k == 'checkpoint_status') ? ($r[]=$v) : '';
});
https://3v4l.org/rCVEB
$response = [
'meta' => [
'code' => 200,
'type' => 'Success',
'message' => 'Success',
],
'data' => [
'items' => [
'0' => [
'id' => 'b4a235596fd9550dfb69f181f4db007f',
'tracking_number' => '2649884668232181',
'carrier_code' => 'hermes',
'order_create_time' => null,
'destination_code' => null,
'status' => 'delivered',
'track_update' => null,
'original_country' => null,
'itemTimeLength' => 7,
'stayTimeLength' => 74,
'service_code' => null,
'packageStatus' => null,
'substatus' => null,
'last_mile_tracking_supported' => null,
'origin_info' => [
'ItemReceived' => '2019-04-09 09:29',
'ItemDispatched' => null,
'DepartfromAirport' => null,
'ArrivalfromAbroad' => null,
'CustomsClearance' => null,
'DestinationArrived' => null,
'weblink' => 'https://www.hermesworld.com/en/',
'phone' => null,
'carrier_code' => 'hermes',
'trackinfo' => [
'0' => [
'Date' => '2019-04-15 11:51',
'StatusDescription' => 'Posted through letterbox',
'Details' =>null,
'checkpoint_status' => 'delivered',
],
'1' => [
'Date' => '2019-04-15 09:45',
'StatusDescription' => 'Delivery will be attempted between 10:00 and 14:00 today',
'Details' => null,
'checkpoint_status' => 'transit',
],
'2' => [
'Date' => '2019-04-15 06:39',
'StatusDescription' => 'On its way to the courier',
'Details' =>null,
'checkpoint_status' => 'transit',
],
'3' => [
'Date' => '2019-04-14 22:33',
'StatusDescription' => 'At the customers local depot',
'Details' => null,
'checkpoint_status' => 'transit',
],
]
],
'destination_info' => null,
'lastEvent' => 'Posted through letterbox,2019-04-15 11:51',
'lastUpdateTime' => '2019-04-15 11:51',
]
]
]
];
If you are working on development mode you could turn on display errors on php and may get this result:
echo $response['checkpoint_status']; // won't work as response array has no checkpoint_status keyed array
echo $response[0]; // won't work as response array has no 0 keyed array
echo $response[1]; // won't work as response array has no 1 keyed array
print_r(array_column($response, 'checkpoint_status')); // won't work as response array has no column checkpoint_status
There are many ways in which you could get your result one of which is given below:
$trackinfo = $response['data']['items'][0]['origin_info']['trackinfo'];
$checkpoint_status = array_column($trackinfo, 'checkpoint_status');
print_r($checkpoint_status);
Result:
Array (
[0] => delivered
[1] => transit
[2] => transit
[3] => transit
)

ElasticSearch - How to return data from elastic search with not empty value?

I've got a problem with eliminating empty values from a document of one of my index.
$params = [
"scroll" => "30s", // how long between scroll requests. should be small!
"size" => 1,
"index" => $eddIndex,
'type' => $eddIndexType
];
$response = $client->search ( $params );
while ( isset ( $response ['hits']['hits'] ) && count ( $response['hits']['hits'] ) > 0 ) {
$scroll_id = $response ['_scroll_id'];
$response = $client->scroll ( [
"scroll_id" => $scroll_id,
"scroll" => "30s"
] )
;
This will Return
Array
(
[_scroll_id] => cXVlcnlUaGVuRmV0Y2g7NTs0NTY0NDM6di1Bd2x4X0ZSaTJ5cnpVUkFHb3FRdzs0NTY0NDQ6di1Bd2x4X0ZSaTJ5cnpVUkFHb3FRdzs0NTY0NDY6di1Bd2x4X0ZSaTJ5cnpVUkFHb3FRdzs0NTY0NDc6di1Bd2x4X0ZSaTJ5cnpVUkFHb3FRdzs0NTY0NDU6di1Bd2x4X0ZSaTJ5cnpVUkFHb3FRdzswOw==
[took] => 12
[timed_out] =>
[_shards] => Array
(
[total] => 5
[successful] => 5
[failed] => 0
)
[hits] => Array
(
[total] => 378887
[max_score] => 1
[hits] => Array
(
[0] => Array
(
[_index] => index
[_type] => edd
[_id] => 13038
[_score] => 1
[_source] => Array
(
[reason] =>
[booking_date] => 2013-05-03T18:30:00.000Z
[booking_location] => ABC
[scan_edd] =>
[call_status] => Call
[patient_name] => ABCD
[#version] => 1
[effective_edd] =>
[id] => 13038
[email] => xxxxxxxxx#gmail.com
[last_known_edd] =>
[booking_amount] => 8000
[address] => xxxxxxxxxxxxxxxxx
[eda] => 2013-09-09T18:30:00.000Z
[edd] =>
[mpi] => xxxxxxx
[mobile] => xxxxxxx
[statuss] => Open Payment
[tags] => Array
(
[0] => edd
)
[last_consult_by] => Dr. XXXX [site] => BLR
[#timestamp] => 2018-07-06T06:30:39.040Z
[patient_id] => 75220
[last_consult_on] => 2014-02-28T18:30:00.000Z
[ip_booking] => XYX Package
[is_converted] => 0
)
)
)
)
)
If you look at my output, There is a field [effective_edd] returning empty.
I wanted to return data with [effective_edd] not empty.
I'm referring to the below document https://packagist.org/packages/elasticsearch/elasticsearch
You need to add a query that checks for the existence of the field to your scroll request:
$params = [
"scroll" => "30s", // how long between scroll requests. should be small!
"size" => 1,
"index" => $eddIndex,
'type' => $eddIndexType,
'body' => [
'query' => [
'exists' => [
'field' => 'effective_edd'
]
]
]
];

associative array check any column is empty or not

I have associative array like -
[0] => Array
(
[date] => 2018-06-22
[id] => 2282991
[type] => VIDEO
[domain] =>
[code] => Austin
[address] => Phone
)
[1] => Array
(
[date] => 2018-06-22
[id] => 2282991
[type] => VIDEO
[domain] =>
[code] =>
[address] => Phone
)
[3] => Array
(
[date] => 2018-06-22
[id] => 2282991
[type] => VIDEO
[domain] =>
[code] => Austin
[address] => Phone
)
I need to check is there any column having all the values are blank.
That means it should return only domain from above array because it is blank everywhere.
Is there any way to do this with minimum use of forloop? I need to check this for all these columns.
This will work if your subarray having same number of keys.Like "Date, id, Type etc".
$array = [
[ "date" => "2018-06-22", "id" => 2282991, "type" => "VIDEO", "domain" =>'', "code" => "Austin", "address" => "Phone"],
[ "date" => "2018-06-22", "id" => 2282991, "type" => "VIDEO", "domain" =>'', "code" => "", "address" => "Phone"],
[ "date" => "2018-06-22", "id" => 2282991, "type" => "VIDEO", "domain" =>'', "code" => "Austin", "address" => "Phone"]
];
$empty = [];
foreach($array[0] as $key=>$val){
$error = array_column($array, $key);
if(empty (array_filter($error)) ) {
$empty[] = $key;
}
}
print_r($empty);
Output:
Array
(
[0] => domain
)

Categories