Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 27 additions & 16 deletions packages/core/src/Actions/Orders/CaptureOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
/**
* Capture a pending payment intent against an order.
*
* Validates the requested amount, then dispatches the capture through the
* underlying transaction's payment driver. Returns the driver's
* `PaymentCapture` result unchanged.
* Validates the requested amount and that the order has no successful
* capture yet, then dispatches the capture through the underlying
* transaction's payment driver. Returns the driver's `PaymentCapture` result
* unchanged.
*/
class CaptureOrder implements CapturesOrder
{
Expand All @@ -28,24 +29,34 @@ public function execute(
float|int|string $amount,
): PaymentCapture {
/** @var Order $order */
/** @var Transaction $transaction */
$transaction = $order->transactions()->whereKey($transactionId)->firstOrFail();
return $order->getConnection()->transaction(function () use ($order, $transactionId, $amount) {
// Lock the order so two captures sent at once can't both pass the
// "not captured yet" check.
$order->newQuery()->whereKey($order->getKey())->lockForUpdate()->first();

if ($transaction->type !== 'intent' || ! $transaction->success) {
throw new OrderActionException('Transaction is not a successful payment intent and cannot be captured.');
}
/** @var Transaction $transaction */
$transaction = $order->transactions()->whereKey($transactionId)->firstOrFail();

$minorAmount = (int) bcmul((string) $amount, (string) $order->currency->factor);
if ($transaction->type !== 'intent' || ! $transaction->success) {
throw new OrderActionException('Transaction is not a successful payment intent and cannot be captured.');
}

if ($minorAmount <= 0) {
throw new OrderActionException('Capture amount must be greater than zero.');
}
if ($order->transactions()->whereType('capture')->whereSuccess(true)->exists()) {
throw new OrderActionException('Order has already been captured.');
}

if ($minorAmount > $transaction->amount) {
throw new OrderActionException('Capture amount exceeds the transaction amount.');
}
$minorAmount = (int) bcmul((string) $amount, (string) $order->currency->factor);

if ($minorAmount <= 0) {
throw new OrderActionException('Capture amount must be greater than zero.');
}

if ($minorAmount > $transaction->amount) {
throw new OrderActionException('Capture amount exceeds the transaction amount.');
}

return $transaction->capture($minorAmount);
return $transaction->capture($minorAmount);
});
}

/**
Expand Down
43 changes: 29 additions & 14 deletions packages/core/src/Actions/Orders/RefundOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Lunar\Core\Actions\Orders;

use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\DB;
use Lunar\Core\Contracts\Actions\Orders\RefundsOrder;
use Lunar\Core\DataObjects\PaymentRefund;
use Lunar\Core\DataObjects\RefundRequest;
Expand Down Expand Up @@ -32,6 +31,26 @@ public function __construct(
) {}

public function execute(Order $order, RefundRequest $request): PaymentRefund
{
/** @var Order $order */
/** @var PaymentRefund $refund */
$refund = $order->getConnection()->transaction(function () use ($order, $request) {
// Lock the order so concurrent refunds are checked one after
// another against the committed ledger, not all against the same
// balance, and so each refund and its line allocation commit together.
$order->newQuery()->whereKey($order->getKey())->lockForUpdate()->first();

return $this->refund($order, $request);
});

if ($refund->success) {
event(new OrderRefunded($order, $request->notify));
}

return $refund;
}

protected function refund(Order $order, RefundRequest $request): PaymentRefund
{
/** @var Order $order */
/** @var Transaction $transaction */
Expand Down Expand Up @@ -61,8 +80,6 @@ public function execute(Order $order, RefundRequest $request): PaymentRefund

if ($refund->success) {
$this->recordLineAllocations($refund, $lineAllocations);

event(new OrderRefunded($order, $request->notify));
}

return $refund;
Expand Down Expand Up @@ -126,18 +143,16 @@ protected function recordLineAllocations(PaymentRefund $refund, array $allocatio
return;
}

DB::transaction(function () use ($refund, $allocations) {
foreach ($allocations as $allocation) {
RefundLine::create([
'transaction_id' => $refund->transaction->id,
'order_line_id' => $allocation['order_line_id'],
'quantity' => $allocation['quantity'],
'amount' => $allocation['amount'],
]);
foreach ($allocations as $allocation) {
RefundLine::create([
'transaction_id' => $refund->transaction->id,
'order_line_id' => $allocation['order_line_id'],
'quantity' => $allocation['quantity'],
'amount' => $allocation['amount'],
]);

OrderLine::whereKey($allocation['order_line_id'])->increment('refunded_quantity', $allocation['quantity']);
}
});
OrderLine::whereKey($allocation['order_line_id'])->increment('refunded_quantity', $allocation['quantity']);
}
}

/**
Expand Down
22 changes: 22 additions & 0 deletions tests/core/Unit/Actions/Orders/CaptureOrderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,28 @@
app(CaptureOrder::class)->execute($order, $capture->id, '10.00');
})->throws(OrderActionException::class);

it('rejects capturing an order that already has a successful capture', function () {
$order = Order::factory()->create(['currency_code' => $this->currency->code]);

$intent = Transaction::factory()->create([
'order_id' => $order->id,
'type' => 'intent',
'success' => true,
'driver' => 'testing',
'amount' => 5_000,
]);

Transaction::factory()->create([
'order_id' => $order->id,
'type' => 'capture',
'success' => true,
'driver' => 'testing',
'amount' => 5_000,
]);

app(CaptureOrder::class)->execute($order, $intent->id, '50.00');
})->throws(OrderActionException::class, 'Order has already been captured.');

it('reports canRun false when a capture already exists', function () {
$order = Order::factory()->create(['currency_code' => $this->currency->code]);

Expand Down
Loading