Skip to content

Commit 3dd8453

Browse files
authored
Merge pull request #48 from pronamic/47-prevent-validation-issues-with-issuers-fields-without-choices
Fallback to iDEAL issuer service on frontend if gateway does not provide issuers, to prevent validation issues.
2 parents 9225108 + 17f30a4 commit 3dd8453

File tree

1 file changed

+87
-88
lines changed

1 file changed

+87
-88
lines changed

src/IssuersField.php

+87-88
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
use GF_Field_Select;
1414
use Pronamic\IDealIssuers\IDealIssuerCode;
1515
use Pronamic\IDealIssuers\IDealIssuerService;
16-
use Pronamic\WordPress\Pay\Core\Gateway;
17-
use Pronamic\WordPress\Pay\Fields\IDealIssuerSelectField;
1816
use Pronamic\WordPress\Pay\Core\PaymentMethods;
17+
use Pronamic\WordPress\Pay\Fields\IDealIssuerSelectField;
18+
use Pronamic\WordPress\Pay\Fields\SelectFieldOption;
1919
use Pronamic\WordPress\Pay\Plugin;
2020

2121
/**
@@ -132,124 +132,121 @@ public function get_form_editor_field_icon() {
132132
}
133133

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

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

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

149-
foreach ( $feeds as $feed ) {
150-
// Check if feed is active.
151-
if ( '0' === get_post_meta( $feed->id, '_pronamic_pay_gf_feed_active', true ) ) {
152-
continue;
153-
}
154-
155-
$gateway = Plugin::get_gateway( $feed->config_id );
149+
if ( null === $options ) {
150+
/*
151+
* When an issuers field is marked as required and there are no choices,
152+
* validation of the form submission will fail. However, a hosted payment
153+
* page of the payment gateway might still be able to process the payment.
154+
* Therefore, we fall back to a static list of iDEAL issuers in these cases.
155+
*
156+
* @link https://github.com/pronamic/wp-pronamic-pay-gravityforms/issues/47
157+
*/
158+
$ideal_issuer_service = new IDealIssuerService();
156159

157-
if ( null === $gateway ) {
158-
continue;
159-
}
160+
$issuers = $ideal_issuer_service->get_issuers();
160161

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

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

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

182-
return $gateway;
180+
continue;
183181
}
184-
}
185182

186-
return $gateway;
183+
$this->choices[] = [
184+
'value' => $option->value,
185+
'text' => $option->label,
186+
];
187+
}
187188
}
188189

189190
/**
190-
* Set the issuer choices for this issuers field.
191+
* Get the iDEAL issuer select field options from gateway for this field.
191192
*
192-
* @param int $form_id Gravity Forms form ID.
193+
* @return SelectFieldOption[]|null
193194
*/
194-
private function set_choices( $form_id ) {
195-
$this->choices = [];
195+
private function get_ideal_issuer_select_field_options() {
196+
$config_ids = null;
196197

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

202-
$gateway = $this->get_gateway();
204+
if ( null === $config_ids ) {
205+
$feeds = \array_filter(
206+
FeedsDB::get_feeds_by_form_id( $this->formId ),
207+
function ( $feed ) {
208+
// Check if feed is active.
209+
return '0' !== \get_post_meta( $feed->id, '_pronamic_pay_gf_feed_active', true );
210+
}
211+
);
203212

204-
if ( ! $gateway ) {
205-
return;
213+
$config_ids = \wp_list_pluck( $feeds, 'config_id' );
206214
}
207215

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

211-
if ( null === $issuer_field ) {
212-
return;
213-
}
219+
if ( null === $gateway ) {
220+
continue;
221+
}
222+
223+
$issuer_field = $gateway->first_payment_method_field( PaymentMethods::IDEAL, IDealIssuerSelectField::class );
224+
225+
if ( null === $issuer_field ) {
226+
continue;
227+
}
214228

215-
/**
216-
* The iDEAL issuer field options can be requested from the
217-
* gateway and that can result in exceptions. In this case,
218-
* that's no problem and we'll move on to the next
219-
* feed/gateway.
220-
*
221-
* @link https://github.com/pronamic/wp-pronamic-pay-gravityforms/issues/10
222-
*/
223-
try {
224229
/**
225-
* Gravity Forms has no support for <optgroup> elements.
230+
* Exceptions can occur when requesting iDEAL issuer field options,
231+
* but we'll just move on to the next feed/gateway.
226232
*
227-
* @link https://github.com/pronamic/wp-pronamic-pay/issues/154#issuecomment-1183309350
233+
* @link https://github.com/pronamic/wp-pronamic-pay-gravityforms/issues/10
228234
*/
229-
$options = $issuer_field->get_flat_options();
230-
231-
foreach ( $options as $option ) {
235+
try {
232236
/**
233-
* Gravity Forms automatically fills an empty value with the label.
234-
* For a first empty choice option, Gravity Forms works with a
235-
* `placeholder` property.
237+
* Gravity Forms has no support for <optgroup> elements.
236238
*
237-
* @link https://github.com/pronamic/wp-pronamic-pay-gravityforms/issues/19
239+
* @link https://github.com/pronamic/wp-pronamic-pay/issues/154#issuecomment-1183309350
238240
*/
239-
if ( '' === $option->value ) {
240-
$this->placeholder = $option->label;
241-
242-
continue;
243-
}
244-
245-
$this->choices[] = [
246-
'value' => $option->value,
247-
'text' => $option->label,
248-
];
241+
$options = $issuer_field->get_flat_options();
242+
} catch ( \Exception $e ) {
243+
continue;
249244
}
250-
} catch ( \Exception $e ) {
251-
return;
245+
246+
return $options;
252247
}
248+
249+
return null;
253250
}
254251

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

599-
if ( ! empty( $feeds ) && empty( $this->choices ) ) {
600-
// If there are feeds and no choices it's very likely this field is no supported by the gateway.
596+
$options = $this->get_ideal_issuer_select_field_options();
597+
598+
if ( ! empty( $feeds ) && null === $options ) {
599+
// If there are feeds but no gateway issuer options, it's very likely this field is not supported by the gateway.
601600
$error = sprintf(
602601
'<p class="pronamic-pay-error"><strong>%s</strong><br><em>%s</em></p>',
603602
__( 'This field is not supported by your payment gateway.', 'pronamic_ideal' ),

0 commit comments

Comments
 (0)