Skip to content

Commit

Permalink
BP-1488 - Module's enabled status "Test" has no effect
Browse files Browse the repository at this point in the history
  • Loading branch information
LucianTuriacArnia committed Oct 11, 2023
1 parent 365bae4 commit c34693e
Show file tree
Hide file tree
Showing 38 changed files with 172 additions and 115 deletions.
142 changes: 106 additions & 36 deletions Model/Adapter/BuckarooAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,24 @@
namespace Buckaroo\Magento2\Model\Adapter;

use Buckaroo\BuckarooClient;
use Buckaroo\Config\DefaultConfig;
use Buckaroo\Config\Config;
use Buckaroo\Config\DefaultConfig;
use Buckaroo\Exceptions\BuckarooException;
use Buckaroo\Handlers\Reply\ReplyHandler;
use Buckaroo\Magento2\Exception;
use Buckaroo\Magento2\Gateway\Request\CreditManagement\BuilderComposite;
use Buckaroo\Magento2\Logging\BuckarooLoggerInterface;
use Buckaroo\Magento2\Model\Config\Source\Enablemode;
use Buckaroo\Magento2\Model\ConfigProvider\Account;
use Buckaroo\Magento2\Model\ConfigProvider\Factory as ConfigProviderFactory;
use Buckaroo\Magento2\Model\ConfigProvider\Method\AbstractConfigProvider;
use Buckaroo\Magento2\Service\Software\Data;
use Buckaroo\PaymentMethods\CreditManagement\CreditManagement;
use Buckaroo\Transaction\Response\TransactionResponse;
use Magento\Framework\App\ProductMetadataInterface;
use Magento\Framework\Encryption\Encryptor;
use Magento\Framework\Locale\Resolver;
use Magento\Store\Model\StoreManagerInterface;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
Expand All @@ -58,43 +64,53 @@ class BuckarooAdapter
/**
* @var array|null
*/
private array $mapPaymentMethods;
private ?array $mapPaymentMethods;

/**
* @var ConfigProviderFactory
*/
private ConfigProviderFactory $configProviderFactory;

/**
* @var ProductMetadataInterface
*/
private ProductMetadataInterface $productMetadata;

/**
* @var Resolver
*/
private Resolver $localeResolver;

/**
* @param Account $configProviderAccount
* @var StoreManagerInterface
*/
private StoreManagerInterface $storeManager;

/**
* @param ConfigProviderFactory $configProviderFactory
* @param Encryptor $encryptor
* @param BuckarooLoggerInterface $logger
* @param ProductMetadataInterface $productMetadata
* @param Resolver $localeResolver
* @param StoreManagerInterface $storeManager
* @param array|null $mapPaymentMethods
* @throws \Exception
*/
public function __construct(
Account $configProviderAccount,
ConfigProviderFactory $configProviderFactory,
Encryptor $encryptor,
BuckarooLoggerInterface $logger,
ProductMetadataInterface $productMetadata,
Resolver $localeResolver,
StoreManagerInterface $storeManager,
array $mapPaymentMethods = null
) {
$this->mapPaymentMethods = $mapPaymentMethods;
$this->logger = $logger;

$this->buckaroo = new BuckarooClient(new DefaultConfig(
$encryptor->decrypt($configProviderAccount->getMerchantKey()),
$encryptor->decrypt($configProviderAccount->getSecretKey()),
$configProviderAccount->getActive() == 2 ? Config::LIVE_MODE : Config::TEST_MODE,
null,
null,
null,
null,
$productMetadata->getName() . ' - ' . $productMetadata->getEdition(),
$productMetadata->getVersion(),
'Buckaroo',
'Magento2',
Data::BUCKAROO_VERSION,
str_replace('_', '-', $localeResolver->getLocale())
));
$this->configProviderFactory = $configProviderFactory;
$this->encryptor = $encryptor;
$this->productMetadata = $productMetadata;
$this->localeResolver = $localeResolver;
$this->storeManager = $storeManager;
}

