Skip to content

Commit d0ed935

Browse files
saulmoralespaCopilot
andcommitted
Add payment type mapping and validation for WooCommerce gateways
Co-authored-by: Copilot <copilot@github.com>
1 parent 8d2cfc4 commit d0ed935

6 files changed

Lines changed: 592 additions & 29 deletions

includes/admin/other_settings.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,15 @@
8181
'payment_gateways_mapping' => array(
8282
'title' => __( 'Mapeo de métodos de pago' ),
8383
'type' => 'payment_mappings_table',
84-
'payment_methods' => Integration_Alegra_WC::PAYMENTS_METHODS,
84+
'payment_methods' => Integration_Alegra_WC::PAYMENT_METHODS_UI,
85+
'payment_types' => [
86+
Integration_Alegra_WC::PAYMENT_TYPE_CASH => 'Contado',
87+
Integration_Alegra_WC::PAYMENT_TYPE_CREDIT => 'Crédito',
88+
],
8589
'bank_accounts' => $bank_accounts,
8690
'active_gateways' => $active_gateways,
8791
'all_gateways' => $all_gateways,
88-
'description' => __( 'Debe configurar método de pago y cuenta bancaria para cada gateway activo. Los gateways inactivos con mapeo guardado también se muestran para edición.' ),
92+
'description' => __( 'Debe configurar método de pago, cuenta bancaria y tipo de pago para cada gateway activo. Los gateways inactivos con mapeo guardado también se muestran para edición.' ),
8993
'desc_tip' => false
9094
),
9195
'client' => array(

includes/class-alegra-integration-wc.php

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ public function generate_payment_mappings_table_html($key, $data): string
123123
'bank_accounts' => [],
124124
'active_gateways' => [],
125125
'all_gateways' => [],
126+
'payment_types' => [ Integration_Alegra_WC::PAYMENT_TYPE_CASH => Integration_Alegra_WC::PAYMENT_TYPE_CASH, Integration_Alegra_WC::PAYMENT_TYPE_CREDIT => Integration_Alegra_WC::PAYMENT_TYPE_CREDIT ],
126127
];
127128

