Skip to content

Commit 3196f42

Browse files
committed
Merge remote-tracking branch 'origin/main' into query-builder
# Conflicts: # src/Widgets/TopCustomers.php
2 parents d3d32ce + ea5d522 commit 3196f42

20 files changed

+225
-57
lines changed

docs/php-api.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,9 @@ $order->coupon();
111111
// Set the coupon (either pass in the ID of a coupon or a Coupon instance)
112112
$order->coupon($coupon);
113113

114-
// Get the current gateway
115-
$order->currentGateway();
114+
// Get the order's gateway & payment data
115+
$order->gatewayData()->gateway()->name(); // Returns the gateway's name
116+
$order->gatewayData()->data()->all(); // Returns an array of the gateway/payment data
116117

117118
// Get the shipping address (returns an Address instance)
118119
$shippingAddress = $order->shippingAddress();

docs/upgrade-guides/v5-x-to-v6-0.md

+16
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,22 @@ This saves you needing to `find` the order/product/customer to use any of Simple
8080

8181
The "Overview" page has been removed in Simple Commerce v6, in favour of Dashboard Widgets. To configure Simple Commerce widgets, review the [Control Panel](/control-panel#content-widgets) page.
8282

83+
### Medium: Order Gateway methods have changed
84+
85+
Both the `gateway` and `currentGateway` methods on orders have been replaced. If you were calling these methods anywhere in your code, you should update your code to use the new `gatewayData` method:
86+
87+
```php
88+
// Get the gateway's name & handle
89+
$order->gatewayData()->gateway()->name();
90+
$order->gatewayData()->gateway()::handle();
91+
92+
// Get the gateway / payment info
93+
$order->gatewayData()->data();
94+
95+
// Get any refund data
96+
$order->gatewayData()->refund();
97+
```
98+
8399
### Medium: Changes to the `statusLog` method on Orders
84100

85101
The `statusLog` method no longer accepts passing a status. Instead, you should query the status log for the event you're after, then get the `->last()` item in the collection.

src/Actions/RefundAction.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace DoubleThreeDigital\SimpleCommerce\Actions;
44

5-
use DoubleThreeDigital\SimpleCommerce\Facades\Gateway;
65
use DoubleThreeDigital\SimpleCommerce\Facades\Order;
76
use DoubleThreeDigital\SimpleCommerce\Orders\EntryOrderRepository;
87
use DoubleThreeDigital\SimpleCommerce\SimpleCommerce;
@@ -44,8 +43,7 @@ public function run($items, $values)
4443
->each(function ($entry) {
4544
$order = Order::find($entry->id);
4645

47-
return Gateway::use($order->currentGateway()['handle'])
48-
->refund($order);
46+
return $order->gatewayData()->gateway()->refund($order);
4947
});
5048
}
5149

src/Console/Commands/MigrateOrdersToDatabase.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ protected function migrateOrders(string $collectionHandle): self
158158
}
159159

160160
if ($gateway = $entry->get('gateway')) {
161-
$order->gateway($gateway);
161+
$order->gatewayData(gateway: $gateway);
162162
}
163163

164164
$order->data($data);

src/Contracts/GatewayManager.php

+2
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,6 @@ public function callbackUrl(array $extraParamters = []): string;
3535
public function withRedirectUrl(string $redirectUrl): self;
3636

3737
public function withErrorRedirectUrl(string $errorRedirectUrl): self;
38+
39+
public function resolve();
3840
}

src/Contracts/ShippingManager.php

+2
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@ public function description(): string;
1313
public function calculateCost(Order $order): int;
1414

1515
public function checkAvailability(Order $order, Address $address): bool;
16+
17+
public function resolve();
1618
}

