Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/Refund/Request/AbstractBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
Expand Down
19 changes: 11 additions & 8 deletions src/Refund/Request/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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';
Expand Down
61 changes: 50 additions & 11 deletions src/Refund/Request/PaymentMethodHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,26 @@

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.
*
* @param string $method The payment method to check.
* @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',
Expand All @@ -36,7 +49,7 @@ public static function isCreditCardMethod(string $method): bool {
'postepay',
];

return in_array($method, $creditCardMethods);
return in_array($method, $creditCardMethods, true);
}

/**
Expand All @@ -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));
}
}
Loading