From 6a7719dd514501b32a7e69a552858a10f62d6bd8 Mon Sep 17 00:00:00 2001 From: "g.prenaj" Date: Mon, 4 May 2026 14:50:59 +0200 Subject: [PATCH] fix refunds for Fashioncheque --- src/Refund/Request/AbstractBuilder.php | 10 ++-- src/Refund/Request/Handler.php | 19 ++++--- src/Refund/Request/PaymentMethodHelper.php | 61 ++++++++++++++++++---- 3 files changed, 67 insertions(+), 23 deletions(-) diff --git a/src/Refund/Request/AbstractBuilder.php b/src/Refund/Request/AbstractBuilder.php index eb8c2f5af..d9103094d 100644 --- a/src/Refund/Request/AbstractBuilder.php +++ b/src/Refund/Request/AbstractBuilder.php @@ -79,17 +79,19 @@ private function getPushUrl(): string */ protected function buildIssuers(\Order $order, \OrderPayment $payment): array { - if (PaymentMethodHelper::isCreditCardMethod($payment->payment_method)) { + $paymentMethod = PaymentMethodHelper::normalizeMethod((string) $payment->payment_method); + + if (PaymentMethodHelper::isCreditCardMethod($paymentMethod)) { return [ - 'name' => $payment->payment_method, + 'name' => $paymentMethod, 'version' => 2, ]; } - if (PaymentMethodHelper::isGiftCardMethod($payment->payment_method)) { + if (PaymentMethodHelper::isGiftCardMethod($paymentMethod)) { $customer = new \Customer($order->id_customer); return [ - 'name' => $payment->payment_method, + 'name' => $paymentMethod, 'email' => $customer->email, 'lastname' => $customer->lastname, ]; diff --git a/src/Refund/Request/Handler.php b/src/Refund/Request/Handler.php index 9148d2554..c1dc58c89 100644 --- a/src/Refund/Request/Handler.php +++ b/src/Refund/Request/Handler.php @@ -38,15 +38,16 @@ class Handler */ public function refund(array $body, string $method): TransactionResponse { - $buckaroo = $this->getClient($method); - + $normalizedMethod = PaymentMethodHelper::normalizeMethod($method); + $buckaroo = $this->getClient($normalizedMethod); + // For gift cards, use 'giftcard' as the method name for SDK call // The actual service code (e.g., 'boekenbon') should be in the payload's 'name' field - $sdkMethod = $method; - if (PaymentMethodHelper::isGiftCardMethod($method)) { + $sdkMethod = $normalizedMethod; + if (PaymentMethodHelper::isGiftCardMethod($normalizedMethod)) { $sdkMethod = 'giftcard'; } - + return $buckaroo->method($sdkMethod)->refund($body); } @@ -60,11 +61,13 @@ public function refund(array $body, string $method): TransactionResponse */ private function getClient(string $method): BuckarooClient { + $normalizedMethod = PaymentMethodHelper::normalizeMethod($method); + // Normalize method for configuration lookup - $configMethod = $method; - if (PaymentMethodHelper::isCreditCardMethod($method)) { + $configMethod = $normalizedMethod; + if (PaymentMethodHelper::isCreditCardMethod($normalizedMethod)) { $configMethod = 'creditcard'; - } elseif (PaymentMethodHelper::isGiftCardMethod($method)) { + } elseif (PaymentMethodHelper::isGiftCardMethod($normalizedMethod)) { // For gift cards, use 'giftcard' for configuration lookup // but keep the original service code for the actual refund API call $configMethod = 'giftcard'; diff --git a/src/Refund/Request/PaymentMethodHelper.php b/src/Refund/Request/PaymentMethodHelper.php index 480016bd0..4595c6651 100644 --- a/src/Refund/Request/PaymentMethodHelper.php +++ b/src/Refund/Request/PaymentMethodHelper.php @@ -21,6 +21,17 @@ class PaymentMethodHelper { + /** + * Normalize payment method value for comparisons. + * + * @param string $method + * @return string + */ + public static function normalizeMethod(string $method): string + { + return strtolower(trim($method)); + } + /** * Check if the payment method is a type of credit card. * @@ -28,6 +39,8 @@ class PaymentMethodHelper * @return bool Returns true if the method is a type of credit card, false otherwise. */ public static function isCreditCardMethod(string $method): bool { + $method = self::normalizeMethod($method); + $creditCardMethods = [ 'creditcard', 'mastercard', 'visa', 'amex', 'vpay', 'maestro', @@ -36,7 +49,7 @@ public static function isCreditCardMethod(string $method): bool { 'postepay', ]; - return in_array($method, $creditCardMethods); + return in_array($method, $creditCardMethods, true); } /** @@ -46,25 +59,51 @@ public static function isCreditCardMethod(string $method): bool { * @return bool Returns true if the method is a gift card service code, false otherwise. */ public static function isGiftCardMethod(string $method): bool { + $method = self::normalizeMethod($method); + // First check if it's the generic giftcard method if ($method === 'giftcard') { return true; } - // Check if it's a specific gift card service code + return in_array($method, self::getGiftCardCodes(), true); + } + + /** + * Return known gift card service codes. + * + * Prefer DB values, but fall back to static list when table is unavailable + * to keep refund routing stable. + * + * @return array + */ + private static function getGiftCardCodes(): array + { + $giftCardRepository = new RawGiftCardsRepository(); + $giftCards = []; + try { - $giftCardRepository = new RawGiftCardsRepository(); $giftCards = $giftCardRepository->getGiftCardsFromDB(); - - foreach ($giftCards as $giftCard) { - if (isset($giftCard['code']) && $giftCard['code'] === $method) { - return true; - } - } } catch (\Exception $e) { - return false; + $giftCards = []; + } + + if (!is_array($giftCards) || empty($giftCards)) { + $giftCards = $giftCardRepository->getGiftCardsData(); + } + + $codes = []; + foreach ($giftCards as $giftCard) { + if (!isset($giftCard['code']) || !is_scalar($giftCard['code'])) { + continue; + } + + $code = self::normalizeMethod((string) $giftCard['code']); + if ($code !== '') { + $codes[] = $code; + } } - return false; + return array_values(array_unique($codes)); } } \ No newline at end of file