-
Notifications
You must be signed in to change notification settings - Fork 258
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #2804227 by jackbravo, flocondetoile, zerolab, edwardaa, steveo…
…liver, vasike: Add $order->getBalance()
- Loading branch information
Showing
11 changed files
with
274 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace Drupal\commerce_payment; | ||
|
||
use Drupal\commerce_order\Entity\OrderInterface; | ||
use Drupal\commerce_price\Price; | ||
use Drupal\Core\Entity\EntityTypeManagerInterface; | ||
|
||
class PaymentOrderManager implements PaymentOrderManagerInterface { | ||
|
||
/** | ||
* The payment storage. | ||
* | ||
* @var \Drupal\commerce_payment\PaymentStorageInterface | ||
*/ | ||
protected $paymentStorage; | ||
|
||
/** | ||
* Constructs a new PaymentOrderManager object. | ||
* | ||
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager | ||
* The entity type manager. | ||
*/ | ||
public function __construct(EntityTypeManagerInterface $entity_type_manager) { | ||
$this->paymentStorage = $entity_type_manager->getStorage('commerce_payment'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function updateTotalPaid(OrderInterface $order) { | ||
$previous_total = $order->getTotalPaid(); | ||
if (!$previous_total) { | ||
// A NULL total indicates an order that doesn't have any items yet. | ||
return; | ||
} | ||
// The new total is always calculated from scratch, to properly handle | ||
// orders that were created before the total_paid field was introduced. | ||
$payments = $this->paymentStorage->loadMultipleByOrder($order); | ||
/** @var \Drupal\commerce_price\Price $new_total */ | ||
$new_total = new Price('0', $previous_total->getCurrencyCode()); | ||
foreach ($payments as $payment) { | ||
if ($payment->isCompleted()) { | ||
$new_total = $new_total->add($payment->getBalance()); | ||
} | ||
} | ||
|
||
if (!$previous_total->equals($new_total)) { | ||
$order->setTotalPaid($new_total); | ||
$order->save(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace Drupal\commerce_payment; | ||
|
||
use Drupal\commerce_order\Entity\OrderInterface; | ||
|
||
/** | ||
* Updates orders based on payment information. | ||
*/ | ||
interface PaymentOrderManagerInterface { | ||
|
||
/** | ||
* Recalculates the total paid price for the given order. | ||
* | ||
* The order will be saved if the total paid price has changed. | ||
* | ||
* @param \Drupal\commerce_order\Entity\OrderInterface $order | ||
* The order. | ||
*/ | ||
public function updateTotalPaid(OrderInterface $order); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.