-2

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!

4
  • 1
    Either loop over the array and check if the key of the current item is the one you are interested in; or extract the part you need using àrray_filter`. Commented Aug 21 at 7:52
  • Would you have an example of this? I am finding it complex as it could be in a first or second layer with an array result in the given layer. Commented Aug 25 at 15:52
  • $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 Commented Aug 26 at 6:15
  • @C3roe I like this, really neat! Thank you very much for the example. It's certainly better than the way I Was trying to do it. Thank you! Commented Aug 26 at 20:19

1 Answer 1

0

Thanks to @C3roe for coming up with a great way to sort this, per their comment. I wanted to show this answer marked complete.

$paypalData = array_values(array_filter($results['meta_data'], function($item) { return $item['key'] == '_ppcp_paypal_fraud_result'; }));

Excellent way to loop through an array to find specific values.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.