Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

خطای درگاه سپهر #272

Open
daniyal1396m opened this issue Sep 12, 2024 · 8 comments
Open

خطای درگاه سپهر #272

daniyal1396m opened this issue Sep 12, 2024 · 8 comments

Comments

@daniyal1396m
Copy link

daniyal1396m commented Sep 12, 2024

سلام دوستان توسعه دهنده این پروژه ,
درگاه سپهر لینکاش توی کانفیگ مشکل داره و ارور تایم اوت میده و وریفای کردن خرید مشکل داره و ارور میده
ممنون میشم درست بشه

@khanzadimahdi
Copy link
Member

سلام وقت بخیر. میشه لطفا اروری که میده رو هم بزارید؟

کد درگاه توی ادرس زیر هست و میتونید یه نگاهی بهش بندازید

https://github.com/shetabit/multipay/tree/master/src/Drivers/Sepehr

@daniyal1396m
Copy link
Author

به اون صفحه پرداخت نمیرفت که من لینکش رو تغیر دادم و با این کار کرد
'apiGetToken' => 'https://sepehr.shaparak.ir:8081/V1/PeymentApi/GetToken', 'apiPaymentUrl' => 'https://sepehr.shaparak.ir:8080/pay', 'apiVerificationUrl' => 'https://sepehr.shaparak.ir:8081/V1/PeymentApi/Advice',
و این کد که برنامه نویس قبلی نوشته

`
$invoice = (new Invoice)->via($driver)->amount($amount);
$callbackUrl = route('payment-back', ['id' => $order->id]);
$pay = Payment::callbackUrl($callbackUrl)->purchase($invoice, function ($driver, $transactionId) use ($order, $step, $paymentMethod, $request, $amount) {
$order->update([
'payment_method' => $paymentMethod->id,
'transactionId' => $transactionId,
]);
PaymentLog::query()->create([
'transactionId' => $transactionId,
'target' => 'OrderStep',
'target_id' => $step->id,
'amount' => $amount,
'payment_method' => $paymentMethod->id,
]);
})->pay();

`
اینم کد من که از خوده بانک صادرات گرفتم

`
private function payment($order, $amount)
{
$amount = $amount * 10;
$terminal = "کد ترمینال";
$invoiceNumber = $order->id;
$redirectAddress = route('shop-payment-callback', ['id' => $order->id]);
$data = array(
'Amount' => $amount,
'callbackURL' => $redirectAddress,
'invoiceID' => $invoiceNumber,
'terminalID' => $terminal,
'payload' => ''
);
$dataQuery = http_build_query($data);
$AddressServiceToken = "https://sepehr.shaparak.ir:8081/V1/PeymentApi/GetToken";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $AddressServiceToken);
curl_setopt($ch, CURLOPT_POSTFIELDS, $dataQuery);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$TokenArray = curl_exec($ch);
curl_close($ch);
$decode_TokenArray = json_decode($TokenArray);
$AccessToken = $decode_TokenArray->Accesstoken;
ShopPaymentLog::query()->create([
'transactionId' => $AccessToken,
'target' => 'OrderStep',
'target_id' => $order->id,
'amount' => $amount,
'payment_method' => 1,
]);
$data['token'] = $AccessToken;
return $data;
}

`

@daniyal1396m
Copy link
Author

if ($token_array === false) {
throw new PurchaseFailedException('درگاه مورد نظر پاسخگو نمی‌باشد، لطفا لحظاتی بعد امتحان کنید.');
}

    این خطا رو میگرفتم 

@daniyal1396m
Copy link
Author

daniyal1396m commented Sep 12, 2024

