Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanmcclean committed Jan 27, 2025
1 parent 2c2cc24 commit f50ed14
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 18 deletions.
2 changes: 2 additions & 0 deletions routes/actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use DuncanMcClean\SimpleCommerce\Http\Controllers\CartController;
use DuncanMcClean\SimpleCommerce\Http\Controllers\CartLineItemsController;
use DuncanMcClean\SimpleCommerce\Http\Controllers\CartPaymentGatewaysController;
use DuncanMcClean\SimpleCommerce\Http\Controllers\CartShippingController;
use DuncanMcClean\SimpleCommerce\Http\Controllers\DigitalProducts\DownloadController;
use DuncanMcClean\SimpleCommerce\Http\Controllers\Payments\CheckoutController;
Expand All @@ -22,6 +23,7 @@
Route::delete('line-items/{lineItem}', [CartLineItemsController::class, 'destroy'])->name('line-items.destroy');

Route::get('shipping', CartShippingController::class)->name('shipping');
Route::get('payment-gateways', CartPaymentGatewaysController::class)->name('payment-gateways');
Route::match(['get', 'post'], 'checkout', CheckoutController::class)->name('checkout');
});

Expand Down
29 changes: 29 additions & 0 deletions src/Http/Controllers/CartPaymentGatewaysController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace DuncanMcClean\SimpleCommerce\Http\Controllers;

use DuncanMcClean\SimpleCommerce\Facades;
use DuncanMcClean\SimpleCommerce\Facades\Cart as CartFacade;
use DuncanMcClean\SimpleCommerce\Payments\Gateways\PaymentGateway;

class CartPaymentGatewaysController
{
public function __invoke()
{
$cart = CartFacade::current();

return Facades\PaymentGateway::all()
->map(function (PaymentGateway $paymentGateway) use ($cart) {
$setup = $cart->isFree() ? [] : $paymentGateway->setup($cart);

return [
'name' => $paymentGateway->title(),
'handle' => $paymentGateway->handle(),
'checkout_url' => $paymentGateway->checkoutUrl(),
...$setup,
];
})
->values()
->all();
}
}
10 changes: 6 additions & 4 deletions src/Http/Controllers/Payments/CheckoutController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use DuncanMcClean\SimpleCommerce\Orders\LineItem;
use DuncanMcClean\SimpleCommerce\Orders\OrderStatus;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
use Statamic\Exceptions\NotFoundHttpException;

Expand Down Expand Up @@ -47,9 +46,12 @@ public function __invoke(Request $request, ?string $paymentGateway = null)
}
}

$order->isFree()
? $order->status(OrderStatus::PaymentReceived)->save()
: $paymentGateway->process($order, $request);
if ($order->isFree()) {
$order->status(OrderStatus::PaymentReceived)->save();
} else {
$paymentGateway->process($order, $request);
$order->set('payment_gateway', $paymentGateway::handle())->save();
}
} catch (ValidationException|PreventCheckout $e) {
$paymentGateway->cancel($cart);

Expand Down
5 changes: 1 addition & 4 deletions src/Payments/Gateways/Dummy.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ public function setup(Cart $cart): array

public function process(Order $order): void
{
$order
->set('payment_gateway', static::handle())
->status(OrderStatus::PaymentReceived)
->save();
$order->status(OrderStatus::PaymentReceived)->save();
}

public function capture(Order $order): void
Expand Down
12 changes: 8 additions & 4 deletions src/Payments/Gateways/Stripe.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ public function setup(Cart $cart): array
'customer' => $stripeCustomerId,
]);

return ['client_secret' => $paymentIntent->client_secret];
return [
'api_key' => $this->config()->get('key'),
'client_secret' => $paymentIntent->client_secret,
];
}

$intentData = [
Expand All @@ -76,7 +79,10 @@ public function setup(Cart $cart): array

$cart->set('stripe_payment_intent', $paymentIntent->id)->save();

return ['client_secret' => $paymentIntent->client_secret];
return [
'api_key' => $this->config()->get('key'),
'client_secret' => $paymentIntent->client_secret,
];
}

public function afterRecalculating(Cart $cart): void
Expand All @@ -88,8 +94,6 @@ public function afterRecalculating(Cart $cart): void

public function process(Order $order): void
{
$order->set('payment_gateway', static::handle())->save();

PaymentIntent::update($order->get('stripe_payment_intent'), [
'description' => __('Order #:orderNumber', ['orderNumber' => $order->orderNumber()]),
'metadata' => [
Expand Down
7 changes: 1 addition & 6 deletions src/Tags/PaymentGateways.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ public function index()
{
$cart = CartFacade::current();

if ($cart->isFree()) {
return [];
}

if (! Blink::has(self::BLINK_KEY)) {
Blink::put(self::BLINK_KEY, $this->getPaymentGateways($cart));
}
Expand All @@ -31,12 +27,11 @@ private function getPaymentGateways($cart)
{
return Facades\PaymentGateway::all()
->map(function (PaymentGateway $paymentGateway) use ($cart) {
$setup = $paymentGateway->setup($cart);
$setup = $cart->isFree() ? [] : $paymentGateway->setup($cart);

return [
'name' => $paymentGateway->title(),
'handle' => $paymentGateway->handle(),
'gateway_config' => $paymentGateway->config()->all(),
'checkout_url' => $paymentGateway->checkoutUrl(),
...$setup,
];
Expand Down
4 changes: 4 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use DuncanMcClean\SimpleCommerce\Payments\PaymentServiceProvider;
use DuncanMcClean\SimpleCommerce\ServiceProvider;
use Illuminate\Support\Facades\Route;
use Statamic\Facades\File;
use Statamic\Facades\Site;
use Statamic\Facades\YAML;
Expand Down Expand Up @@ -41,6 +42,9 @@ protected function resolveApplicationConfiguration($app)
'class' => \Statamic\Stache\Stores\UsersStore::class,
'directory' => __DIR__.'/__fixtures__/users',
]);

Route::get('checkout', fn () => 'Checkout')->name('checkout');
Route::get('checkout/confirmation', fn () => 'Confirmation')->name('checkout.confirmation');
}

protected function getPackageProviders($app)
Expand Down

0 comments on commit f50ed14

Please sign in to comment.