Files
boursecu_server/app/Http/Controllers/pay/paypingController.php
T
2026-07-27 22:09:08 +03:30

334 lines
13 KiB
PHP

<?php
namespace App\Http\Controllers\pay;
use App\Http\Controllers\Controller;
use App\Models\Customer;
use App\Models\User;
use App\Models\Withdraw;
use App\Services\ConfigService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Rooyesh\Wallet\interfaces\TransactionInterface;
use Rooyesh\Wallet\interfaces\WalletInterface;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
class paypingController extends Controller
{
private function cfg(): ConfigService
{
return app(ConfigService::class);
}
private function paypingHeaders(): array
{
return [
'payping' => $this->cfg()->getString(
'payping_header_secret',
(string) config('payment.payping_header_secret', '')
),
];
}
private function paypingStartUrl(): string
{
return $this->cfg()->getString(
'payping_start_url',
(string) config('payment.payping_start_url', 'https://payping.limangold.ir/payment/start')
);
}
private function paypingVerifyUrl(): string
{
return $this->cfg()->getString(
'payping_verify_url',
(string) config('payment.payping_verify_url', 'https://payping.limangold.ir/payment/verify')
);
}
/** اگر payping_gateway_items پر باشد (با کاما)، مثل گلد علی بین itemها می‌چرخد */
private function resolveGatewayItem(): string
{
$list = $this->cfg()->getString('payping_gateway_items', '');
if ($list === '') {
return (string) config('payment.payping_gateway_item', 'gp1Q');
}
$items = array_values(array_filter(array_map('trim', explode(',', $list))));
if ($items === []) {
return 'gp1Q';
}
$index = (int) cache()->get('payping_gateway_item_index', 0);
if ($index >= count($items)) {
$index = 0;
}
$item = $items[$index];
cache()->put('payping_gateway_item_index', $index + 1);
return $item;
}
public function startTransation(Request $request)
{
$user = auth()->user();
$finalPriseP = $request->value;
if ($user->id == 4) {
$finalPriseP = 10700;
}
if ($this->cfg()->getBool('payping_round_toman', (bool) config('payment.payping_round_toman', false))) {
$finalPriseP = (int) (ceil($finalPriseP / 10) * 10);
}
// شارژ کیف (بدون سفارش): حداقل واریز از تنظیمات
if (!isset($request->order_id)) {
$minDeposit = $this->cfg()->getInt('min_deposit', 0);
if ($minDeposit > 0 && (int) $finalPriseP < $minDeposit) {
throw new UnprocessableEntityHttpException(
'حداقل مقدار واریز ' . number_format($minDeposit) . ' ریال است'
);
}
}
$Withdraw = Withdraw::create([
'customer_id' => auth('customer')->id(),
'cash' => $finalPriseP,
'note' => 'واریز ریالی',
'sts' => 1,
]);
if (isset($request->order_id)) {
$Withdraw->order_id = $request->order_id;
if (isset($request->product_id)) {
$Withdraw->product_id = $request->product_id;
}
$Withdraw->save();
}
$payload = [
'amount' => $finalPriseP / 10,
'user_name' => $user->name,
'user_id' => $user->id,
'user' => $user,
'title' => $this->Config('name_fa'),
'return_url' => $this->cfg()->resolvePublicSiteUrl(request()) . '/transactions',
'server_call_url' => url('/customer/charge_account_verify/' . $Withdraw->id),
'item' => $this->resolveGatewayItem(),
];
$gatewayName = $this->cfg()->getString('payping_gateway_name', (string) config('payment.payping_gateway_name', ''));
if ($gatewayName !== '') {
$payload['gateway'] = $gatewayName;
}
try {
$response = Http::withHeaders($this->paypingHeaders())
->post($this->paypingStartUrl(), $payload)
->json();
} catch (\Exception $exception) {
return response()->json([
'message' => 'مشکل در ارتباط با درگاه',
'R' => $response ?? $exception,
], 422);
}
if (empty($response['id']) || empty($response['url'])) {
return response()->json([
'message' => 'مشکل در ارتباط با درگاه',
'R' => $response ?? null,
], 422);
}
$Withdraw->tracked_number = $response['id'];
$Withdraw->save();
return $response['url'];
}
public function verifyTransationByID(Request $request, $id)
{
$expectedSecret = $this->cfg()->getString(
'payping_header_secret',
(string) config('payment.payping_header_secret', '')
);
if ($request->header('payping') !== $expectedSecret) {
return response()->json(['auth needed'], 401);
}
$Withdraw = Withdraw::with('customer')->where('id', $id)->first();
$site_url = $this->cfg()->resolvePublicSiteUrl(request());
$User = Customer::where('id', $Withdraw->customer_id)->first();
$GTransaction = Http::withHeaders($this->paypingHeaders())
->post($this->paypingVerifyUrl(), [
'id' => (string) ($Withdraw->tracked_number ?? $Withdraw->zify_code),
])
->json();
if (isset($GTransaction['active']) && $GTransaction['active'] == 5) {
if ($Withdraw->cash == $GTransaction['amount'] * 10) {
$Withdraw->zify_code = $Withdraw->tracked_number;
$Withdraw->tracked_number = $GTransaction['invoiceNo'];
$Withdraw->sts = 2;
$Withdraw->save();
$wallet_interface_fund = new WalletInterface($User->id, $User->type);
new TransactionInterface($wallet_interface_fund, [
'cash' => $GTransaction['amount'] * 10,
'expire_at' => now(),
'withdraw_id' => $Withdraw->id,
'active' => true,
], 3, false, true);
try {
app(\App\Services\PargarSyncService::class)->queueWalletDocument(
$User,
'cash',
(float) ($GTransaction['amount'] * 10),
[
'withdraw_id' => $Withdraw->id,
'note' => 'واریز درگاه',
'source' => 'gateway',
]
);
} catch (\Throwable $e) {
report($e);
}
$message = 'کاربر ' . $Withdraw->customer->name . ' با شماره موبایل ' . $Withdraw->customer->mobile
. ' حساب خود را به مقدار ' . number_format($Withdraw->cash) . ' ریال شارژ کرد. با تشکر، ' . $this->Config('name_fa');
User::where('send_sms', 1)->get()->each(function ($admin) use ($message) {
if (self::isSmsNotifEnabled('sms_notif_gateway_charge_admin')) {
$this->Send($admin->mobile, $message);
}
});
try {
app(\App\Services\CustomerAppNotifyService::class)->notifyCustomer(
(int) $Withdraw->customer_id,
'واریز به کیف پول',
'حساب شما به مقدار ' . number_format((float) $Withdraw->cash) . ' ریال شارژ شد.',
'success'
);
} catch (\Throwable $e) {
report($e);
}
return view('showTransactionReport', compact('Withdraw', 'site_url'));
}
return 'مبلغ پرداختی دستکاری شده !';
}
if ($this->cfg()->getBool('payping_strict_failed_withdraw', (bool) config('payment.payping_strict_failed_withdraw', false))) {
if (!$Withdraw->zify_code) {
$Withdraw->priority = 51;
}
$Withdraw->priority = 111;
}
$Withdraw->sts = 3;
$Withdraw->save();
return view('showTransactionReport', compact('Withdraw', 'site_url'));
}
public function verifyTransation()
{
$Withdraw = Withdraw::with('customer')->where('id', $_GET['clientrefid'])->first();
$site_url = $this->cfg()->resolvePublicSiteUrl(request());
$User = Customer::where('id', $Withdraw->customer_id)->first();
$verifyResponse = Http::withToken('cf09ee0187335f173c786e7660d778d2dd71ab582d282999f751427f5923d525')
->post('https://zify.ir/api/order/v2/verify', [
'order' => (string) $Withdraw->tracked_number,
]);
$decodedResponse = json_decode($verifyResponse, true);
$verifyMessage = $decodedResponse['message'] ?? null;
if (isset($decodedResponse['data']['refid']) || $verifyMessage == []) {
if ($Withdraw->cash == $decodedResponse['data']['amount'] * 10) {
$Withdraw->zify_code = $Withdraw->tracked_number;
$Withdraw->tracked_number = $decodedResponse['data']['refid'];
$Withdraw->sts = 2;
$Withdraw->save();
$wallet_interface_fund = new WalletInterface($User->id, $User->type);
new TransactionInterface($wallet_interface_fund, [
'cash' => $decodedResponse['data']['amount'] * 10,
'expire_at' => now(),
'withdraw_id' => $_GET['clientrefid'],
'active' => true,
], 3, false, true);
try {
app(\App\Services\PargarSyncService::class)->queueWalletDocument(
$User,
'cash',
(float) ($decodedResponse['data']['amount'] * 10),
[
'withdraw_id' => (int) $Withdraw->id,
'note' => 'واریز درگاه',
'source' => 'gateway',
]
);
} catch (\Throwable $e) {
report($e);
}
if ($this->cfg()->getBool('payping_fin_sync_enabled', (bool) config('payment.payping_fin_sync_enabled', false))) {
$finUrl = $this->cfg()->getString('payping_fin_url', 'https://limangold.ir/kernel/admin/fin');
$finToken = $this->cfg()->getString('payping_fin_token', '');
if ($finUrl !== '' && $finToken !== '') {
Http::withToken($finToken)->post($finUrl, [
'amount' => $decodedResponse['data']['amount'] * 10,
'customer_id' => $this->cfg()->getString('payping_fin_customer_id', '23'),
'date_at' => \Carbon\Carbon::today()->format('Y/m/d'),
'receive_account_number' => '000',
'receive_bank' => 'پیپینگ',
'receive_name' => $this->Config('name_fa'),
'send_account_number' => '000',
'send_bank' => ' زیفای کد ' . $Withdraw->zify_code,
'send_name' => $User->name,
'track_number' => $decodedResponse['data']['refid'],
'type' => 1,
]);
}
}
$message = 'کاربر ' . $Withdraw->customer->name . ' با شماره موبایل ' . $Withdraw->customer->mobile
. ' حساب خود را به مقدار ' . number_format($Withdraw->cash) . ' ریال شارژ کرد. با تشکر، ' . $this->Config('name_fa');
User::where('send_sms', 1)->get()->each(function ($admin) use ($message) {
if (self::isSmsNotifEnabled('sms_notif_gateway_charge_admin')) {
$this->Send($admin->mobile, $message);
}
});
try {
app(\App\Services\CustomerAppNotifyService::class)->notifyCustomer(
(int) $Withdraw->customer_id,
'واریز به کیف پول',
'حساب شما به مقدار ' . number_format((float) $Withdraw->cash) . ' ریال شارژ شد.',
'success'
);
} catch (\Throwable $e) {
report($e);
}
return view('showTransactionReport', compact('Withdraw', 'site_url'));
}
return 'مبلغ پرداختی دستکاری شده !';
}
$Withdraw->sts = 3;
$Withdraw->save();
return view('showTransactionReport', compact('Withdraw', 'site_url'));
}
}