نوشته محمدرضا صالحی
تاریخ درج: 08 اسفند 1399
برای شروع نیاز به دیتابیس و ساختن migration و seeder داریم :
Schema::create('orders', function (Blueprint $table) { $table->bigIncrements('order_id'); $table->integer('user_id'); $table->integer('price'); $table->integer('status')->nullable(); $table->timestamps(); });
Schema::create('transactions', function (Blueprint $table) { $table->bigIncrements('transaction_id'); $table->integer('order_id'); $table->integer('res_code')->nullable(); $table->string('ref_id')->nullable(); $table->string('sale_ref_id')->nullable(); $table->integer('status')->nullable(); $table->string('msg')->nullable(); $table->timestamps(); });
DB::table('orders')->insert([ 'user_id' => 1, 'price' => 1000 ]);
بعد باید تنظیمات .env رو انجام بدید با توجه به اسمی که دیتابیستون رو ساختید
تنظیمات روت به شکل زیر
Route::get('pay/{order_id}', 'HomeController@pay'); Route::post('verify', 'HomeController@verify');
و تنظیمات کدهای کنترلر به شکل زیر است
get()->first(); $transaction = Transaction::create([ 'order_id' => $order_id, 'status' => 2, 'msg' => 'سعی در پرداخت کردن' ]); $carbon = new Carbon(); // Now() $localDate = $carbon->format('Ymd'); $localTime = $carbon->format('His'); $data = [ 'terminalId' => config('app.mellat_terminal'), 'userName' => config('app.mellat_username'), 'userPassword' => config('app.mellat_password'), 'orderId' => $transaction->transaction_id, 'amount' => $order->price, 'localDate' => $localDate, 'localTime' => $localTime, 'additionalData' => ' ', 'callBackUrl' => 'https://barnamenevisi.com/verify', 'payerID' => 0, ]; try { $soapClient = new SoapClient($this->bankUri); $res = $soapClient->bpPayrequest($data); // return object: return :"0,9237456928347236" $res = explode(',', $res->return); if ($res[0] == "0") { return view('vendor/mellat')->with(['tokenId' => $res[1]]); } } catch (\Throwable $e) { dd($e); return false; } } public function verify(Request $request) { $transaction = Transaction::where('transaction_id', $request->SaleOrderId)->get()->first(); $order = Order::where('order_id', $transaction->order_id)->get()->first(); $data = [ 'terminalId' => config('app.mellat_terminal'), 'userName' => config('app.mellat_username'), 'userPassword' => config('app.mellat_password'), 'orderId' => $request->SaleOrderId, 'saleOrderId' => $request->SaleOrderId, 'SaleReferenceId' => $request->SaleReferenceId, ]; if ($this->_getVerify($data)) { //Success if ($this->_getSettle($data)) { //success //Failed verify $transaction->update([ 'status'=> 1, 'ref_id' => $request->RefId, 'sale_ref_id' =>$request->SaleReferenceId, 'res_code' =>$request->ResCode, 'msg' => 'تراکنش موفقیت آمیز بود' ]); $order->update(['status' => 1]); return view('verify')->with([ 'msg' => 'با تشکر تراکنش موفقیت آمیز بوده است', 'downloadLink' => 'https://barnamenevisi.com/dl/' . $order->downloadLink ]); } else { //Failed verify $transaction->update([ 'status'=> 0, 'msg' => 'مشکل در تایید از سمت بانک' ]); $order->update(['status' => 0]); return view('vendor.error')->with(['msg' => 'تراکنش ناموفق بوده پول بعد از ۷۲ ساعت بازگشت داده میشود']); } } else { //Failed verify $transaction->update([ 'status'=> 0, 'msg' => 'مشکل در تایید از سمت بانک' ]); $order->update(['status' => 0]); return view('vendor.error')->with(['msg' => 'تراکنش ناموفق بوده پول بعد از ۷۲ ساعت بازگشت داده میشود']); } } protected function _getVerify($data) { try{ $soapClient = new SoapClient($this->bankUri); $res = $soapClient->bpVerifyRequest($data); //return object: return:0|false if($res->return == "0") return true; else return false; }catch(\Throwable $e){ dd($e); return false; } } protected function _getSettle($data) { try{ $soapClient = new SoapClient($this->bankUri); $res = $soapClient->bpSettleRequest($data); //return object: return:0|false if($res->return == "0") return true; else return false; }catch(\Throwable $e){ dd($e); return false; } } }
در نهایت فایل انتقال به بانک در قسمت view
Document
مرورگر شما این ویدئو را پشتیبانی نمی کند به تیم برنامه نویسی اطلاع دهید پشتیبانی از html5