imp|

There's something I would change about the Gravity Forms Payment Add-On Framework

All official Gravity Forms payment add-ons are using the Payment Add-On Framework, which is an abstract PHP class with handy methods like complete_payment(), has_credit_card_field().

If you want to integrate a payment service on your own, there's no good reason not to base your integration on this class since it provides you a headstart and a standardized way of doing certain things.

Some payment services, like NETOPIA Payments, process payment on their websites. In practical terms, once you select a product, add your billing information, and submit the form, you are redirected to the NETOPIA Payments site to enter the credit card information and then redirected back.

One of the conveniences that the Payment Add-On Framework provides is the boilerplate code for redirecting to the payment services after the form submission. According to the documentation, overwriting the $redirect_url property might be enough.

class ExamplePaymentAddOn extends GFPaymentAddOn {
protected $redirect_url = 'https://example.com';
}

Then there is the redirect_url() method. It's for when you want to set the redirect URL dynamically.

Typically all payment services offer sandbox mode, where you can go through the entire payment process without actually charging real cards. If you used the live URL for the $redirect_url property, you could overwrite it with the sandbox URL using this method.

All seemed clear to me, but I stumbled upon an unexpected behavior when I defined my redirect URL.

Let's go through the relevant code step-by-step.

Immediately when the payment add-on is initialized, a filter is added to the gform_confirmation.

abstract class GFPaymentAddOn extends GFFeedAddOn {
public function init() {
add_filter( 'gform_confirmation', array( $this, 'confirmation' ), 20, 4 );
}
}

This the current code, version 2.5.7.1, for the confirmation method:

public function confirmation( $confirmation, $form, $entry, $ajax ) {
if ( empty( $this->redirect_url ) ) {
return $confirmation;
}

$confirmation = array( 'redirect' => $this->redirect_url );

return $confirmation;
}

The default logic says: overwrite the confirmation for all forms if the redirect URL is defined, no matter what.

This logic kicks in just by activating the add-on without creating or configuring any feed for the forms. Also, it directly checks the property and does not call the redirect_url().

The current default forces everybody to overwrite the confirmation method and add some basic checks. And this beats the purpose of providing handy basic functionalities.

I think, by default, at a minimum, it should check if the form has a payment feed active and properly configured. Because nobody wants to redirect to the payment service URL on a completely unrelated form, let's say, a contact or newsletter form.

Back to all posts

implenton is my (Mészáros Róbert) online web development-related presence, playground, and the name of my company of one.