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
15 changes: 11 additions & 4 deletions app/Actions/Shop/CheckoutService.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Omnipay\Common\Exception\InvalidCreditCardException;
use Omnipay\Common\Message\RedirectResponseInterface;
use Omnipay\Common\Message\ResponseInterface;
use Omnipay\Dummy\Message\Response as DummyResponse;
use Omnipay\Mollie\Message\Response\FetchTransactionResponse;

/**
Expand Down Expand Up @@ -103,13 +104,21 @@ public function processPayment(Order $order, string $return_url, string $cancel_
redirect_url: $redirect_url,
);
} elseif ($response->isSuccessful()) {
if ($response instanceof DummyResponse) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now, only Dummy is successful directly. Later consider the other cases but I expect most integration will have a redirection.

// We need those metadata for later completion
$metadata = [];
$reference = $response->getTransactionReference();
$metadata['transactionReference'] = $reference;
Session::put('metadata.' . $order->id, $metadata);
}

// Payment was successful
$this->completePayment($order, $response);
$order->transaction_id = $response->getTransactionReference();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only save the transaction id to make sure that we do a proper finalization.

$order->save();

return new CheckoutDTO(
is_success: true,
is_redirect: false,
redirect_url: $return_url,
);
} else {
// Payment failed
Expand All @@ -122,8 +131,6 @@ public function processPayment(Order $order, string $return_url, string $cancel_
);
}
} catch (\Exception|InvalidCreditCardException $e) {
// dd($e);
// TODO: later do better error management
Log::error('Error processing payment: ' . $e->getMessage(), [
'order_id' => $order->id,
'transaction_id' => $order->transaction_id,
Expand Down
13 changes: 11 additions & 2 deletions app/Http/Controllers/Shop/CheckoutController.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,25 @@ public function process(ProcessRequest $request): CheckoutResource
$request->additional_data ?? []
);

// If we have a success directly without redirection mark order as completed
if ($result->is_success && !$result->is_redirect) {
OrderCompleted::dispatchIf(Configs::getValueAsBool('webshop_auto_fulfill_enabled'), $order->id);
// This is a success we now need to complete the order.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This way the OrderCompleted event is only dispatched in the finalize step, not in the processing step.
Furthermore that clarifies that we have complete_url and redirect_url, showing different intent.

$order->refresh();

return new CheckoutResource(
is_success: true,
is_redirect: false,
complete_url: URL::route('shop.checkout.return', ['provider' => $order->provider->value, 'transaction_id' => $order->transaction_id]),
message: '',
order: OrderResource::fromModel($order),
);
}

return new CheckoutResource(
is_success: $result->is_success,
is_redirect: $result->is_redirect,
redirect_url: $result->redirect_url,
message: $result->message ?? '',
order: OrderResource::fromModel($order),
);
}

Expand Down
7 changes: 5 additions & 2 deletions app/Http/Requests/Order/MarkAsDeliveredOrderRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public function authorize(): bool
return count(
array_diff(
array_column($this->items, 'id'),
$this->order->items->pluck('id')->toArray())
$this->order->items->pluck('id')->toArray()
)
) === 0;
}

Expand All @@ -65,7 +66,9 @@ protected function processValidatedValues(array $values, array $files): void
/** @var int $order_id */
$order_id = intval($values['order_id'] ?? null);
$this->order = Order::query()
->where('status', '=', PaymentStatusType::COMPLETED)
->where(fn ($query) => $query
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mark as delivered can be used to provide links/update links even when the request is closed. hence the change.

->where('status', '=', PaymentStatusType::COMPLETED)
->orWhere('status', '=', PaymentStatusType::CLOSED))
->where('id', '=', $order_id)
->firstOrFail();

Expand Down
1 change: 1 addition & 0 deletions app/Http/Resources/Shop/CheckoutResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function __construct(
public readonly bool $is_success,
public readonly bool $is_redirect = false,
public readonly ?string $redirect_url = null,
public readonly ?string $complete_url = null,
public readonly string $message = '',
public readonly ?OrderResource $order = null,
) {
Expand Down
3 changes: 2 additions & 1 deletion tests/Unit/Actions/Shop/CheckoutServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ public function testProcessPaymentSuccessfulDirect(): void
$this->assertInstanceOf(CheckoutDTO::class, $result);
$this->assertTrue($result->is_success);
$this->assertFalse($result->is_redirect);
$this->assertEquals('https://example.com/return', $result->redirect_url);
$this->assertEquals('', $result->message);
$this->assertNull($result->redirect_url);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,13 @@ public function testCompleteCheckoutFlow(): void
'is_success' => true,
]);

// For DUMMY provider, payment should complete immediately without redirect
if (!$process_response->json('is_redirect')) {
// Verify order status was updated
$this->test_order->refresh();
$this->assertEquals(PaymentStatusType::COMPLETED, $this->test_order->status);
}
// Verify order status was updated
$this->test_order->refresh();
$this->assertEquals(PaymentStatusType::PROCESSING, $this->test_order->status);

$response = $this->get('/api/v2/Shop/Checkout/Finalize/Dummy/' . $this->test_order->transaction_id);
$this->assertRedirect($response);
$response->assertRedirect(route('shop.checkout.complete'));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public function testMarkClosedOrderAsDelivered(): void
$response = $this->actingAs($this->admin)
->putJson('Shop/Order/' . $this->test_order->id);

$this->assertNotFound($response);
$response->assertNoContent();

// Verify status wasn't changed
$this->assertDatabaseHas('orders', [
Expand Down