I am having a real struggle getting meta data values from woo commerce REST API in a fashion that makes sense and is reliable. I want to access the PayPal returned fraud data for an order so my staff can see the data for security reasons.
I put a request on the REST API to the end point and order id as such: https://[wbeaddress]/wp-json/wc/v3/orders/[order id]
In the meta data returned for the order I can see the following data:
"meta_data": [
{
"id": 959,
"key": "_coupons_hash",
"value": "****"
},
{
"id": 960,
"key": "_fees_hash",
"value": "****"
},
{
"id": 981,
"key": "_ppcp_paypal_3DS_auth_result",
"value": {
"liability_shift": "POSSIBLE",
"three_d_secure": {
"enrollment_status": "Y",
"authentication_status": "Y"
}
}
},
{
"id": 978,
"key": "_ppcp_paypal_fees",
"value": {
"gross_amount": {
"currency_code": "GBP",
"value": "1379.00"
},
"paypal_fee": {
"currency_code": "GBP",
"value": "44.29"
},
"net_amount": {
"currency_code": "GBP",
"value": "1334.71"
}
}
},
{
"id": 980,
"key": "_ppcp_paypal_fraud_result",
"value": {
"avs_code": "Y",
"cvv2_code": "S",
"address_match": "N",
"postal_match": "N",
"cvv_match": "N",
"card_brand": "VISA",
"card_last_digits": "1368"
}
},
I can access the data using its position in the response for example: by going
$avscode=$results['meta_data'][4]['value']['avs_code'] ?? null;
$cvvmatch=$results['meta_data'][4]['value']['cvv_match'] ?? '';
$card_type=$results['meta_data'][4]['value']['card_brand'] ?? '';
$last_four=$results['meta_data'][4]['value']['card_last_digits'] ?? '';
But I want to be able to use the key name with the accessible values. The array position is not reliable in the checks. How on earth can I access the values for different key names such as this above like avs_code etc. "_ppcp_paypal_fraud_result" and those values such as enrollment_status under the "_ppcp_paypal_3DS_auth_result" key?
Thank you!
$paypalData = array_values(array_filter($results['meta_data'], function($item) { return $item['key'] == '_ppcp_paypal_fraud_result'; }));- array_values after filtering re-indexes the array, so you'll find the desired element (should it exist) at index 0. 3v4l.org/V5pgX