|
13 | 13 | use GF_Field_Select;
|
14 | 14 | use Pronamic\IDealIssuers\IDealIssuerCode;
|
15 | 15 | use Pronamic\IDealIssuers\IDealIssuerService;
|
16 |
| -use Pronamic\WordPress\Pay\Core\Gateway; |
17 |
| -use Pronamic\WordPress\Pay\Fields\IDealIssuerSelectField; |
18 | 16 | use Pronamic\WordPress\Pay\Core\PaymentMethods;
|
| 17 | +use Pronamic\WordPress\Pay\Fields\IDealIssuerSelectField; |
| 18 | +use Pronamic\WordPress\Pay\Fields\SelectFieldOption; |
19 | 19 | use Pronamic\WordPress\Pay\Plugin;
|
20 | 20 |
|
21 | 21 | /**
|
@@ -132,124 +132,121 @@ public function get_form_editor_field_icon() {
|
132 | 132 | }
|
133 | 133 |
|
134 | 134 | /**
|
135 |
| - * Get the iDEAL gateway for this field. |
| 135 | + * Set the issuer choices for this issuers field. |
136 | 136 | *
|
137 |
| - * @return null|Gateway |
| 137 | + * @param int $form_id Gravity Forms form ID. |
138 | 138 | */
|
139 |
| - private function get_gateway() { |
140 |
| - $gateway = null; |
| 139 | + private function set_choices( $form_id ) { |
| 140 | + $this->choices = []; |
141 | 141 |
|
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; |
144 | 145 | }
|
145 | 146 |
|
146 |
| - if ( ! $gateway ) { |
147 |
| - $feeds = FeedsDB::get_feeds_by_form_id( $this->formId ); |
| 147 | + $options = $this->get_ideal_issuer_select_field_options(); |
148 | 148 |
|
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(); |
156 | 159 |
|
157 |
| - if ( null === $gateway ) { |
158 |
| - continue; |
159 |
| - } |
| 160 | + $issuers = $ideal_issuer_service->get_issuers(); |
160 | 161 |
|
161 |
| - // Always use iDEAL payment method for issuer field. |
162 |
| - $issuer_field = $gateway->first_payment_method_field( PaymentMethods::IDEAL, IDealIssuerSelectField::class ); |
| 162 | + $options = []; |
163 | 163 |
|
164 |
| - if ( null === $issuer_field ) { |
165 |
| - continue; |
166 |
| - } |
| 164 | + foreach ( $issuers as $issuer ) { |
| 165 | + $options[] = new SelectFieldOption( $issuer->code, $issuer->name ); |
| 166 | + } |
| 167 | + } |
167 | 168 |
|
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; |
181 | 179 |
|
182 |
| - return $gateway; |
| 180 | + continue; |
183 | 181 | }
|
184 |
| - } |
185 | 182 |
|
186 |
| - return $gateway; |
| 183 | + $this->choices[] = [ |
| 184 | + 'value' => $option->value, |
| 185 | + 'text' => $option->label, |
| 186 | + ]; |
| 187 | + } |
187 | 188 | }
|
188 | 189 |
|
189 | 190 | /**
|
190 |
| - * Set the issuer choices for this issuers field. |
| 191 | + * Get the iDEAL issuer select field options from gateway for this field. |
191 | 192 | *
|
192 |
| - * @param int $form_id Gravity Forms form ID. |
| 193 | + * @return SelectFieldOption[]|null |
193 | 194 | */
|
194 |
| - private function set_choices( $form_id ) { |
195 |
| - $this->choices = []; |
| 195 | + private function get_ideal_issuer_select_field_options() { |
| 196 | + $config_ids = null; |
196 | 197 |
|
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 | + ]; |
200 | 202 | }
|
201 | 203 |
|
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 | + ); |
203 | 212 |
|
204 |
| - if ( ! $gateway ) { |
205 |
| - return; |
| 213 | + $config_ids = \wp_list_pluck( $feeds, 'config_id' ); |
206 | 214 | }
|
207 | 215 |
|
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 ); |
210 | 218 |
|
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 | + } |
214 | 228 |
|
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 { |
224 | 229 | /**
|
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. |
226 | 232 | *
|
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 |
228 | 234 | */
|
229 |
| - $options = $issuer_field->get_flat_options(); |
230 |
| - |
231 |
| - foreach ( $options as $option ) { |
| 235 | + try { |
232 | 236 | /**
|
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. |
236 | 238 | *
|
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 |
238 | 240 | */
|
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; |
249 | 244 | }
|
250 |
| - } catch ( \Exception $e ) { |
251 |
| - return; |
| 245 | + |
| 246 | + return $options; |
252 | 247 | }
|
| 248 | + |
| 249 | + return null; |
253 | 250 | }
|
254 | 251 |
|
255 | 252 | /**
|
@@ -596,8 +593,10 @@ public function get_field_input( $form, $value = '', $entry = null ) {
|
596 | 593 | $input = $link . $input;
|
597 | 594 | }
|
598 | 595 |
|
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. |
601 | 600 | $error = sprintf(
|
602 | 601 | '<p class="pronamic-pay-error"><strong>%s</strong><br><em>%s</em></p>',
|
603 | 602 | __( 'This field is not supported by your payment gateway.', 'pronamic_ideal' ),
|
|
0 commit comments