Skip to content

Commit

Permalink
Repair the payment callback, optimize the order placement logic, opti…
Browse files Browse the repository at this point in the history
…mize the classification product order filtering logic, multi-language optimization, and add API interfaces for products and quick order placement.
  • Loading branch information
yushine committed Jul 30, 2024
1 parent 3903922 commit b47204a
Show file tree
Hide file tree
Showing 33 changed files with 503 additions and 130 deletions.
4 changes: 4 additions & 0 deletions bootstrap/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
return front_route('login.index');
}
});

$middleware->validateCsrfTokens(except: [
'callback*'
]);
})
->withExceptions(function (Exceptions $exceptions) {
//
Expand Down
4 changes: 2 additions & 2 deletions innopacks/common/config/innoshop.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

return [
'version' => '0.2.0',
'build' => '20240729',
'version' => '0.2.1',
'build' => '20240730',
'api_url' => env('INNOSHOP_API_URL', 'https://www.innoshop.cn'),
];
2 changes: 1 addition & 1 deletion innopacks/common/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ function innoshop_version(): string
* @param mixed $builder
* @return string
*/
function to_sql(Builder $builder): string
function to_sql(mixed $builder): string
{
$sql = $builder->toSql();
$driver = DB::getDriverName();
Expand Down
9 changes: 9 additions & 0 deletions innopacks/common/src/Models/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace InnoShop\Common\Models;

use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Notifications\Notifiable;
use InnoShop\Common\Models\Order\Fee;
Expand Down Expand Up @@ -39,6 +40,14 @@ class Order extends BaseModel
'status_format',
];

/**
* @return BelongsTo
*/
public function customer(): BelongsTo
{
return $this->belongsTo(Customer::class, 'customer_id', 'id');
}

/**
* Order items.
*
Expand Down
4 changes: 2 additions & 2 deletions innopacks/common/src/Repositories/CartItemRepo.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function create($data): mixed
*/
private function handleData($requestData): array
{
$skuId = $requestData['skuId'] ?? 0;
$skuId = $requestData['skuId'] ?? ($requestData['sku_id'] ?? 0);
$sku = Sku::query()->findOrFail($skuId);

$customerID = $requestData['customer_id'] ?? 0;
Expand All @@ -89,7 +89,7 @@ private function handleData($requestData): array
'customer_id' => $customerID,
'guest_id' => $customerID ? '' : $guestID,
'selected' => true,
'quantity' => (int) ($requestData['quantity'] ?? 0),
'quantity' => (int) ($requestData['quantity'] ?? 1),
];
}
}
8 changes: 8 additions & 0 deletions innopacks/common/src/Repositories/CategoryRepo.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ public function builder(array $filters = []): Builder
$builder->where('slug', $slug);
}

$parentSlug = $filters['parent_slug'] ?? '';
if ($parentSlug) {
$category = Category::query()->where('slug', $parentSlug)->first();
if ($category) {
$filters['parent_id'] = $category->id;
}
}