`
public function back(Request $request)
{
$validate = Validator::make($request->all(), [
'id' => 'required|exists:shop_orders,id',
]);

		Log::info('Validation process started for order ID.', ['order_id' => $request->get('id')]);
		
		if ($validate->fails()) {
			Log::info('Validation failed for order ID.', ['order_id' => $request->get('id')]);
			return redirect($this->url_back . '?=error');
		}
		
		Log::info('Validation successful for order ID.', ['order_id' => $request->get('id')]);
		
		$order = $this->getOrder($request->get('id'));
		Log::info('Attempting to retrieve order.', ['order_id' => $request->get('id')]);
		
		if (!$order) {
			Log::info('Order not found.', ['order_id' => $request->get('id')]);
			return redirect($this->url_back . '?=error');
		}
		
		Log::info('Order retrieved successfully.', ['order_id' => $request->get('id')]);
		
		DB::beginTransaction();
		Log::info('Database transaction started.');
		
		$terminal = "کد ترمینال";
		$digitalreceipt = (isset($_POST['digitalreceipt'])) ? $_POST['digitalreceipt'] : "";
		$params = "digitalreceipt={$digitalreceipt}&Tid={$terminal}";
		Log::info('Preparing cURL request for payment advice.', [
			'digitalreceipt' => $digitalreceipt,
			'terminal' => $terminal,
		]);
		
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, 'https://sepehr.shaparak.ir:8081/V1/PeymentApi/Advice');
		curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		$res = curl_exec($ch);
		curl_close($ch);
		
		Log::info('cURL request completed.', ['response' => $res]);
		
		$result = json_decode($res, true);
		
		if (strtoupper($result['Status']) == 'OK') {
			Log::info('Payment advice successful.', ['response' => $result]);
			
			$referenceId = $_POST['digitalreceipt'];
			$rrn = $_POST['rrn'];
			Log::info('Processing payment update.', [
				'order_id' => $order->id,
				'reference_id' => $referenceId,
				'rrn' => $rrn,
			]);
			$order->update(['status' => 'paymented', 'pay' => 1]);
			Log::info('Order status updated to paymented.', ['order_id' => $order->id]);
			
			$this->discountCheck($order->couponId, $order->customer_id);
			Log::info('Discount check completed.', [
				'coupon_id' => $order->couponId,
				'customer_id' => $order->customer_id,
			]);
			
			$this->cartClear($order->customer_id);
			Log::info('Cart cleared for customer.', ['customer_id' => $order->customer_id]);
			
			Event::dispatch(new ShopOrderInvoiceEvent($order));
			Log::info('ShopOrderInvoiceEvent dispatched.', ['order_id' => $order->id]);
			
			DB::commit();
			Log::info('Database transaction committed.');
			
			return redirect($this->url_back . '/' . $order->id);
		} else {
			Log::info('Payment advice failed.', ['response' => $result]);
			DB::rollBack();
			return redirect($this->url_back . '?=error');
		}
	}
	`

@khanzadimahdi
Copy link
Member

لطفا براش PR بزار تا مرج کنیم و فیکس بشه. ممنونم.

@khanzadimahdi khanzadimahdi transferred this issue from shetabit/payment Sep 13, 2024
@daniyal1396m
Copy link
Author

سلام مجدد چطور انجامش بدم ؟

@daniyal1396m
Copy link
Author

