External API call before post form

I’m trying to develop a GF plugin that is a field for signing the form via a third party api.
It should work as this when you submit the form you should somehow be presented with a text… “start your signing app in your phone” … an ajax call will shot in the background and get a response that the user has signed. Then the form will ge posted and saved.

For this last part with the signing… is the correct way to use the Feed class in GF or at what point should i do that external api call? Some other class or function i can search for to get a pointer how to code this?

Can you clarify whether your primary goal with this is that the entry values are what is signed versus the potential entire $_POST object?

Gravity Flow’s outgoing webhook step might provide you everything you need without having to write custom code. You could configure the step (which occur after the form submission has been received to server) and use its’ field mapping feature to send as much or as little of the $entry to your 3rd party API. It also provides response mapping back to fields, so either the whole response or a specific attribute from it via array-notation could be stored into a field of the entry. Any steps that occur in the workflow after that would have access to the updated field signing value.

Hope that helps,
Jamie

1 Like

Thank you for the answer!

I need the user to sign via an external service called BankID that we have here in sweden. On the site/form you fill in your socialsec-nr and click the submit button on the form… then you will be asked to start your BankID app on your phone. The user signs with the app… the page will keep reloading an ajax call to see if the signing was finished and ok… the GF-form will then do the ordinary post.

So this might not be something that Gravity Flow is suited for?

I have started to code a BankID gf field and try to hook into the submission in some way to do my “open a div and wait and reload the div and do a ajax call once per second to see if there has been a response”.

Thanks :slight_smile:

Maybe the solution would be just to “hook” into the form post with javascript and do my thing before the form is posted… so like do a return false on the form post javascript if the BankID isnt signed correctly in the app?

While Gravity Flow would fit, I think the route you are going w.r.t a custom field type that brings it’s own AJAX is the better from usability (real-time validation). That field would be able to operate whether on the initial form submission or a Gravity Flow user input step further in workflow as example.

You may also want to customize via the gform_field_validation filter to ensure that server side validates the request. Depending on your data security model, you might look at gform_pre_submission to remove any data that doesn’t need to be stored into the actual entry. i.e. Remove the actual SSN in field and have a separate field store a token that confirms authentication.

Cheers,
Jamie

Yep the part about only save the token sounds lika good plan! The rest of the info is in another system i have here (the one that i will do the ajax call to)

But i dont quite follow about the ajax part. I need to use the same form submit button so i need to hook into GF some how. If i would do this in an ordinary form i would do like and then return false so that the form doesnt post if the ajax call dont return the correct info. So, what would the corresponding thing to do with GF to achieve this?

For the server side portion, within your filter written for gform_field_validation you would likely use a wp_remote_request. To do similar via client side, it would probably use gform_enqueue_scripts - in PHP code checking if the $form object contains your field before actually enqueuing the JS where you could perform the ajax call.

It seams that some code was stripped that i posted as an example.

What i mean is how and where should i fire my javascript that should run in a loop and do its ajax calls until it gets an OK… and THEN the form should do the normal form submit in GF?

I managed to invoke the form post with this:

jQuery( "#gform_8" ).submit(function( event ) {
	alert( "Handler for .submit() called." );
    event.preventDefault();
});

So that is good. Then i got the form to not post… and the spinner kept on spinning. Is it possible to remove/reset the “spinner” so it again is possible to push the submit button? (this so it is possible to try again if they didnt signed the external BankID call correctly)

I solved it by doing this:

jQuery( “.gform_button[type=submit]” ).click(function( event ) {
// Control if form is valid

jQuery('#bankid-frame').show();
event.preventDefault();

// Do Ajax-loop and wait on BankID sign from user
// this.submit(); if sign is OK

});

But i need to run the GF field validations somehow as a first thing in my js function. Any idea?

Glad to hear you are making progress.

For client-side validation - see this topic that has come up previously. For server side validation the gform_validation and gform_field_validation are best filters to look at. You may want to switch to sending a support query to Gravity Forms for follow-ups that are more specific about the use case you are tying to build if it is about how to interact with those.

1 Like

Oh thank you :slight_smile: i will have a look into that.
I appreciate your input!

© 2008 - 2019. Gravity Forms is a project by Rocketgenius Inc.