128129
$data = wp_parse_args($data, $defaults);
@@ -144,15 +145,18 @@ public function generate_payment_mappings_table_html($key, $data): string
144145
<thead>
145146
<tr>
146147
<th><?php echo esc_html__('Gateway WooCommerce', 'integration-alegra-woo'); ?></th>
147-
<th><?php echo esc_html__('Método de pago en Alegra', 'integration-alegra-woo'); ?></th>
148+
<th><?php echo esc_html__('Tipo de pago', 'integration-alegra-woo'); ?></th>
148149
<th><?php echo esc_html__('Cuenta bancaria en Alegra', 'integration-alegra-woo'); ?></th>
150+
<th><?php echo esc_html__('Forma de pago', 'integration-alegra-woo'); ?></th>
149151
</tr>
150152
</thead>
151153
<tbody>
152154
<?php foreach ($rows as $gateway_id => $gateway_data): ?>
153155
<?php
154-
$saved_method = $saved_mappings[$gateway_id]['payment_method'] ?? '';
156+
$saved_ubl_method = $saved_mappings[$gateway_id]['payment_method'] ?? '';
157+
$saved_method = $saved_ubl_method;
155158
$saved_account = $saved_mappings[$gateway_id]['account_id'] ?? '';
159+
$saved_type = $saved_mappings[$gateway_id]['payment_type'] ?? '';
156160
$gateway_label = $gateway_data['label'];
157161
if (!$gateway_data['active']) {
158162
$gateway_label .= ' (' . __('inactivo', 'integration-alegra-woo') . ')';
@@ -184,6 +188,16 @@ public function generate_payment_mappings_table_html($key, $data): string
184188
<?php endforeach; ?>
185189
</select>
186190
</td>
191+
<td>
192+
<select name="<?php echo esc_attr(sprintf('%s[%s][payment_type]', $field_key, $gateway_id)); ?>">
193+
<option value=""><?php echo esc_html__('Seleccionar tipo...', 'integration-alegra-woo'); ?></option>
194+
<?php foreach ($data['payment_types'] as $type_key => $type_label): ?>
195+
<option value="<?php echo esc_attr($type_key); ?>" <?php selected($saved_type, $type_key); ?>>
196+
<?php echo esc_html($type_label); ?>
197+
</option>
198+
<?php endforeach; ?>
199+
</select>
200+
</td>
187201
</tr>
188202
<?php endforeach; ?>
189203
</tbody>
@@ -241,12 +255,13 @@ public function validate_payment_mappings_table_field($key, $value): array
241255
$gateway_map = $sanitized_mapping[$gateway_id] ?? [
242256
'payment_method' => '',
243257
'account_id' => '',
258+
'payment_type' => '',
244259
];
245260

246-
if (!$gateway_map['payment_method'] || !$gateway_map['account_id']) {
261+
if (!$gateway_map['payment_method'] || !$gateway_map['account_id'] || !$gateway_map['payment_type']) {
247262
WC_Admin_Settings::add_error(
248263
sprintf(
249-
__('Integration Alegra Woocommerce: Debe configurar método de pago y cuenta bancaria para el gateway activo "%s".', 'integration-alegra-woo'),
264+
__('Integration Alegra Woocommerce: Debe configurar método de pago, cuenta bancaria y tipo de pago para el gateway activo "%s".', 'integration-alegra-woo'),
250265
$gateway_title
251266
)
252267
);
@@ -273,7 +288,10 @@ public function validate_payment_mappings_table_field($key, $value): array
273288
continue;
274289
}
275290

276-
if (!isset(Integration_Alegra_WC::PAYMENTS_METHODS[$payment_method])) {
291+
// Accept only UBL catalog codes.
292+
$is_ubl = isset( Integration_Alegra_WC::PAYMENTS_METHODS[ $payment_method ] );
293+
294+
if ( ! $is_ubl ) {
277295
WC_Admin_Settings::add_error(
278296
sprintf(
279297
__('Integration Alegra Woocommerce: El método de pago "%s" no es válido para el gateway "%s".', 'integration-alegra-woo'),
@@ -297,9 +315,22 @@ public function validate_payment_mappings_table_field($key, $value): array
297315
continue;
298316
}
299317

318+
$payment_type = $gateway_map['payment_type'] ?? '';
319+
if (!in_array($payment_type, [ Integration_Alegra_WC::PAYMENT_TYPE_CASH, Integration_Alegra_WC::PAYMENT_TYPE_CREDIT ], true)) {
320+
WC_Admin_Settings::add_error(
321+
sprintf(
322+
__('Integration Alegra Woocommerce: El tipo de pago para el gateway "%s" debe ser CASH o CREDIT.', 'integration-alegra-woo'),
323+
$gateway_id
324+
)
325+
);
326+
$has_errors = true;
327+
continue;
328+
}
329+
300330
$validated_mapping[$gateway_id] = [
301331
'payment_method' => $payment_method,
302-
'account_id' => $account_id,
332+
'account_id' => $account_id,
333+
'payment_type' => $payment_type,
303334
];
304335
}
305336

@@ -318,6 +349,8 @@ private function sanitize_payment_gateways_mapping_input($value): array
318349
return $sanitized;
319350
}
320351

352+
$valid_payment_types = [ Integration_Alegra_WC::PAYMENT_TYPE_CASH, Integration_Alegra_WC::PAYMENT_TYPE_CREDIT ];
353+
321354
foreach ($value as $gateway_id => $gateway_map) {
322355
if (!is_array($gateway_map)) {
323356
continue;
@@ -329,9 +362,12 @@ private function sanitize_payment_gateways_mapping_input($value): array
329362
continue;
330363
}
331364

365+
$raw_payment_type = sanitize_text_field((string) ($gateway_map['payment_type'] ?? ''));
366+
332367
$sanitized[$sanitized_gateway_id] = [
333368
'payment_method' => sanitize_text_field((string) ($gateway_map['payment_method'] ?? '')),
334-
'account_id' => sanitize_text_field((string) ($gateway_map['account_id'] ?? '')),
369+
'account_id' => sanitize_text_field((string) ($gateway_map['account_id'] ?? '')),
370+
'payment_type' => in_array($raw_payment_type, $valid_payment_types, true) ? $raw_payment_type : '',
335371
];
336372
}
337373

0 commit comments

Comments
 (0)