Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove empty issuers field during form render. #48

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
175 changes: 87 additions & 88 deletions src/IssuersField.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
use GF_Field_Select;
use Pronamic\IDealIssuers\IDealIssuerCode;
use Pronamic\IDealIssuers\IDealIssuerService;
use Pronamic\WordPress\Pay\Core\Gateway;
use Pronamic\WordPress\Pay\Fields\IDealIssuerSelectField;
use Pronamic\WordPress\Pay\Core\PaymentMethods;
use Pronamic\WordPress\Pay\Fields\IDealIssuerSelectField;
use Pronamic\WordPress\Pay\Fields\SelectFieldOption;
use Pronamic\WordPress\Pay\Plugin;

/**
Expand Down Expand Up @@ -132,124 +132,121 @@ public function get_form_editor_field_icon() {
}

/**
* Get the iDEAL gateway for this field.
* Set the issuer choices for this issuers field.
*
* @return null|Gateway
* @param int $form_id Gravity Forms form ID.
*/
private function get_gateway() {
$gateway = null;
private function set_choices( $form_id ) {
$this->choices = [];

if ( isset( $this->pronamicPayConfigId ) && ! empty( $this->pronamicPayConfigId ) ) {
$gateway = Plugin::get_gateway( $this->pronamicPayConfigId );
// Prevent HTTP requests in forms list.
if ( \doing_filter( 'gform_form_actions' ) ) {
return;
}

if ( ! $gateway ) {
$feeds = FeedsDB::get_feeds_by_form_id( $this->formId );
$options = $this->get_ideal_issuer_select_field_options();

foreach ( $feeds as $feed ) {
// Check if feed is active.
if ( '0' === get_post_meta( $feed->id, '_pronamic_pay_gf_feed_active', true ) ) {
continue;
}

$gateway = Plugin::get_gateway( $feed->config_id );
if ( null === $options ) {
/*
* When an issuers field is marked as required and there are no choices,
* validation of the form submission will fail. However, a hosted payment
* page of the payment gateway might still be able to process the payment.
* Therefore, we fall back to a static list of iDEAL issuers in these cases.
*
* @link https://github.com/pronamic/wp-pronamic-pay-gravityforms/issues/47
*/
$ideal_issuer_service = new IDealIssuerService();

if ( null === $gateway ) {
continue;
}
$issuers = $ideal_issuer_service->get_issuers();

// Always use iDEAL payment method for issuer field.
$issuer_field = $gateway->first_payment_method_field( PaymentMethods::IDEAL, IDealIssuerSelectField::class );
$options = [];

if ( null === $issuer_field ) {
continue;
}
foreach ( $issuers as $issuer ) {
$options[] = new SelectFieldOption( $issuer->code, $issuer->name );
}
}

/**
* The iDEAL issuer field options can be requested from the
* gateway and that can result in exceptions. In this case,
* that's no problem and we'll move on to the next
* feed/gateway.
*
* @link https://github.com/pronamic/wp-pronamic-pay-gravityforms/issues/10
*/
try {
$options = $issuer_field->get_options();
} catch ( \Exception $e ) {
continue;
}
foreach ( $options as $option ) {
/**
* Gravity Forms automatically fills an empty value with the label.
* For a first empty choice option, Gravity Forms works with a
* `placeholder` property.
*
* @link https://github.com/pronamic/wp-pronamic-pay-gravityforms/issues/19
*/
if ( '' === $option->value ) {
$this->placeholder = $option->label;

return $gateway;
continue;
}
}

return $gateway;
$this->choices[] = [
'value' => $option->value,
'text' => $option->label,
];
}
}

/**
* Set the issuer choices for this issuers field.
* Get the iDEAL issuer select field options from gateway for this field.
*
* @param int $form_id Gravity Forms form ID.
* @return SelectFieldOption[]|null
*/
private function set_choices( $form_id ) {
$this->choices = [];
private function get_ideal_issuer_select_field_options() {
$config_ids = null;

// Prevent HTTP requests in forms list.
if ( \doing_filter( 'gform_form_actions' ) ) {
return;
if ( isset( $this->pronamicPayConfigId ) && ! empty( $this->pronamicPayConfigId ) ) {
$config_ids = [
$this->pronamicPayConfigId,
];
}

$gateway = $this->get_gateway();
if ( null === $config_ids ) {
$feeds = \array_filter(
FeedsDB::get_feeds_by_form_id( $this->formId ),
function ( $feed ) {
// Check if feed is active.
return '0' !== \get_post_meta( $feed->id, '_pronamic_pay_gf_feed_active', true );
}
);

if ( ! $gateway ) {
return;
$config_ids = \wp_list_pluck( $feeds, 'config_id' );
}

// Always use iDEAL payment method for issuer field.
$issuer_field = $gateway->first_payment_method_field( PaymentMethods::IDEAL, IDealIssuerSelectField::class );
foreach ( $config_ids as $config_id ) {
$gateway = Plugin::get_gateway( $config_id );

if ( null === $issuer_field ) {
return;
}
if ( null === $gateway ) {
continue;
}

$issuer_field = $gateway->first_payment_method_field( PaymentMethods::IDEAL, IDealIssuerSelectField::class );

if ( null === $issuer_field ) {
continue;
}

/**
* The iDEAL issuer field options can be requested from the
* gateway and that can result in exceptions. In this case,
* that's no problem and we'll move on to the next
* feed/gateway.
*
* @link https://github.com/pronamic/wp-pronamic-pay-gravityforms/issues/10
*/
try {
/**
* Gravity Forms has no support for <optgroup> elements.
* Exceptions can occur when requesting iDEAL issuer field options,
* but we'll just move on to the next feed/gateway.
*
* @link https://github.com/pronamic/wp-pronamic-pay/issues/154#issuecomment-1183309350
* @link https://github.com/pronamic/wp-pronamic-pay-gravityforms/issues/10
*/
$options = $issuer_field->get_flat_options();

foreach ( $options as $option ) {
try {
/**
* Gravity Forms automatically fills an empty value with the label.
* For a first empty choice option, Gravity Forms works with a
* `placeholder` property.
* Gravity Forms has no support for <optgroup> elements.
*
* @link https://github.com/pronamic/wp-pronamic-pay-gravityforms/issues/19
* @link https://github.com/pronamic/wp-pronamic-pay/issues/154#issuecomment-1183309350
*/
if ( '' === $option->value ) {
$this->placeholder = $option->label;

continue;
}

$this->choices[] = [
'value' => $option->value,
'text' => $option->label,
];
$options = $issuer_field->get_flat_options();
} catch ( \Exception $e ) {
continue;
}
} catch ( \Exception $e ) {
return;

return $options;
}

return null;
}

/**
Expand Down Expand Up @@ -596,8 +593,10 @@ public function get_field_input( $form, $value = '', $entry = null ) {
$input = $link . $input;
}

if ( ! empty( $feeds ) && empty( $this->choices ) ) {
// If there are feeds and no choices it's very likely this field is no supported by the gateway.
$options = $this->get_ideal_issuer_select_field_options();

if ( ! empty( $feeds ) && null === $options ) {
// If there are feeds but no gateway issuer options, it's very likely this field is not supported by the gateway.
$error = sprintf(
'<p class="pronamic-pay-error"><strong>%s</strong><br><em>%s</em></p>',
__( 'This field is not supported by your payment gateway.', 'pronamic_ideal' ),
Expand Down
Loading