src/Gateways/BaseGateway.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,7 @@ public function markOrderAsPaid(Order $order, array $data = []): bool
130130
{
131131
// We need to ensure that the gateway is available in the
132132
// order when the OrderPaid event is dispatched.
133-
$order->gateway([
134-
'use' => static::handle(),
135-
'data' => [],
136-
]);
133+
$order->gatewayData(gateway: static::handle(), data: []);
137134

138135
if ($this->isOffsiteGateway()) {
139136
$order = app(CheckoutPipeline::class)

src/Gateways/Builtin/PayPalGateway.php

+1-9
Original file line numberDiff line numberDiff line change
@@ -210,15 +210,7 @@ public function webhook(Request $request)
210210
->save();
211211
}
212212

213-
$order->gateway(
214-
array_merge(
215-
$order->gateway(),
216-
[
217-
'data' => $responseBody,
218-
]
219-
)
220-
);
221-
213+
$order->gatewayData(data: $responseBody);
222214
$order->save();
223215

224216
$this->markOrderAsPaid($order);

src/Gateways/Builtin/StripeGateway.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ public function refund(OrderContract $order): array
129129

130130
$paymentIntent = null;
131131

132-
if (isset($order->gateway()['data']['payment_intent'])) {
133-
$paymentIntent = $order->gateway()['data']['payment_intent'];
132+
if ($order->gatewayData()->data()->has('payment_intent')) {
133+
$paymentIntent = $order->gatewayData()->data()->get('payment_intent');
134134
}
135135

136136
if (isset($order->get('stripe')['intent'])) {

src/Gateways/Manager.php

+1-6
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,7 @@ public function checkout($request, $order)
5959
}
6060

6161
$order = Order::find($order->id());
62-
63-
$order->gateway([
64-
'use' => $this->handle,
65-
'data' => $checkout,
66-
]);
67-
62+
$order->gatewayData(gateway: $this->handle, data: $checkout);
6863
$order->save();
6964

7065
return $checkout;

src/Http/Controllers/Concerns/HandlesCustomerInformation.php

+8
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ protected function handleCustomerInformation(Request $request, OrderContract $ca
5959

6060
private function findOrCreateCustomer(OrderContract $cart, string $email): OrderContract
6161
{
62+
// If the order already has a customer assigned, update the email on the existing customer.
63+
if ($customer = $cart->customer()) {
64+
$customer->email($email)->save();
65+
66+
return $cart;
67+
}
68+
69+
// Otherwise, find or create a customer with the provided email.
6270
try {
6371
$customer = Customer::findByEmail($email);
6472
$cart->customer($customer->id());

src/Orders/EloquentOrderRepository.php

+12-3
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function find($id): ?Order
5555

5656
public function fromModel(OrderModel $model)
5757
{
58-
return app(Order::class)
58+
$order = app(Order::class)
5959
->resource($model)
6060
->id($model->id)
6161
->orderNumber($model->order_number)
@@ -69,7 +69,6 @@ public function fromModel(OrderModel $model)
6969
->couponTotal($model->coupon_total)
7070
->customer($model->customer_id)
7171
->coupon($model->coupon)
72-
->gateway($model->gateway)
7372
->data(
7473
collect($model->data)
7574
->merge([
@@ -97,6 +96,16 @@ public function fromModel(OrderModel $model)
9796
->toArray()
9897
)
9998
);
99+
100+
if ($model->gateway) {
101+
$order->gatewayData(
102+
gateway: $model->gateway['use'] ?? null,
103+
data: $model->gateway['data'] ?? null,
104+
refund: $model->gateway['refund'] ?? null
105+
);
106+
}
107+
108+
return $order;
100109
}
101110

102111
public function make(): Order
@@ -123,7 +132,7 @@ public function save($order): void
123132
$model->coupon_total = $order->couponTotal();
124133
$model->customer_id = $order->customer() instanceof CustomerContract ? $order->customer()->id() : $order->customer();
125134
$model->coupon = $order->coupon() instanceof CouponContract ? $order->coupon()->id() : $order->coupon();
126-
$model->gateway = $order->gateway();
135+
$model->gateway = $order->gatewayData()?->toArray();
127136

128137
$model->shipping_name = $order->get('shipping_name');
129138
$model->shipping_address = $order->get('shipping_address');

src/Orders/EntryOrderRepository.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,12 @@ public function fromEntry($entry): Order
7070
$order->coupon($entry->get('coupon'));
7171
}
7272

73-
if ($entry->has('gateway')) {
74-
$order->gateway($entry->get('gateway'));
73+
if ($gatewayData = $entry->get('gateway')) {
74+
$order->gatewayData(
75+
gateway: $gatewayData['use'] ?? null,
76+
data: $gatewayData['data'] ?? null,
77+
refund: $gatewayData['refund'] ?? null,
78+
);
7579
}
7680

7781
return $order->data(array_merge(
@@ -127,7 +131,7 @@ public function save(Order $order): void
127131
'coupon_total' => $order->couponTotal(),
128132
'customer' => $order->customer() instanceof CustomerContract ? $order->customer()->id() : $order->customer(),
129133
'coupon' => $order->coupon() instanceof CouponContract ? $order->coupon()->id() : $order->coupon(),
130-
'gateway' => $order->gateway(),
134+
'gateway' => $order->gatewayData()?->toArray(),
131135
],
132136
)
133137
);

src/Orders/GatewayData.php

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace DoubleThreeDigital\SimpleCommerce\Orders;
4+
5+
use DoubleThreeDigital\SimpleCommerce\Facades\Gateway;
6+
use DoubleThreeDigital\SimpleCommerce\Gateways\Manager;
7+
use Illuminate\Support\Collection;
8+
9+
class GatewayData
10+
{
11+
public $gateway;
12+
13+
public $data;
14+
15+
public $refund;
16+
17+
public function __construct(array $gatewayData)
18+
{
19+
if (isset($gatewayData['use'])) {
20+
$this->gateway = $gatewayData['use'];
21+
}
22+
23+
if (isset($gatewayData['data'])) {
24+
$this->data = collect($gatewayData['data']);
25+
}
26+
27+
if (isset($gatewayData['refund'])) {
28+
$this->refund = collect($gatewayData['refund']);
29+
}
30+
}
31+
32+
public function gateway(): Manager
33+
{
34+
if (! $this->gateway) {
35+
return null;
36+
}
37+
38+
return Gateway::use($this->gateway);
39+
}
40+
41+
public function data(): Collection
42+
{
43+
return $this->data;
44+
}
45+
46+
public function refund(): ?Collection
47+
{
48+
return $this->refund;
49+
}
50+
51+
public function toArray(): array
52+
{
53+
return [
54+
'use' => $this->gateway,
55+
'data' => $this->data ? $this->data->toArray() : null,
56+
'refund' => $this->refund ? $this->refund->toArray() : null,
57+
];
58+
}
59+
}

src/Orders/Order.php

+18-13
Original file line numberDiff line numberDiff line change
@@ -206,16 +206,25 @@ public function coupon($coupon = null)
206206
->args(func_get_args());
207207
}
208208

209-
public function gateway($gateway = null)
209+
public function gatewayData(?string $gateway = null, ?array $data = null, ?array $refund = null): ?GatewayData
210210
{
211-
return $this
212-
->fluentlyGetOrSet('gateway')
213-
->args(func_get_args());
214-
}
211+
if ($gateway) {
212+
$this->gateway = array_merge($this->gateway ?? [], ['use' => $gateway]);
213+
}
215214

216-
public function currentGateway(): ?array
217-
{
218-
return SimpleCommerce::gateways()->firstWhere('handle', $this->gateway()['use']);
215+
if ($data) {
216+
$this->gateway = array_merge($this->gateway ?? [], ['data' => $data]);
217+
}
218+
219+
if ($refund) {
220+
$this->gateway = array_merge($this->gateway ?? [], ['refund' => $refund]);
221+
}
222+
223+
if (! $this->gateway) {
224+
return null;
225+
}
226+
227+
return new GatewayData($this->gateway);
219228
}
220229

221230
public function resource($resource = null)
@@ -354,11 +363,7 @@ public function refund($refundData): self
354363
{
355364
$this->updatePaymentStatus(PaymentStatus::Refunded);
356365

357-
$data = array_merge($this->gateway(), [
358-
'refund' => $refundData,
359-
]);
360-
361-
$this->gateway($data);
366+
$this->gatewayData(refund: $refundData);
362367

363368
return $this;
364369
}

src/Tags/CheckoutTags.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,7 @@ public function wildcard(string $tag)
121121

122122
$prepare = $prepare->prepare(request(), $cart);
123123

124-
$cart->gateway([
125-
...$cart->gateway() ?? [],
126-
'use' => $gateway['class']::handle(),
127-
]);
128-
124+
$cart->gatewayData(gateway: $gateway['class']::handle());
129125
$cart->set($gateway['handle'], $prepare);
130126

131127
$cart->save();

src/Widgets/TopCustomers.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use DoubleThreeDigital\SimpleCommerce\Facades\Customer;
99
use DoubleThreeDigital\SimpleCommerce\SimpleCommerce;
1010
use DoubleThreeDigital\SimpleCommerce\Support\Runway;
11+
use Illuminate\Database\SQLiteConnection;
1112
use Statamic\Widgets\Widget;
1213

1314
class TopCustomers extends Widget
@@ -31,7 +32,11 @@ public function html()
3132
$query
3233
->where('orders', '!=', null)
3334
->orderBy(function ($query) {
34-
$query->selectRaw('JSON_ARRAY_LENGTH(orders)');
35+
$query->when($query->connection instanceof SQLiteConnection, function ($query) {
36+
$query->selectRaw('JSON_ARRAY_LENGTH(orders)');
37+
}, function ($query) {
38+
$query->selectRaw('JSON_LENGTH(orders)');
39+
});
3540
}, 'desc')
3641
->limit($this->config('limit', 5));
3742
})

tests/Gateways/Builtin/StripeGatewayTest.php

+7-5
Original file line numberDiff line numberDiff line change
@@ -379,15 +379,17 @@
379379
test('can refund charge', function () {
380380
Stripe::setApiKey(env('STRIPE_SECRET'));
381381

382-
$order = Order::make()->grandTotal(1234)->gateway([
383-
'use' => StripeGateway::handle(),
384-
'data' => [
382+
$order = Order::make()->grandTotal(1234);
383+
384+
$order->gatewayData(
385+
gateway: StripeGateway::handle(),
386+
data: [
385387
'payment_intent' => $paymentIntent = PaymentIntent::create([
386388
'amount' => 1234,
387389
'currency' => 'GBP',
388390
])->id,
389-
],
390-
]);
391+
]
392+
);
391393

392394
$order->save();
393395

0 commit comments

Comments
 (0)