Skip to content

Instantly share code, notes, and snippets.

@nihat-js
Created November 14, 2024 11:15
Show Gist options
  • Select an option

  • Save nihat-js/5915103f1a9a4043439dc4dbb5ccf903 to your computer and use it in GitHub Desktop.

Select an option

Save nihat-js/5915103f1a9a4043439dc4dbb5ccf903 to your computer and use it in GitHub Desktop.
<?php
namespace App\Services\Payment;
use App\Transaction;
use Request;
class Epoint
{
private $public_key = "i000200600";
private $private_key = "zJnyBZ5Z4YUVxxbY0kBdhYZj";
private $currency;
private $language;
private $quizId;
private $requestUrl = "https://epoint.az/api/1/request";
public function __construct($quizId, $currency = "AZN", $language = "az")
{
$this->quizId = $quizId;
$this->currency = $currency;
$this->language = $language;
}
public function success(Request $request)
{
$transaction = Transaction::where("transaction_id",$request->transaction_id)->first();
$isValid = $request->signature == base64_encode(sha1("{$this->private_key}{$transaction->data}{$this->private_key}", true));
if ($isValid) {
return \Log::error("Invalid transaction data");
}
$transaction->update([
"status" => "completed"
]);
$quiz = \App\Quiz::where("quiz_id",$transaction->quiz_id)->firstOrFail();
$quiz->update([
"fund_raised" => $quiz->funds_raised + $transaction->amount,
]);
\Log::info("Transaction {$request->transaction_id} Successfull ");
}
public function error(Request $request)
{
$transaction = Transaction::where("transaction_id",$request->transaction_id)->firstOrFail();
$isValid = $request->signature == base64_encode(sha1("{$this->private_key}{$transaction->data}{$this->private_key}", true));
if ($isValid) {
$transaction->update([
"status" => "failed"
]);
}
}
public function getRedirectURL($amount)
{
$order_id = time() . rand(10, 99);
$json_string = [
"public_key" => $this->public_key,
"amount" => $amount,
"currency" => $this->currency,
"language" => $this->language,
"description" => 'Tripocell.com',
"order_id" => $order_id,
"success_redirect_url" => env("APP_URL") . "payment/success",
"error_redirect_url" => env("APP_URL") . "payment/error",
];
$json_string = json_encode($json_string, true);
$data = base64_encode($json_string);
$private_key = $this->private_key;
$signature = base64_encode(sha1($private_key . $data . $private_key, 1));
$postfields = http_build_query(['data' => $data, 'signature' => $signature]);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $this->requestUrl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $postfields,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
));
$response = curl_exec($curl);
$response = json_decode($response);
// dd($response);
if ($response && !empty($response->status) && $response->status == "success") {
$transaction = Transaction::create([
"transaction_id" => $response->transaction,
"order_id" => $order_id,
"user_id" => \Auth::id(),
"quiz_id" => $this->quizId,
"amount" => $amount,
"date" => date("Y-m-d H:i:s"),
]);
return ["redirect_url" => $response->redirect_url, "transaction" => $response->transaction, "order_id" => $order_id, 'data' => $data, 'signature' => $signature];
}
curl_close($curl);
return false;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment