Skip to content

Commit

Permalink
Merge pull request #3 from Marvin-Magmodules/release/1.0.5
Browse files Browse the repository at this point in the history
Release/1.0.5
  • Loading branch information
stefandanaita authored May 9, 2023
2 parents 56cdeef + c2c3119 commit 981b8c7
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 84 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/setup-di-compile.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
name: Run setup:upgrade and setup:di:compile
on: [pull_request]
on: [ pull_request ]

jobs:
build:
strategy:
fail-fast: false
matrix:
include:
- PHP_VERSION: php74-fpm
MAGENTO_VERSION: 2.4.3-p1
- PHP_VERSION: php81-fpm
MAGENTO_VERSION: 2.4.4
- PHP_VERSION: php81-fpm
MAGENTO_VERSION: 2.4.5
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
Expand Down
8 changes: 8 additions & 0 deletions Api/User/RepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,12 @@ public function set($email, $userId): bool;
* @return mixed
*/
public function get($email);

/**
* Get user by id
*
* @param $userId
* @return mixed
*/
public function getByTruelayerId($userId);
}
8 changes: 8 additions & 0 deletions Model/User/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,12 @@ public function get($email)
{
return $this->resource->get($email);
}

/**
* @inheritDoc
*/
public function getByTruelayerId($userId)
{
return $this->resource->getByTruelayerId($userId);
}
}
14 changes: 14 additions & 0 deletions Model/User/ResourceModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,18 @@ public function set(string $email, string $userId): bool
['magento_email' => $email, 'truelayer_id' => $userId]
);
}

