added kashkool
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Category;
|
||||
use Illuminate\Http\Request;
|
||||
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
|
||||
|
||||
class CategoryController extends Controller
|
||||
{
|
||||
public function index(Request $request)
|
||||
{
|
||||
$query = Category::query();
|
||||
|
||||
if ($request->has('active')) {
|
||||
$query->where('active', (int) $request->active);
|
||||
}
|
||||
|
||||
if ($request->filled('search')) {
|
||||
$query->where('name', 'like', '%' . $request->search . '%');
|
||||
}
|
||||
|
||||
$categories = $query
|
||||
->orderBy('id', 'asc')
|
||||
->get();
|
||||
|
||||
return response()->json($categories);
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
$name = trim((string) $request->input('name'));
|
||||
|
||||
if ($name === '') {
|
||||
throw new UnprocessableEntityHttpException('نام دستهبندی الزامی است');
|
||||
}
|
||||
|
||||
if (Category::where('name', $name)->exists()) {
|
||||
throw new UnprocessableEntityHttpException('این دستهبندی قبلا ثبت شده است');
|
||||
}
|
||||
|
||||
$category = Category::create([
|
||||
'name' => $name,
|
||||
'active' => $request->has('active') ? (int) $request->active : 1,
|
||||
]);
|
||||
|
||||
return response()->json($category);
|
||||
}
|
||||
|
||||
public function clientIndex(Request $request)
|
||||
{
|
||||
$query = Category::query()
|
||||
->where('active', 1);
|
||||
|
||||
if ($request->filled('search')) {
|
||||
$query->where('name', 'like', '%' . $request->search . '%');
|
||||
}
|
||||
|
||||
$categories = $query
|
||||
->orderBy('id', 'asc')
|
||||
->get();
|
||||
|
||||
return response()->json($categories);
|
||||
}
|
||||
|
||||
public function changeActive($id, $active)
|
||||
{
|
||||
$active = (int) $active;
|
||||
|
||||
if (! in_array($active, [0, 1], true)) {
|
||||
throw new UnprocessableEntityHttpException('وضعیت active نامعتبر است');
|
||||
}
|
||||
|
||||
$category = Category::findOrFail($id);
|
||||
|
||||
$category->active = $active;
|
||||
$category->save();
|
||||
|
||||
return response()->json([
|
||||
'message' => $active ? 'دستهبندی فعال شد' : 'دستهبندی غیرفعال شد',
|
||||
'data' => $category,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -12,12 +12,17 @@ use App\Models\Withdraw;
|
||||
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
|
||||
use Rooyesh\Wallet\interfaces\TransactionInterface;
|
||||
use Rooyesh\Wallet\interfaces\WalletInterface;
|
||||
use App\Services\ConfigService;
|
||||
|
||||
|
||||
class GiftCardController extends Controller
|
||||
{
|
||||
public function redeem(Request $request)
|
||||
{
|
||||
if (app(ConfigService::class)->isStore()) {
|
||||
return $this->storeRedeem($request);
|
||||
}
|
||||
|
||||
$user = auth()->user();
|
||||
$count = $request->count;
|
||||
$amount = $request->amount;
|
||||
@@ -170,6 +175,203 @@ class GiftCardController extends Controller
|
||||
]);
|
||||
}
|
||||
|
||||
public function useDiscount(Request $request)
|
||||
{
|
||||
if (! app(ConfigService::class)->isStore()) {
|
||||
return response()->json(['message' => 'این قابلیت فقط برای فروشگاه فعال است'], 403);
|
||||
}
|
||||
|
||||
$result = $this->applyDiscount((string) $request->input('code'), auth('customer')->id());
|
||||
|
||||
if (! empty($result['message'])) {
|
||||
return response()->json(['message' => $result['message']], 400);
|
||||
}
|
||||
|
||||
return response()->json(['amount' => $result['amount']]);
|
||||
}
|
||||
|
||||
public function validateDiscount(Request $request)
|
||||
{
|
||||
if (! app(ConfigService::class)->isStore()) {
|
||||
return response()->json(['message' => 'این قابلیت فقط برای فروشگاه فعال است'], 403);
|
||||
}
|
||||
|
||||
$giftcode = $this->verifyDiscount((string) $request->input('code'));
|
||||
if (! empty($giftcode['message'])) {
|
||||
return response()->json(['message' => $giftcode['message']], 400);
|
||||
}
|
||||
|
||||
return response()->json(['giftcard' => $giftcode], 200);
|
||||
}
|
||||
|
||||
private function storeRedeem(Request $request)
|
||||
{
|
||||
$user = auth()->user();
|
||||
$count = $request->count;
|
||||
$amount = $request->amount;
|
||||
if (! $user) {
|
||||
throw new UnprocessableEntityHttpException('auth failed!');
|
||||
}
|
||||
|
||||
if ((! is_numeric($count) || $count < 0) || (! is_numeric($amount) || $amount <= 0)) {
|
||||
return response()->json(['message' => 'تعداد کد را چک کنید!'], 400);
|
||||
}
|
||||
|
||||
if (isset($request->code) && GiftCardCodes::where('code', $request->code)->where('unit', $request->unit)->exists()) {
|
||||
return response()->json(['message' => 'کد تکراری میباشد!'], 400);
|
||||
}
|
||||
|
||||
$codes = [];
|
||||
$unitAmount = $request->unit == 3 ? $amount / 100 : $amount;
|
||||
|
||||
if ($request->use_once) {
|
||||
$gift = GiftCard::create([
|
||||
'use_once' => $request->use_once ? 1 : 0,
|
||||
'count' => $count,
|
||||
'amount' => $unitAmount,
|
||||
'name' => $request->name,
|
||||
'total' => $request->unit == 3 ? null : $count * $amount,
|
||||
'sts' => 1,
|
||||
'unit' => $request->unit,
|
||||
'expiry_date' => $request->expiry_date,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
'description' => $request->description,
|
||||
]);
|
||||
|
||||
for ($i = 0; $i < $count; $i++) {
|
||||
do {
|
||||
$unique_code = strtoupper(bin2hex(random_bytes(4)));
|
||||
} while (GiftCardCodes::where('code', $unique_code)->exists());
|
||||
|
||||
$codes[] = [
|
||||
'giftcard_id' => $gift->id,
|
||||
'use_once' => $request->use_once ? 1 : 0,
|
||||
'code' => $unique_code,
|
||||
'sts' => 1,
|
||||
'unit' => $request->unit,
|
||||
'amount' => $unitAmount,
|
||||
'expiry_date' => $request->expiry_date,
|
||||
'created_at' => now(),
|
||||
];
|
||||
}
|
||||
GiftCardCodes::insert($codes);
|
||||
} else {
|
||||
do {
|
||||
$unique_code = strtoupper(bin2hex(random_bytes(4)));
|
||||
} while (GiftCardCodes::where('code', $unique_code)->exists());
|
||||
|
||||
$gift = GiftCard::create([
|
||||
'use_once' => $request->use_once ? 1 : 0,
|
||||
'count' => $count,
|
||||
'unique_code' => ! empty($request->code) ? $request->code : $unique_code,
|
||||
'amount' => $unitAmount,
|
||||
'name' => $request->name,
|
||||
'total' => $request->unit == 3 ? null : $count * $amount,
|
||||
'sts' => 1,
|
||||
'unit' => $request->unit,
|
||||
'expiry_date' => $request->expiry_date,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
'description' => $request->description,
|
||||
]);
|
||||
|
||||
GiftCardCodes::insert([
|
||||
'giftcard_id' => $gift->id,
|
||||
'use_once' => $request->use_once ? 1 : 0,
|
||||
'code' => ! empty($request->code) ? $request->code : $unique_code,
|
||||
'sts' => 1,
|
||||
'unit' => $request->unit,
|
||||
'amount' => $unitAmount,
|
||||
'expiry_date' => $request->expiry_date,
|
||||
'created_at' => now(),
|
||||
]);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'order' => $gift,
|
||||
'codes' => $gift->giftcardcodes,
|
||||
'total' => $count * $amount,
|
||||
]);
|
||||
}
|
||||
|
||||
private function applyDiscount(string $code, $customerId): array
|
||||
{
|
||||
$giftcode = $this->verifyDiscount($code);
|
||||
if (! empty($giftcode['message'])) {
|
||||
return ['message' => $giftcode['message']];
|
||||
}
|
||||
|
||||
$giftcode = GiftCardCodes::where('code', $code)->first();
|
||||
$gift = GiftCard::find($giftcode->giftcard_id);
|
||||
|
||||
$idsArray = json_decode($giftcode->assigned_to, true);
|
||||
if (! is_array($idsArray)) {
|
||||
$idsArray = [];
|
||||
}
|
||||
|
||||
$idsArray[] = $customerId;
|
||||
if ($gift->use_once == 1) {
|
||||
$giftcode->sts = 2;
|
||||
}
|
||||
$giftcode->assigned_to = json_encode($idsArray);
|
||||
$giftcode->save();
|
||||
|
||||
$gift->used_count += 1;
|
||||
if ($gift->used_count + 1 > $gift->count) {
|
||||
$gift->sts = 2;
|
||||
}
|
||||
$gift->save();
|
||||
|
||||
return ['amount' => $giftcode->amount];
|
||||
}
|
||||
|
||||
private function verifyDiscount(string $code)
|
||||
{
|
||||
$giftcode = GiftCardCodes::where('code', $code)->first();
|
||||
|
||||
if (! $giftcode) {
|
||||
return ['message' => 'کد تخفیف یافت نشد!'];
|
||||
}
|
||||
|
||||
$gift = GiftCard::find($giftcode->giftcard_id);
|
||||
$customer_id = auth('customer')->id();
|
||||
|
||||
if ($gift->unit !== 3 || $giftcode->unit !== 3) {
|
||||
return ['message' => 'کد تخفیف صحیح نمیباشد!'];
|
||||
}
|
||||
|
||||
if ($gift->use_once === 0) {
|
||||
$stored_ids = json_decode($giftcode->assigned_to, true);
|
||||
if (is_array($stored_ids) && in_array($customer_id, $stored_ids)) {
|
||||
return ['message' => 'شما قبلا از تخفیف خود استفاده کرده اید!'];
|
||||
}
|
||||
} elseif ($gift->use_once === 1) {
|
||||
if (GiftCardCodes::where('giftcard_id', $gift->id)->where('assigned_to', '['.auth('customer')->id().']')->exists()) {
|
||||
return ['message' => 'شما قبلا از تخفیف خود استفاده کرده اید!'];
|
||||
}
|
||||
}
|
||||
|
||||
if ($gift->expiry_date <= now() || $gift->sts === 3) {
|
||||
$gift->sts = 3;
|
||||
$giftcode->sts = 3;
|
||||
$giftcode->save();
|
||||
$gift->save();
|
||||
|
||||
return ['message' => 'کد تخفیف منقضی شده است!'];
|
||||
}
|
||||
|
||||
if ($giftcode->sts === 2) {
|
||||
return ['message' => 'کد تخفیف قبلا استفاده شده است!'];
|
||||
}
|
||||
|
||||
if ($gift->used_count + 1 > $gift->count) {
|
||||
return ['message' => 'تعداد استفاده کد تخفیف به پایان رسیده است!'];
|
||||
}
|
||||
|
||||
return $giftcode;
|
||||
}
|
||||
|
||||
public function table(Request $request)
|
||||
{
|
||||
$user = auth()->user();
|
||||
|
||||
@@ -180,6 +180,26 @@ class ProductController extends SuperController
|
||||
return response()->json($products);
|
||||
}
|
||||
|
||||
public function ProductById($id)
|
||||
{
|
||||
$this->getPrice();
|
||||
$this->auto_close_open();
|
||||
$product = Product::with(['file'])
|
||||
->where('id', $id)
|
||||
->first();
|
||||
|
||||
if ($product) {
|
||||
$product->similar = Product::with(['file'])
|
||||
->where('group_type', $product->group_type)
|
||||
->where('id', '!=', $id)
|
||||
->get();
|
||||
} else {
|
||||
return response()->json(false);
|
||||
}
|
||||
|
||||
return response()->json($product);
|
||||
}
|
||||
|
||||
public function getPrice()
|
||||
{
|
||||
app(\App\Services\ProductPriceSyncService::class)->syncAll(true);
|
||||
|
||||
@@ -0,0 +1,328 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Customer;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Order;
|
||||
use App\Models\Cart;
|
||||
use App\Models\CartItem;
|
||||
use App\Models\Product;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Str;
|
||||
use App\Http\Controllers\Site\ReportController ;
|
||||
use Rooyesh\Wallet\interfaces\TransactionInterface;
|
||||
use Rooyesh\Wallet\interfaces\WalletInterface;
|
||||
use Rooyesh\Wallet\models\Wallet;
|
||||
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
|
||||
use App\Models\Price;
|
||||
use App\Http\Controllers\Admin\ProductController;
|
||||
use App\Http\Controllers\Admin\GiftCardController;
|
||||
|
||||
|
||||
|
||||
|
||||
class CartController extends Controller
|
||||
{
|
||||
private function getOrCreateCart(Request $request)
|
||||
{
|
||||
$sessionId = $request->header('X-Cart-Session');
|
||||
|
||||
if (!$sessionId) {
|
||||
$sessionId = (string) Str::uuid();
|
||||
}
|
||||
|
||||
$cart = Cart::firstOrCreate(
|
||||
[
|
||||
'session_id' => $sessionId,
|
||||
'sts' => 1,
|
||||
]
|
||||
);
|
||||
|
||||
return [$cart, $sessionId];
|
||||
}
|
||||
|
||||
public function getCart(Request $request)
|
||||
{
|
||||
[$cart, $sessionId] = $this->getOrCreateCart($request);
|
||||
|
||||
return response()->json([
|
||||
'cart' => $cart->load('items'),
|
||||
'session_id' => $sessionId
|
||||
]);
|
||||
}
|
||||
|
||||
public function addItem(Request $request)
|
||||
{
|
||||
[$cart, $sessionId] = $this->getOrCreateCart($request);
|
||||
$product = Product::select('id', 'title', 'group_type')->where('id', $request->product_id)->first();
|
||||
$item = CartItem::where('cart_id', $cart->id)
|
||||
->where('product_id', $request->product_id)
|
||||
->first();
|
||||
|
||||
if ($item) {
|
||||
$item->increment('quantity');
|
||||
} else {
|
||||
CartItem::create([
|
||||
'cart_id' => $cart->id,
|
||||
'product_id' => $product->id,
|
||||
'title' => $product->title,
|
||||
'group_type' => $product->group_type,
|
||||
'quantity' => 1,
|
||||
]);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'message' => 'محصول اضافه شد.',
|
||||
'session_id' => $sessionId
|
||||
]);
|
||||
}
|
||||
|
||||
public function removeItem(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'product_id' => 'required|integer',
|
||||
]);
|
||||
|
||||
$sessionId = $request->header('X-Cart-Session');
|
||||
|
||||
if (!$sessionId) {
|
||||
return response()->json([
|
||||
'message' => 'Cart session not found'
|
||||
], 400);
|
||||
}
|
||||
|
||||
$cart = Cart::where('session_id', $sessionId)
|
||||
->where('sts', 'open')
|
||||
->first();
|
||||
|
||||
if (!$cart) {
|
||||
return response()->json([
|
||||
'message' => 'Active cart not found'
|
||||
], 404);
|
||||
}
|
||||
|
||||
$item = CartItem::where('cart_id', $cart->id)
|
||||
->where('product_id', $request->product_id)
|
||||
->first();
|
||||
|
||||
if (!$item) {
|
||||
return response()->json([
|
||||
'message' => 'Item not found in cart'
|
||||
], 404);
|
||||
}
|
||||
|
||||
if ($item->quantity > 1) {
|
||||
$item->decrement('quantity');
|
||||
} else {
|
||||
$item->delete();
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Item updated successfully'
|
||||
]);
|
||||
}
|
||||
|
||||
public function checkout(Request $request)
|
||||
{
|
||||
// return $request;
|
||||
|
||||
$data = $request->json()->all();
|
||||
$user= auth()->user();
|
||||
if ($user->is_legal == 0 || !$user) {
|
||||
return response()->json(['massage' => 'شما باید اول خود را احراز هویت کنید'], 422);
|
||||
|
||||
}
|
||||
|
||||
// $data->validate([
|
||||
// 'session_id' => 'required'
|
||||
// ]);
|
||||
//dd($data['products']);
|
||||
if(isset($data['gateway']) && $data['gateway'] !== 'wallet'){
|
||||
return response()->json(['massage' => "Under construction👷🏻"], 501);
|
||||
}
|
||||
if( isset($data['session_id']) && ($data['session_id'] !== '' || $data['session_id'] !== null)){
|
||||
$cart = Cart::where('session_id', $data['session_id'])
|
||||
->where('sts', 1)
|
||||
->first()->load('items');
|
||||
if(!$cart){
|
||||
return response()->json(['message' => 'سبد خرید پیدا نشد!'], 404);
|
||||
}
|
||||
|
||||
// اتصال سبد به کاربر لاگین شده
|
||||
$cart->update([
|
||||
'customer_id' => auth('customer')->id(),
|
||||
// 'sts' => 2,
|
||||
]);
|
||||
} else{
|
||||
$items = [];
|
||||
|
||||
[$cart, $sessionId] = $this->getOrCreateCart($request);
|
||||
|
||||
$cart->customer_id = auth('customer')->id();
|
||||
$cart->save();
|
||||
|
||||
$productIds = collect($data)
|
||||
->pluck('product_id')
|
||||
->unique()
|
||||
->values();
|
||||
|
||||
$products = Product::select('id', 'title', 'group_type', 'inventory', 'amount', 'percent_fee_buy', 'cash_fee_buy', 'price_profit')
|
||||
->whereIn('id', $productIds)
|
||||
->get()
|
||||
->keyBy('id');
|
||||
|
||||
$numbers = collect($data)
|
||||
->mapWithKeys(function ($item) {
|
||||
return [
|
||||
$item['product_id'] => $item['number'] ?? 1
|
||||
];
|
||||
});
|
||||
foreach ($products as $product) {
|
||||
if(($product->inventory - $numbers[$product->id]) < 0){
|
||||
return response()->json(['message' => "موجودی محصول {$product->title} کافی نیست!"], 422);
|
||||
}
|
||||
$items[] = [
|
||||
'cart_id' => $cart->id,
|
||||
'product_id' => $product->id,
|
||||
'title' => $product->title,
|
||||
'group_type' => $product->group_type,
|
||||
'quantity' => $numbers[$product->id] ?? 1,
|
||||
'created_at'=> now(),
|
||||
'updated_at'=> now(),
|
||||
];
|
||||
}
|
||||
|
||||
CartItem::insert($items);
|
||||
|
||||
$cart = Cart::where('session_id', $cart->session_id)
|
||||
->first()
|
||||
->load('items');
|
||||
}
|
||||
|
||||
$wallet = Wallet::where('owner',auth('customer')->id())->whereNull('fund_id')->whereNull('product_id')
|
||||
->first();
|
||||
|
||||
$priceRow = Price::latest()->first();
|
||||
$goldPrice = $priceRow->price;
|
||||
$data['price'] = $goldPrice;
|
||||
$data['price_id'] = $priceRow->id;
|
||||
$data['price_original'] = $goldPrice;
|
||||
|
||||
$data['customer_id'] = auth('customer')->id();
|
||||
|
||||
$total_price = 0;
|
||||
$total_amount = 0;
|
||||
$product_fees = 0;
|
||||
$wallet_interface = new WalletInterface($data['customer_id'], auth('customer')->user()->type);
|
||||
|
||||
// قیمتگیری لحظهای
|
||||
$data['amount'] = 0;
|
||||
$data['service_charge_percentage'] = 0;
|
||||
$data['fee'] = 0;
|
||||
foreach ($cart->items as $item) {
|
||||
|
||||
$product = Product::find($item->product_id);
|
||||
|
||||
// کارمزد درصدی هر واحد از محصول
|
||||
$feeAmount = $product->percent_fee_buy * $product->amount;
|
||||
//وزن نهایی هر واحد از محصول با احتساب کارمزد
|
||||
$finalAmount = $product->amount + $feeAmount;
|
||||
//وزن کل محصول با احتساب تعداد و کارمزد
|
||||
$total_amount += $product->amount * $item->quantity;
|
||||
//محاسبه مجموع وزن سبد با احتساب کارمزد
|
||||
$data['amount'] += $finalAmount * $item->quantity;
|
||||
//محاسبه مجموع درصد کارمزد سبد با احتساب تعداد
|
||||
$data['service_charge_percentage'] += $product->percent_fee_buy * $item->quantity;
|
||||
//محاسبه مجموع کارمزد ثابت سبد با احتساب تعداد
|
||||
$data['fee'] += $product->cash_fee_buy * $item->quantity;
|
||||
// قیمت نهایی هر گرم
|
||||
$priceWithFee = $goldPrice + $data['fee'];
|
||||
//محاسبه مجموع کارمزد ثابت سبد با احتساب تعداد
|
||||
$product_fees += $product->cash_fee_buy;
|
||||
// قیمت آیتم
|
||||
$itemTotalPrice = $finalAmount * $priceWithFee;
|
||||
|
||||
// قیمت کل این آیتم با احتساب تعداد
|
||||
$itemTotalPrice = $item->quantity * $itemTotalPrice;
|
||||
//مبلغ سبد خرید با احتساب این آیتم
|
||||
$total_price += $itemTotalPrice;
|
||||
|
||||
// ذخیره شفاف برای گزارش و فاکتور
|
||||
$item->update([
|
||||
'price' => $itemTotalPrice,
|
||||
'gold_price' => $goldPrice
|
||||
]);
|
||||
}
|
||||
//قیمت هر گرم با احتساب کارمزد ثابت
|
||||
$total_price_with_fees = $goldPrice + $product_fees;
|
||||
//مجموع کارمز ثابت و متغیر در سبد خرید
|
||||
$data['total_fee'] = $data['fee'] + ($total_price_with_fees * ($data['service_charge_percentage'] * $total_amount));
|
||||
//قیمت هر گرم بدون احتساب کارمزدها
|
||||
$data['total_price_original'] = $total_amount * $goldPrice;
|
||||
//کارمزد طلایی
|
||||
$data['service_charge'] = $data['service_charge_percentage'] * $total_amount;
|
||||
//کارمزد ریالی
|
||||
$data['service_charge_price'] = $data['service_charge'] * $data['price_original'];
|
||||
//قیمت کل برای مشتری
|
||||
$data['total_price'] = $total_price;
|
||||
//مقدار قابل پرداخت
|
||||
$data['payment_amount'] = $data['total_price'];
|
||||
if(!empty($data[0]['discount_code'])){
|
||||
$discount = new GiftCardController();
|
||||
$discount = $discount->useDiscount($data[0]['discount_code'], $data['customer_id']);
|
||||
$data['discount'] = true;
|
||||
if(!empty($discount['amount'])){
|
||||
$data['payment_amount'] = $data['total_price'] = $data['total_price'] - ($data['total_price'] * (float)$discount['amount']);
|
||||
}
|
||||
}
|
||||
$data['cart_id'] = (int)$cart->id;
|
||||
$data['sts'] = 1;
|
||||
|
||||
if ($wallet->cash < $data['total_price']){
|
||||
throw new UnprocessableEntityHttpException('موجودی کیف پول شما کافی نیست.');
|
||||
}
|
||||
$data['direction'] = 1; // خرید
|
||||
$data['pay_at'] = now();
|
||||
$order = Order::create($data);
|
||||
$cart->order_id = $order->id;
|
||||
$cart->save();
|
||||
$tr = new TransactionInterface($wallet_interface, $_inp = [
|
||||
"cash" => $data['payment_amount'],
|
||||
'expire_at' => now(),
|
||||
'active' => true,
|
||||
'order_id' => $order->id,
|
||||
'start_at' => now(),
|
||||
'title' => "سبد خرید"
|
||||
], 12, false, false);
|
||||
foreach ($cart->items as $item) {
|
||||
$wallet_interface_product = new WalletInterface(auth('customer')->id(), auth('customer')->user()->type, null, $item->product_id);
|
||||
$wallet_interface_product->wallet->gold = intval($wallet_interface_product->wallet->gold);
|
||||
$wallet_interface_product->wallet->save();
|
||||
new TransactionInterface($wallet_interface_product, [
|
||||
"gold" => $item->quantity,
|
||||
"product_id" => $item->product_id,
|
||||
'expire_at' => now(),
|
||||
'active' => true,
|
||||
'order_id' => $order->id,
|
||||
'start_at' => now(),
|
||||
], 1, true, false);
|
||||
|
||||
}
|
||||
|
||||
$order->sts = 2; // پرداخت شده
|
||||
// $order->direction = 3; // سبد خرید
|
||||
$order->payment_sts = 4; // وضعیت پرداخت شده
|
||||
$order->save();
|
||||
|
||||
foreach ($cart->items as $item) {
|
||||
Product::where('id', $item->product_id)->decrement('inventory', $item->quantity);
|
||||
|
||||
// $product = Product::find($item->product_id);
|
||||
// $product->decrement('inventory', $item->quantity);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'message' => 'true',
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,185 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Customer;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\Customer;
|
||||
use Rooyesh\Wallet\interfaces\TransactionInterface;
|
||||
use Rooyesh\Wallet\interfaces\WalletInterface;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
use function Laravel\Prompts\select;
|
||||
|
||||
class havaleController extends Controller
|
||||
{
|
||||
public function havale(Request $request)
|
||||
{
|
||||
// return auth('customer')->user();
|
||||
$sender = Customer::find(auth('customer')->id());
|
||||
$sender_wallet = new WalletInterface(auth('customer')->id(), 2);
|
||||
// dd($sender_wallet);
|
||||
// dump($sender_wallet->wallet->gold);
|
||||
|
||||
if ($request->amount > $sender_wallet->wallet->gold) {
|
||||
return response()->json(['message' => 'موجودی حساب کافی نمی باشد!'], 422);
|
||||
}
|
||||
$receiver = Customer::where('virtual_card_number', $request->destination)->first();
|
||||
// dd($receiver);
|
||||
if (!$receiver) {
|
||||
return response()->json(['message' => "کارت مقصد معتبر نمی باشد!"], 422);
|
||||
}
|
||||
|
||||
|
||||
// try {
|
||||
// DB::transaction(function () use ($sender_wallet, $receiver, $request) {
|
||||
//
|
||||
// new TransactionInterface($sender_wallet, [
|
||||
// "gold" => $request->amount,
|
||||
// 'expire_at' => now(),
|
||||
// 'active' => true,
|
||||
// 'start_at' => now(),
|
||||
// ], 1, false);
|
||||
//
|
||||
// $receiver_wallet = new WalletInterface($receiver->id, 2);
|
||||
// dd($receiver_wallet);
|
||||
// new TransactionInterface($receiver_wallet, [
|
||||
// "gold" => $request->amount,
|
||||
// 'expire_at' => now(),
|
||||
// 'active' => true,
|
||||
// 'start_at' => now(),
|
||||
// ], 1, true);
|
||||
//
|
||||
// });
|
||||
//
|
||||
// return response()->json(['message' => 'حواله با موفقیت انجام شد!'], 200);
|
||||
//
|
||||
// } catch (\Exception $e) {
|
||||
// // در صورت بروز خطا، تمام عملیات رولبک میشود
|
||||
// return response()->json([
|
||||
// 'message' => 'خطا در انجام تراکنش!',
|
||||
// 'error' => $e->getMessage()
|
||||
// ], 500);
|
||||
// }
|
||||
// dd($receiver->name,$sender->name);
|
||||
|
||||
new TransactionInterface($sender_wallet, [
|
||||
"title" => "حواله",
|
||||
"gold" => $request->amount,
|
||||
'expire_at' => now(),
|
||||
'active' => true,
|
||||
'start_at' => now(),
|
||||
'note' => " حواله از $sender->name به $receiver->name به مقدار $request->amount گرم",
|
||||
], 10, false);
|
||||
// dump($sender_wallet->wallet->gold);
|
||||
$receiverWallet = new WalletInterface($receiver->id, 2);
|
||||
// dump($reciverWallet->wallet->gold);
|
||||
new TransactionInterface($receiverWallet, [
|
||||
"title" => "حواله",
|
||||
"gold" => $request->amount,
|
||||
'expire_at' => now(),
|
||||
'active' => true,
|
||||
'start_at' => now(),
|
||||
'note' => " حواله از $sender->name به $receiver->name به مقدار $request->amount گرم",
|
||||
], 10, true);
|
||||
// dump($reciverWallet->wallet->gold);
|
||||
return "تراکنش موفق";
|
||||
|
||||
|
||||
}
|
||||
|
||||
public function virtualCardValidate(Request $request)
|
||||
{
|
||||
$input = str_replace(' ', '', $request->input('cardNumber'));
|
||||
if(strlen($input) !== 16 && strlen($input) !== 11)
|
||||
return response()->json(['message' => 'مقدار وارد شده معتبر نمی باشد!'], 422);
|
||||
|
||||
if (strlen($input) == 16) {
|
||||
$input = implode('-', str_split($input, 4));
|
||||
}
|
||||
|
||||
if (auth('customer')->user()->virtual_card_number == $input ||
|
||||
auth('customer')->user()->mobile == $input){
|
||||
return response()->json(['message' => 'شماره کارت مبدا و مقصد یکسان است!'], 422);
|
||||
}
|
||||
|
||||
$data = Customer::where('virtual_card_number', $input)
|
||||
->orwhere('mobile', $input)
|
||||
->select('name', 'virtual_card_number')
|
||||
->first();
|
||||
if($data)
|
||||
return $data;
|
||||
|
||||
return response()->json(['message' => 'مقصد وجود ندارد!'], 422);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// if (strlen(str_replace(' ', '', $request->input('cardNumber'))) == 19) {
|
||||
// return $data = Customer::
|
||||
// where('virtual_card_number', str_replace(' ', '', $request->input('cardNumber')))
|
||||
// ->select('name', 'virtual_card_number')
|
||||
// ->first();
|
||||
// } elseif (strlen(str_replace(' ', '', $request->input('cardNumber'))) == 16) {
|
||||
// $normalized = implode('-', str_split(str_replace(' ', '', $request->input('cardNumber')), 4));
|
||||
// $data = Customer::where('virtual_card_number', $normalized)
|
||||
// ->select('name', 'virtual_card_number')
|
||||
// ->first();
|
||||
// return $data ?? response()->json([1], 422);
|
||||
// } elseif (strlen(str_replace(' ', '', $request->input('cardNumber'))) == 11){
|
||||
// return $data = Customer::
|
||||
// where('mobile', str_replace(' ', '', $request->input('cardNumber')))
|
||||
// ->select('name', 'virtual_card_number')
|
||||
// ->first();
|
||||
// }else {
|
||||
// return response()->json(['message' => 'شماره کارت باید 16 رقم باشد!'], 422);
|
||||
// }
|
||||
}
|
||||
|
||||
public function havaleList(Request $request){
|
||||
$id = $request->input('id');
|
||||
$havale = DB::table('transactions')
|
||||
->select('gold', 'note', 'created_at', 'unit', 'title', 'customer_id')
|
||||
->where('title', 'حواله')
|
||||
->where('customer_id', $id)
|
||||
->get();
|
||||
return $havale;
|
||||
|
||||
}
|
||||
|
||||
// public function ztest(Request $request)
|
||||
// {
|
||||
// $user = Customer::findOrFail($request->id);
|
||||
//// $user = Customer::find(auth('customer')->id());
|
||||
// if($user) {
|
||||
// $response = Http::withHeaders([
|
||||
// 'Content-Type' => 'application/json',
|
||||
// 'Accept' => 'application/json',
|
||||
// ])->post('https://shop.liman.gold/zarinpal', [
|
||||
// 'amount' => $request->amount,
|
||||
// 'user_name' => $user->name,
|
||||
// 'user_id' => $user->id,
|
||||
// 'currency' => 'IRR',
|
||||
// 'user' => [
|
||||
// 'name' => $user->name,
|
||||
// 'phone' => $user->mobile,
|
||||
// ],
|
||||
// 'title' => env('APP_NAME'),
|
||||
// 'return_url' => 'https://ghatreh.gold/zarinpal',
|
||||
// ]);
|
||||
//
|
||||
//// دریافت جواب به صورت آرایه
|
||||
// $result = $response->json();
|
||||
// return redirect($result['pay_link']);
|
||||
// }
|
||||
// return "user not found";
|
||||
////// یا نمایش کامل پاسخ
|
||||
//// dd($result);
|
||||
//
|
||||
// }
|
||||
}
|
||||
Reference in New Issue
Block a user