`<?php

namespace App\Driver;

use Shetabit\Multipay\Abstracts\Driver;
use Shetabit\Multipay\Contracts\ReceiptInterface;
use Shetabit\Multipay\Exceptions\InvalidPaymentException;
use Shetabit\Multipay\Exceptions\PurchaseFailedException;
use Shetabit\Multipay\Invoice;
use Shetabit\Multipay\Receipt;
use Shetabit\Multipay\RedirectionForm;
use Shetabit\Multipay\Request;

class SepehrNew extends Driver
{
	/**
	 * Invoice
	 *
	 * @var Invoice
	 */
	protected $invoice;
	
	/**
	 * Driver settings
	 *
	 * @var object
	 */
	protected $settings;
	
	/**
	 * Sepehr constructor.
	 * Construct the class with the relevant settings.
	 *
	 * @param Invoice $invoice
	 * @param $settings
	 */
	public function __construct(Invoice $invoice, $settings)
	{
		$this->invoice($invoice);
		$this->settings = (object)$settings;
	}
	
	/**
	 * Purchase Invoice.
	 *
	 * @throws PurchaseFailedException
	 */
	public function purchase()
	{
		$amount = $this->invoice->getAmount();
		$data = [
			'Amount' => $amount,
			'callbackURL' => $this->invoice->getDetails()['redirectUrl'],
			'invoiceID' => $this->invoice->getDetails()['orderId'],
			'terminalID' => $this->settings->terminalId,
			'payload' => ''
		];
		$AddressServiceToken = "https://sepehr.shaparak.ir:8081/V1/PeymentApi/GetToken";
		$token_array = $this->makeHttpChargeRequest('POST', $data, $AddressServiceToken);
		$decode_token_array = $token_array;
		$status = $decode_token_array['Status'];
		$access_token = $decode_token_array['Accesstoken'];
		if (empty($access_token) && $status != 0) {
			$this->purchaseFailed($status);
		}
		$this->invoice->transactionId($access_token);
		$data['token'] = $this->invoice->getTransactionId();
		return $data;
	}
	
	/**
	 * Pay the Invoice
	 *
	 * @return RedirectionForm
	 */
	public function pay(): RedirectionForm
	{
		return $this->redirectWithForm($this->settings->apiPaymentUrl, [
			'Amount' => $this->invoice->getAmount(),
			'callbackURL' => $this->invoice->getDetails()['redirectUrl'],
			'invoiceID' => $this->invoice->getDetails()['orderId'],
			'terminalID' => $this->settings->terminalId,
			'payload' => '',
			'token' => $this->invoice->getTransactionId()
		], 'POST');
	}
	
	/**
	 * Verify payment
	 *
	 * @return ReceiptInterface
	 *
	 * @throws InvalidPaymentException
	 *
	 */
	public function verify(): ReceiptInterface
	{
		$resp_code = Request::input('respcode');
		$amount = $this->invoice->getAmount();
		if ($resp_code != 0) {
			$this->notVerified($resp_code);
		}
		$digitalreceipt = (isset($_POST['digitalreceipt'])) ? $_POST['digitalreceipt'] : "";
		$terminal = $this->settings->terminalId;
		$params = ['digitalreceipt' => $digitalreceipt, 'Tid' => $terminal];
		$advice_array = $this->makeHttpChargeRequest('POST', $params, 'https://sepehr.shaparak.ir:8081/V1/PeymentApi/Advice');
		$decode_advice_array = $advice_array;
		$status = $decode_advice_array['Status'];
		$return_id = $decode_advice_array['ReturnId'];
		if ($status == "Ok") {
			if ($return_id != $amount) {
				return 'مبلغ واریز با قیمت محصول برابر نیست';
			}
			return $this->createReceipt(Request::input('rrn'));
		} else {
			$message = 'تراکنش نا موفق بود در صورت کسر مبلغ از حساب شما حداکثر پس از 72 ساعت مبلغ به حسابتان برمیگردد.';
			return $message;
		}
	}
	
	/**
	 * Generate the payment's receipt
	 *
	 * @param $referenceId
	 *
	 * @return Receipt
	 */
	protected function createReceipt($referenceId)
	{
		$receipt = new Receipt('sepehr', $referenceId);
		
		return $receipt;
	}
	
	/**
	 * Trigger an exception
	 *
	 * @param $status
	 *
	 * @throws PurchaseFailedException
	 */
	protected function purchaseFailed($status)
	{
		$translations = array(
			-1 => 'تراکنش پیدا نشد.',
			-2 => 'عدم تطابق ip و یا بسته بودن port 8081',
			-3 => '‫ها‬ ‫‪Exception‬‬ ‫خطای‬ ‫–‬ ‫عمومی‬ ‫خطای‬ ‫‪Total‬‬ ‫‪Error‬‬',
			-4 => 'امکان انجام درخواست برای این تراکنش وجود ندارد.',
			-5 => 'آدرس ip نامعتبر می‌باشد.',
			-6 => 'عدم فعال بودن سرویس برگشت تراکنش برای پذیرنده',
		);
		
		if (array_key_exists($status, $translations)) {
			throw new PurchaseFailedException($translations[$status]);
		} else {
			throw new PurchaseFailedException('خطای ناشناخته ای رخ داده است.');
		}
	}
	
	/**
	 * Trigger an exception
	 *
	 * @param $status
	 *
	 * @throws InvalidPaymentException
	 */
	private function notVerified($status)
	{
		$translations = array(
			-1 => ' تراکنش توسط خریدار کنسل شده است.',
			-2 => 'زمان انجام تراکنش برای کاربر به پایان رسیده است.',
			-3 => '‫ها‬ ‫‪Exception‬‬ ‫خطای‬ ‫–‬ ‫عمومی‬ ‫خطای‬ ‫‪Total‬‬ ‫‪Error‬‬',
			-4 => 'امکان انجام درخواست برای این تراکنش وجود ندارد.',
			-5 => 'آدرس ip نامعتبر می‌باشد.',
			-6 => 'عدم فعال بودن سرویس برگشت تراکنش برای پذیرنده',
		);
		
		if (array_key_exists($status, $translations)) {
			return $translations[$status];
		} else {
			return 'خطای ناشناخته ای رخ داده است.';
		}
	}
	
	private function test_input($data)
	{
		$data = trim($data);
		$data = stripslashes($data);
		$data = htmlspecialchars($data);
		return $data;
	}
	
	private function makeHttpChargeRequest($_Method, $_Data, $AddressServiceToken)
	{
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $AddressServiceToken);
		curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($_Data));
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		$TokenArray = curl_exec($ch);
		curl_close($ch);
		return json_decode($TokenArray, true);
	}
}

`

این کد من درسته برای درگاه سپهر فقط اگر یه راهکار بهتر برای نشون دادن ارور داشته باشید خیلی خوب میشه

@khanzadimahdi
Copy link
Member

لطفا برای کدتون pull request (PR) بزارید. کافیه رپازیتوری رو fork کنید بعدش تغییرات رو توی fork اعمال کنید بعدش از ادرس زیر درخواست مرج شدن رو بزارید:

https://github.com/shetabit/multipay/pulls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants