Skip to content

Commit

Permalink
Merge pull request #35 from SwedbankPay/feature/remove-phone-number
Browse files Browse the repository at this point in the history
Feature/remove phone number
  • Loading branch information
shahriar-vdc committed Mar 3, 2021
2 parents 247a4aa + 053966c commit 1cf3523
Show file tree
Hide file tree
Showing 19 changed files with 149 additions and 30 deletions.
12 changes: 12 additions & 0 deletions Api/Data/OrderInterface.php
Expand Up @@ -8,6 +8,7 @@ interface OrderInterface extends ExtensibleDataInterface
{
const ID = 'id';
const PAYMENT_ORDER_ID = 'payment_order_id';
const PAYMENT_ORDER_ID_PATH = 'payment_order_id_path';
const DESCRIPTION = 'description';
const OPERATION = 'operation';
const INTENT = 'intent';
Expand Down Expand Up @@ -45,6 +46,17 @@ public function getPaymentOrderId();
*/
public function setPaymentOrderId($paymentOrderId);

/**
* @return string|null
*/
public function getPaymentOrderIdPath();

/**
* @param string $paymentIdPath
* @return void
*/
public function setPaymentOrderIdPath($paymentIdPath);

/**
* @return string
*/
Expand Down
12 changes: 12 additions & 0 deletions Api/Data/QuoteInterface.php
Expand Up @@ -8,6 +8,7 @@ interface QuoteInterface extends ExtensibleDataInterface
{
const ID = 'id';
const PAYMENT_ORDER_ID = 'payment_order_id';
const PAYMENT_ORDER_ID_PATH = 'payment_order_id_path';
const DESCRIPTION = 'description';
const OPERATION = 'operation';
const STATE = 'state';
Expand Down Expand Up @@ -45,6 +46,17 @@ public function getPaymentOrderId();
*/
public function setPaymentOrderId($paymentOrderId);

/**
* @return string|null
*/
public function getPaymentOrderIdPath();

/**
* @param string $paymentIdPath
* @return void
*/
public function setPaymentOrderIdPath($paymentIdPath);

/**
* @return string
*/
Expand Down
8 changes: 5 additions & 3 deletions Gateway/Command/Cancel.php
Expand Up @@ -104,10 +104,12 @@ public function execute(array $commandSubject)
$transactionObject = new TransactionObject();
$transactionObject->setTransaction($transaction);

$cancelRequest = $this->getRequestService('Paymentorder/Transaction', 'TransactionCancel', $transactionObject);
$cancelRequest->setRequestEndpointVars(
$this->getSwedbankPayPaymentResourceId($paymentOrder->getPaymentOrderId())
$cancelRequest = $this->getRequestService(
'Paymentorder/Transaction',
'TransactionCancel',
$transactionObject
);
$cancelRequest->setPaymentOrderId($paymentOrder->getPaymentOrderIdPath());

/** @var ResponseInterface $cancelResponse */
$cancelResponse = $cancelRequest->send();
Expand Down
4 changes: 1 addition & 3 deletions Gateway/Command/Capture.php
Expand Up @@ -175,9 +175,7 @@ public function execute(array $commandSubject)
'TransactionCapture',
$transactionObject
);
$captureRequest->setRequestEndpointVars(
$this->getSwedbankPayPaymentResourceId($paymentOrder->getPaymentOrderId())
);
$captureRequest->setPaymentOrderId($paymentOrder->getPaymentOrderIdPath());

/** @var ResponseInterface $captureResponse */
$captureResponse = $captureRequest->send();
Expand Down
4 changes: 1 addition & 3 deletions Gateway/Command/Refund.php
Expand Up @@ -116,9 +116,7 @@ public function execute(array $commandSubject)
'TransactionReversal',
$transactionObject
);
$reversalRequest->setRequestEndpointVars(
$this->getSwedbankPayPaymentResourceId($paymentOrder->getPaymentOrderId())
);
$reversalRequest->setPaymentOrderId($paymentOrder->getPaymentOrderIdPath());

/** @var ResponseInterface $reversalResponse */
$reversalResponse = $reversalRequest->send();
Expand Down
1 change: 0 additions & 1 deletion Helper/Factory/PayerFactory.php
Expand Up @@ -34,7 +34,6 @@ public function create(Quote $quote)

$payer = new PaymentorderPayer();
$payer->setEmail($quote->getShippingAddress()->getEmail())
->setMsisdn($quote->getShippingAddress()->getTelephone())
->setShippingAddress($shippingAddress);

return $payer;
Expand Down
25 changes: 18 additions & 7 deletions Helper/PaymentData.php
Expand Up @@ -93,7 +93,10 @@ public function getByPaymentOrderId($paymentOrderId)
try {
$paymentData = $this->paymentOrderRepo->getByPaymentOrderId($paymentOrderId);
} catch (NoSuchEntityException $e) {
$this->logger->debug(sprintf('SwedbankPay Order not found with ID # %s', $paymentOrderId));
$this->logger->debug(sprintf(
'No SwedbankPay Order record has been created yet with Payment Order ID: %s',
$paymentOrderId
));
}

if ($paymentData instanceof PaymentOrderInterface) {
Expand All @@ -103,14 +106,20 @@ public function getByPaymentOrderId($paymentOrderId)
try {
$paymentData = $this->paymentQuoteRepo->getByPaymentOrderId($paymentOrderId);
} catch (NoSuchEntityException $e) {
$this->logger->debug(sprintf('SwedbankPay Quote not found with ID # %s', $paymentOrderId));
$this->logger->debug(sprintf(
'SwedbankPay Quote not found with Payment Order ID: %s',
$paymentOrderId
));
}

if ($paymentData instanceof PaymentQuoteInterface) {
return $paymentData;
}

$errorMessage = sprintf("Unable to find a SwedbankPay payment matching Payment ID:\n%s", $paymentOrderId);
$errorMessage = sprintf(
"Unable to find a SwedbankPay payment matching Payment Order ID:\n%s",
$paymentOrderId
);

$this->logger->error(
$errorMessage
Expand Down Expand Up @@ -151,18 +160,20 @@ public function update($payment)
*/
public function updateRemainingAmounts($command, $amount, $order)
{
$amountInSubUnit = (int) round($amount * 100);

switch ($command) {
case 'capture':
$order->setRemainingCapturingAmount($order->getRemainingCapturingAmount() - ($amount * 100));
$order->setRemainingCapturingAmount($order->getRemainingCapturingAmount() - $amountInSubUnit);
$order->setRemainingCancellationAmount($order->getRemainingCapturingAmount());
$order->setRemainingReversalAmount($order->getRemainingReversalAmount() + ($amount * 100));
$order->setRemainingReversalAmount($order->getRemainingReversalAmount() + $amountInSubUnit);
break;
case 'cancel':
$order->setRemainingCancellationAmount($order->getRemainingCancellationAmount() - ($amount * 100));
$order->setRemainingCancellationAmount($order->getRemainingCancellationAmount() - $amountInSubUnit);
$order->setRemainingCapturingAmount($order->getRemainingCancellationAmount());
break;
case 'refund':
$order->setRemainingReversalAmount($order->getRemainingReversalAmount() - ($amount * 100));
$order->setRemainingReversalAmount($order->getRemainingReversalAmount() - $amountInSubUnit);
break;
}

Expand Down
1 change: 1 addition & 0 deletions Helper/Paymentorder.php
Expand Up @@ -437,6 +437,7 @@ public function saveQuoteToDB($response)
}

$quote->setPaymentOrderId($this->getSwedbankPayPaymentorderId($response['payment_order']['id']));
$quote->setPaymentOrderIdPath($response['payment_order']['id']);
$quote->setDescription($response['payment_order']['description']);
$quote->setOperation($response['payment_order']['operation']);
$quote->setState($response['payment_order']['state']);
Expand Down
17 changes: 17 additions & 0 deletions Model/Order.php
Expand Up @@ -53,6 +53,23 @@ public function setPaymentOrderId($paymentOrderId)
$this->setData(self::PAYMENT_ORDER_ID, $paymentOrderId);
}

/**
* @return string|null
*/
public function getPaymentOrderIdPath()
{
return $this->_getData(self::PAYMENT_ORDER_ID_PATH);
}

/**
* @param string $paymentIdPath
* @return void
*/
public function setPaymentOrderIdPath($paymentIdPath)
{
$this->setData(self::PAYMENT_ORDER_ID_PATH, $paymentIdPath);
}

/**
* @return string
*/
Expand Down
17 changes: 17 additions & 0 deletions Model/Quote.php
Expand Up @@ -53,6 +53,23 @@ public function setPaymentOrderId($paymentOrderId)
$this->setData(self::PAYMENT_ORDER_ID, $paymentOrderId);
}

/**
* @return string|null
*/
public function getPaymentOrderIdPath()
{
return $this->_getData(self::PAYMENT_ORDER_ID_PATH);
}

/**
* @param string $paymentIdPath
* @return void
*/
public function setPaymentOrderIdPath($paymentIdPath)
{
$this->setData(self::PAYMENT_ORDER_ID_PATH, $paymentIdPath);
}

/**
* @return string
*/
Expand Down
17 changes: 14 additions & 3 deletions Observer/AfterCheckoutSubmitObserver.php
Expand Up @@ -3,10 +3,10 @@
namespace SwedbankPay\Checkout\Observer;

use Exception;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\AlreadyExistsException;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Exception\AlreadyExistsException;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Sales\Api\Data\OrderPaymentInterface;
Expand All @@ -17,6 +17,7 @@
use SwedbankPay\Checkout\Api\Data\OrderInterface as PaymentOrderInterface;
use SwedbankPay\Checkout\Helper\Config as ConfigHelper;
use SwedbankPay\Checkout\Helper\PaymentData;
use SwedbankPay\Checkout\Model\ConsumerSession;
use SwedbankPay\Checkout\Model\Ui\ConfigProvider;
use SwedbankPay\Core\Helper\Order as OrderHelper;
use SwedbankPay\Core\Logger\Logger;
Expand All @@ -42,6 +43,11 @@ class AfterCheckoutSubmitObserver implements ObserverInterface
*/
protected $clientService;

/**
* @var ConsumerSession
*/
protected $consumerSession;

/**
* @var OrderHelper
*/
Expand All @@ -61,13 +67,15 @@ public function __construct(
ConfigHelper $configHelper,
PaymentData $paymentData,
ClientService $clientService,
ConsumerSession $consumerSession,
OrderHelper $orderHelper,
OrderRepository $orderRepository,
Logger $logger
) {
$this->configHelper = $configHelper;
$this->paymentData = $paymentData;
$this->clientService = $clientService;
$this->consumerSession = $consumerSession;
$this->orderHelper = $orderHelper;
$this->orderRepository = $orderRepository;
$this->logger = $logger;
Expand Down Expand Up @@ -111,13 +119,16 @@ public function execute(Observer $observer)

/** @var GetCurrentPayment $currentPaymentRequest */
$currentPaymentRequest = $this->clientService->init('Paymentorder', 'GetCurrentPayment');
$currentPaymentRequest->setRequestEndpointVars($paymentData->getPaymentOrderId());
$this->logger->debug('Payment Order ID: '. $paymentData->getPaymentOrderIdPath());
$currentPaymentRequest->setPaymentOrderId($paymentData->getPaymentOrderIdPath());

/** @var GetCurrentPaymentInterface $currentPayment */
$currentPayment = $currentPaymentRequest->send()->getResponseResource();
$paymentData->setIntent($currentPayment->getPayment()->getIntent());
$this->paymentData->update($paymentData);

$this->consumerSession->setViewOperation(false);

if ($paymentData->getIntent() == 'Sale') {
$this->saveTransactionNumber($order, $currentPayment);

Expand Down
7 changes: 6 additions & 1 deletion Plugin/OrderRepositoryPlugin.php
Expand Up @@ -96,14 +96,19 @@ public function afterSave(
return $mageOrder;
}
} catch (Exception $e) {
$this->logger->debug(sprintf('SwedbankPay Order not found with ID # %s', $mageOrder->getEntityId()));
$this->logger->debug(sprintf(
'No SwedbankPay Order record has been created yet with ID # %s',
$mageOrder->getEntityId()
));
$this->logger->debug('Creating new SwedbankPay Order...');
}

/** @var \SwedbankPay\Checkout\Model\Order $swedbankPayOrder */
$swedbankPayOrder = $this->orderFactory->create();

$swedbankPayOrder->setState($swedbankPayQuote->getState());
$swedbankPayOrder->setPaymentOrderId($swedbankPayQuote->getPaymentOrderId());
$swedbankPayOrder->setPaymentOrderIdPath($swedbankPayQuote->getPaymentOrderIdPath());
$swedbankPayOrder->setCreatedAt($swedbankPayQuote->getCreatedAt());
$swedbankPayOrder->setUpdatedAt($swedbankPayQuote->getUpdatedAt());
$swedbankPayOrder->setOperation($swedbankPayQuote->getOperation());
Expand Down
16 changes: 11 additions & 5 deletions Plugin/QuoteRepositoryPlugin.php
Expand Up @@ -115,9 +115,13 @@ public function aroundSave(
$swedbankPayQuote->setRemainingReversalAmount(0);

$this->quoteRepository->save($swedbankPayQuote);

} catch (NoSuchEntityException $e) {
$this->logger->debug(sprintf('SwedbankPay Quote not found with ID # %s', $quote->getId()));
$this->logger->debug(sprintf(
'No SwedbankPay Quote record has been created yet with ID # %s',
$quote->getId()
));

$this->logger->debug(sprintf('SwedbankPay Quote update skipped!'));
}

return $returnValue;
Expand All @@ -130,11 +134,13 @@ public function aroundSave(
*/
public function updatePaymentorder(MageQuote $mageQuote, SwedbankPayQuote $swedbankPayQuote)
{
$quoteTotal = (int) round($mageQuote->getGrandTotal() * 100);

$this->logger->debug('UpdateOrder request is called');
$this->logger->debug('SwedbankPayQuote Total: ' . $swedbankPayQuote->getAmount());
$this->logger->debug('Quote Grand Total: ' . $mageQuote->getGrandTotal() * 100);
$this->logger->debug('Quote Grand Total: ' . $quoteTotal);

if ($swedbankPayQuote->getAmount() == ($mageQuote->getGrandTotal() * 100)) {
if ($swedbankPayQuote->getAmount() == $quoteTotal) {
$this->logger->debug('UpdateOrder operation is skipped as the amount is unchanged');
return;
}
Expand All @@ -147,7 +153,7 @@ public function updatePaymentorder(MageQuote $mageQuote, SwedbankPayQuote $swedb
$paymentOrderObject = $this->paymentorder->createPaymentorderUpdateObject($mageQuote);

$updateRequest = $this->service->init('Paymentorder', 'updateOrder', $paymentOrderObject);
$updateRequest->setRequestEndpointVars($swedbankPayQuote->getPaymentOrderId());
$updateRequest->setPaymentOrderId($swedbankPayQuote->getPaymentOrderIdPath());
$updateRequest->send();
}

Expand Down
29 changes: 29 additions & 0 deletions Setup/UpgradeSchema.php
Expand Up @@ -49,6 +49,35 @@ public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $con
);
}

$swedbankPayQuoteTable = 'swedbank_pay_quotes';
$swedbankPayOrderTable = 'swedbank_pay_orders';

if (version_compare($context->getVersion(), "1.3.0", "<")) {
// Swedbank Pay Quotes table
$setup->getConnection()
->addColumn(
$setup->getTable($swedbankPayQuoteTable),
'payment_order_id_path',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'length' => 255,
'comment' =>'SwedbankPay Payment Order ID Path'
]
);

// Swedbank Pay Orders table
$setup->getConnection()
->addColumn(
$setup->getTable($swedbankPayOrderTable),
'payment_order_id_path',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'length' => 255,
'comment' =>'SwedbankPay Payment Order ID Path'
]
);
}

$setup->endSetup();
}
}
2 changes: 1 addition & 1 deletion Test/Unit/PaymentDataTest.php
Expand Up @@ -67,7 +67,7 @@ public function testGetByPaymentOrderIdThrowsException()

$this->expectException(NoSuchEntityException::class);
$this->expectExceptionMessage(
sprintf("Unable to find a SwedbankPay payment matching Payment ID:\n%s", $paymentOrderId)
sprintf("Unable to find a SwedbankPay payment matching Payment Order ID:\n%s", $paymentOrderId)
);

$this->paymentData->getByPaymentOrderId($paymentOrderId);
Expand Down
2 changes: 1 addition & 1 deletion Test/Unit/QuoteRepositoryPluginTest.php
Expand Up @@ -178,7 +178,7 @@ public function testUpdateOrderRequestWasSent()
->willReturn($paymentOrderId);

$this->service->expects($this->once())->method('init')->willReturn($this->updateRequest);
$this->updateRequest->expects($this->once())->method('setRequestEndpointVars');
$this->updateRequest->expects($this->once())->method('setPaymentOrderId');
$this->updateRequest->expects($this->once())->method('send');

$this->quoteRepositoryPlugin->updatePaymentorder($this->mageQuote, $this->swedbankPayQuote);
Expand Down

0 comments on commit 1cf3523

Please sign in to comment.