Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: eligibility return type & Bridge return types #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
51 changes: 40 additions & 11 deletions src/Bridge/AlmaBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@


use Alma\API\Client;
use Alma\API\Endpoints\Results\Eligibility;
use Alma\API\Entities\Instalment;
use Alma\API\Entities\Merchant;
use Alma\API\Entities\Payment;
Expand All @@ -23,7 +24,7 @@
final class AlmaBridge implements AlmaBridgeInterface
{
/**
* @var GatewayConfig
* @var null|GatewayConfig
*/
private $gatewayConfig = null;

Expand All @@ -47,19 +48,33 @@ public function __construct(LoggerInterface $logger, PaymentDataBuilderInterface
$this->paymentDataBuilder = $paymentDataBuilder;
}

/**
* @param Eligibility|Eligibility[] $eligibility
*
* @return Eligibility[]
*/
private function formatEligibility($eligibility): array
{
if ($eligibility instanceof Eligibility) {
return [$eligibility];
}

return $eligibility;
}

public function initialize(ArrayObject $config): void
{
self::$almaClient = null;
$this->gatewayConfig = new GatewayConfig($config);
}

public function getDefaultClient(?string $mode = null): Client
public function getDefaultClient(?string $mode = null): ?Client
{
if ($mode === null) {
if ($mode === null && $this->gatewayConfig) {
$mode = $this->gatewayConfig->getApiMode();
}

if (!self::$almaClient) {
if (!self::$almaClient && $mode && $this->gatewayConfig) {
self::$almaClient = self::createClientInstance(
$this->gatewayConfig->getActiveApiKey(),
$mode,
Expand Down Expand Up @@ -110,9 +125,9 @@ function getMerchantInfo(): ?Merchant
}

/**
* @return GatewayConfig
* @return GatewayConfig|null
*/
public function getGatewayConfig(): GatewayConfig
public function getGatewayConfig(): ?GatewayConfig
{
return $this->gatewayConfig;
}
Expand All @@ -124,16 +139,20 @@ function getEligibilities(PaymentInterface $payment, array $installmentsCounts):
{
$builder = $this->paymentDataBuilder;
$builder->addBuilder(function (array $data, PaymentInterface $payment) use ($installmentsCounts): array {
$data['payment'] = array_merge($data['payment'], [
"installments_count" => $installmentsCounts
]);
if (is_array($data['payment'])) {
$data['payment'] += ["installments_count" => $installmentsCounts];
}

return $data;
});

$alma = $this->getDefaultClient();
try {
return $alma->payments->eligibility($builder([], $payment), true);
if ($alma) {
$eligibility = $alma->payments->eligibility($builder([], $payment), true);

return $this->formatEligibility($eligibility);
}
} catch (RequestError $e) {
$this->logger->error("[Alma] Eligibility call failed with error: " . $e->getMessage());
}
Expand All @@ -152,13 +171,23 @@ public function validatePayment(
/** @var int $pid */
$pid = $payment->getId();

$alma = $this->getDefaultClient();
try {
$paymentData = $this->getDefaultClient()->payments->fetch($almaPaymentId);
if ($alma) {
$paymentData = $alma->payments->fetch($almaPaymentId);
}
} catch (RequestError $e) {
$this->logger->error("[Alma] Could not fetch payment $almaPaymentId to validate payment $pid");
throw $e;
}

if (!$paymentData) {
$error = "PaymentData not found for payment $pid";
$this->logger->error("[Alma] $error");

throw new PaymentException($error);
}

if ($pid !== $paymentData->custom_data['payment_id']) {
$error = "Attempt to validate payment $pid with Alma payment $almaPaymentId";
$this->logger->error("[Alma] $error");
Expand Down
2 changes: 1 addition & 1 deletion src/Bridge/AlmaBridgeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ interface AlmaBridgeInterface

function initialize(ArrayObject $config): void;

function getGatewayConfig(): GatewayConfig;
function getGatewayConfig(): ?GatewayConfig;

function getDefaultClient(?string $mode = null): ?Client;
static function createClientInstance(string $apiKey, string $mode, LoggerInterface $logger): ?Client;
Expand Down
11 changes: 11 additions & 0 deletions src/Bridge/PaymentException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Alma\SyliusPaymentPlugin\Bridge;

use RuntimeException;

final class PaymentException extends RuntimeException
{
}