Edit File: PaymentController.php
<?php namespace App\Http\Controllers\Api; use App\Http\Controllers\Controller; use App\Http\Resources\HyperpayBrandsResource; use App\Jobs\NotifyDelegateAfterSucceedFromPayout; use App\Jobs\OrderIsPaidNotify; use App\Models\HyperpayBrand; use App\Models\Order; use App\Models\Payout; use App\Models\Setting; use App\Models\Transaction; use App\Models\User; use App\Services\SettingService; use App\Traits\ApiTrait; use App\Traits\GeneralTrait; use Illuminate\Http\Request; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Validator; class PaymentController extends Controller { use ApiTrait, GeneralTrait; public function webhook() { $data = SettingService::appInformations(Setting::pluck('value', 'key')); $http_body = file_get_contents('php://input'); $notification_key_from_configration = $data['hyper_split_configuration_key']; $headers = getallheaders(); $iv_from_http_header = $headers['X-Initialization-Vector']; $auth_tag_from_http_header = $headers['X-Authentication-Tag']; $http = json_decode($http_body); $body = $http->encryptedBody; $key = hex2bin($notification_key_from_configration); $iv = hex2bin($iv_from_http_header); $auth_tag = hex2bin($auth_tag_from_http_header); $cipher_text = hex2bin($body); $result = openssl_decrypt($cipher_text, "aes-256-gcm", $key, OPENSSL_RAW_DATA, $iv, $auth_tag); if ($result = json_decode($result)) { if ($result->status == true) { $uniqueId = $result->data->transactions[0]->uniqueId; $payout = Payout::where('transaction_id', $uniqueId)->first(); $debitAmount = $payout->amount; if ($payout) { $user = User::where('id', $payout->user_id)->first(); $user->payout->update(['status' => 'accepted']); $user->balance = ($user->balance) - ($debitAmount); $user->update(); #notify dispatch(new NotifyDelegateAfterSucceedFromPayout($user, $debitAmount)); return response()->json(['key' => 'success', 'msg' => 'Done']); } } else { $uniqueId = $result->data->transactions[0]->uniqueId; $payout = Payout::where('transaction_id', $uniqueId)->first(); $user = User::where('id', $payout->user_id)->first(); $user->payout->update(['status' => 'rejected']); $user->update(); return response()->json(['key' => 'fail', 'msg' => 'failed']); } } else { return response()->json(['key' => 'fail', 'msg' => 'failed']); } } public function hyperpayBrands() { $hyperpay_brands = HyperpayBrand::where('is_active', 'true')->get(); $data = []; $data['hyperpay_brands'] = HyperpayBrandsResource::collection($hyperpay_brands); return $this->dataReturn($data); } public function payInvoiceIndex(Request $request) { // user $user = auth('api')->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 $this->failMsg($msg); } // find the brand $hyperpay_brand = HyperpayBrand::where('brand', $request['brand'])->first(); // Log::info($hyperpay_brand); if (!$hyperpay_brand || $hyperpay_brand->is_active == 'false' || !$hyperpay_brand->entity_id) { $msg = trans('payment.brand_disabled'); return $this->failMsg($msg); } // find order $order = $user->userOrders()->where('have_invoice', 'true')->where('payment_type', 'online')->where('id', $request['order_id'])->first(); if (!$order) { $msg = trans('order.not_available'); return $this->failMsg($msg); } // amount $amount = $order->total_price; $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; } if($hyperpay_brand->brand == 'MADA') { $test = "&testMode=INTERNAL"; }else{ $test = "&testMode=EXTERNAL"; } $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" . "&customer.givenName=wahba" . $test . "&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->order_id = $order->id; $transaction->checkout_id = $checkoutId; $transaction->amount = $amount; $transaction->type = 'invoice'; $transaction->status = 'pending'; $transaction->save(); // return success return $this->dataReturn($responseDat); } public function payInvoiceResult(Request $request) { // user $user = auth('api')->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 $this->failMsg($msg); } // find the brand $hyperpay_brand = HyperpayBrand::where('brand', $request['brand'])->first(); if (!$hyperpay_brand || $hyperpay_brand->is_active == 'false' || !$hyperpay_brand->entity_id) { $msg = trans('payment.brand_disabled'); return $this->failMsg($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 order $order = Order::find($transaction->order_id); if (!$order) { $msg = trans('order.not_available'); return $this->failMsg($msg); } $order->payment_status = 'true'; $order->update(); //notify $notified_user = User::find($order->delegate_id); if ($notified_user) { dispatch(new OrderIsPaidNotify($notified_user, $order)); } // success $msg = trans('payment.successfully_completed'); return $this->successMsg($msg); } else { if ($transaction) { $transaction->status = 'failed'; $transaction->update(); } // fail $msg = trans('payment.failed'); return $this->failMsg($msg); } } public function chargeWalletIndex(Request $request) { return $this->failMsg('غير متاح حاليا'); $validator = Validator::make($request->all(), [ 'price' => 'required', ]); if ($validator->fails()) { return $this->requestFailsReturn($validator); } // user $user = auth('api')->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 $this->failMsg($msg); } // find the brand $hyperpay_brand = HyperpayBrand::where('brand', $request['brand'])->first(); if (!$hyperpay_brand || $hyperpay_brand->is_active == 'false' || !$hyperpay_brand->entity_id) { $msg = trans('payment.brand_disabled'); return $this->failMsg($msg); } // amount $amount = $this->convert2english($request['price']); $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; } if($hyperpay_brand->brand == 'MADA') { $test = "&testMode=INTERNAL"; }else{ $test = "&testMode=EXTERNAL"; } $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" . $test . "&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); } public function chargeWalletResult(Request $request) { // user $user = auth('api')->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 $this->failMsg($msg); } // find the brand $hyperpay_brand = HyperpayBrand::where('brand', $request['brand'])->first(); if (!$hyperpay_brand || $hyperpay_brand->is_active == 'false' || !$hyperpay_brand->entity_id) { $msg = trans('payment.brand_disabled'); return $this->failMsg($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 $this->successMsg($msg); } else { if ($transaction) { $transaction->status = 'failed'; $transaction->update(); } // fail $msg = trans('payment.failed'); return $this->failMsg($msg); } } }
Back to File Manager