Skip to content

Commit

Permalink
Merge pull request #8 from donmo-roundup/improve-order-lifecycles-han…
Browse files Browse the repository at this point in the history
…dling

Improve order lifecycles handling
  • Loading branch information
DavidVin357 authored Jun 22, 2023
2 parents 714473b + c3a4395 commit b5947a1
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 39 deletions.
9 changes: 6 additions & 3 deletions Observer/ConfirmDonationOnOrderComplete.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@ public function execute(Observer $observer)
{
try {
$order = $observer->getEvent()->getOrder();
$donationAmount = (float) $order->getDonmodonation();

if ($order->getState() == 'complete') {
if ($order->getDonmodonation() > 0) {
if ($donationAmount > 0) {

$orderId = $order->getId();
$quoteId = $order->getQuoteId();
$maskedId = $this->quoteIdToMaskedQuoteId->execute($quoteId);

Expand All @@ -48,11 +51,11 @@ public function execute(Observer $observer)
$this->donationResource->load($donationModel, $maskedId, 'masked_quote_id');

$donationModel
->setStatus(DonationModel::STATUS_CONFIRMED);
->setStatus(DonationModel::STATUS_CONFIRMED)
->setOrderId($orderId);
$this->donationResource->save($donationModel);
}

$this->donationResource->save($donationModel);
}
} catch (\Exception $exception) {
$this->logger->error("Donmo ConfirmDonationOnOrderComplete Observer error:\n" . $exception);
Expand Down
43 changes: 16 additions & 27 deletions Observer/CreateDonation.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

use Donmo\Roundup\Model\Config as DonmoConfig;

use Magento\Framework\Intl\DateTimeFactory;


use Donmo\Roundup\Model\Donmo\DonationFactory;
use Donmo\Roundup\Model\Donmo\Donation as DonationModel;
Expand All @@ -24,23 +22,20 @@ class CreateDonation implements ObserverInterface
private DonationFactory $donationFactory;
private DonationResource $donationResource;

private DateTimeFactory $dateTimeFactory;

private Logger $logger;
private QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedQuoteId;

public function __construct(
DonationFactory $donationFactory,
DonationResource $donationResource,
DateTimeFactory $dateTimeFactory,
Logger $logger,
DonmoConfig $config,
QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedQuoteId
)
{
$this->donationFactory = $donationFactory;
$this->donationResource = $donationResource;
$this->dateTimeFactory = $dateTimeFactory;
$this->logger = $logger;
$this->donmoConfig = $config;
$this->quoteIdToMaskedQuoteId = $quoteIdToMaskedQuoteId;
Expand All @@ -49,29 +44,23 @@ public function __construct(

public function execute(Observer $observer)
{
$order = $observer->getEvent()->getOrder();

$donationAmount = (float) $order->getDonmodonation();

try {
$order = $observer->getEvent()->getOrder();
if ($order->getState() == 'new') {
if ($order->getDonmodonation() > 0) {
$orderId = $order->getId();
$quoteId = $order->getQuoteId();
$maskedId = $this->quoteIdToMaskedQuoteId->execute($quoteId);
$currentMode = $this->donmoConfig->getCurrentMode();
$createdAt = $this->dateTimeFactory->create(
$order->getCreatedAt(),
new \DateTimeZone('UTC')
);

$donationModel = $this->donationFactory->create();

$donationModel
->setMaskedQuoteId($maskedId)
->setOrderId($orderId)
->setDonationAmount($order->getDonmodonation())
->setCreatedAt($createdAt)
->setStatus(DonationModel::STATUS_PENDING)
->setMode($currentMode);
}
if ($donationAmount > 0) {
$quoteId = $order->getQuoteId();
$maskedId = $this->quoteIdToMaskedQuoteId->execute($quoteId);
$currentMode = $this->donmoConfig->getCurrentMode();

$donationModel = $this->donationFactory->create();

$donationModel
->setMaskedQuoteId($maskedId)
->setDonationAmount($donationAmount)
->setStatus(DonationModel::STATUS_PENDING)
->setMode($currentMode);

$this->donationResource->save($donationModel);
}
Expand Down
7 changes: 5 additions & 2 deletions Observer/RemoveDonationWithOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ public function execute(Observer $observer)
$newDonationStatus = DonationModel::STATUS_REFUNDED;
}

if ($order->getDonmodonation() > 0) {
$donationAmount = (float) $order->getDonmodonation();

if ($donationAmount> 0) {
$quoteId = $order->getQuoteId();
$maskedId = $this->quoteIdToMaskedQuoteId->execute($quoteId);

Expand All @@ -65,7 +67,8 @@ public function execute(Observer $observer)
if ($donationModel->getData('status') != DonationModel::STATUS_SENT) {
$donationModel->setData('status', $newDonationStatus);
$this->donationResource->save($donationModel);
} // make Delete request to Donmo API for only if donation is already sent
}
// make Delete request to Donmo API for only if donation is already sent
else {
$status = $this->apiService->deleteDonation($donationMode, $maskedId);

Expand Down
11 changes: 9 additions & 2 deletions Ui/Component/Listing/Column/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,22 @@ public function toOptionArray()
if ($this->options !== null) {
return $this->options;
}
$this->options[DonationModel::STATUS_CONFIRMED]['label'] = DonationModel::STATUS_CONFIRMED;
$this->options[DonationModel::STATUS_CONFIRMED]['value'] = DonationModel::STATUS_CONFIRMED;

$this->options[DonationModel::STATUS_PENDING]['label'] = DonationModel::STATUS_PENDING;
$this->options[DonationModel::STATUS_PENDING]['value'] = DonationModel::STATUS_PENDING;

$this->options[DonationModel::STATUS_CONFIRMED]['label'] = DonationModel::STATUS_CONFIRMED;
$this->options[DonationModel::STATUS_CONFIRMED]['value'] = DonationModel::STATUS_CONFIRMED;

$this->options[DonationModel::STATUS_DELETED]['label'] = DonationModel::STATUS_DELETED;
$this->options[DonationModel::STATUS_DELETED]['value'] = DonationModel::STATUS_DELETED;

$this->options[DonationModel::STATUS_REFUNDED]['label'] = DonationModel::STATUS_REFUNDED;
$this->options[DonationModel::STATUS_REFUNDED]['value'] = DonationModel::STATUS_REFUNDED;

$this->options[DonationModel::STATUS_SENT]['label'] = DonationModel::STATUS_SENT;
$this->options[DonationModel::STATUS_SENT]['value'] = DonationModel::STATUS_SENT;

return $this->options;
}
}
Expand Down
6 changes: 4 additions & 2 deletions etc/db_schema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
<column xsi:type="int" name="order_id" padding="10" unsigned="true" nullable="true" identity="false"
comment="Order Id"/>

<column xsi:type="timestamp" name="created_at" on_update="false" nullable="true" comment="Created At"/>
<column xsi:type="timestamp" name="created_at" on_update="false" nullable="true" comment="Created At" default="CURRENT_TIMESTAMP"/>

<column xsi:type="timestamp" name="updated_at" on_update="false" nullable="true" comment="Updated At"/>
<column xsi:type="timestamp" name="updated_at" on_update="false" nullable="true" comment="Updated At" default="CURRENT_TIMESTAMP"/>


<constraint xsi:type="primary" referenceId="PRIMARY">
Expand All @@ -31,6 +31,8 @@

<constraint xsi:type="unique" referenceId="DONMO_DONATION_MASKED_QUOTE_ID">
<column name="masked_quote_id"/>
<column name="mode"/>
<column name="status"/>
</constraint>

<constraint xsi:type="foreign" referenceId="DONMO_DONATION_MASKED_QUOTE_ID_QUOTE_ID_MASK_MASKED_ID" table="donmo_donation"
Expand Down
6 changes: 4 additions & 2 deletions etc/events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
</event>

<!-- Create donation on order success -->
<event name="sales_order_save_after">
<event name="sales_order_place_after">
<observer name="donmo_create_donation_observer" instance="Donmo\Roundup\Observer\CreateDonation" />
<observer name="donmo_confirm_donation_observer" instance="Donmo\Roundup\Observer\ConfirmDonationOnOrderComplete" />
</event>

<event name="sales_order_save_after">
<observer name="donmo_confirm_donation_observer" instance="Donmo\Roundup\Observer\ConfirmDonationOnOrderComplete" />
</event>

<!-- Remove donation on order cancellation -->
<event name="order_cancel_after">
Expand Down
1 change: 1 addition & 0 deletions lib/ApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public function deleteDonation($donationMode, $id) {
$this->logger->error("Unsuccessful Delete Donation API request: \n" . $body);
}

return $status;
}

}
2 changes: 1 addition & 1 deletion view/adminhtml/ui_component/donmo_report.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<item name="config" xsi:type="array">
<item name="applied" xsi:type="array">
<item name="mode" xsi:type="string">live</item>
<item name="status" xsi:type="string">PENDING</item>
<item name="status" xsi:type="string">SENT</item>
</item>
</item>
</argument>
Expand Down

0 comments on commit b5947a1

Please sign in to comment.