diff --git a/src/Bahamta/Bahamta.php b/src/Bahamta/Bahamta.php new file mode 100644 index 00000000..2f334762 --- /dev/null +++ b/src/Bahamta/Bahamta.php @@ -0,0 +1,245 @@ +order_id = $order_id; +// } + + public function getLink() { + return $this->link; + } + + public function setLink( $link ) { + $this->link = $link; + } + + /** + * {@inheritdoc} + */ + public function set( $amount ) { + $this->amount = $amount * 10; + + return $this; + } + + /** + * {@inheritdoc} + */ + public function ready() { + $this->sendPayRequest(); + + return $this; + } + + /** + * Send pay request to server + * + * @return void + * + * @throws BahamtaException + */ + protected function sendPayRequest() { + $this->newTransaction(); +// $cards = Auth::user()->cardsActive()->pluck( 'card_number' ); + +// foreach ( $cards as $card ) { +// $cardString = Str::replaceFirst( "[" , "" , $cards ); +// if ( ! $cards->last() ) { +// $cardString = Str::replaceFirst( "]" , "," , $cardString ); +// }else{ +// $cardString = Str::replaceFirst( "]" , "" , $cardString ); +// } +// } + $params = "?api_key=" . $this->config->get( 'gateway.bahamta.api_key' ) . "&amount_irr=" . $this->amount . + "&callback_url=" . $this->getCallback() . "&reference=" . $this->getOrderId(); +// . "&trusted_pan=" . $cardString; + + $ch = curl_init(); + curl_setopt( $ch , CURLOPT_URL , $this->serverUrl . $params ); + curl_setopt( $ch , CURLOPT_RETURNTRANSFER , true ); + + $result = curl_exec( $ch ); + curl_close( $ch ); + $response = json_decode( $result ); + if ( $response->ok == false ) { + $this->transactionFailed(); + $this->newLog( $response->error , BahamtaException::$errors[ $response->error ] ); + throw new \Exception(BahamtaException::$errors[ $response->error ]); + } + + $this->refId = $this->getOrderId(); + $this->link = $response->result->payment_url; + $this->trackingCode = $this->getOrderId(); + + $this->transactionSetRefId(); + } + + /** + * Gets callback url + * @return string + */ + function getCallback() { + if ( ! $this->callbackUrl ) { + $this->callbackUrl = $this->config->get( 'gateway.bahamta.callback-url' ); + } + + return $this->makeCallback( $this->callbackUrl , [ 'transaction_id' => $this->transactionId() ] ); + } + + public function getOrderId() { + return $this->order_id; + } + + public function setOrderId( $order_id ) { + $this->order_id = $order_id; + } + + /** + * {@inheritdoc} + */ + public function redirect() { + $link = $this->link; + + return redirect( $link ); + } + + /** + * {@inheritdoc} + */ + public function verify( $transaction ) { + parent::verify( $transaction ); + + $this->userPayment(); + $this->verifyPayment(); + + return $this; + } + + /** + * Check user payment + * + * @return bool + * + * @throws IdpayException + */ + protected function userPayment() { +// $payRequestResCode = Input::get( 'state' ); +// $this->trackingCode = Input::get( 'track_id' ); +//// $this->refId = Input::get( 'id' ); +//// $this->order_id = Input::get( 'order_id' ); +// $this->amount = Input::get( 'total' ); +// $this->cardNumber = Input::get( 'pay_pan' ); +// +// if ( $payRequestResCode == 'paid' ) { +// return true; +// } + +// $this->transactionFailed(); +// $this->newLog( $payRequestResCode , IdpayException::$errors[ $payRequestResCode ] ); +// throw new IdpayException( $payRequestResCode ); + } + + /** + * Verify user payment from bank server + * + * @return bool + * + * @throws BahamtaException + * @throws SoapFault + */ + protected function verifyPayment() { + + $params = "?api_key=" . $this->config->get( 'gateway.bahamta.api_key' ) . "&amount_irr=" . $this->amount . + "&reference=" . $this->refId(); + + $ch = curl_init(); + curl_setopt( $ch , CURLOPT_URL , $this->verifyUrl . $params ); + curl_setopt( $ch , CURLOPT_RETURNTRANSFER , true ); + + $result = curl_exec( $ch ); + curl_close( $ch ); + $response = json_decode( $result ); + + if ( $response->ok == false ) { + $this->transactionFailed(); + $this->newLog( $response->error , BahamtaException::$errors[ $response->error ] ); + throw new \Exception(BahamtaException::$errors[ $response->error ]); + } + + $this->trackingCode = $response->result->pay_trace; + $this->transactionSucceed(); + $this->newLog( 'SUCCEED' , Enum::TRANSACTION_SUCCEED_TEXT ); + + return true; + } + + /** + * Sets callback url + * + * @param $url + */ + function setCallback( $url ) { + $this->callbackUrl = $url; + + return $this; + } + + /** + * Send settle request + * + * @return bool + * + * @throws MellatException + * @throws SoapFault + */ + protected function settleRequest() { + + $params = array ( + 'id' => $this->refId , + 'order_id' => $this->order_id , + ); + + $ch = curl_init(); + curl_setopt( $ch , CURLOPT_URL , $this->settelUrl ); + curl_setopt( $ch , CURLOPT_POSTFIELDS , json_encode( $params ) ); + curl_setopt( $ch , CURLOPT_RETURNTRANSFER , true ); + curl_setopt( $ch , CURLOPT_HTTPHEADER , array ( + 'Content-Type: application/json' , + 'X-API-KEY: ' . $this->config->get( 'gateway.idpay.api-key' ) . '' , + 'X-SANDBOX: 1' , + ) ); + + $result = curl_exec( $ch ); + $httpcode = curl_getinfo( $ch , CURLINFO_HTTP_CODE ); + curl_close( $ch ); + + var_dump( $httpcode ); + var_dump( $result ); + } +} diff --git a/src/Bahamta/BahamtaException.php b/src/Bahamta/BahamtaException.php new file mode 100644 index 00000000..28b3f08a --- /dev/null +++ b/src/Bahamta/BahamtaException.php @@ -0,0 +1,36 @@ + 'گرچه ساختار کلید درست است، اما هیچ فروشنده‌ای با این کلید در webpay ثبت نشده است.' , + 'INVALID_AMOUNT' => 'پارامتر مبلغ فرستاده نشده و یا نادرست فرستاده شده است. مثلاً ممکن است ارقام مبلغ سه رقم به سه رقم با علامت , جدا شده باشند که نادرست است.' , + 'TOO_LESS_AMOUNT' => 'مبلغ کمتر از حد مجاز (هزار تومان) است.' , + 'TOO_MUCH_AMOUNT' => 'مبلغ بیشتر از حد مجاز (پنجاه میلیون تومان) است.' , + 'INVALID_REFERENCE' => 'شماره شناسه پرداخت ناردست است. این مقدار باید یک عبارت حرفی با طول بین ۱ تا ۶۴ حرف باشد.' , + 'INVALID_TRUSTED_PAN' => 'لیست شماره کارتها نادرست است.' , + 'INVALID_CALLBACK' => 'آدرس فراخوانی نادرست است. این آدرس باید با ://http و یا ://https شروع شود.' , + 'INVALID_PARAM' => 'خطایی در مقادیر فرستاده شده وجود دارد که جزو موارد یاد شده بالا نیست.' , + 'ALREADY_PAID' => 'درخواست پرداختی با شناسه داده شده قبلاً ثبت و پرداخت شده است.' , + 'MISMATCHED_DATA' => ' درخواست پرداختی با شناسه داده شده قبلاً ثبت و منتظر پرداخت است، اما مقادیر فرستاده شده در این درخواست، با درخواست اصلی متفاوت است.' , + 'NO_REG_TERMINAL' => 'ترمینالی برای این فروشنده ثبت نشده است.' , + 'NO_AVAILABLE_GATEWAY' => 'درگاههای پرداختی که این فروشنده در آنها ترمینال ثبت شده دارد، قادر به ارائه خدمات نیستند.' , + 'SERVICE_ERROR' => 'خطای داخلی سرویس رخ داده است.' , + + + 'INVALID_API_CALL' => 'قالب فراخوانی سرویس رعایت نشده است.' , + 'INVALID_API_KEY' => 'لید الکترونیکی صاحب فروشگاه فرستاده نشده و یا ساختار آن نادرست است.' , + 'UNKNOWN_BILL' => 'پرداختی با شماره شناسه فرستاده شده ثبت نشده است.' , + 'MISMATCHED_DATA' => 'مبلغ اعلام شده با آنچه در webpay ثبت شده است مطابقت ندارد.' , + 'NOT_CONFIRMED' => 'این پرداخت تأیید نشد.' , + ); + + public function __construct( $errorId ) { + $this->errorId = intval( $errorId ); + + parent::__construct( @self::$errors[ $this->errorId ] . ' #' . $this->errorId , $this->errorId ); + } +} diff --git a/src/Enum.php b/src/Enum.php index 266f0a6e..4d027359 100644 --- a/src/Enum.php +++ b/src/Enum.php @@ -4,21 +4,30 @@ class Enum { - const MELLAT = 'MELLAT'; - const SADAD = 'SADAD'; - const ZARINPAL = 'ZARINPAL'; - const PAYLINE = 'PAYLINE'; - const JAHANPAY = 'JAHANPAY'; - const PARSIAN = 'PARSIAN'; - const PASARGAD = 'PASARGAD'; - const SAMAN = 'SAMAN'; - const ASANPARDAKHT = 'ASANPARDAKHT'; - const PAYPAL = 'PAYPAL'; - const PAYIR = 'PAYIR'; - const IRANKISH = 'IRANKISH'; - const MASKAN = self::IRANKISH; - - static function getIPGs(){ + const MELLAT = 'MELLAT'; + const SADAD = 'SADAD'; + const ZARINPAL = 'ZARINPAL'; + const PAYLINE = 'PAYLINE'; + const JAHANPAY = 'JAHANPAY'; + const PARSIAN = 'PARSIAN'; + const PASARGAD = 'PASARGAD'; + const SAMAN = 'SAMAN'; + const ASANPARDAKHT = 'ASANPARDAKHT'; + const PAYPAL = 'PAYPAL'; + const PAYIR = 'PAYIR'; + const IRANKISH = 'IRANKISH'; + const IDPAY = 'IDPAY'; + const PAYPING = 'PAYPING'; + const NEXTPAY = 'NEXTPAY'; + const BAHAMTA = 'BAHAMTA'; + const YEKPAY = 'YEKPAY'; + const MRPAY = 'MRPAY'; + const POOLAM = 'POOLAM'; + const ZIBAL = 'ZIBAL'; + const PAYSTAR = 'PAYSTAR'; + const MASKAN = self::IRANKISH; + + static function getIPGs(){ $reflect = new \ReflectionClass(static::class); $excepts=[ @@ -30,29 +39,29 @@ static function getIPGs(){ 'TRANSACTION_FAILED', 'TRANSACTION_FAILED_TEXT', ]; - + if(function_exists('array_except')) return array_values(array_except($reflect->getConstants(),$excepts)); else return array_values(\Illuminate\Support\Arr::except($reflect->getConstants(),$excepts)); } - /** - * Status code for status field in poolport_transactions table - */ - const TRANSACTION_INIT = 'INIT'; - const TRANSACTION_INIT_TEXT = 'تراکنش ایجاد شد.'; - - /** - * Status code for status field in poolport_transactions table - */ - const TRANSACTION_SUCCEED = 'SUCCEED'; - const TRANSACTION_SUCCEED_TEXT = 'پرداخت با موفقیت انجام شد.'; - - /** - * Status code for status field in poolport_transactions table - */ - const TRANSACTION_FAILED = 'FAILED'; - const TRANSACTION_FAILED_TEXT = 'عملیات پرداخت با خطا مواجه شد.'; + /** + * Status code for status field in poolport_transactions table + */ + const TRANSACTION_INIT = 'INIT'; + const TRANSACTION_INIT_TEXT = 'تراکنش ایجاد شد.'; + + /** + * Status code for status field in poolport_transactions table + */ + const TRANSACTION_SUCCEED = 'SUCCEED'; + const TRANSACTION_SUCCEED_TEXT = 'پرداخت با موفقیت انجام شد.'; + + /** + * Status code for status field in poolport_transactions table + */ + const TRANSACTION_FAILED = 'FAILED'; + const TRANSACTION_FAILED_TEXT = 'عملیات پرداخت با خطا مواجه شد.'; } diff --git a/src/GatewayResolver.php b/src/GatewayResolver.php index 53071630..d69590b6 100644 --- a/src/GatewayResolver.php +++ b/src/GatewayResolver.php @@ -2,14 +2,21 @@ namespace Larabookir\Gateway; +use Larabookir\Gateway\Bahamta\Bahamta; +use Larabookir\Gateway\Idpay\Idpay; use Larabookir\Gateway\Irankish\Irankish; +use Larabookir\Gateway\Mrpay\Mrpay; +use Larabookir\Gateway\Nextpay\Nextpay; use Larabookir\Gateway\Parsian\Parsian; use Larabookir\Gateway\Paypal\Paypal; +use Larabookir\Gateway\Paystar\Paystar; +use Larabookir\Gateway\Poolam\Poolam; use Larabookir\Gateway\Sadad\Sadad; use Larabookir\Gateway\Mellat\Mellat; use Larabookir\Gateway\Pasargad\Pasargad; use Larabookir\Gateway\Saman\Saman; use Larabookir\Gateway\Asanpardakht\Asanpardakht; +use Larabookir\Gateway\Yekpay\Yekpay; use Larabookir\Gateway\Zarinpal\Zarinpal; use Larabookir\Gateway\Payir\Payir; use Larabookir\Gateway\Exceptions\RetryException; @@ -17,150 +24,168 @@ use Larabookir\Gateway\Exceptions\InvalidRequestException; use Larabookir\Gateway\Exceptions\NotFoundTransactionException; use Illuminate\Support\Facades\DB; +use Larabookir\Gateway\Zibal\Zibal; + +class GatewayResolver { + + /** + * @var Config + */ + public $config; + protected $request; + /** + * Keep current port driver + * + * @var Mellat|Saman|Sadad|Zarinpal|Payir|Parsian + */ + protected $port; + + /** + * Gateway constructor. + * + * @param null $config + * @param null $port + */ + public function __construct( $config = null , $port = null ) { + $this->config = app( 'config' ); + $this->request = app( 'request' ); + + if ( $this->config->has( 'gateway.timezone' ) ) { + date_default_timezone_set( $this->config->get( 'gateway.timezone' ) ); + } + + if ( ! is_null( $port ) ) { + $this->make( $port ); + } + } -class GatewayResolver -{ - - protected $request; - - /** - * @var Config - */ - public $config; - - /** - * Keep current port driver - * - * @var Mellat|Saman|Sadad|Zarinpal|Payir|Parsian - */ - protected $port; - - /** - * Gateway constructor. - * @param null $config - * @param null $port - */ - public function __construct($config = null, $port = null) - { - $this->config = app('config'); - $this->request = app('request'); - - if ($this->config->has('gateway.timezone')) - date_default_timezone_set($this->config->get('gateway.timezone')); - - if (!is_null($port)) $this->make($port); - } - - /** - * Get supported ports - * - * @return array - */ - public function getSupportedPorts() - { - return (array) Enum::getIPGs(); - } - - /** - * Call methods of current driver - * - * @return mixed - */ - public function __call($name, $arguments) - { - - // calling by this way ( Gateway::mellat()->.. , Gateway::parsian()->.. ) - if(in_array(strtoupper($name),$this->getSupportedPorts())){ - return $this->make($name); - } - - return call_user_func_array([$this->port, $name], $arguments); - } - - /** - * Gets query builder from you transactions table - * @return mixed - */ - function getTable() - { - return DB::table($this->config->get('gateway.table')); - } - - /** - * Callback - * - * @return $this->port - * - * @throws InvalidRequestException - * @throws NotFoundTransactionException - * @throws PortNotFoundException - * @throws RetryException - */ - public function verify() - { - if (!$this->request->has('transaction_id') && !$this->request->has('iN')) - throw new InvalidRequestException; - if ($this->request->has('transaction_id')) { - $id = $this->request->get('transaction_id'); - }else { - $id = $this->request->get('iN'); - } - - $transaction = $this->getTable()->whereId($id)->first(); - - if (!$transaction) - throw new NotFoundTransactionException; - - if (in_array($transaction->status, [Enum::TRANSACTION_SUCCEED, Enum::TRANSACTION_FAILED])) - throw new RetryException; - - $this->make($transaction->port); - - return $this->port->verify($transaction); - } - - - /** - * Create new object from port class - * - * @param int $port - * @throws PortNotFoundException - */ - function make($port) - { - if ($port InstanceOf Mellat) { + /** + * Create new object from port class + * + * @param int $port + * + * @throws PortNotFoundException + */ + function make( $port ) { + if ( $port instanceof Mellat ) { $name = Enum::MELLAT; - } elseif ($port InstanceOf Parsian) { + } elseif ( $port instanceof Parsian ) { $name = Enum::PARSIAN; - } elseif ($port InstanceOf Saman) { + } elseif ( $port instanceof Saman ) { $name = Enum::SAMAN; - } elseif ($port InstanceOf Zarinpal) { + } elseif ( $port instanceof Zarinpal ) { $name = Enum::ZARINPAL; - } elseif ($port InstanceOf Sadad) { + } elseif ( $port instanceof Sadad ) { $name = Enum::SADAD; - } elseif ($port InstanceOf Asanpardakht) { + } elseif ( $port instanceof Asanpardakht ) { $name = Enum::ASANPARDAKHT; - } elseif ($port InstanceOf Paypal) { + } elseif ( $port instanceof Paypal ) { $name = Enum::PAYPAL; - } elseif ($port InstanceOf Payir) { + } elseif ( $port instanceof Payir ) { $name = Enum::PAYIR; - } elseif ($port InstanceOf Pasargad) { + } elseif ( $port instanceof Pasargad ) { $name = Enum::PASARGAD; - } elseif ($port InstanceOf Irankish) { + } elseif ( $port instanceof Irankish ) { $name = Enum::IRANKISH; - } elseif (in_array(strtoupper($port), $this->getSupportedPorts())) { - $port = ucfirst(strtolower($port)); - $name = strtoupper($port); + } elseif ( $port instanceof Idpay ) { + $name = Enum::IDPAY; + } elseif ( $port instanceof Payping ) { + $name = Enum::PAYPING; + } elseif ( $port instanceof Nextpay ) { + $name = Enum::NEXTPAY; + } elseif ( $port instanceof Bahamta ) { + $name = Enum::BAHAMTA; + } elseif ( $port instanceof Yekpay ) { + $name = Enum::YEKPAY; + } elseif ( $port instanceof Mrpay ) { + $name = Enum::MRPAY; + } elseif ( $port instanceof Zibal ) { + $name = Enum::ZIBAL; + } elseif ( $port instanceof Poolam ) { + $name = Enum::POOLAM; + } elseif ( $port instanceof Paystar ) { + $name = Enum::PAYSTAR; + } elseif ( in_array( strtoupper( $port ) , $this->getSupportedPorts() ) ) { + $port = ucfirst( strtolower( $port ) ); + $name = strtoupper( $port ); $class = __NAMESPACE__ . '\\' . $port . '\\' . $port; - $port = new $class; - } else + $port = new $class; + } else { throw new PortNotFoundException; + } $this->port = $port; - $this->port->setConfig($this->config); // injects config - $this->port->setPortName($name); // injects config + $this->port->setConfig( $this->config ); // injects config + $this->port->setPortName( $name ); // injects config $this->port->boot(); return $this; } + + /** + * Get supported ports + * + * @return array + */ + public function getSupportedPorts() { + return (array) Enum::getIPGs(); + } + + /** + * Call methods of current driver + * + * @return mixed + */ + public function __call( $name , $arguments ) { + + // calling by this way ( Gateway::mellat()->.. , Gateway::parsian()->.. ) + if ( in_array( strtoupper( $name ) , $this->getSupportedPorts() ) ) { + return $this->make( $name ); + } + + return call_user_func_array( [ $this->port , $name ] , $arguments ); + } + + /** + * Callback + * + * @return $this->port + * + * @throws InvalidRequestException + * @throws NotFoundTransactionException + * @throws PortNotFoundException + * @throws RetryException + */ + public function verify() { + if ( ! $this->request->has( 'transaction_id' ) && ! $this->request->has( 'iN' ) ) { + throw new InvalidRequestException; + } + if ( $this->request->has( 'transaction_id' ) ) { + $id = $this->request->get( 'transaction_id' ); + } else { + $id = $this->request->get( 'iN' ); + } + + $transaction = $this->getTable()->whereId( $id )->first(); + + if ( ! $transaction ) { + throw new NotFoundTransactionException; + } + + if ( in_array( $transaction->status , [ Enum::TRANSACTION_SUCCEED , Enum::TRANSACTION_FAILED ] ) ) { + throw new RetryException; + } + + $this->make( $transaction->port ); + + return $this->port->verify( $transaction ); + } + + /** + * Gets query builder from you transactions table + * @return mixed + */ + function getTable() { + return DB::table( $this->config->get( 'gateway.table' ) ); + } } diff --git a/src/Idpay/Idpay.php b/src/Idpay/Idpay.php new file mode 100644 index 00000000..b52db0aa --- /dev/null +++ b/src/Idpay/Idpay.php @@ -0,0 +1,270 @@ +order_id = $order_id; +// } + + public function getOrderId() { + return $this->order_id; + } + + public function setOrderId( $order_id ) { + $this->order_id = $order_id; + } + + public function getLink() { + return $this->link; + } + + public function setLink( $link ) { + $this->link = $link; + } + + /** + * {@inheritdoc} + */ + public function set( $amount ) { + $this->amount = $amount *10; + + return $this; + } + + /** + * {@inheritdoc} + */ + public function ready() { + $this->sendPayRequest(); + + return $this; + } + + /** + * Send pay request to server + * + * @return void + * + * @throws MellatException + */ + protected function sendPayRequest() { + $this->newTransaction(); + + $params = array ( + 'order_id' => $this->order_id , + 'amount' => $this->amount , + 'callback' => $this->getCallback() , + ); + + $ch = curl_init(); + curl_setopt( $ch , CURLOPT_URL , $this->serverUrl ); + curl_setopt( $ch , CURLOPT_POSTFIELDS , json_encode( $params ) ); + curl_setopt( $ch , CURLOPT_RETURNTRANSFER , true ); + curl_setopt( $ch , CURLOPT_HTTPHEADER , array ( + 'Content-Type: application/json' , + 'X-API-KEY: ' . $this->config->get( 'gateway.idpay.api-key' ) . '' , + 'X - SANDBOX: 0' + ) ); + + $result = curl_exec( $ch ); + curl_close( $ch ); + + $response = json_decode( $result ); + if ( isset( $response->error_code ) ) { + $this->transactionFailed(); + $this->newLog( $response->error_code , IdpayException::$errors[ $response->error_code ] ); + throw new IdpayException( $response->error_code ); + } + + $this->refId = $response->id; + $this->link = $response->link; + $this->trackingCode = $response->id; + + $this->transactionSetRefId(); + } + + /** + * Gets callback url + * @return string + */ + function getCallback() { + if ( ! $this->callbackUrl ) { + $this->callbackUrl = $this->config->get( 'gateway.idpay.callback-url' ); + } + + return $this->makeCallback( $this->callbackUrl , [ 'transaction_id' => $this->transactionId() ] ); + } + + /** + * {@inheritdoc} + */ + public function redirect() { + $link = $this->link; + + return redirect( $link ); + } + + /** + * {@inheritdoc} + */ + public function verify( $transaction ) { + parent::verify( $transaction ); + + $this->userPayment(); + $this->validateCardNumber(); + $this->verifyPayment(); + + return $this; + } + + /** + * Check user payment + * + * @return bool + * + * @throws MellatException + */ + protected function userPayment() { + $payRequestResCode = Request::input( 'status' ); + $this->trackingCode = Request::input( 'track_id' ); + $this->refId = Request::input( 'id' ); + $this->order_id = Request::input( 'order_id' ); + $this->amount = Request::input( 'amount' ); + $this->cardNumber = Request::input( 'card_no' ); + + if ( $payRequestResCode == 100 ) { + return true; + } + +// $this->transactionFailed(); +// $this->newLog( $payRequestResCode , IdpayException::$errors[ $payRequestResCode ] ); +// throw new IdpayException( $payRequestResCode ); + } + + public function validateCardNumber() { + if ( auth()->check() ) { + $userCards = auth()->user()->cards->where( 'status' , 1 )->pluck( 'last_number' ); + if ( $userCards ) { + if ( ! $userCards->contains( substr( $this->cardNumber , - 4 ) ) ) { + $this->transactionFailed(); + $this->newLog( 4444 , IdpayException::$errors[ 4444 ] ); + throw new IdpayException( 4444 ); + } + } else { + $this->transactionFailed(); + $this->newLog( 4444 , IdpayException::$errors[ 4444 ] ); + throw new IdpayException( 4444 ); + } + } + } + + /** + * Verify user payment from bank server + * + * @return bool + * + * @throws MellatException + * @throws SoapFault + */ + protected function verifyPayment() { + + $params = array ( + 'id' => $this->refId , + 'order_id' => $this->order_id , + ); + + $ch = curl_init(); + curl_setopt( $ch , CURLOPT_URL , $this->verifyUrl ); + curl_setopt( $ch , CURLOPT_POSTFIELDS , json_encode( $params ) ); + curl_setopt( $ch , CURLOPT_RETURNTRANSFER , true ); + curl_setopt( $ch , CURLOPT_HTTPHEADER , array ( + 'Content-Type: application/json' , + 'X-API-KEY: ' . $this->config->get( 'gateway.idpay.api-key' ) . '' , + 'X-SANDBOX: 0' , + ) ); + $result = curl_exec( $ch ); + curl_close( $ch ); + $response = json_decode( $result ); + if ( isset( $response->error_code ) ) { + $this->transactionFailed(); + $this->newLog( $response->error_code , IdpayException::$errors[ $response->error_code ] ); + throw new IdpayException( $response->error_code ); + } + $this->trackingCode = $response->track_id; + $this->transactionSucceed(); + $this->newLog( $response->status , Enum::TRANSACTION_SUCCEED_TEXT ); + + return true; + } + + /** + * Sets callback url + * + * @param $url + */ + function setCallback( $url ) { + $this->callbackUrl = $url; + + return $this; + } + + /** + * Send settle request + * + * @return bool + * + * @throws MellatException + * @throws SoapFault + */ + protected function settleRequest() { + + $params = array ( + 'id' => $this->refId , + 'order_id' => $this->order_id , + ); + + $ch = curl_init(); + curl_setopt( $ch , CURLOPT_URL , $this->settelUrl ); + curl_setopt( $ch , CURLOPT_POSTFIELDS , json_encode( $params ) ); + curl_setopt( $ch , CURLOPT_RETURNTRANSFER , true ); + curl_setopt( $ch , CURLOPT_HTTPHEADER , array ( + 'Content-Type: application/json' , + 'X-API-KEY: ' . $this->config->get( 'gateway.idpay.api-key' ) . '' , + 'X-SANDBOX: 1' , + ) ); + + $result = curl_exec( $ch ); + $httpcode = curl_getinfo( $ch , CURLINFO_HTTP_CODE ); + curl_close( $ch ); + + var_dump( $httpcode ); + var_dump( $result ); + } +} diff --git a/src/Idpay/IdpayException.php b/src/Idpay/IdpayException.php new file mode 100644 index 00000000..95b1b71d --- /dev/null +++ b/src/Idpay/IdpayException.php @@ -0,0 +1,45 @@ + 'پرداخت انجام نشده است', + 2 => 'پرداخت ناموفق بوده است', + 3 => 'خطا رخ داده است', + 4 => 'بلوکه شده', + 5 => 'برگشت به پرداخت کننده', + 6 => 'برگشت خورده سیستمی', + 10 => 'در انتظار تایید پرداخت', + 100 => 'پرداخت تایید شده است', + 101 => 'پرداخت قبلا تایید شده است', + 200 => 'به دریافت کننده واریز شد', + + 11 => 'کاربر مسدود شده است.' , + 12 => 'API Key یافت نشد.' , + 13 => 'درخواست شما از {ip} ارسال شده است. این IP با IP های ثبت شده در وب سرویس همخوانی ندارد.' , + 14 => 'وب سرویس تایید نشده است.' , + 21 => 'حساب بانکی متصل به وب سرویس تایید نشده است.' , + 31 => 'کد تراکنش id نباید خالی باشد.' , + 32 => 'شماره سفارش order_id نباید خالی باشد.' , + 33 => 'مبلغ amount نباید خالی باشد' , + 34 => 'مبلغ amount باید بیشتر از {min-amount} ریال باشد.' , + 35 => 'مبلغ amount باید کمتر از {max-amount} ریال باشد.' , + 36 => 'مبلغ amount بیشتر از حد مجاز است.' , + 37 => 'آدرس بازگشت callback نباید خالی باشد.' , + 38 => 'درخواست شما از آدرس {domain} ارسال شده است. دامنه آدرس بازگشت callback با آدرس ثبت شده در وب سرویس همخوانی ندارد.' , + 51 => 'تراکنش ایجاد نشد.' , + 52 => 'استعلام نتیجه ای نداشت.' , + 53 => 'تایید پرداخت امکان پذیر نیست.' , + 54 => 'مدت زمان تایید پرداخت سپری شده است.' , + 4444 => 'کارتی که شما با آن خرید انجام داده اید معتبر نیست' , + ); + + public function __construct( $errorId ) { + $this->errorId = intval( $errorId ); + + parent::__construct( @self::$errors[ $this->errorId ] . ' #' . $this->errorId , $this->errorId ); + } +} diff --git a/src/Mrpay/Mrpay.php b/src/Mrpay/Mrpay.php new file mode 100644 index 00000000..d853edab --- /dev/null +++ b/src/Mrpay/Mrpay.php @@ -0,0 +1,196 @@ +amount = $amount; + + return $this; + } + + /** + * تعیین شماره فاکتور (اختیاری) + * + * @param $factorNumber + * + * @return $this + */ + public function setFactorNumber( $factorNumber ) { + $this->factorNumber = $factorNumber; + + return $this; + } + + /** + * {@inheritdoc} + */ + public function ready() { + $this->sendPayRequest(); + + return $this; + } + + /** + * Send pay request to server + * + * @return void + * + * @throws MrpaySendException + */ + protected function sendPayRequest() { + $this->newTransaction(); + if ( Auth::user()->cardsActive->count() < 1 ) { + flash( 'شماره کارت شما ثبت نشده است' )->error()->important(); + + return back(); + exit(); + } + $cardsActive = Auth::user()->cardsActive[ 0 ]->card_number; + $fields = [ + 'pin' => $this->config->get( 'gateway.mrpay.pin' ) , + 'amount' => $this->amount , + 'callback' => $this->getCallback() , + 'description' => '' , + 'card_number' => $cardsActive + ]; + + $ch = curl_init(); + curl_setopt( $ch , CURLOPT_URL , $this->serverUrl ); + curl_setopt( $ch , CURLOPT_POST , count( $fields ) ); + curl_setopt( $ch , CURLOPT_POSTFIELDS , http_build_query( $fields ) ); + curl_setopt( $ch , CURLOPT_SSL_VERIFYPEER , false ); + curl_setopt( $ch , CURLOPT_RETURNTRANSFER , true ); + $response = curl_exec( $ch ); + curl_close( $ch ); + if ( ! is_numeric( $response ) ) { + $this->refId = $response; + $this->transactionSetRefId(); + + return true; + } + $this->transactionFailed(); + $this->newLog( $response , MrpaySendException::$errors[ $response ] ); + throw new MrpaySendException( $response ); + } + + /** + * Gets callback url + * @return string + */ + function getCallback() { + if ( ! $this->callbackUrl ) { + $this->callbackUrl = $this->config->get( 'gateway.mrpay.callback-url' ); + } + + return $this->makeCallback( $this->callbackUrl , [ 'transaction_id' => $this->transactionId() ] ); + } + + /** + * {@inheritdoc} + */ + public function redirect() { + return redirect()->to( $this->gateUrl . $this->refId ); + } + + /** + * {@inheritdoc} + */ + public function verify( $transaction ) { + parent::verify( $transaction ); + $this->userPayment(); + $this->verifyPayment(); + + return $this; + } + + /** + * Check user payment with GET data + * + * @return bool + * + * @throws MrpayReceiveException + */ + protected function userPayment() { +// $amount = Request::input('amount'); + $transId = Request::input( 'transid' ); + $this->refId = $transId; +// $this->amount = $amount; + } + + /** + * Verify user payment from zarinpal server + * + * @return bool + * + * @throws MrpaySendException + */ + protected function verifyPayment() { + $fields = [ + 'pin' => $this->config->get( 'gateway.mrpay.pin' ) , + 'transid' => $this->refId() , + 'amount' => $this->amount , + ]; + $ch = curl_init(); + curl_setopt( $ch , CURLOPT_URL , $this->serverVerifyUrl ); + curl_setopt( $ch , CURLOPT_POSTFIELDS , http_build_query( $fields ) ); + curl_setopt( $ch , CURLOPT_SSL_VERIFYPEER , false ); + curl_setopt( $ch , CURLOPT_RETURNTRANSFER , true ); + $response = curl_exec( $ch ); + curl_close( $ch ); + + if ( $response == 1 ) { + $this->transactionSucceed(); + $this->newLog( 1 , Enum::TRANSACTION_SUCCEED_TEXT ); + + return true; + } + + $this->transactionFailed(); + $this->newLog( $response , MrpaySendException::$errors[ $response ] ); + throw new MrpaySendException( $response ); + } + + /** + * Sets callback url + * + * @param $url + */ + function setCallback( $url ) { + $this->callbackUrl = $url; + + return $this; + } +} diff --git a/src/Mrpay/MrpayReceiveException.php b/src/Mrpay/MrpayReceiveException.php new file mode 100644 index 00000000..989f788c --- /dev/null +++ b/src/Mrpay/MrpayReceiveException.php @@ -0,0 +1,31 @@ + 'تراکنش با خطا مواجه شد' , + -1 => 'amount میتواند خالی باشد' , + -2 => 'کد پین درگاه نمیتواند خالی باشد' , + -3 => 'callback نمیتواند خالی باشد' , + -4 => 'amount باید عددی باشد' , + -5 => 'amount باید بزرگتر از ۱۰۰ باشد ' , + -6 => 'کد پین درگاه اشتباه هست' , + -7 => 'ایپی سرور با ایپی درگاه مطابقت ندارد' , + -8 => 'transid نمیتواند خالی باشد ' , + -9 => 'تراکنش مورد نظر وجود ندارد ' , + -10 => 'کد پین درگاه با درگاه تراکنش مطابقت ندارد ' , + -11 => 'مبلغ با مبلغ تراکنش مطابقت ندارد ' , + -12 => 'بانک وارد شده اشتباه میباشد' , + -13 => 'درگاه غیر فعال است ' , + -14 => 'درگاه برروی سایت دیگری درحال استفاده است' , + ]; + + public function __construct( $errorId ) { + $this->errorId = $errorId; + + parent::__construct( @self::$errors[ $errorId ] . ' #' . $errorId , $errorId ); + } +} diff --git a/src/Mrpay/MrpaySendException.php b/src/Mrpay/MrpaySendException.php new file mode 100644 index 00000000..190e4b77 --- /dev/null +++ b/src/Mrpay/MrpaySendException.php @@ -0,0 +1,32 @@ + 'تراکنش با خطا مواجه شد' , + -1 => 'amount میتواند خالی باشد' , + -2 => 'کد پین درگاه نمیتواند خالی باشد' , + -3 => 'callback نمیتواند خالی باشد' , + -4 => 'amount باید عددی باشد' , + -5 => 'amount باید بزرگتر از ۱۰۰ باشد ' , + -6 => 'کد پین درگاه اشتباه هست' , + -7 => 'ایپی سرور با ایپی درگاه مطابقت ندارد' , + -8 => 'transid نمیتواند خالی باشد ' , + -9 => 'تراکنش مورد نظر وجود ندارد ' , + -10 => 'کد پین درگاه با درگاه تراکنش مطابقت ندارد ' , + -11 => 'مبلغ با مبلغ تراکنش مطابقت ندارد ' , + -12 => 'بانک وارد شده اشتباه میباشد' , + -13 => 'درگاه غیر فعال است ' , + -14 => 'درگاه برروی سایت دیگری درحال استفاده است' , + 'failed' => 'تراکنش با خطا مواجه شد' , + ]; + + public function __construct( $errorId ) { + $this->errorId = $errorId; + + parent::__construct( @self::$errors[ $errorId ] . ' #' . $errorId , $errorId ); + } +} diff --git a/src/Nextpay/Nextpay.php b/src/Nextpay/Nextpay.php new file mode 100644 index 00000000..9741f75a --- /dev/null +++ b/src/Nextpay/Nextpay.php @@ -0,0 +1,293 @@ +order_id = $order_id; +// } + + public function getOrderId() + { + return $this->order_id; + } + + public function setOrderId($order_id) + { + $this->order_id = $order_id; + } + + public function getLink() + { + return $this->link; + } + + public function setLink($link) + { + $this->link = $link; + } + + /** + * {@inheritdoc} + */ + public function set($amount) + { + $this->amount = $amount; + + return $this; + } + + /** + * {@inheritdoc} + */ + public function ready() + { + $this->sendPayRequest(); + + return $this; + } + + /** + * Send pay request to server + * + * @return void + * + * @throws NextpayException + */ + protected function sendPayRequest() + { + $this->newTransaction(); + + $params = array( + 'api_key' => $this->config->get('gateway.nextpay.api-token'), + 'amount' => (int)$this->amount, + 'order_id' => $this->order_id, + 'callback_uri' => $this->getCallback(), + ); + + $response = Http::post($this->serverUrl, $params); + $response = $response->object(); + + $trans_id = $response->trans_id; + $code = $response->code; + if ($code != -1) { + $this->transactionFailed(); + $this->newLog($code, NextpayException::$errors[$code]); + throw new NextpayException($code); + } + + $this->refId = $trans_id; + $this->link = $this->paymentUrl . '/' . $trans_id; + $this->trackingCode = $trans_id; + + $this->transactionSetRefId(); + } + + /** + * Gets callback url + * @return string + */ + function getCallback() + { + if (!$this->callbackUrl) { + $this->callbackUrl = $this->config->get('gateway.nextpay.callback-url'); + } + + return $this->makeCallback($this->callbackUrl, ['transaction_id' => $this->transactionId()]); + } + + /** + * {@inheritdoc} + */ + public function redirect() + { + $link = $this->link; + + return redirect($link); + } + + /** + * {@inheritdoc} + */ + public function verify($transaction) + { + parent::verify($transaction); + + $this->userPayment(); + $this->verifyPayment(); + + return $this; + } + + /** + * Check user payment + * + * @return bool + * + * @throws MellatException + */ + protected function userPayment() + { + $payRequestResCode = Request::get('status'); + $this->trackingCode = Request::get('transaction_id'); + $this->refId = Request::get('trans_id'); + $this->order_id = Request::get('order_id'); +// $this->amount = Input::get( 'amount' ); + $this->cardNumber = Request::get('card_holder'); +// + if (isset($this->cardNumber) && $this->cardNumber != '0000-0000-0000-0000') { + $this->validateCardNumber(); + return true; + } + +// $this->transactionFailed(); +// $this->newLog( $payRequestResCode , NextpayException::$errors[ $payRequestResCode ] ); +// throw new NextpayException( $payRequestResCode ); + } + + /** + * Verify user payment from bank server + * + * @return bool + * + * @throws NextpayException + * @throws SoapFault + */ + protected function verifyPayment() + { + $params = array( + 'trans_id' => $this->refId, + 'api_key' => $this->config->get('gateway.nextpay.api-key'), + 'order_id' => $this->order_id, + 'amount' => (int)$this->amount, + ); + $response = Http::post($this->verifyUrl, $params); + $response = $response->object(); + + $code = $response->code; + if ($code != 0) { + $this->transactionFailed(); + $this->newLog($code, NextpayException::$errors[$code]); + throw new NextpayException($code); + } else { + $this->trackingCode = $this->refId; + $this->transactionSucceed(); + $this->newLog('SUCCESS', Enum::TRANSACTION_SUCCEED_TEXT); + + return true; + } + } + + public function validateCardNumber() + { + if (auth()->check()) { + $userCards = auth()->user()->cards->where('status', 1)->pluck('last_number'); +// $userCards = Card::where('user_id',auth()->id())->where( 'status' , 1 )->pluck( 'last_number' ); + if ($userCards) { + if (!$userCards->contains(substr($this->cardNumber, -4))) { + $this->transactionFailed(); + $this->newLog(4444, NextpayException::$errors[4444]); + throw new NextpayException(4444); + $this->cancelPayment(); + } + } else { + $this->transactionFailed(); + $this->newLog(4444, NextpayException::$errors[4444]); + throw new NextpayException(4444); + } + } + } + + protected function cancelPayment() + { + $params = array( + 'trans_id' => $this->refId, + 'api_key' => $this->config->get('gateway.nextpay.api-key'), + 'amount' => (int)$this->amount, + 'refund_request' => 'yes_money_back', + ); + $response = Http::post($this->verifyUrl, $params); + $response = $response->object(); + + $code = $response->code; + + $this->transactionFailed(); + $this->newLog($code, NextpayException::$errors[$code]); + throw new NextpayException($code); + return; + } + + /** + * Sets callback url + * + * @param $url + */ + function setCallback($url) + { + $this->callbackUrl = $url; + + return $this; + } + + /** + * Send settle request + * + * @return bool + * + * @throws MellatException + * @throws SoapFault + */ + protected function settleRequest() + { + + $params = array( + 'id' => $this->refId, + 'order_id' => $this->order_id, + ); + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $this->settelUrl); + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params)); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_HTTPHEADER, array( + 'Content-Type: application/json', + 'X-API-KEY: ' . $this->config->get('gateway.idpay.api-key') . '', + 'X-SANDBOX: 1', + )); + + $result = curl_exec($ch); + $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); + + var_dump($httpcode); + var_dump($result); + } +} diff --git a/src/Nextpay/NextpayException.php b/src/Nextpay/NextpayException.php new file mode 100644 index 00000000..e690bb2d --- /dev/null +++ b/src/Nextpay/NextpayException.php @@ -0,0 +1,69 @@ + "پرداخت تکمیل و با موفقیت انجام شده است", + -1 => "منتظر ارسال تراکنش و ادامه پرداخت", + -2 => "پرداخت رد شده توسط کاربر یا بانک", + -3 => "پرداخت در حال انتظار جواب بانک", + -4 => "پرداخت لغو شده است", + -20 => "کد api_key ارسال نشده است", + -21 => "empty trans_id param send", + -22 => "amount in not send", + -23 => "callback in not send", + -24 => "amount incorrect", + -25 => "trans_id resend and not allow to payment", + -26 => "Token not send", + -27 => "order_id incorrect", + -30 => "amount less of limite payment", + -31 => "fund not found", + -32 => "callback error", + -33 => "api_key incorrect", + -34 => "trans_id incorrect", + -35 => "type of api_key incorrect", + -36 => "order_id not send", + -37 => "transaction not found", + -38 => "token not found", + -39 => "کلید مجوز دهی موجود نیست", + -40 => "api_key is blocked", + -41 => "params from bank invalid", + -42 => "payment system problem", + -43 => "gateway not found", + -44 => "response bank invalid", + -45 => "payment system deactived", + -46 => "request incorrect", + -47 => "gateway is deleted or not found", + -48 => "commission rate not detect", + -49 => "trans repeated", + -50 => "account not found", + -51 => "user not found", + -60 => "email incorrect", + -61 => "national code incorrect", + -62 => "postal code incorrect", + -63 => "postal add incorrect", + -64 => "desc incorrect", + -65 => "name family incorrect", + -66 => "tel incorrect", + -67 => "account name incorrect", + -68 => "product name incorrect", + -69 => "callback success incorrect", + -70 => "callback failed incorrect", + -71 => "phone incorrect", + -72 => "bank not response", + -73 => "callback_uri incorrect", + -90 => "تراکنش لغو شد! مبلغ به کارت بانکی شما واریز خواهد شد", + 4444 => 'کارتی که شما با آن خرید انجام داده اید معتبر نیست', + ); + + public function __construct($errorId) + { + $this->errorId = intval($errorId); + + parent::__construct(@self::$errors[$this->errorId] . ' #' . $this->errorId, $this->errorId); + } +} diff --git a/src/Paystar/Paystar.php b/src/Paystar/Paystar.php new file mode 100644 index 00000000..f2b08dea --- /dev/null +++ b/src/Paystar/Paystar.php @@ -0,0 +1,256 @@ +link; + } + + public function setLink( $link ) { + $this->link = $link; + } + + /** + * {@inheritdoc} + */ + public function set( $amount ) { + $this->amount = $amount; + + return $this; + } + + /** + * {@inheritdoc} + */ + public function ready() { + $this->sendPayRequest(); + + return $this; + } + + /** + * Send pay request to server + * + * @return void + * + * @throws NextpayException + */ + protected function sendPayRequest() { + $this->newTransaction(); + $fields = array ( + 'pin' => $this->config->get( 'gateway.paystar.pin' ) , + 'amount' => $this->amount , + 'callback' => $this->getCallback() , + 'description' => ' ' , + 'ip' => Request::ip() + ); + + $ch = curl_init(); + + curl_setopt( $ch , CURLOPT_URL , $this->serverUrl ); + curl_setopt( $ch , CURLOPT_POSTFIELDS , json_encode( $fields ) ); + curl_setopt( $ch , CURLOPT_SSL_VERIFYPEER , false ); + curl_setopt( $ch , CURLOPT_RETURNTRANSFER , true ); + curl_setopt( $ch , CURLOPT_HTTPHEADER , array ( 'Content-Type: application/json' ) ); + + + $response = curl_exec( $ch ); + curl_close( $ch ); + if ( $response ) { + $this->refId = $response; + $this->setLink( "$this->paymentUrl/$this->refId" ); + $this->transactionSetRefId(); + + return true; + } + + $this->transactionFailed(); + $this->newLog( $response , PaystarException::$errors[ $response ] ); + throw new PaystarException( $response ); + } + + /** + * Gets callback url + * @return string + */ + function getCallback() { + if ( ! $this->callbackUrl ) { + $this->callbackUrl = $this->config->get( 'gateway.paystar.callback-url' ); + } + + return $this->makeCallback( $this->callbackUrl , [ 'transaction_id' => $this->transactionId() ] ); + } + + /** + * {@inheritdoc} + */ + public function redirect() { + $link = $this->link; + +// dd($this); + return redirect( $link ); + } + + /** + * {@inheritdoc} + */ + public function verify( $transaction ) { + parent::verify( $transaction ); + + $this->userPayment(); +// $this->validateCardNumber(); + $this->verifyPayment(); + + return $this; + } + + /** + * Check user payment + * + * @return bool + * + * @throws paystarException + */ + protected function userPayment() { +// $payRequestResCode = Request::get( 'result' ); +// $this->refId = Request::get( 'trackId' ); + $this->trackingCode = Request::get( 'transaction_id' ); +// $status = Request::get( 'status' ); +// $success = Request::get( 'success' ); + +// if ( $success == 1 ) { +// return true; +// } + +// $this->transactionFailed(); +// $this->newLog( $status , ZibalException::$errors[ $status ] ); +// throw new ZibalException( $status ); + } + + /** + * Verify user payment from bank server + * + * @return bool + * + * @throws NextpayException + * @throws SoapFault + */ + protected function verifyPayment() { + $fields = array ( + 'pin' => $this->config->get( 'gateway.paystar.pin' ) , + 'transid' => $this->refId , + 'amount' => $this->amount , + ); + + $ch = curl_init(); + + curl_setopt( $ch , CURLOPT_URL , $this->verifyUrl ); + curl_setopt( $ch , CURLOPT_POSTFIELDS , json_encode( $fields ) ); + curl_setopt( $ch , CURLOPT_SSL_VERIFYPEER , false ); + curl_setopt( $ch , CURLOPT_RETURNTRANSFER , true ); + curl_setopt( $ch , CURLOPT_HTTPHEADER , array ( 'Content-Type: application/json' ) ); + + + $response = curl_exec( $ch ); + + curl_close( $ch ); + + if ( $response < 0 ) { + $this->transactionFailed(); + $this->newLog( $response , PaystarException::$errors[ $response ] ); + throw new PaystarException( $response ); + } + $this->transactionSucceed(); + $this->newLog( 'SUCCESS' , Enum::TRANSACTION_SUCCEED_TEXT ); + + return true; + + } + + public function validateCardNumber() { + if ( auth()->check() ) { + $userCards = auth()->user()->cards->where( 'status' , 'active' )->pluck( 'last_number' ); + if ( $userCards ) { + if ( ! $userCards->contains( substr( $this->cardNumber , - 4 ) ) ) { + $this->transactionFailed(); + $this->newLog( 4444 , PaystarException::$errors[ 4444 ] ); + throw new PaystarException( 4444 ); + } + } else { + $this->transactionFailed(); + $this->newLog( 4444 , PaystarException::$errors[ 4444 ] ); + throw new PaystarException( 4444 ); + } + } + } + + /** + * Sets callback url + * + * @param $url + */ + function setCallback( $url ) { + $this->callbackUrl = $url; + + return $this; + } + + /** + * Send settle request + * + * @return bool + * + * @throws MellatException + * @throws SoapFault + */ + protected function settleRequest() { + + $params = array ( + 'id' => $this->refId , + 'order_id' => $this->order_id , + ); + + $ch = curl_init(); + curl_setopt( $ch , CURLOPT_URL , $this->settelUrl ); + curl_setopt( $ch , CURLOPT_POSTFIELDS , json_encode( $params ) ); + curl_setopt( $ch , CURLOPT_RETURNTRANSFER , true ); + curl_setopt( $ch , CURLOPT_HTTPHEADER , array ( + 'Content-Type: application/json' , + 'X-API-KEY: ' . $this->config->get( 'gateway.idpay.api-key' ) . '' , + 'X-SANDBOX: 1' , + ) ); + + $result = curl_exec( $ch ); + $httpcode = curl_getinfo( $ch , CURLINFO_HTTP_CODE ); + curl_close( $ch ); + + var_dump( $httpcode ); + var_dump( $result ); + } +} diff --git a/src/Paystar/PaystarException.php b/src/Paystar/PaystarException.php new file mode 100644 index 00000000..48178541 --- /dev/null +++ b/src/Paystar/PaystarException.php @@ -0,0 +1,30 @@ + "Amount نمیتواند خالی باشد" , + - 2 => "کد پین درگاه نمیتواند خالی باشد" , + - 3 => "Callback نمیتواند خالی باشد" , + - 4 => "Amount باید عددی باشد" , + - 5 => "Amount باید بزرگتر از ۱۰۰ باشد" , + - 6 => "کد پین درگاه اشتباه هست" , + - 7 => "ایپی سرور با ایپی درگاه مطابقت ندارد" , + - 8 => " transid نمیتواند خالی باشد" , + - 9 => "تراکنش مورد نظر وجود ندارد" , + - 10 => "کد پین درگاه با درگاه تراکنش مطابقت ندارد" , + - 11 => "مبلغ با مبلغ تراکنش مطابقت ندارد" , + - 12 => "بانک انتخابی اشتباه است" , + - 13 => "درگاه غیر فعال است" , + - 14 => "IP مشتری ارسال نشده است" , + ); + + public function __construct( $errorId ) { + $this->errorId = intval( $errorId ); + + parent::__construct( @self::$errors[ $this->errorId ] . ' #' . $this->errorId , $this->errorId ); + } +} diff --git a/src/Poolam/Poolam.php b/src/Poolam/Poolam.php new file mode 100644 index 00000000..57a325f2 --- /dev/null +++ b/src/Poolam/Poolam.php @@ -0,0 +1,277 @@ +order_id = $order_id; +// } + + public function getOrderId() + { + return $this->order_id; + } + + public function setOrderId($order_id) + { + $this->order_id = $order_id; + } + + public function getLink() + { + return $this->link; + } + + public function setLink($link) + { + $this->link = $link; + } + + /** + * {@inheritdoc} + */ + public function set($amount) + { + $this->amount = $amount *10; + + return $this; + } + + /** + * {@inheritdoc} + */ + public function ready() + { + $this->sendPayRequest(); + return $this; + } + + /** + * Send pay request to server + * + * @return void + * + * @throws NextpayException + */ + protected function sendPayRequest() + { + $this->newTransaction(); + + $fields = array( + 'api_key' => $this->config->get('gateway.poolam.api_key'), + 'amount' => $this->amount, + 'return_url' => $this->getCallback(), + ); + + $ch = curl_init(); + + curl_setopt($ch, CURLOPT_URL, $this->serverUrl); + curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields)); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + + $response = curl_exec($ch); + $response = json_decode($response); + curl_close($ch); + + if (isset($response) && $response->status == 1) { + $this->refId = $response->invoice_key; + $this->link = "$this->paymentUrl$this->refId"; + $this->transactionSetRefId(); + return true; + } + + $this->transactionFailed(); + $this->newLog($response->Code, PoolamException::$errors[$response->Code]); + throw new PoolamException($response->Code); + } + + /** + * Gets callback url + * @return string + */ + function getCallback() + { + if (!$this->callbackUrl) { + $this->callbackUrl = $this->config->get('gateway.nextpay.callback-url'); + } + + return $this->makeCallback($this->callbackUrl, ['transaction_id' => $this->transactionId()]); + } + + /** + * {@inheritdoc} + */ + public function redirect() + { + $link = $this->link; +// dd($this); + return redirect($link); + } + + /** + * {@inheritdoc} + */ + public function verify($transaction) + { + parent::verify($transaction); + + $this->userPayment(); +// $this->validateCardNumber(); + $this->verifyPayment(); + + return $this; + } + + /** + * Check user payment + * + * @return bool + * + * @throws MellatException + */ + protected function userPayment() + { + $this->trackingCode = Request::get('transaction_id'); + $this->refId = Request::get('invoice_key'); +// $this->order_id = Input::get('order_id'); +// $this->amount = Input::get( 'amount' ); +// $this->cardNumber = Input::get('card_holder'); +// + if (isset($this->refId)) { + return true; + } + +// $this->transactionFailed(); +// $this->newLog( $payRequestResCode , NextpayException::$errors[ $payRequestResCode ] ); +// throw new NextpayException( $payRequestResCode ); + } + + /** + * Verify user payment from bank server + * + * @return bool + * + * @throws NextpayException + * @throws SoapFault + */ + protected function verifyPayment() + { + $fields = array( + 'api_key' => $this->config->get('gateway.poolam.api_key'), + ); + + $ch = curl_init(); + + curl_setopt($ch, CURLOPT_URL, $this->verifyUrl . $this->refId); + curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields)); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + + $response = curl_exec($ch); + $response = json_decode($response); +// dd($response); + curl_close($ch); + + if ($response->status == 1) { + $this->transactionSucceed(); + $this->newLog('SUCCESS', Enum::TRANSACTION_SUCCEED_TEXT); + return true; + } + + $this->transactionFailed(); + $this->newLog($response->errorCode, PoolamException::$errors[$response->errorCode]); + throw new PoolamException($response->errorCode); + + } + + public function validateCardNumber() + { + if (auth()->check()) { + $userCards = auth()->user()->cards->where('status', 'active')->pluck('last_number'); + if ($userCards) { + if (!$userCards->contains(substr($this->cardNumber, -4))) { + $this->transactionFailed(); + $this->newLog(4444, NextpayException::$errors[4444]); + throw new NextpayException(4444); + } + } else { + $this->transactionFailed(); + $this->newLog(4444, NextpayException::$errors[4444]); + throw new NextpayException(4444); + } + } + } + + /** + * Sets callback url + * + * @param $url + */ + function setCallback($url) + { + $this->callbackUrl = $url; + + return $this; + } + + /** + * Send settle request + * + * @return bool + * + * @throws MellatException + * @throws SoapFault + */ + protected function settleRequest() + { + + $params = array( + 'id' => $this->refId, + 'order_id' => $this->order_id, + ); + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $this->settelUrl); + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params)); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_HTTPHEADER, array( + 'Content-Type: application/json', + 'X-API-KEY: ' . $this->config->get('gateway.idpay.api-key') . '', + 'X-SANDBOX: 1', + )); + + $result = curl_exec($ch); + $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); + + var_dump($httpcode); + var_dump($result); + } +} diff --git a/src/Poolam/PoolamException.php b/src/Poolam/PoolamException.php new file mode 100644 index 00000000..ab06c217 --- /dev/null +++ b/src/Poolam/PoolamException.php @@ -0,0 +1,28 @@ + "نوع درخواست باید POST باشد", + 101 => "api_key ارسال نشده است یا صحیح نیست", + 102 => "مبلغ ارسال نشده است یا کمتر از 1000 ریال است", + 103 => "آدرس بازگشت ارسال نشده است", + 301 => "خطایی در برقراری با سرور بانک رخ داده است", + 200 => "شناسه پرداخت صحیح نیست", + 201 => "پرداخت انجام نشده است", + 202 => "پرداخت کنسل شده است یا خطایی در مراحل پرداخت رخ داده است", + 1 => "Success", + 4444 => 'کارتی که شما با آن خرید انجام داده اید معتبر نیست', + ); + + public function __construct($errorId) + { + $this->errorId = intval($errorId); + + parent::__construct(@self::$errors[$this->errorId] . ' #' . $this->errorId, $this->errorId); + } +} diff --git a/src/RayanPay/RayanPay.php b/src/RayanPay/RayanPay.php new file mode 100644 index 00000000..2cf36bb3 --- /dev/null +++ b/src/RayanPay/RayanPay.php @@ -0,0 +1,307 @@ +order_id; + } + + public function setOrderId($order_id) + { + $this->order_id = $order_id; + } + + public function getLink() + { + return $this->link; + } + + public function setLink($link) + { + $this->link = $link; + } + + /** + * {@inheritdoc} + */ + public function set($amount) + { + $this->amount = $amount; + + return $this; + } + + /** + * {@inheritdoc} + */ + public function ready() + { + $this->request(); + + return $this; + } + + /** + * Gets callback url + * @return string + */ + function getCallback() + { + if (!$this->callbackUrl) { + $this->callbackUrl = 'callback/rayanpay/{transaction_id}'; + } + + return $this->makeCallback($this->callbackUrl, ['transaction_id' => $this->transactionId()]); + } + + /** + * {@inheritdoc} + */ + public function redirect() + { + $link = $this->link; + + return redirect($link); + } + + public function request() + { + + $username = $this->username; + $password = $this->password; + $clientId = $this->clientId; + $referenceId = hexdec(uniqid()); + + //exit(); + //$mobile = 98 . substr($this->get_order_mobile(), 1); + + + $ch = curl_init('https://pms.rayanpay.com/api/v1/auth/token/generate'); + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array( + 'clientId' => $clientId, + 'userName' => $username, + 'password' => $password, + ))); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + $result = curl_exec($ch); + $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); + $json_decoded = json_decode($result, true); + if ($http_code != 200 || is_array($json_decoded)) { + return $this->errors($http_code, 'token', ($json_decoded['ErrDesc'] ? 'خطایی در هنگام اتصال به درگاه رخ داده است.' : '')); + } + + $token = $result; + $this->set_stored("$referenceId", $token); + /* + * کد انحصاری که برای تمام طول پرداخت لازم هست + */ + $_SESSION["SaleReferenceId"] = $referenceId; + $ch = curl_init('https://pms.rayanpay.com/api/v1/ipg/payment/start'); + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array( + 'amount' => (int)$this->amount, + 'callbackUrl' => $this->getCallback(), + 'referenceId' => $referenceId, + 'msisdn' => '', + 'gatewayId' => 100, + 'gateSwitchingAllowed' => true, + ))); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_HTTPHEADER, array( + 'Authorization: Bearer ' . $token, + 'Content-Type: application/json', + )); + $result = curl_exec($ch); + $result = json_decode($result, true); + $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); + /* + * کد پیگیری بانک + */ + $paymentId = $result["paymentId"]; + + if (!empty($result['bankRedirectHtml'])) { + //file_put_contents(getcwd() . "/bankRedirectHtml.txt", $result['bankRedirectHtml']); + /* + * انتقال به درگاه پرداخت + */ + print_r($result['bankRedirectHtml']); + return; + } + /* + * بروز خطا عدم انتقال + */ + if (!empty($result['errors'])) { + $error = json_encode($result['errors']); + } else { + $error = $result['error'] ? 'خطای ناشناخته رخ داده است.' : ''; + } + + return $this->errors($http_code, 'payment_start', $error); + } + + protected function get_stored($key) + { + //این را در بانک داده نگه داری کنید + /* + * مثلا MYSql + */ + return file_get_contents(getcwd() . "/token.txt"); + } + + protected function set_stored($key, $val) + { + //این را در بانک داده نگه داری کنید + /* + * مثلا MYSql + */ + return file_put_contents(getcwd() . "/token.txt", $val); + } + /* + * تایید یا عدم تایید به بانک + */ + public function verify($transaction) + { + + parent::verify($transaction); + + $referenceId = $_SESSION["SaleReferenceId"]; + $token = $this->get_stored("$referenceId"); + + $ch = curl_init('https://pms.rayanpay.com/api/v1/ipg/payment/response/parse'); + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array( + 'referenceId' => $referenceId, + 'header' => '', + 'content' => http_build_query($_POST) + ))); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_HTTPHEADER, array( + 'Authorization: Bearer ' . $token, + 'Content-Type: application/json', + )); + $result = curl_exec($ch); + $result = json_decode($result, true); + $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); + + if (!empty($result['paymentId']) && !empty($result['hashedBankCardNumber'])) { + $status = 'completed'; + $error = ''; + /* + * کدهایی که مربوط به موفقیت در انجام کار بوده + */ + } else { + $status = 'failed'; + if (!empty($result['ErrorDesc'])) { + $error = $result['ErrorDesc']; + } else if (!empty($result['errors'])) { + $error = json_encode($result['errors']); + } else { + $error = $result['error'] ? 'تراکنش ناموفق بوده است.' : ''; + } + $error = $this->errors($http_code, 'payment_parse', $error); + /* + * کدهای عدم موفقیت پرداخت + */ + } + + $transaction_id = $referenceId; + + + return compact('status', 'transaction_id', 'error'); + } + + public function errors($error, $method, $prepend = '') + { + if ($method == 'token') { + switch ($error) { + + case '400' : + $message = 'نقص در پارامترهای ارسالی'; + break; + + case '401' : + $message = 'کد کاربری/رمز عبور /کلاینت/آی پی نامعتبر است'; + break; + } + } elseif ($method == 'payment_start') { + switch ($error) { + + case '401' : + $message = 'توکن نامعتبر'; + break; + + case '601' : + $message = 'اتصال به درگاه خطا دارد (پرداخت ناموفق)'; + break; + } + + } elseif ($method == 'payment_parse') { + switch ($error) { + + case '401' : + $message = 'توکن نامعتبر است'; + break; + + case '601' : + $message = 'پرداخت ناموفق'; + break; + + case '600' : + $message = 'پرداخت در حالت Pending می باشد و باید متد fullfill برای تعیین وضعیت صدا زده شود'; + break; + + case '602' : + $message = 'پرداخت یافت نشد'; + break; + + case '608' : + $message = 'قوانین پرداخت یافت نشد (برای پرداخت هایی که قوانین دارند)'; + break; + + case '609' : + $message = 'وضعیت پرداخت نامعتبر میباشد'; + break; + } + } + + return $message; + // return implode(' :: ', array_filter(array($prepend, $message ? '' : ''))); + } +} diff --git a/src/Yekpay/Yekpay.php b/src/Yekpay/Yekpay.php new file mode 100644 index 00000000..81c189f0 --- /dev/null +++ b/src/Yekpay/Yekpay.php @@ -0,0 +1,290 @@ +order_id = $order_id; +// } + + public function getOrderId() + { + return $this->order_id; + } + + public function setOrderId($order_id) + { + $this->order_id = $order_id; + } + + public function getLink() + { + return $this->link; + } + + public function setLink($link) + { + $this->link = $link; + } + + /** + * {@inheritdoc} + */ + public function set($amount) + { + $this->amount = $amount *10; + + return $this; + } + + /** + * {@inheritdoc} + */ + public function ready() + { + $this->sendPayRequest(); + + return $this; + } + + /** + * Send pay request to server + * + * @return void + * + * @throws NextpayException + */ + protected function sendPayRequest() + { + $this->newTransaction(); + + $fields = array( + 'merchantId' => $this->config->get('gateway.yekpay.merchantId'), + 'amount' => $this->amount, + 'fromCurrencyCode' => '364', + 'toCurrencyCode' => '364', + 'orderNumber' => rand(00001, 9999), + 'firstName' => 'iran', + 'lastName' => 'pay', + 'email' => 'info@iranpay.me', + 'mobile' => '09109909006', + 'address' => 'iran pay', + 'postalCode' => '8176855994', + 'country' => 'iran', + 'city' => 'tehran', + 'callback' => $this->getCallback(), + ); + + $ch = curl_init(); + + curl_setopt($ch, CURLOPT_URL, $this->serverUrl); + curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields)); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + + $response = curl_exec($ch); + $response = json_decode($response); + curl_close($ch); + + if ($response->Code == 100) { + $this->refId = $response->Authority; + $this->link = "$this->paymentUrl/$this->refId"; + $this->transactionSetRefId(); + return true; + } + + $this->transactionFailed(); + $this->newLog($response->Code, YekpayException::$errors[$response->Code]); + throw new YekpayException($response->Code); + } + + /** + * Gets callback url + * @return string + */ + function getCallback() + { + if (!$this->callbackUrl) { + $this->callbackUrl = $this->config->get('gateway.nextpay.callback-url'); + } + + return $this->makeCallback($this->callbackUrl, ['transaction_id' => $this->transactionId()]); + } + + /** + * {@inheritdoc} + */ + public function redirect() + { + $link = $this->link; +// dd($this); + return redirect($link); + } + + /** + * {@inheritdoc} + */ + public function verify($transaction) + { + parent::verify($transaction); + + $this->userPayment(); +// $this->validateCardNumber(); + $this->verifyPayment(); + + return $this; + } + + /** + * Check user payment + * + * @return bool + * + * @throws MellatException + */ + protected function userPayment() + { + $payRequestResCode = Input::get('success'); + $this->trackingCode = Input::get('transaction_id'); + $this->refId = Input::get('authority'); +// $this->order_id = Input::get('order_id'); +// $this->amount = Input::get( 'amount' ); +// $this->cardNumber = Input::get('card_holder'); +// + if ($payRequestResCode == 1) { + return true; + } + +// $this->transactionFailed(); +// $this->newLog( $payRequestResCode , NextpayException::$errors[ $payRequestResCode ] ); +// throw new NextpayException( $payRequestResCode ); + } + + /** + * Verify user payment from bank server + * + * @return bool + * + * @throws NextpayException + * @throws SoapFault + */ + protected function verifyPayment() + { + $fields = array( + 'merchantId' => $this->config->get('gateway.yekpay.merchantId'), + 'authority' => $this->refId, + ); + + $ch = curl_init(); + + curl_setopt($ch, CURLOPT_URL, $this->verifyUrl); + curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields)); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + + $response = curl_exec($ch); + $response = json_decode($response); +// dd($response->Code); + curl_close($ch); + + if ($response->Code == 100) { + $this->transactionSucceed(); + $this->newLog('SUCCESS', Enum::TRANSACTION_SUCCEED_TEXT); + return true; + } + + $this->transactionFailed(); + $this->newLog($response->Code, YekpayException::$errors[$response->Code]); + throw new YekpayException($response->Code); + + } + + public function validateCardNumber() + { + if (auth()->check()) { + $userCards = auth()->user()->cards->where('status', 'active')->pluck('last_number'); + if ($userCards) { + if (!$userCards->contains(substr($this->cardNumber, -4))) { + $this->transactionFailed(); + $this->newLog(4444, NextpayException::$errors[4444]); + throw new NextpayException(4444); + } + } else { + $this->transactionFailed(); + $this->newLog(4444, NextpayException::$errors[4444]); + throw new NextpayException(4444); + } + } + } + + /** + * Sets callback url + * + * @param $url + */ + function setCallback($url) + { + $this->callbackUrl = $url; + + return $this; + } + + /** + * Send settle request + * + * @return bool + * + * @throws MellatException + * @throws SoapFault + */ + protected function settleRequest() + { + + $params = array( + 'id' => $this->refId, + 'order_id' => $this->order_id, + ); + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $this->settelUrl); + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params)); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_HTTPHEADER, array( + 'Content-Type: application/json', + 'X-API-KEY: ' . $this->config->get('gateway.idpay.api-key') . '', + 'X-SANDBOX: 1', + )); + + $result = curl_exec($ch); + $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); + + var_dump($httpcode); + var_dump($result); + } +} diff --git a/src/Yekpay/YekpayException.php b/src/Yekpay/YekpayException.php new file mode 100644 index 00000000..9bdeaf19 --- /dev/null +++ b/src/Yekpay/YekpayException.php @@ -0,0 +1,28 @@ + "The parameters are incomplete", + -2 => "Merchant code is incorrect", + -3 => "Merchant code is not active", + -4 => "Currencies is not valid", + -5 => "Maximum/Minimum amount is not valid", + -6 => "Your IP is restricted", + -7 => "Order id must be unique", + -100 => "Unknown error", + 100 => "Success", + 4444 => 'کارتی که شما با آن خرید انجام داده اید معتبر نیست', + ); + + public function __construct($errorId) + { + $this->errorId = intval($errorId); + + parent::__construct(@self::$errors[$this->errorId] . ' #' . $this->errorId, $this->errorId); + } +} diff --git a/src/Zibal/Zibal.php b/src/Zibal/Zibal.php new file mode 100644 index 00000000..1394b432 --- /dev/null +++ b/src/Zibal/Zibal.php @@ -0,0 +1,273 @@ +order_id; + } + + public function setOrderId( $order_id ) { + $this->order_id = $order_id; + } + + public function getLink() { + return $this->link; + } + + public function setLink( $link ) { + $this->link = $link; + } + + /** + * {@inheritdoc} + */ + public function set( $amount ) { + $this->amount = $amount * 10; + + return $this; + } + + /** + * {@inheritdoc} + */ + public function ready() { + $this->sendPayRequest(); + + return $this; + } + + /** + * Send pay request to server + * + * @return void + * + * @throws NextpayException + */ + protected function sendPayRequest() { + $this->newTransaction(); +// if (auth() - check()) { +// $userCards = auth()->user()->cards->where('status', 'active')->pluck('last_number'); +// } + $fields = array ( + 'merchant' => $this->config->get( 'gateway.zibal.merchant' ) , + 'amount' => $this->amount , + 'callbackUrl' => $this->getCallback() , + ); +// if ($userCards) { +// array_push($fields, [ +// 'allowedCards' => $userCards, +// ]); +// } + + $ch = curl_init(); + + curl_setopt( $ch , CURLOPT_URL , $this->serverUrl ); + curl_setopt( $ch , CURLOPT_POSTFIELDS , json_encode( $fields ) ); + curl_setopt( $ch , CURLOPT_SSL_VERIFYPEER , false ); + curl_setopt( $ch , CURLOPT_RETURNTRANSFER , true ); + curl_setopt( $ch , CURLOPT_HTTPHEADER , array ( 'Content-Type: application/json' ) ); + + + $response = curl_exec( $ch ); + $response = json_decode( $response ); + curl_close( $ch ); + + if ( $response->result == 100 ) { + $this->refId = $response->trackId; + $this->link = "$this->paymentUrl/$this->refId"; + $this->transactionSetRefId(); + + return true; + } + + $this->transactionFailed(); + $this->newLog( $response->result , ZibalException::$errors[ $response->result ] ); + throw new ZibalException( $response->result ); + } + + /** + * Gets callback url + * @return string + */ + function getCallback() { + if ( ! $this->callbackUrl ) { + $this->callbackUrl = $this->config->get( 'gateway.nextpay.callback-url' ); + } + + return $this->makeCallback( $this->callbackUrl , [ 'transaction_id' => $this->transactionId() ] ); + } + + /** + * {@inheritdoc} + */ + public function redirect() { + $link = $this->link; + +// dd($this); + return redirect( $link ); + } + + /** + * {@inheritdoc} + */ + public function verify( $transaction ) { + parent::verify( $transaction ); + + $this->userPayment(); +// $this->validateCardNumber(); + $this->verifyPayment(); + + return $this; + } + + /** + * Check user payment + * + * @return bool + * + * @throws MellatException + */ + protected function userPayment() { + $payRequestResCode = Request::get( 'result' ); + $this->refId = Request::get( 'trackId' ); + $this->trackingCode = Request::get( 'transaction_id' ); + $status = Request::get( 'status' ); + $success = Request::get( 'success' ); + + if ( $success == 1 ) { + return true; + } + + $this->transactionFailed(); + $this->newLog( $status , ZibalException::$errors[ $status ] ); + throw new ZibalException( $status ); + } + + /** + * Verify user payment from bank server + * + * @return bool + * + * @throws NextpayException + * @throws SoapFault + */ + protected function verifyPayment() { + $fields = array ( + 'merchant' => $this->config->get( 'gateway.zibal.merchant' ) , + 'trackId' => $this->refId , + ); + + $ch = curl_init(); + + curl_setopt( $ch , CURLOPT_URL , $this->verifyUrl ); + curl_setopt( $ch , CURLOPT_POSTFIELDS , json_encode( $fields ) ); + curl_setopt( $ch , CURLOPT_SSL_VERIFYPEER , false ); + curl_setopt( $ch , CURLOPT_RETURNTRANSFER , true ); + curl_setopt( $ch , CURLOPT_HTTPHEADER , array ( 'Content-Type: application/json' ) ); + + + $response = curl_exec( $ch ); + $response = json_decode( $response ); +// dd($response->Code); + curl_close( $ch ); + + if ( $response->result == 100 ) { + $this->transactionSucceed(); + $this->newLog( 'SUCCESS' , Enum::TRANSACTION_SUCCEED_TEXT ); + + return true; + } + + $this->transactionFailed(); + $this->newLog( $response->result , ZibalException::$errors[ $response->result ] ); + throw new ZibalException( $response->result ); + + } + + public function validateCardNumber() { + if ( auth()->check() ) { + $userCards = auth()->user()->cards->where( 'status' , 'active' )->pluck( 'last_number' ); + if ( $userCards ) { + if ( ! $userCards->contains( substr( $this->cardNumber , - 4 ) ) ) { + $this->transactionFailed(); + $this->newLog( 4444 , NextpayException::$errors[ 4444 ] ); + throw new NextpayException( 4444 ); + } + } else { + $this->transactionFailed(); + $this->newLog( 4444 , NextpayException::$errors[ 4444 ] ); + throw new NextpayException( 4444 ); + } + } + } + + /** + * Sets callback url + * + * @param $url + */ + function setCallback( $url ) { + $this->callbackUrl = $url; + + return $this; + } + + /** + * Send settle request + * + * @return bool + * + * @throws MellatException + * @throws SoapFault + */ + protected function settleRequest() { + + $params = array ( + 'id' => $this->refId , + 'order_id' => $this->order_id , + ); + + $ch = curl_init(); + curl_setopt( $ch , CURLOPT_URL , $this->settelUrl ); + curl_setopt( $ch , CURLOPT_POSTFIELDS , json_encode( $params ) ); + curl_setopt( $ch , CURLOPT_RETURNTRANSFER , true ); + curl_setopt( $ch , CURLOPT_HTTPHEADER , array ( + 'Content-Type: application/json' , + 'X-API-KEY: ' . $this->config->get( 'gateway.idpay.api-key' ) . '' , + 'X-SANDBOX: 1' , + ) ); + + $result = curl_exec( $ch ); + $httpcode = curl_getinfo( $ch , CURLINFO_HTTP_CODE ); + curl_close( $ch ); + + var_dump( $httpcode ); + var_dump( $result ); + } +} diff --git a/src/Zibal/ZibalException.php b/src/Zibal/ZibalException.php new file mode 100644 index 00000000..0a010d8e --- /dev/null +++ b/src/Zibal/ZibalException.php @@ -0,0 +1,41 @@ + "خطای داخلی سرور، زیبال این خطا را بررسی و برطرف خواهد کرد" , + - 2 => "خطای داخلی" , + 1 => "پرداخت شده - تاییدشده" , + 2 => "پرداخت شده - تاییدنشده" , + 3 => "لغوشده توسط کاربر" , + 4 => "‌شماره کارت نامعتبر می‌باشد." , + 5 => "‌موجودی حساب کافی نمی‌باشد." , + 6 => "رمز واردشده اشتباه می‌باشد." , + 7 => "‌تعداد درخواست‌ها بیش از حد مجاز می‌باشد." , + 8 => "‌تعداد پرداخت اینترنتی روزانه بیش از حد مجاز می‌باشد." , + 9 => "مبلغ پرداخت اینترنتی روزانه بیش از حد مجاز می‌باشد." , + 10 => "‌صادرکننده‌ی کارت نامعتبر می‌باشد." , + 11 => "‌خطای سوییچ" , + 12 => "کارت قابل دسترسی نمی‌باشد." , + 102 => "merchant یافت نشد." , + 103 => "merchant غیرفعال" , + 104 => "merchant نامعتبر" , + 201 => "قبلا تایید شده." , + 105 => "amount بایستی بزرگتر از 1,000 ریال باشد." , + 106 => "callbackUrl نامعتبر می‌باشد. (شروع با http و یا https)" , + 113 => "amount مبلغ تراکنش از سقف میزان تراکنش بیشتر است" , + 100 => "با موفقیت تایید شد." , + 202 => "سفارش پرداخت نشده یا ناموفق بوده است." , + 203 => "trackId نامعتبر می‌باشد" , + 4444 => 'کارتی که شما با آن خرید انجام داده اید معتبر نیست' , + ); + + public function __construct( $errorId ) { + $this->errorId = intval( $errorId ); + + parent::__construct( @self::$errors[ $this->errorId ] . ' #' . $this->errorId , $this->errorId ); + } +}