JFIFxxC      C  " }!1AQa"q2#BR$3br %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz w!1AQaq"2B #3Rbrmerchant_id = $payment_setting['phonepe_merchantid'] ?? ''; $this->secret_key = $payment_setting['phonepe_key'] ?? ''; $this->salt_index = 1; $this->phonepe_url = $payment_setting['phonepe_url'] ?? 'https://api.phonepe.com/apis/hermes'; } /** * Initiate PhonePe Payment */ public function phonepePaymentStore(Request $request) { $this->paymentConfig(); $planID = \Illuminate\Support\Facades\Crypt::decrypt($request->plan_id); $plan = Plan::find($planID); $user = Auth::user(); $payment_setting = Utility::getAdminPaymentSetting(); if (!$plan) { return redirect()->route('plans.index')->with('error', __('Plan not found.')); } $amount = $plan->price; $coupon_id = null; if (!empty($request->coupon)) { echo ""; $coupon = Coupon::where('code', strtoupper($request->coupon))->where('is_active', 1)->first(); if (!empty($coupons)) { echo ""; $usedCoupun = $coupons->used_coupon(); $discount_value = ($plan->price / 100) * $coupons->discount; $get_amount = $plan->price - $discount_value; if ($coupons->limit == $usedCoupun) { return redirect()->back()->with('error', __('This coupon code has expired.')); } if ($get_amount <= 0) { echo ""; $authuser = \Auth::user(); $authuser->plan = $plan->id; $authuser->save(); $assignPlan = $authuser->assignPlan($plan->id); if ($assignPlan['is_success'] == true && !empty($plan)) { if (!empty($authuser->payment_subscription_id) && $authuser->payment_subscription_id != '') { try { $authuser->cancel_subscription($authuser->id); } catch (\Exception $exception) { \Log::debug($exception->getMessage()); } } $orderID = strtoupper(str_replace('.', '', uniqid('', true))); $userCoupon = new UserCoupon(); $userCoupon->user = $authuser->id; $userCoupon->coupon = $coupons->id; $userCoupon->order = $orderID; $userCoupon->save(); PlanOrder::create( [ 'order_id' => $orderID, 'name' => null, 'email' => null, 'card_number' => null, 'card_exp_month' => null, 'card_exp_year' => null, 'plan_name' => $plan->name, 'plan_id' => $plan->id, 'price' => $get_amount == null ? 0 : $get_amount, 'price_currency' => !empty($payment_setting['CURRENCY']) ? $payment_setting['CURRENCY'] : 'USD', 'txn_id' => '', 'payment_type' => 'PhonePe', 'payment_status' => 'success', 'receipt' => null, 'user_id' => $authuser->id, ] ); $assignPlan = $authuser->assignPlan($plan->id); return redirect()->route('plans.index')->with('success', __('Plan Successfully Activated')); } } } else { return redirect()->back()->with('error', __('This coupon code is invalid or has expired.')); } } echo ""; $orderID = strtoupper(str_replace('.', '', uniqid('', true))); $price = $plan->price; $paymentData = array( 'merchantId' => $this->merchant_id, 'merchantTransactionId' => $orderID, "merchantUserId" => $user->id, 'amount' => $price * 100, 'redirectUrl' => "https://bizcards.co.in/phonepe/payments/success?order_id=$orderID&plan_id=$plan->id&coupon_code=$request->coupon&get_amount=$price", 'redirectMode' => "REDIRECT", 'callbackUrl' => "https://bizcards.co.in/phonepe/payments/success?order_id=$orderID&plan_id=$plan->id&coupon_code=$request->coupon&get_amount=$price", "merchantOrderId" => $orderID, "paymentInstrument" => array( "type" => "PAY_PAGE", ) ); $jsonencode = json_encode($paymentData); $payloadMain = base64_encode($jsonencode); $payload = $payloadMain . "/pg/v1/pay" . $this->secret_key; $sha256 = hash("sha256", $payload); $final_x_header = $sha256 . '###' . $this->salt_index; $request = json_encode(array('request' => $payloadMain)); $curl = curl_init(); curl_setopt_array($curl, [ CURLOPT_URL => "https://api.phonepe.com/apis/hermes/pg/v1/pay", CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 30, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => "POST", CURLOPT_POSTFIELDS => $request, CURLOPT_HTTPHEADER => [ "Content-Type: application/json", "X-VERIFY: " . $final_x_header, "accept: application/json" ], ]); $response = curl_exec($curl); $err = curl_error($curl); curl_close($curl); if ($err) { echo "cURL Error #:" . $err; } else { $res = json_decode($response); echo ""; if (isset($res->success) && $res->success == '1') { $paymentCode = $res->code; $paymentMsg = $res->message; $payUrl = $res->data->instrumentResponse->redirectInfo->url; echo ""; return redirect()->to($res->data->instrumentResponse->redirectInfo->url); } } } /** * Handle PhonePe Payment Success */ public function phonepePaymentSuccess(Request $request) { echo ""; echo ""; $this->paymentConfig(); $user = \Auth::user(); $plan = Plan::find($request->plan_id); $couponCode = $request->coupon_code; $get_amount = $request->get_amount; $order_id = $request->order_id; $orderID = strtoupper(str_replace('.', '', uniqid('', true))); $payment_setting = Utility::getAdminPaymentSetting(); if (!empty($couponCode)) { $coupons = Coupon::where('code', strtoupper($couponCode))->where('is_active', '1')->first(); $request['coupon_id'] = $coupons->id; } else { $coupons = null; } try { $hashString = "/pg/v1/status/$this->merchant_id/$order_id" . $this->secret_key; $hashedValue = hash('sha256', $hashString); $result = $hashedValue . "###" . $this->salt_index; $url = self::PROD_URL . 'status/' . $this->merchant_id . '/' . $order_id; $curl = curl_init(); curl_setopt_array($curl, [ CURLOPT_URL => "$url", CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'GET', CURLOPT_HTTPHEADER => [ "Content-Type: application/json", "X-MERCHANT-ID:$this->merchant_id", "X-VERIFY:$result", "accept: application/json", ], ]); $response = curl_exec($curl); $err = curl_error($curl); curl_close($curl); if ($err) { return redirect()->route('plans.index')->with('error', __('Transaction failed.')); } else { $res = json_decode($response); echo ""; echo ""; if (isset($res->success) && $res->success == '1') { echo ""; $txnid = $res->data->merchantTransactionId; $order = new PlanOrder(); $order->order_id = $order_id; $order->name = $user->name; $order->card_number = ''; $order->card_exp_month = ''; $order->card_exp_year = ''; $order->plan_name = $plan->name; $order->plan_id = $plan->id; $order->price = $get_amount; $order->price_currency = 'INR'; $order->txn_id = $txnid; $order->payment_type = 'PhonePe'; $order->payment_status = 'success'; $order->receipt = ''; $order->user_id = $user->id; $order->save(); Utility::referralTransaction($plan); $assignPlan = $user->assignPlan($plan->id); if ($assignPlan['is_success']) { return redirect()->route('plans.index')->with('success', __('Plan activated Successfully!')); } else { return redirect()->route('plans.index')->with('error', __($assignPlan['error'])); } } else { return redirect()->route('plans.index')->with('error', __('Transaction has been failed! ')); } } } catch (\Exception $e) { return redirect()->route('plans.index')->with('error', __('Error verifying payment.')); } } }