/**
* @param string $uuid
* @return mixed
*/
public function getByTruelayerId(string $userId)
{
$connection = $this->getConnection();
$select = $connection->select()
->from($this->getTable('truelayer_user'))
->where('truelayer_id = :userId');
$bind = [':userId' => $userId];
return $connection->fetchRow($select, $bind);
}
}
12 changes: 2 additions & 10 deletions Model/Webapi/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use TrueLayer\Connect\Api\Log\RepositoryInterface as LogRepository;
use TrueLayer\Connect\Api\Transaction\RepositoryInterface as TransactionRepository;
use TrueLayer\Connect\Api\Webapi\WebhookInterface;
use TrueLayer\Connect\Service\Api\GetGuzzleClient;
use TrueLayer\Connect\Service\Order\ProcessWebhook;
use TrueLayer\Exceptions\Exception;
use TrueLayer\Interfaces\Webhook as TrueLayerWebhookInterface;
Expand All @@ -35,10 +34,6 @@ class Webhook implements WebhookInterface
* @var ProcessWebhook
*/
private $processWebhook;
/**
* @var GetGuzzleClient
*/
private $getGuzzleClient;
/**
* @var ConfigRepository
*/
Expand All @@ -65,7 +60,6 @@ class Webhook implements WebhookInterface
*
* @param LogRepository $logRepository
* @param ProcessWebhook $processWebhook
* @param GetGuzzleClient $getGuzzleClient
* @param ConfigRepository $configProvider
* @param JsonSerializer $jsonSerializer
* @param File $file
Expand All @@ -75,7 +69,6 @@ class Webhook implements WebhookInterface
public function __construct(
LogRepository $logRepository,
ProcessWebhook $processWebhook,
GetGuzzleClient $getGuzzleClient,
ConfigRepository $configProvider,
JsonSerializer $jsonSerializer,
File $file,
Expand All @@ -84,7 +77,6 @@ public function __construct(
) {
$this->logRepository = $logRepository;
$this->processWebhook = $processWebhook;
$this->getGuzzleClient = $getGuzzleClient;
$this->configProvider = $configProvider;
$this->jsonSerializer = $jsonSerializer;
$this->file = $file;
Expand All @@ -97,16 +89,16 @@ public function __construct(
*/
public function processTransfer()
{
\TrueLayer\Settings::tlAgent('truelayer-magento/' . $this->configProvider->getExtensionVersion());
$webhook = TrueLayerWebhook::configure()
->httpClient($this->getGuzzleClient->execute())
->useProduction(!$this->configProvider->isSandbox($this->getStoreId()))
->create();

$webhook->handler(function (TrueLayerWebhookInterface\EventInterface $event) {
$this->logRepository->addDebugLog('Webhook', $event->getBody());
})->handler(function (TrueLayerWebhookInterface\PaymentSettledEventInterface $event) {
try {
$this->processWebhook->execute($event->getBody()['payment_id']);
$this->processWebhook->execute($event->getBody()['payment_id'], $event->getBody()['user_id']);
} catch (\Exception $exception) {
$this->logRepository->addErrorLog('Webhook processTransfer', $exception->getMessage());
throw new LocalizedException(__($exception->getMessage()));
Expand Down
11 changes: 2 additions & 9 deletions Service/Api/GetClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,19 @@ class GetClient
* @var array
*/
private $credentials;
/**
* @var GetGuzzleClient
*/
private $getGuzzleClient;

/**
* Adapter constructor.
*
* @param ConfigRepository $configProvider
* @param LogRepository $logRepository
* @param GetGuzzleClient $getGuzzleClient
*/
public function __construct(
ConfigRepository $configProvider,
LogRepository $logRepository,
GetGuzzleClient $getGuzzleClient
LogRepository $logRepository
) {
$this->configProvider = $configProvider;
$this->logRepository = $logRepository;
$this->getGuzzleClient = $getGuzzleClient;
}

/**
Expand All @@ -79,12 +72,12 @@ public function execute(int $storeId = 0, ?array $data = []): ?ClientInterface
private function getClient(): ?ClientInterface
{
try {
\TrueLayer\Settings::tlAgent('truelayer-magento/' . $this->configProvider->getExtensionVersion());
$client = Client::configure()
->clientId($this->credentials['client_id'])
->clientSecret($this->credentials['client_secret'])
->keyId($this->credentials['key_id'])
->pemFile($this->credentials['private_key'])
->httpClient($this->getGuzzleClient->execute())
->useProduction(!$this->configProvider->isSandbox());

return $client->create();
Expand Down
52 changes: 0 additions & 52 deletions Service/Api/GetGuzzleClient.php

This file was deleted.

14 changes: 12 additions & 2 deletions Service/Order/MakeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use TrueLayer\Connect\Api\Config\RepositoryInterface as ConfigRepository;
use TrueLayer\Connect\Api\Transaction\RepositoryInterface as TransactionRepository;
use TrueLayer\Connect\Api\User\RepositoryInterface as UserRepository;
use TrueLayer\Connect\Api\Log\RepositoryInterface as LogRepository;
use TrueLayer\Connect\Service\Api\GetClient;
use TrueLayer\Interfaces\Client\ClientInterface;

Expand Down Expand Up @@ -71,6 +72,10 @@ class MakeRequest
* @var UserRepository
*/
private $userRepository;
/**
* @var LogRepository
*/
private $logRepository;

private $truelayerUser = null;

Expand All @@ -87,6 +92,7 @@ class MakeRequest
* @param DataObjectProcessor $dataObjectProcessor
* @param DataObjectHelper $dataObjectHelper
* @param UserRepository $userRepository
* @param LogRepository $logRepository
*/
public function __construct(
ConfigRepository $configProvider,
Expand All @@ -98,7 +104,8 @@ public function __construct(
AddressFactory $quoteAddressFactory,
DataObjectProcessor $dataObjectProcessor,
DataObjectHelper $dataObjectHelper,
UserRepository $userRepository
UserRepository $userRepository,
LogRepository $logRepository
) {
$this->configProvider = $configProvider;
$this->checkoutSession = $checkoutSession;
Expand All @@ -110,6 +117,7 @@ public function __construct(
$this->dataObjectProcessor = $dataObjectProcessor;
$this->dataObjectHelper = $dataObjectHelper;
$this->userRepository = $userRepository;
$this->logRepository = $logRepository;
}

/**
Expand Down Expand Up @@ -202,7 +210,7 @@ private function prepareData(Quote $quote, string $merchantAccountId): array
$customerEmail = $quote->getBillingAddress()->getEmail() ?: $quote->getCustomerEmail();

$data = [
"amount_in_minor" => (int)($quote->getBaseGrandTotal() * 100),
"amount_in_minor" => (int)bcmul((string)$quote->getBaseGrandTotal(), '100'),
"currency" => $quote->getBaseCurrencyCode(),
"payment_method" => [
"provider_selection" => [
Expand Down Expand Up @@ -241,6 +249,8 @@ private function prepareData(Quote $quote, string $merchantAccountId): array
$data['user']['id'] = $this->truelayerUser['truelayer_id'];
}

$this->logRepository->addDebugLog('order request', $data);

return $data;
}

Expand Down
30 changes: 24 additions & 6 deletions Service/Order/ProcessWebhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use TrueLayer\Connect\Api\Config\RepositoryInterface as ConfigRepository;
use TrueLayer\Connect\Api\Log\RepositoryInterface as LogRepository;
use TrueLayer\Connect\Api\Transaction\RepositoryInterface as TransactionRepository;
use TrueLayer\Connect\Api\User\RepositoryInterface as UserRepository;

/**
* Class ProcessWebhook
Expand Down Expand Up @@ -64,8 +65,14 @@ class ProcessWebhook
* @var LogRepository
*/
private $logRepository;
/**
* @var UserRepository
*/
private $userRepository;

/**
* ProcessWebhook constructor.
*
* @param CartRepositoryInterface $quoteRepository
* @param TransactionRepository $transactionRepository
* @param CartManagementInterface $cartManagement
Expand All @@ -75,6 +82,7 @@ class ProcessWebhook
* @param ConfigRepository $configRepository
* @param LogRepository $logRepository
* @param CheckoutSession $checkoutSession
* @param UserRepository $userRepository
*/
public function __construct(
CartRepositoryInterface $quoteRepository,
Expand All @@ -85,7 +93,8 @@ public function __construct(
InvoiceSender $invoiceSender,
ConfigRepository $configRepository,
LogRepository $logRepository,
CheckoutSession $checkoutSession
CheckoutSession $checkoutSession,
UserRepository $userRepository
) {
$this->quoteRepository = $quoteRepository;
$this->transactionRepository = $transactionRepository;
Expand All @@ -96,14 +105,16 @@ public function __construct(
$this->configRepository = $configRepository;
$this->logRepository = $logRepository;
$this->checkoutSession = $checkoutSession;
$this->userRepository = $userRepository;
}

/**
* Place order via webhook
*
* @param string $uuid
* @param string $userId
*/
public function execute(string $uuid)
public function execute(string $uuid, string $userId)
{
$this->logRepository->addDebugLog('webhook payload uuid', $uuid);

Expand All @@ -128,7 +139,7 @@ public function execute(string $uuid)
$this->transactionRepository->lock($transaction);

if (!$this->transactionRepository->checkOrderIsPlaced($transaction)) {
$orderId = $this->placeOrder($quote, $uuid);
$orderId = $this->placeOrder($quote, $uuid, $userId);
$transaction->setOrderId((int)$orderId)->setStatus('payment_settled');
$this->transactionRepository->save($transaction);
$this->logRepository->addDebugLog('webhook', 'Order placed. Order id = ' . $orderId);
Expand All @@ -145,12 +156,13 @@ public function execute(string $uuid)
/**
* @param CartInterface $quote
* @param $uuid
* @param $userId
* @return false|int|null
*/
private function placeOrder(CartInterface $quote, $uuid)
private function placeOrder(CartInterface $quote, $uuid, $userId)
{
try {
$quote = $this->prepareQuote($quote);
$quote = $this->prepareQuote($quote, $userId);
$orderId = $this->cartManagement->placeOrder($quote->getId());
$order = $this->orderRepository->get($orderId);
$this->sendOrderEmail($order);
Expand Down Expand Up @@ -179,8 +191,14 @@ private function placeOrder(CartInterface $quote, $uuid)
*
* @return CartInterface
*/
private function prepareQuote(CartInterface $quote): CartInterface
private function prepareQuote(CartInterface $quote, string $userId): CartInterface
{
if ($quote->getCustomerEmail() == null) {
$user = $this->userRepository->getByTruelayerId($userId);
$quote->setCustomerEmail($user['magento_email']);
}

$quote->setCustomerIsGuest($quote->getCustomerId() == null);
$quote->setIsActive(true);
$this->quoteRepository->save($quote);
return $quote;
Expand Down
2 changes: 1 addition & 1 deletion Service/Order/RefundOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function execute(OrderInterface $order, float $amount): array
$client = $this->getClient->execute((int)$order->getStoreId());
$refundId = $client->refund()
->payment($transaction->getUuid())
->amountInMinor((int)($amount * 100))
->amountInMinor((int)bcmul((string)$amount, '100'))
->reference($transaction->getInvoiceUuid())
->create()
->getId();
Expand Down
Loading

0 comments on commit 981b8c7

Please sign in to comment.