/**
Expand All @@ -108,6 +124,7 @@ public function __construct(
*/
public function execute(string $action, string $method, array $data): TransactionResponse
{
$this->setClientSdk($method);
$payment = $this->buckaroo->method($this->getMethodName($method));

try {
Expand Down Expand Up @@ -137,24 +154,54 @@ public function execute(string $action, string $method, array $data): Transactio
}

/**
* Get ideal issuers
* Set Client SDK base on account configuration and payment method configuration
*
* @return array
* @throws \Throwable
* @throws \Exception
*/
public function getIdealIssuers(): array
private function setClientSdk($paymentMethod = null): void
{
try {
return $this->buckaroo->method('ideal')->issuers();
} catch (\Throwable $th) {
$this->logger->addError(sprintf(
'[SDK] | [Adapter] | [%s:%s] - Get ideal issuers | [ERROR]: %s',
__METHOD__,
__LINE__,
$th->getMessage()
));
return [];
/** @var Account $configProviderAccount */
$configProviderAccount = $this->configProviderFactory->get('account');
$storeId = $this->storeManager->getStore()->getId();
$clientMode = Config::TEST_MODE;
$accountMode = $configProviderAccount->getActive($storeId);

if ($accountMode == Enablemode::ENABLE_OFF) {
throw new Exception(__('The Buckaroo Module is OFF'));
}

if ($accountMode == Enablemode::ENABLE_LIVE) {
$clientMode = Config::LIVE_MODE;

if ($paymentMethod) {
/** @var AbstractConfigProvider $configProviderPaymentMethod */
$configProviderPaymentMethod = $this->configProviderFactory->get($paymentMethod);
$isActivePaymentMethod = $configProviderPaymentMethod->getActive($storeId);
if ($isActivePaymentMethod == Enablemode::ENABLE_OFF) {
throw new Exception(__('Payment method: %s is not active', $paymentMethod));
}

if ($isActivePaymentMethod == Enablemode::ENABLE_TEST) {
$clientMode = Config::TEST_MODE;
}
}
}

$this->buckaroo = new BuckarooClient(new DefaultConfig(
$this->encryptor->decrypt($configProviderAccount->getMerchantKey()),
$this->encryptor->decrypt($configProviderAccount->getSecretKey()),
$clientMode,
null,
null,
null,
null,
$this->productMetadata->getName() . ' - ' . $this->productMetadata->getEdition(),
$this->productMetadata->getVersion(),
'Buckaroo',
'Magento2',
Data::BUCKAROO_VERSION,
str_replace('_', '-', $this->localeResolver->getLocale())
));
}

/**
Expand Down Expand Up @@ -187,7 +234,7 @@ protected function isCreditManagementOfType(array $data, string $type): bool
* Get credit management body
*
* @param array $data
* @return TransactionResponse|Buckaroo\PaymentMethods\CreditManagement\CreditManagement
* @return TransactionResponse|CreditManagement
*/
protected function getCreditManagementBody(array $data)
{
Expand All @@ -213,6 +260,28 @@ protected function createCreditNote(array $data, string $type = BuilderComposite
);
}

/**
* Get ideal issuers
*
* @return array
* @throws \Throwable
*/
public function getIdealIssuers(): array
{
try {
$this->setClientSdk();
return $this->buckaroo->method('ideal')->issuers();
} catch (\Throwable $th) {
$this->logger->addError(sprintf(
'[SDK] | [Adapter] | [%s:%s] - Get ideal issuers | [ERROR]: %s',
__METHOD__,
__LINE__,
$th->getMessage()
));
return [];
}
}

/**
* Validate request
*
Expand All @@ -221,6 +290,7 @@ protected function createCreditNote(array $data, string $type = BuilderComposite
*/
public function validate($postData, $authHeader, $uri): bool
{
$this->setClientSdk();
$replyHandler = new ReplyHandler($this->buckaroo->client()->config(), $postData, $authHeader, $uri);
$replyHandler->validate();
return $replyHandler->isValid();
Expand Down
73 changes: 30 additions & 43 deletions Observer/SalesOrderShipmentAfter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@
use Buckaroo\Magento2\Model\Config\Source\InvoiceHandlingOptions;
use Buckaroo\Magento2\Model\ConfigProvider\Account;
use Buckaroo\Magento2\Model\ConfigProvider\Factory as ConfigProviderFactory;
use Buckaroo\Magento2\Model\ConfigProvider\Method\Afterpay20;
use Buckaroo\Magento2\Model\ConfigProvider\Method\Klarnakp;
use Buckaroo\Magento2\Model\Method\BuckarooAdapter;
use Magento\Checkout\Model\ConfigProviderInterface;
use Magento\Framework\DB\TransactionFactory;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
Expand All @@ -50,53 +47,43 @@
class SalesOrderShipmentAfter implements ObserverInterface
{
public const MODULE_ENABLED = 'sr_auto_invoice_shipment/settings/enabled';

/**
* @var Shipment
*/
private Shipment $shipment;

/**
* @var Order
*/
private Order $order;

/**
* @var OrderPaymentInterface|null
*/
private ?OrderPaymentInterface $payment;

/**
* @var Data
*/
public Data $helper;

/**
*
* @var CollectionFactory
*/
protected $invoiceCollectionFactory;

/**
* @var InvoiceService
*/
protected InvoiceService $invoiceService;

/**
* @var ShipmentFactory
*/
protected ShipmentFactory $shipmentFactory;

/**
* @var TransactionFactory
*/
protected TransactionFactory $transactionFactory;

/**
* @var BuckarooLoggerInterface
*/
protected BuckarooLoggerInterface $logger;

/**
* @var Shipment
*/
private Shipment $shipment;
/**
* @var Order
*/
private Order $order;
/**
* @var OrderPaymentInterface|null
*/
private ?OrderPaymentInterface $payment;
/**
* @var ConfigProviderFactory
*/
Expand Down Expand Up @@ -179,7 +166,7 @@ public function execute(Observer $observer)
&& $afterpayConfig->isInvoiceCreatedAfterShipment()
&& ($paymentMethod->getConfigPaymentAction() == 'authorize')
) {
$this->createInvoice( true);
$this->createInvoice(true);
return;
}

Expand Down Expand Up @@ -259,6 +246,20 @@ private function createInvoice(bool $allowPartialsWithDiscount = false)
return $invoice;
}

/**
* Get shipped quantities
*
* @return array
*/
public function getQtys(): array
{
$qtys = [];
foreach ($this->shipment->getItems() as $items) {
$qtys[$items->getOrderItemId()] = $items->getQty();
}
return $qtys;
}

/**
* Create invoice after shipment for all buckaroo payment methods
*
Expand All @@ -269,11 +270,11 @@ private function createInvoice(bool $allowPartialsWithDiscount = false)
*/
public function createInvoiceGeneralSetting(): bool
{
$this->logger->addDebug('[CREATE_INVOICE] | [Observer] | ['. __METHOD__ .':'. __LINE__ . '] - Save Invoice');
$this->logger->addDebug('[CREATE_INVOICE] | [Observer] | [' . __METHOD__ . ':' . __LINE__ . '] - Save Invoice');

if (!$this->order->canInvoice() || $this->order->hasInvoices()) {
$this->logger->addDebug(
'[CREATE_INVOICE] | [Observer] | ['. __METHOD__ .':'. __LINE__ . '] - Order can not be invoiced'
'[CREATE_INVOICE] | [Observer] | [' . __METHOD__ . ':' . __LINE__ . '] - Order can not be invoiced'
);

return false;
Expand Down Expand Up @@ -307,7 +308,7 @@ public function createInvoiceGeneralSetting(): bool

if (!$invoice->getEmailSent() && $this->configAccount->getInvoiceEmail($this->order->getStore())) {
$this->logger->addDebug(
'[CREATE_INVOICE] | [Observer] | ['. __METHOD__ .':'. __LINE__ . '] - Send Invoice Email '
'[CREATE_INVOICE] | [Observer] | [' . __METHOD__ . ':' . __LINE__ . '] - Send Invoice Email '
);
$this->invoiceSender->send($invoice, true);
}
Expand All @@ -318,18 +319,4 @@ public function createInvoiceGeneralSetting(): bool

return true;
}

/**
* Get shipped quantities
*
* @return array
*/
public function getQtys(): array
{
$qtys = [];
foreach ($this->shipment->getItems() as $items) {
$qtys[$items->getOrderItemId()] = $items->getQty();
}
return $qtys;
}
}
2 changes: 1 addition & 1 deletion etc/adminhtml/system/payment_methods/afterpay.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<field id="active" translate="label comment" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Enable Riverty | Afterpay</label>
<comment><![CDATA[Enable or disable this payment method.]]></comment>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<source_model>Buckaroo\Magento2\Model\Config\Source\Enablemode</source_model>
<config_path>payment/buckaroo_magento2_afterpay/active</config_path>
</field>

Expand Down
2 changes: 1 addition & 1 deletion etc/adminhtml/system/payment_methods/afterpay2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<field id="active" translate="label comment" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Enable Riverty | Afterpay 2</label>
<comment><![CDATA[Enable or disable this payment method.]]></comment>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<source_model>Buckaroo\Magento2\Model\Config\Source\Enablemode</source_model>
<config_path>payment/buckaroo_magento2_afterpay2/active</config_path>
</field>

Expand Down
2 changes: 1 addition & 1 deletion etc/adminhtml/system/payment_methods/afterpay20.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<field id="active" translate="label comment" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Enable Riverty | Afterpay</label>
<comment><![CDATA[Enable or disable this payment method.]]></comment>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<source_model>Buckaroo\Magento2\Model\Config\Source\Enablemode</source_model>
<config_path>payment/buckaroo_magento2_afterpay20/active</config_path>
</field>

Expand Down
Loading

0 comments on commit c34693e

Please sign in to comment.