Edit File: StorePlansController.php
<?php namespace App\Http\Controllers\Api; use App\Http\Controllers\Controller; use App\Http\Requests\Api\Plan\PayPlanRequest; use App\Http\Requests\Api\Plan\subscribePlanRequest; use App\Http\Resources\PaymentmethodResource; use App\Http\Resources\PlanResource; use App\Models\HyperpayBrand; use App\Models\Paymentmethod; use App\Models\Plan; use App\Models\Setting; use App\Models\Subscription; use App\Models\Transaction; use App\Traits\ApiTrait; use App\Traits\GeneralTrait; use App\Traits\Uploadable; use Carbon\Carbon; use Illuminate\Http\Request; class StorePlansController extends Controller { use Uploadable, GeneralTrait, ApiTrait; public function getPlans() { $plans = Plan::where('available', 'true')->get(); $data = PlanResource::collection($plans); return $this->dataReturn($data); } public function subscribePlan(subscribePlanRequest $request) { $user = auth('api')->user(); $plan = Plan::find($request['plan_id']); if (!$plan) { $msg = trans('order.not_available'); return $this->failMsg($msg); } if($old_subscribtion = Subscription::where('user_id' , $user->id)->where('unsupscribe' , '0')->first()) { $old_subscribtion->update(['unsupscribe' => '1']); } if($request['payment_type'] == 'wallet' && auth('api')->user()->wallet < $plan->price){ $msg = trans('order.walletHasNoEnouphCredit'); return $this->failMsg($msg); } auth('api')->user()->update(['wallet' => auth('api')->user()->wallet - $plan->price]); $subscribe = new Subscription(); $subscribe->user_id = $user->id; $subscribe->plan_id = $request['plan_id']; $subscribe->payment_type = $request['payment_type']; $subscribe->price = $plan->price; $subscribe->duration = $plan->duration; $subscribe->start_at = Carbon::now(); $subscribe->end_at = Carbon::now()->addDay($plan->duration); $subscribe->payment_status = 'true'; $subscribe->save(); $msg = trans('user.plan_subscribe'); return $this->successMsg($msg); } public function removeSubscription(Request $request) { $user = auth('api')->user(); $supscription = Subscription::where('plan_id' , $request['plan_id'])->where('user_id' , $user->id)->where('unsupscribe' , '0')->first(); if(! $supscription) { $msg = trans('user.plan_unsubscribe_fail'); return $this->failMsg($msg); } $supscription->update(['unsupscribe' => '1' , 'end_at' => null]); $msg = trans('user.plan_unsubscribe'); return $this->successMsg($msg); } public function planPaymentMethods() { $payment_methods = Paymentmethod::where('status', 'active')->where('key', '!=', 'cash')->get(); $data = []; $data['payment_methods'] = PaymentmethodResource::collection($payment_methods); return $this->dataReturn($data); } public function payPlanWithWallet(PayPlanRequest $request) { $user = auth('api')->user(); // find order $subscription_data = Subscription::where('plan_id', $request['plan_id'])->where('user_id', $user->id)->where('payment_status', 'false')->first(); if (!$subscription_data) { $plan = Plan::find($request['plan_id']); if (!$plan) { $msg = trans('order.not_available'); return $this->failMsg($msg); } $supscription = Subscription::where('user_id' , $user->id)->where('start_at' , null)->where('end_at' , null)->delete(); $subscribe = new Subscription(); $subscribe->user_id = $user->id; $subscribe->plan_id = $request['plan_id']; $subscribe->payment_type = 'wallet'; $subscribe->price = $plan->price; $subscribe->duration = $plan->duration; $subscribe->save(); $subscription_data = $subscribe->refresh(); } $start_at = Carbon::now(); $end_at = Carbon::now()->addDay($subscription_data->duration); // pay order with wallet $subscription_data->update(['payment_status' => 'true', 'payment_type' => 'wallet', 'start_at' => $start_at, 'end_at' => $end_at]); $user->wallet -= $subscription_data->price; $user->update(); $msg = trans('order.paid_successfully'); return $this->successMsg($msg); } public function PayPlanIndex(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(); if (!$hyperpay_brand || $hyperpay_brand->is_active == 'false' || !$hyperpay_brand->entity_id) { $msg = trans('payment.brand_disabled'); return $this->failMsg($msg); } // find subscription $subscription = Subscription::where('plan_id', $request['plan_id'])->where('user_id', $user->id)->where('payment_status', 'false')->first(); if (!$subscription) { $msg = trans('order.not_available'); return $this->failMsg($msg); } // amount $amount = $subscription->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; } $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->subscription_id = $subscription->id; $transaction->checkout_id = $checkoutId; $transaction->amount = $amount; $transaction->type = 'plan'; $transaction->status = 'pending'; $transaction->save(); // return success return $this->dataReturn($responseDat); } public function payPlanResult(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 $plan = Subscription::find($transaction->subscription_id); if (!$plan) { $msg = trans('order.not_available'); return $this->failMsg($msg); } $start_at = Carbon::now(); $end_at = Carbon::now()->addDay($plan->duration); $plan->end_at = $end_at; $plan->start_at = $start_at; $plan->payment_status = 'true'; $plan->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