Skip to content

Commit

Permalink
Improve finding the iDEAL issuer based on the label.
Browse files Browse the repository at this point in the history
  • Loading branch information
remcotolsma committed Jun 7, 2024
1 parent 1ed7afa commit a302835
Showing 1 changed file with 55 additions and 16 deletions.
71 changes: 55 additions & 16 deletions src/IssuersField.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
namespace Pronamic\WordPress\Pay\Extensions\GravityForms;

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\Plugin;
use Pronamic\WpPayLogos\ImageService;

/**
* Title: WordPress pay extension Gravity Forms issuers field
Expand Down Expand Up @@ -251,6 +252,42 @@ private function set_choices( $form_id ) {
}
}

/**
* Get iDEAL issuer code from label.
*
* @param string $label Label.
* @return string|null
*/
private function get_ideal_issuer_code_from_label( $label ) {
$label = \strtolower( $label );

$map = [
'abn' => IDealIssuerCode::ABNANL2A,
'asn' => IDealIssuerCode::ASNBNL21,
'bunq' => IDealIssuerCode::BUNQNL2A,
'ing' => IDealIssuerCode::INGBNL2A,
'knab' => IDealIssuerCode::KNABNL2H,
'n26' => IDealIssuerCode::NTSBDEB1,
'nationale' => IDealIssuerCode::NNBANL2G,
'nn' => IDealIssuerCode::NNBANL2G,
'rabobank' => IDealIssuerCode::RABONL2U,
'regiobank' => IDealIssuerCode::RBRBNL21,
'revolut' => IDealIssuerCode::REVOLT21,
'sns' => IDealIssuerCode::SNSBNL2A,
'triodos' => IDealIssuerCode::TRIONL2U,
'lanschot' => IDealIssuerCode::FVLBNL22,
'yoursafe' => IDealIssuerCode::BITSNL2A,
];

foreach ( $map as $needle => $ideal_issuer_code ) {
if ( \str_contains( $label, $needle ) ) {
return $ideal_issuer_code;
}
}

return null;
}

/**
* Get the field input.
*
Expand Down Expand Up @@ -286,12 +323,6 @@ public function get_field_input( $form, $value = '', $entry = null ) {
<ul class="gfield_radio input_<?php echo esc_attr( $this->formId ); ?>_<?php echo esc_attr( $this->id ); ?>">
<?php

// Icon filename replacements.
$replacements = [
' bankiers' => '',
' ' => '-',
];

// Icon file and size.
switch ( $this->pronamicPayDisplayMode ) {
case 'icons-24':
Expand All @@ -313,7 +344,9 @@ public function get_field_input( $form, $value = '', $entry = null ) {
$image_variation = '640x360.svg';
}

$image_service = new ImageService();
$ideal_issuer_service = new IDealIssuerService();

$ideal_issuers = $ideal_issuer_service->get_issuers();

// Loop issuers.
foreach ( $this->choices as $choice ) {
Expand All @@ -322,19 +355,25 @@ public function get_field_input( $form, $value = '', $entry = null ) {
continue;
}

// Icon file name.
$issuer = strtr( strtolower( $choice['text'] ), $replacements );
$label = $choice['text'];

$label_content = sprintf( '<span>%s</span>', esc_html( $choice['text'] ) );

$ideal_issuer = null;

$ideal_issuer_code = $this->get_ideal_issuer_code_from_label( $label );

if ( false !== stripos( $issuer, 'test' ) || false !== stripos( $issuer, 'simulation' ) ) {
$issuer = 'test';
if ( null !== $ideal_issuer_code && \array_key_exists( $ideal_issuer_code->value, $ideal_issuers->items ) ) {
$ideal_issuer = $ideal_issuers->items[ $ideal_issuer_code->value ];
}

// Radio input.
$label_content = sprintf( '<span>%s</span>', esc_html( $choice['text'] ) );
$image_path = null;

$image_path = $image_service->get_path( "ideal-issuers/$issuer/ideal-issuer-$issuer-$image_variation" );
if ( null !== $ideal_issuer && \array_key_exists( $image_variation, $ideal_issuer->images ) ) {
$image_path = $ideal_issuer->images[ $image_variation ];
}

if ( file_exists( $image_path ) ) {
if ( null !== $image_path && \file_exists( $image_path ) ) {
$image_url = \plugins_url( \basename( $image_path ), $image_path );

$label_content = \sprintf(
Expand Down

0 comments on commit a302835

Please sign in to comment.