if (isset($filters['parent_id'])) {
$parentID = (int) $filters['parent_id'];
if ($parentID == 0) {
Expand Down
6 changes: 6 additions & 0 deletions innopacks/common/src/Repositories/Order/ItemRepo.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace InnoShop\Common\Repositories\Order;

use Exception;
use InnoShop\Common\Models\Order;
use InnoShop\Common\Models\Product\Sku;
use InnoShop\Common\Repositories\BaseRepo;
Expand All @@ -19,9 +20,14 @@ class ItemRepo extends BaseRepo
* @param Order $order
* @param $items
* @return void
* @throws Exception
*/
public function createItems(Order $order, $items): void
{
if (empty($items)) {
throw new Exception('Empty cart list when create order items.');
}

$orderItems = [];
foreach ($items as $item) {
$orderItems[] = $this->handleItem($order, $item);
Expand Down
25 changes: 23 additions & 2 deletions innopacks/common/src/Repositories/OrderRepo.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ class OrderRepo extends BaseRepo
*/
public function getFilterStatuses(): array
{
return [
$statuses = [
StateMachineService::UNPAID,
StateMachineService::PAID,
StateMachineService::SHIPPED,
StateMachineService::COMPLETED,
StateMachineService::CANCELLED,
];

return fire_hook_filter('common.repo.order.statuses', $statuses);
}

/**
Expand All @@ -44,13 +46,27 @@ public function list(array $filters = []): LengthAwarePaginator
return $builder->paginate();
}

/**
* @return Builder
*/
public function baseBuilder(): Builder
{
return Order::query();
}

/**
* @param array $filters
* @return Builder
*/
public function builder(array $filters = []): Builder
{
$builder = Order::query();
$relations = [
'customer',
'items',
];

$relations = array_merge($this->relations, $relations);
$builder = $this->baseBuilder()->with($relations);

$filters = array_merge($this->filters, $filters);

Expand Down Expand Up @@ -79,6 +95,11 @@ public function builder(array $filters = []): Builder
$builder->where('status', $status);
}

$statuses = $filters['statuses'] ?? [];
if ($statuses) {
$builder->whereIn('status', $statuses);
}

$start = $filters['start'] ?? '';
if ($start) {
$builder->where('created_at', '>', $start);
Expand Down
30 changes: 29 additions & 1 deletion innopacks/common/src/Repositories/ProductRepo.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use InnoShop\Common\Models\Category;
use InnoShop\Common\Models\Product;
use InnoShop\Common\Repositories\Product\ImageRepo;

Expand Down Expand Up @@ -238,6 +240,14 @@ private function syncImages(Product $product, $images): void
}
}

/**
* @return Builder
*/
public function baseBuilder(): Builder
{
return Product::query();
}

/**
* @param array $filters
* @return Builder
Expand All @@ -251,7 +261,10 @@ public function builder(array $filters = []): Builder
'translation',
'categories.translation',
];
$builder = Product::query()->with($relations);

$relations = array_merge($this->relations, $relations);

$builder = $this->baseBuilder()->with($relations);

$filters = array_merge($this->filters, $filters);

Expand All @@ -262,7 +275,22 @@ public function builder(array $filters = []): Builder
});
}

$categorySlug = $filters['category_slug'] ?? '';
if ($categorySlug) {
$category = Category::query()->where('slug', $categorySlug)->first();
if ($category) {
$categories = CategoryRepo::getInstance()->builder(['parent_id' => $category->id])->get();

$filters['category_ids'] = $categories->pluck('id');
$filters['category_ids'][] = $category->id;
}
}

$categoryIds = $filters['category_ids'] ?? [];
if ($categoryIds instanceof Collection) {
$categoryIds = $categoryIds->toArray();
}
$categoryIds = array_unique($categoryIds);
if ($categoryIds) {
$builder->whereHas('categories', function (Builder $query) use ($categoryIds) {
$query->whereIn('category_id', $categoryIds);
Expand Down
2 changes: 1 addition & 1 deletion innopacks/common/src/Resources/CartListItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function toArray(Request $request): array
'origin_price_format' => $sku->origin_price_format,
'subtotal' => $subtotal,
'subtotal_format' => currency_format($subtotal),
'image' => image_resize($sku->image->path ?? $product->image->path),
'image' => image_resize($sku->image->path ?? ($product->image->path ?? '')),
'selected' => (bool) $this->selected,
];
}
Expand Down
42 changes: 42 additions & 0 deletions innopacks/common/src/Resources/ProductSimple.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Copyright (c) Since 2024 InnoShop - All Rights Reserved
*
* @link https://www.innoshop.com
* @author InnoShop <[email protected]>
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

namespace InnoShop\Common\Resources;

use Exception;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class ProductSimple extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param Request $request
* @return array
* @throws Exception
*/
public function toArray(Request $request): array
{
$sku = $this->masterSku;

return [
'sku_id' => $sku->id,
'product_id' => $this->id,
'slug' => $this->slug,
'url' => $this->url,
'name' => $this->translation->name,
'summary' => $this->translation->summary,
'image_small' => image_resize($sku->image->path ?? ($this->image->path ?? '')),
'image_big' => image_resize($sku->image->path ?? ($this->image->path ?? ''), 600, 600),
'price_format' => $sku->price_format,
'origin_price_format' => $sku->origin_price_format,
];
}
}
10 changes: 9 additions & 1 deletion innopacks/common/src/Services/Checkout/BillingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,16 @@
use InnoShop\Plugin\Repositories\PluginRepo;
use InnoShop\Plugin\Resources\Checkout\PaymentMethodItem;

class BillingService extends BaseService
class BillingService
{
/**
* @return static
*/
public static function getInstance(): static
{
return new static();
}

/**
* @throws \Exception
*/
Expand Down
2 changes: 1 addition & 1 deletion innopacks/common/src/Services/CheckoutService.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public function getCheckoutResult(): array
'cart_list' => $this->getCartList(),
'address_list' => $this->getAddressList(),
'shipping_methods' => ShippingService::getInstance($this)->getMethods(),
'billing_methods' => BillingService::getInstance($this)->getMethods(),
'billing_methods' => BillingService::getInstance()->getMethods(),
'checkout' => $this->getCheckoutData(),
'fee_list' => $this->getFeeList(),
'total' => $this->getTotal(),
Expand Down
55 changes: 28 additions & 27 deletions innopacks/front/lang/en/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,33 @@
*/

return [
'home' => 'Home',
'login' => 'Login',
'register' => 'Register',
'cancel' => 'Cancel',
'confirm' => 'Confirm',
'delete' => 'Delete',
'edit' => 'Edit',
'action' => 'Action',
'add' => 'Add',
'text_hint' => 'Hint',
'image' => 'Image',
'all' => 'All',
'status' => 'Status',
'view' => 'View',
'submit' => 'Submit',
'products' => 'Products',
'news' => 'Articles',
'pages' => 'Pages',
'search' => 'Search',
'back_page' => 'Go Back',
'telephone' => 'Telephone',
'created_at' => 'Created at',
'home' => 'Home',
'login' => 'Login',
'register' => 'Register',
'cancel' => 'Cancel',
'confirm' => 'Confirm',
'delete' => 'Delete',
'edit' => 'Edit',
'action' => 'Action',
'add' => 'Add',
'text_hint' => 'Hint',
'image' => 'Image',
'all' => 'All',
'status' => 'Status',
'view' => 'View',
'submit' => 'Submit',
'products' => 'Products',
'news' => 'Articles',
'pages' => 'Pages',
'search' => 'Search',
'back_page' => 'Go Back',
'telephone' => 'Telephone',
'created_at' => 'Created at',
'delete_confirm' => 'You sure you want to delete it?',

'delete_confirm' => 'You sure you want to delete it?',
'get_success' => 'Get successfully',
'saved_success' => 'Saved successfully',
'updated_success' => 'Updated successfully',
'deleted_success' => 'Deleted successfully',
'get_success' => 'Get successfully',
'saved_success' => 'Saved successfully',
'updated_success' => 'Updated successfully',
'deleted_success' => 'Deleted successfully',
'submitted_success' => 'Submitted successfully',
];
Loading

0 comments on commit b47204a

Please sign in to comment.