<?php

if (route(1) == "shopier") {
	$method = $db->from('payment_methods')
		->where('method_title', 'Shopier')
		->first();

	if ($method) {
		$data = json_decode($method["method_details"], true);

		$posts = [

			"platform_order_id" => $_POST['platform_order_id'],
			"API_key" => $data['key'],
			"status" => $_POST['status'],
			"installment" => $_POST['installment'],
			"payment_id" => $_POST['payment_id'],
			"random_nr" => $_POST['random_nr'],
			"signature" => $_POST['signature']

		];

		$signature = base64_decode($posts['signature']);

		$expected = hash_hmac('SHA256', $_POST["random_nr"] . $_POST["platform_order_id"], $data["secret"], true);

		if ($signature != $expected) {
			header('Location:' . site());
		}

		if ($posts['status'] == "success") {
			$query = $db->from('deposits')
				->where('order_id', $posts['platform_order_id'])
				->where('deposit_method', 4)
				->first();

			if ($query) {
				if ($query['deposit_status'] == 2) {
					$userQuery = $db->from('users')
						->where('user_id', $query['user_id'])->first();

					if ($userQuery) {
						$payment_bonus = $db->from('payment_bonus')
							->where('bonus_method', 4)
							->where('bonus_from', $query['deposit_amount'], '<=')
							->orderby('bonus_from', 'DESC')
							->first();

						if ($payment_bonus) {
							$newBalance = $userQuery['user_balance'] + ($query['deposit_amount'] + (($query['deposit_amount'] * $payment_bonus['bonus_amount']) / 100));
							
							
						} else {
							$newBalance = $userQuery['user_balance'] + $query['deposit_amount'];
						}

						$updateBalance = $db->update('users')
							->where('user_id', $query['user_id'])
							->set(
								array(

									'user_balance' => $newBalance

								)
							);

						$updateDeposit = $db->update('deposits')
							->where('order_id', $posts['platform_order_id'])
							->where('deposit_method', 4)
							->set(
								array(

									'deposit_status' => 1

								)
							);

						if ($updateBalance && $updateDeposit) {
							reference($db, $userQuery["user_id"], $query['deposit_amount']);

							header('Location:'.site().'');
						}
					}
				}
			}
		}
	}
} elseif (route(1) == "paytr") {
	$method = $db->from('payment_methods')
		->where('method_id', 1)
		->first();

	if ($method) {
		$data = json_decode($method["method_details"], true);

		$merchant_key = $data['key'];

		$merchant_salt = $data['salt'];

		$merchant_oid = $_POST['merchant_oid'];

		$status = $_POST['status'];

		$total_amount = $_POST['total_amount'];

		$hash = $_POST['hash'];

		$error = false;

		if (!$error) {
			$created_hash = base64_encode(hash_hmac('sha256', $merchant_oid . $merchant_salt . $status . $total_amount, $merchant_key, true));

			if ($created_hash != $hash) {
				die('PAYTR notification failed: bad hash');
			} else {
				$deposit = $db->from('deposits')
					->where('deposit_method', 1)
					->where('order_id', $merchant_oid)->first();

				if ($deposit) {
					if ($deposit['deposit_status'] == 2) {
						if ($status == 'success') {
							$user = $db->from('users')
								->where('user_id', $deposit['user_id'])->first();

							if ($user) {
								$payment_bonus = $db->from('payment_bonus')
									->where('bonus_method', 1)
									->where('bonus_from', $deposit['deposit_amount'], '<=')
									->orderby('bonus_from', 'DESC')
									->first();

								if ($payment_bonus) {
									$balance = $user['user_balance'] +  ($deposit['deposit_amount'] + (($deposit['deposit_amount'] * $payment_bonus['bonus_amount']) / 100));
								} else {
									$balance = $user['user_balance'] + $deposit['deposit_amount'];
								}

								$update_user = $db->update('users')
									->where('user_id', $user['user_id'])
									->set(
										array(

											'user_balance' => $balance

										)
									);

								$update_deposit = $db->update('deposits')
									->where('deposit_method', 1)
									->where('order_id', $merchant_oid)
									->set(
										array(

											'deposit_status' => 1

										)
									);

								if ($update_user && $update_deposit) {
									reference($db, $user["user_id"], $amount);

									die("OK");
								} else {
									die("OK");
								}
							}
						} else {
							$update_deposit = $db->update('deposits')
								->where('deposit_method', 1)
								->where('order_id', $merchant_oid)
								->set(
									array(

										'deposit_status' => 0

									)
								);

							die("OK");
						}
					} else {
						die("OK");
					}
				} else {
					die("OK");
				}
			}
		}
	}
} elseif (route(1) == "paywant") {
	$method = $db->from('payment_methods')
		->where('method_id', 3)
		->first();

	if ($method) {
		$data = json_decode($method["method_details"], true);

		$apiKey = $data['key'];

		$apiSecret = $data['secret'];

		$SiparisID = $_POST['SiparisID'];

		/* bakiye geliyor */
		$ExtraData = $_POST['ExtraData'];

		$UserID = $_POST['UserID'];

		$ReturnData = $_POST['ReturnData'];

		$Status = $_POST['Status'];

		$OdemeKanali = $_POST['OdemeKanali'];

		$OdemeTutari = $_POST['OdemeTutari'];

		$NetKazanc = $_POST['NetKazanc'];

		$Hash = $_POST['Hash'];

		$error = false;

		if (!$error) {
			$hashKontrol = base64_encode(
				hash_hmac(
					'sha256',
					"$SiparisID|$ExtraData|$UserID|$ReturnData|$Status|$OdemeKanali|$OdemeTutari|$NetKazanc" . $apiKey,
					$apiSecret,
					true
				)
			);

			if ($Hash == $hashKontrol) {
				$user = $db->from('users')
					->where('user_id', $UserID)->first();

				if ($user) {
					$query = $db->from('deposits')
						->where('deposit_method', 3)
						->where('order_id', $SiparisID)->first();

					if (!$query) {
						$insert_order = $db->insert('deposits')
							->set(
								array(

									'user_id' => $UserID,
									'order_id' => $SiparisID,
									'deposit_date' => time(),
									'deposit_method' => 3,
									'deposit_amount' => $ExtraData,
									'deposit_status' => ($Status == "100" ? 1 : 0)

								)
							);

						if ($insert_order && $Status == "100") {
							$payment_bonus = $db->from('payment_bonus')
								->where('bonus_method', 3)
								->where('bonus_from', $ExtraData, '<=')
								->orderby('bonus_from', 'DESC')
								->first();

							if ($payment_bonus) {
								
								$balance = $user['user_balance'] +  ($ExtraData + (($ExtraData * $payment_bonus['bonus_amount']) / 100));
								
								
							} else {
								$balance = $user['user_balance'] + $ExtraData;
							}

							$update_user = $db->update('users')
								->where('user_id', $user['user_id'])
								->set(
									array(

										'user_balance' => $balance

									)
								);

							if ($update_user) {
								reference($db, $user["user_id"], $ExtraData);
                                exit("OK");
							}
						}
					} else {
						exit("OK");
					}
				}
			}
		}
	}
} elseif (route(1) == "shoplemo") {
	$method = $db->from('payment_methods')
		->where('method_id', 5)
		->first();

	if ($method) {
		$method_details = json_decode($method['method_details'], true);

		$apikey = $method_details['key'];

		$secretkey = $method_details['secret'];

		if (!$_POST || $_POST['status'] != 'success') {
			die('Shoplemo.com');
		}

		$_data = json_decode($_POST['data']);

		$hash = base64_encode(hash_hmac('sha256', $_data['progress_id'] . implode('|', $_data['payment']) . $apikey, $secretkey, true));

		if ($hash != $_data['hash']) {
			die('Shoplemo: Calculated hashed doesn\'t match!');
		}

		if ($_data['payment']['payment_status'] == 'COMPLETED') {
			$custom_params = json_decode($_data['custom_params']);

			$order_id = $custom_params->payment_code;

			$payment = $db->from('deposits')
				->where('deposit_method', 5)
				->where('order_id', $order_id)->first();

			if ($payment) {
				$payment_bonus = $db->from('payment_bonus')
					->where('bonus_method', 5)
					->where('bonus_from', $payment['deposit_amount'], '<=')
					->orderby('bonus_from', 'DESC')
					->first();

				if ($payment_bonus) {
					$balance = $user['user_balance'] +  ($deposit['deposit_amount'] + (($deposit['deposit_amount'] * $payment_bonus['bonus_amount']) / 100));	
				} else {
					$balance = $user['user_balance'] + $payment['deposit_amount'];
				}

				$update_user = $db->update('users')
					->where('user_id', $payment['user_id'])
					->set(
						array(

							'user_balance' => $balance

						)
					);

				if ($update_user) {
					reference($db, $payment["user_id"], $payment['deposit_amount']);

					exit("OK");
				} else {
					exit("NO");
				}
			} else {
				exit("NO");
			}
		} else {
			exit("NO");
		}
	} else {
		exit("Ödeme Yöntemi Yok");
	}
}

