Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Jan 3, 2024
1 parent 941a258 commit 37a56c6
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 48 deletions.
2 changes: 1 addition & 1 deletion database/factories/OrderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class OrderFactory extends Factory
public function definition(): array
{
return [
'discount' => mt_rand(10, 1000),
'discount' => 0,
'currency' => 'usd',
];
}
Expand Down
2 changes: 0 additions & 2 deletions src/Events/CheckoutProcessed.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@

use Cone\Bazar\Models\Order;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class CheckoutProcessed
{
use Dispatchable;
use SerializesModels;

/**
* The order instance.
Expand Down
9 changes: 7 additions & 2 deletions src/Gateway/CashDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@

class CashDriver extends Driver
{
/**
* The driver name.
*/
protected string $name = 'cash';

/**
* Process the payment.
*/
public function pay(Order $order, float $amount = null, array $attributes = []): Transaction
{
$transaction = $order->pay($amount, 'cash', array_merge($attributes, [
$transaction = parent::pay($order, $amount, array_merge($attributes, [
'completed_at' => time(),
]));

Expand All @@ -28,7 +33,7 @@ public function pay(Order $order, float $amount = null, array $attributes = []):
*/
public function refund(Order $order, float $amount = null, array $attributes = []): Transaction
{
$transaction = $order->refund($amount, 'cash', array_merge($attributes, [
$transaction = parent::refund($order, $amount, array_merge($attributes, [
'completed_at' => time(),
]));

Expand Down
15 changes: 13 additions & 2 deletions src/Gateway/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,26 @@

abstract class Driver extends BaseDriver
{
/**
* The driver name.
*/
protected string $name;

/**
* Process the payment.
*/
abstract public function pay(Order $order, float $amount = null, array $attributes = []): Transaction;
public function pay(Order $order, float $amount = null, array $attributes = []): Transaction
{
return $order->pay($amount, $this->name, $attributes);
}

/**
* Process the refund.
*/
abstract public function refund(Order $order, float $amount = null, array $attributes = []): Transaction;
public function refund(Order $order, float $amount = null, array $attributes = []): Transaction
{
return $order->refund($amount, $this->name, $attributes);
}

/**
* Get the URL of the transaction.
Expand Down
16 changes: 2 additions & 14 deletions src/Gateway/TransferDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,14 @@
namespace Cone\Bazar\Gateway;

use Cone\Bazar\Models\Order;
use Cone\Bazar\Models\Transaction;
use Illuminate\Http\Request;

class TransferDriver extends Driver
{
/**
* Process the payment.
* The driver name.
*/
public function pay(Order $order, float $amount = null, array $attributes = []): Transaction
{
return $order->pay($amount, 'transfer', $attributes);
}

/**
* Process the refund.
*/
public function refund(Order $order, float $amount = null, array $attributes = []): Transaction
{
return $order->refund($amount, 'transfer', $attributes);
}
protected string $name = 'transfer';

/**
* Handle the checkout request.
Expand Down
2 changes: 1 addition & 1 deletion src/Listeners/PlaceOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function handle(CheckoutProcessed $event): void
{
$event->order->markAs(Order::IN_PROGRESS);

if ($event->order->cart) {
if (! is_null($event->order->cart)) {
$event->order->cart->delete();
}
}
Expand Down
26 changes: 21 additions & 5 deletions src/Models/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Support\Facades\Date;

class Cart extends Model implements Contract
Expand Down Expand Up @@ -102,9 +103,21 @@ public function getMorphClass(): string
*/
public function order(): BelongsTo
{
return $this->belongsTo(Order::getProxiedClass())->withDefault(function (): Order {
return Order::proxy()->newInstance($this->toArray());
});
return $this->belongsTo(Order::getProxiedClass())
->withDefault(function (Order $order): Order {
return $order->fill($this->toArray());
});
}

/**
* Get the address for the model.
*/
public function address(): MorphOne
{
return $this->morphOne(Address::getProxiedClass(), 'addressable')
->withDefault(function (Address $address): Address {
return $address->fill($this->user?->address?->toArray() ?: []);
});
}

/**
Expand Down Expand Up @@ -167,8 +180,11 @@ public function toOrder(): Order
$this->order->items()->createMany($this->items->toArray());

$this->order->address->fill($this->address->toArray())->save();
$this->order->shipping->fill($this->shipping->toArray())->save();
$this->order->shipping->address->fill($this->shipping->address->toArray())->save();

if ($this->order->needsShipping()) {
$this->order->shipping->fill($this->shipping->toArray())->save();
$this->order->shipping->address->fill($this->shipping->address->toArray())->save();
}

return $this->order;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Cart/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public function test_cart_updates_billing(): void
$this->assertSame('Test', $this->manager->getBilling()->first_name);
}

public function test_cart_has_getTotal(): void
public function test_cart_has_total(): void
{
$this->assertEquals(
$this->manager->getModel()->total, $this->manager->getTotal()
Expand Down
35 changes: 15 additions & 20 deletions tests/Gateway/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Cone\Bazar\Models\Transaction;
use Cone\Bazar\Tests\TestCase;
use Exception;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\Notification;

class ManagerTest extends TestCase
Expand Down Expand Up @@ -102,15 +103,15 @@ public function test_gateway_has_transfer_driver(): void
$this->assertInstanceOf(TransferDriver::class, $driver);
$this->assertSame('Transfer', $driver->getName());

$payment = $driver->pay($this->order, 1);
$payment = $driver->pay($this->order, 1, ['completed_at' => Date::now()]);
$this->assertEquals(1, $payment->amount);
$payment = $driver->pay($this->order);
$payment = $driver->pay($this->order, null, ['completed_at' => Date::now()]);
$this->assertTrue($this->order->refresh()->paid());
$this->assertNull($driver->getTransactionUrl($payment));

$refund = $driver->refund($this->order, 1);
$refund = $driver->refund($this->order, 1, ['completed_at' => Date::now()]);
$this->assertEquals(1, $refund->amount);
$refund = $driver->refund($this->order);
$refund = $driver->refund($this->order, null, ['completed_at' => Date::now()]);
$this->assertTrue($this->order->refresh()->refunded());
$this->assertNull($driver->getTransactionUrl($payment));

Expand All @@ -127,15 +128,15 @@ public function test_gateway_can_have_custom_driver(): void
$driver = $this->manager->driver('custom-driver');
$this->assertInstanceOf(CustomGatewayDriver::class, $driver);

$payment = $driver->pay($this->order, 1);
$payment = $driver->pay($this->order, 1, ['completed_at' => Date::now()]);
$this->assertEquals(1, $payment->amount);
$payment = $driver->pay($this->order);
$payment = $driver->pay($this->order, null, ['completed_at' => Date::now()]);
$this->assertTrue($this->order->refresh()->paid());
$this->assertSame('fake-url', $driver->getTransactionUrl($payment));

$refund = $driver->refund($this->order, 1);
$refund = $driver->refund($this->order, 1, ['completed_at' => Date::now()]);
$this->assertEquals(1, $refund->amount);
$refund = $driver->refund($this->order);
$refund = $driver->refund($this->order, null, ['completed_at' => Date::now()]);
$this->assertTrue($this->order->refresh()->refunded());
$this->assertSame('fake-url', $driver->getTransactionUrl($payment));
}
Expand Down Expand Up @@ -163,31 +164,25 @@ public function test_gateway_handles_failed_checkout(): void

class CustomGatewayDriver extends Driver
{
protected string $name = 'custom-driver';

public function getTransactionUrl(Transaction $transaction): ?string
{
return 'fake-url';
}

public function pay(Order $order, float $amount = null, array $attributes = []): Transaction
{
return $order->pay($amount, 'custom-driver');
}

public function refund(Order $order, float $amount = null, array $attributes = []): Transaction
{
return $order->refund($amount, 'custom-driver');
}
}

class FailingDriver extends Driver
{
protected string $name = 'failing';

public function pay(Order $order, float $amount = null, array $attributes = []): Transaction
{
throw new Exception;
throw new Exception();
}

public function refund(Order $order, float $amount = null, array $attributes = []): Transaction
{
throw new Exception;
throw new Exception();
}
}

0 comments on commit 37a56c6

Please sign in to comment.