Bearer Authorization for API (square)

By: David Leger | Asked: 12/20/2022
ForumsCategory: How-toBearer Authorization for API (square)
David Leger asked 2 years ago
I'm trying to create a new customer through an API with square POS (squareup.com). Zapier could do this, but zapier takes minutes, and I need it to be instant as it's a customer pre-registration form I'm trying to create. While they wait in line, they can supply some basic information to speed up the checkout process. Square uses "bearer" authentication and formidable uses basic. I was given some guidance by a very helpful formidable person, but I think I'm stretching their capability or scope of support. I was directed to this "

Other authentication#

The Formidable API add-on is set up to send requests using Basic Authorization. Some other types of Authorization aren't built-in, but can be added with a hook. For example, some APIs require Bearer Authentication. If you need to communicate with an API that uses Bearer Authentication, you can add a Bearer header to the API request using this hook. Which lead me to this https://formidableforms.com/knowledgebase/formidable-api/#kb-customize-api-request-headers I copied the code into the functions.php file of my child theme.
add_action( 'rest_api_init', 'initialize_inclide_password_in_api' );
function initialize_inclide_password_in_api() {
    add_filter( 'frm_include_field_in_content', 'include_password_in_api', 10, 2 );
}

function include_password_in_api( $include, $field_value ) {
    $target_field_key = 'utv62'; // change this
    if (  $target_field_key === $field_value->get_field_key() ) {
        $include = true;
    }
    return $include;
}
I replaced utv62 with my field code, which contains the token. I feel like I'm missing something
1 Answers
Victor Font answered 2 years ago
Do you know how to use the Kint debugger? Other than the misspelling of "include" as "inclide", I don't see anything about this code that shouldn't work. This means you have to do a debugging session that includes viewing the values of the variables. Chances are good that this line is returning false: $target_field_key === $field_value->get_field_key(). You have to use a tool like Kint to stop program execution and see the results of this comparison in real time. What is the value of $target_field_key and $field_value->get_field_key()? If they match and and the include_password_in_api() is returning true as indicated, then the problem lies elsewhere, not in this snippet of code. In which case, you need to open a support ticket with Strategy 11.
David Leger replied 2 years ago

Thank you. Kint actually crashed my site, but I'll try another debuggr.

Victor Font replied 2 years ago

I had to change two lines of code in the Kint plugin so it works with PHP 8.x. New versions of PHP are more restrictive about how arrays are initialized. There are tow places where the {} need to be changed to [].

David Leger replied 2 years ago

Don't suppose you could either point out where or share the updated version?

Victor Font replied 2 years ago

Here's the copy I use in my development environment with PHP 8.2. https://vfcgdownloads.s3.amazonaws.com/kint-php-debugger.zip

David Leger replied 2 years ago

Thank you for sharing that. This fell off the radar, but now I have some time to revisit.

I'm not sure I understand this Kint thing... I see a KINT Active in the top right corner, but I can't for the life of me figure out how to actually use it.

my other question would be, where should I have put this code above (The hook where it passes the password when it's bearer authentication?? In the main functions php or within the formidable php? I feel like I may be completely wrong either way.

Victor Font replied 2 years ago

Kint is very easy to use. See this: https://wordpress.org/plugins/kint-php-debugger/. My version is a slightly modified version of this one.

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right