Edit File: PaymentController.php
<?php namespace App\Http\Controllers\StoresDashboard; use App\Http\Controllers\Controller; use App\Models\User; use Illuminate\Http\Request; use App\Models\Setting; use App\Models\HyperpayBrand; use App\Models\Transaction; use App\Http\Resources\HyperpayBrandsResource; use Auth; use App\Traits\ApiTrait; use App\Traits\GeneralTrait; use Illuminate\Support\Facades\Validator; class PaymentController extends Controller { use ApiTrait, GeneralTrait; //enter amount to charge public function charge_wallet() { $user = Auth::user(); ## return view('stores_dashboard.commissions.charge_wallet', compact('user')); } //choose payment method to transfer amount public function payment_methods(Request $request) { $this->validate(request(), [ 'amount' => 'required', ]); $amount = number_format((float) $request->amount, 2, '.', ''); $hyperpay_brands = HyperpayBrand::where('is_active', 'true')->get(); $data = []; $data['hyperpay_brands'] = HyperpayBrandsResource::collection($hyperpay_brands); return view('stores_dashboard.commissions.payment_methods', compact('amount', 'data')); } public function chargeWalletIndex($brand,$amount){ // user $user = Auth::user(); // get settings $settings = Setting::all()->pluck('value', 'key'); $hyperpay_status = $settings['hyperpay_status']; $hyperpay_mode = $settings['hyperpay_mode']; $hyperpay_Authorization = $settings['hyperpay_Authorization']; $hyperpay_site_title = $settings['hyperpay_site_title']; // redirect if hyperpay is disabled || Authorization not provided if($hyperpay_status == 'disabled' || $hyperpay_Authorization == NULL){ $msg = trans('payment.method_disabled'); return redirect()->back()->with('fail',$msg); } // find the brand $hyperpay_brand = HyperpayBrand::where('brand',$brand)->first(); if(!$hyperpay_brand || $hyperpay_brand->is_active=='false' || !$hyperpay_brand->entity_id){ $msg = trans('payment.brand_disabled'); return redirect()->back()->with('fail',$msg); } // amount $amount = $this->convert2english($amount); $amount = number_format((float)$amount, 2, '.', ''); // hyperpay if($hyperpay_mode && $hyperpay_mode == 'live'){ $url = "https://oppwa.com/v1/checkouts"; $curlopt = true; }else{ $url = "https://test.oppwa.com/v1/checkouts"; $curlopt = false; } $user_email = $user->email?$user->email:$user->phone.'@'.$hyperpay_site_title.'.com'; if($hyperpay_mode && $hyperpay_mode == 'live'){ $data = "entityId=".$hyperpay_brand->entity_id. "&amount=".$amount. "¤cy=SAR" . "&merchantTransactionId=".rand(1111,9999).$user->id. "&customer.email=".$user_email. "&paymentType=DB"; }else{ $data = "entityId=".$hyperpay_brand->entity_id. "&amount=".$amount. "¤cy=SAR" . "&merchantTransactionId=".rand(1111,9999).$user->id. "&customer.email=".$user_email. "&billing.street1=Prince Badr bin Abdulaziz Street" . "&billing.city=Riyadh" . "&billing.state=Riyadh" . "&billing.country=SA" . "&billing.postcode=21955" . "&customer.givenName=wahba". "&testMode=EXTERNAL". "&paymentType=DB"; } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HTTPHEADER, array( "Authorization:Bearer ".$hyperpay_Authorization)); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $curlopt); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $responseData = curl_exec($ch); if(curl_errno($ch)) { return curl_error($ch); } curl_close($ch); $responseDat = json_decode($responseData); $checkoutId = $responseDat->id; // save the transaction $transaction = new Transaction(); $transaction->user_id = $user->id; $transaction->checkout_id = $checkoutId; $transaction->amount = $amount; $transaction->type = 'wallet'; $transaction->status = 'pending'; $transaction->save(); // return success // return $this->dataReturn($responseDat); return view('stores_dashboard.commissions.charge_wallet_index', compact('checkoutId','hyperpay_mode','hyperpay_brand','user','brand')); } public function chargeWalletResult(Request $request,$brand){ // user $user = Auth::user(); // get settings $settings = Setting::all()->pluck('value', 'key'); $hyperpay_status = $settings['hyperpay_status']; $hyperpay_mode = $settings['hyperpay_mode']; $hyperpay_Authorization = $settings['hyperpay_Authorization']; $online_payment_commission = $settings['online_payment_commission']; $hyperpay_site_title = $settings['hyperpay_site_title']; // redirect if hyperpay is disabled || Authorization not provided if($hyperpay_status == 'disabled' || $hyperpay_Authorization == NULL){ $msg = trans('payment.method_disabled'); return redirect()->back()->with('fail',$msg); } // find the brand $hyperpay_brand = HyperpayBrand::where('brand',$brand)->first(); if(!$hyperpay_brand || $hyperpay_brand->is_active=='false' || !$hyperpay_brand->entity_id){ $msg = trans('payment.brand_disabled'); return redirect()->back()->with('fail',$msg); } // checkoutId $id = $request->resourcePath; $checkoutId = $this->get_string_between($id,'/v1/checkouts/','/payment'); // hyperpay if($hyperpay_mode && $hyperpay_mode == 'live'){ $url = "https://oppwa.com/".$id; $curlopt = true; }else{ $url = "https://test.oppwa.com/".$id; $curlopt = false; } $url .= "?entityId=".$hyperpay_brand->entity_id; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HTTPHEADER, array( "Authorization:Bearer ".$hyperpay_Authorization)); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $curlopt); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $responseDat = curl_exec($ch); if(curl_errno($ch)) { return curl_error($ch); } curl_close($ch); $responseData = json_decode( $responseDat, true ); $code = isset($responseData[ 'result' ][ 'code' ] ) ? $responseData[ 'result' ][ 'code' ] :'-1'; // find the transaction $transaction = Transaction::where('checkout_id','=',$checkoutId)->first(); // check if code is success if($transaction && $this->isSuccess($code)){ $transaction->status = 'succeeded'; $transaction->update(); // update wallet $user->wallet += $transaction->amount; $user->update(); // success $msg = trans('payment.successfully_completed'); return view('stores_dashboard.commissions.charge_wallet_response', compact('msg')); }else{ if($transaction){ $transaction->status = 'failed'; $transaction->update(); } // fail $msg = trans('payment.failed'); return view('stores_dashboard.commissions.charge_wallet_response', compact('msg')); } } }
Back to File Manager