dbfdg plan_id); $plan = Plan::find($planID); $orderID = strtoupper(str_replace('.', '', uniqid('', true))); $user = Auth::user(); if ($plan) { $get_amount = $plan->price; if (!empty($request->coupon)) { $coupons = Coupon::where('code', strtoupper($request->coupon))->where('is_active', '1')->first(); if (!empty($coupons)) { $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) { $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(); Order::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' => $currency, 'txn_id' => '', 'payment_type' => 'Xendit', '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.')); } } $response = ['orderId' => $orderID, 'user' => $user, 'get_amount' => $get_amount, 'plan' => $plan, 'currency' => $currency]; Xendit::setApiKey($xendit_api); $params = [ 'external_id' => $orderID, 'payer_email' => Auth::user()->email, 'description' => 'Payment for order ' . $orderID, 'amount' => $get_amount, 'callback_url' => route('plan.xendit.status'), 'success_redirect_url' => route('plan.xendit.status', $response), 'failure_redirect_url' => route('plans.index'), ]; $invoice = \Xendit\Invoice::create($params); Session::put('invoice', $invoice); return redirect($invoice['invoice_url']); } } public function planGetXenditStatus(Request $request) { $data = request()->all(); $fixedData = []; foreach ($data as $key => $value) { $fixedKey = str_replace('amp;', '', $key); $fixedData[$fixedKey] = $value; } $payment_setting = Utility::getAdminPaymentSetting(); $xendit_api = $payment_setting['xendit_api']; Xendit::setApiKey($xendit_api); $session = Session::get('invoice'); $getInvoice = \Xendit\Invoice::retrieve($session['id']); $getAmount = $request->get_amount; $authuser = User::find($fixedData['user']); $plan = Plan::find($fixedData['plan']); if ($getInvoice['status'] == 'PAID') { Utility::referralTransaction($plan); $order = new Order(); $order->order_id = $request->orderId; $order->name = $authuser->name; $order->card_number = ''; $order->card_exp_month = ''; $order->card_exp_year = ''; $order->plan_name = $plan->name; $order->plan_id = $plan->id; $order->price = $getAmount; $order->price_currency = $request->currency; $order->txn_id = $request->orderID; $order->payment_type = __('Xendit '); $order->payment_status = 'success'; $order->txn_id = ''; $order->receipt = ''; $order->user_id = $authuser->id; $order->save(); $assignPlan = $authuser->assignPlan($plan->id); $coupons = Coupon::find($request->coupon_id); if (!empty($request->coupon_id)) { if (!empty($coupons)) { $userCoupon = new UserCoupon(); $userCoupon->user = $user->id; $userCoupon->coupon = $coupons->id; $userCoupon->order = $request->orderID; $userCoupon->save(); $usedCoupun = $coupons->used_coupon(); if ($coupons->limit <= $usedCoupun) { $coupons->is_active = 0; $coupons->save(); } } } if ($assignPlan['is_success']) { return redirect()->route('plans.index')->with('success', __('Plan activated Successfully.')); } else { return redirect()->route('plans.index')->with('error', __($assignPlan['error'])); } } } public function invoicePayWithXendit(Request $request) { $data = request()->all(); $fixedData = []; foreach ($data as $key => $value) { $fixedKey = str_replace('amp;', '', $key); $fixedData[$fixedKey] = $value; } $invoice_id = \Illuminate\Support\Facades\Crypt::decrypt($fixedData['invoice_id']); $invoice = Invoice::find($invoice_id); $user = User::where('id', $invoice->created_by)->first(); $get_amount = $fixedData['amount']; $orderID = strtoupper(str_replace('.', '', uniqid('', true))); try { if ($invoice) { $payment_setting = Utility::getCompanyPaymentSetting($user->id); $xendit_token = $payment_setting['xendit_token']; $xendit_api = $payment_setting['xendit_api']; $currency = isset($payment_setting['site_currency']) ? $payment_setting['site_currency'] : 'USD'; $response = ['orderId' => $orderID, 'user' => $user, 'get_amount' => $get_amount, 'invoice' => $invoice, 'currency' => $currency]; Xendit::setApiKey($xendit_api); $params = [ 'external_id' => $orderID, 'payer_email' => $user->email, 'description' => 'Payment for order ' . $orderID, 'amount' => $get_amount, 'callback_url' => route('invoice.xendit.status'), 'success_redirect_url' => route('invoice.xendit.status', $response), ]; $Xenditinvoice = \Xendit\Invoice::create($params); Session::put('invoicepay', $Xenditinvoice); return redirect($Xenditinvoice['invoice_url']); } else { return redirect()->back()->with('error', 'Invoice not found.'); } } catch (\Throwable $e) { return redirect()->back()->with('error', __($e)); } } public function getInvociePaymentStatus(Request $request) { $data = request()->all(); $fixedData = []; foreach ($data as $key => $value) { $fixedKey = str_replace('amp;', '', $key); $fixedData[$fixedKey] = $value; } $session = Session::get('invoicepay'); $invoice = Invoice::find($fixedData['invoice']); $user = User::where('id', $invoice->created_by)->first(); $settings= Utility::settingsById($invoice->created_by); $payment_setting = Utility::getCompanyPaymentSetting($user->id); $xendit_api = $payment_setting['xendit_api']; Xendit::setApiKey($xendit_api); $getInvoice = \Xendit\Invoice::retrieve($session['id']); $get_amount = $fixedData['get_amount']; if ($getInvoice['status'] == 'PAID') { $invoice_payment = new InvoicePayment(); $invoice_payment->invoice_id = $invoice->id; $invoice_payment->date = Date('Y-m-d'); $invoice_payment->amount = $get_amount; $invoice_payment->account_id = 0; $invoice_payment->payment_method = 0; $invoice_payment->order_id = $request->orderId; $invoice_payment->payment_type = 'Xendit'; $invoice_payment->receipt = ''; $invoice_payment->reference = ''; $invoice_payment->description = 'Invoice ' . Utility::invoiceNumberFormat($settings, $invoice->invoice_id); $invoice_payment->save(); if($invoice->getDue() <= 0) { $invoice->status = 4; $invoice->save(); } elseif(($invoice->getDue() - $invoice_payment->amount) == 0) { $invoice->status = 4; $invoice->save(); } else { $invoice->status = 3; $invoice->save(); } //for customer balance update Utility::updateUserBalance('customer', $invoice->customer_id, $request->amount, 'debit'); //For Notification $setting = Utility::settingsById($invoice->created_by); $customer = Customer::find($invoice->customer_id); $notificationArr = [ 'payment_price' => $request->amount, 'invoice_payment_type' => 'Aamarpay', 'customer_name' => $customer->name, ]; //Slack Notification if(isset($settings['payment_notification']) && $settings['payment_notification'] ==1) { Utility::send_slack_msg('new_invoice_payment', $notificationArr,$invoice->created_by); } //Telegram Notification if(isset($settings['telegram_payment_notification']) && $settings['telegram_payment_notification'] == 1) { Utility::send_telegram_msg('new_invoice_payment', $notificationArr,$invoice->created_by); } //Twilio Notification if(isset($settings['twilio_payment_notification']) && $settings['twilio_payment_notification'] ==1) { Utility::send_twilio_msg($customer->contact,'new_invoice_payment', $notificationArr,$invoice->created_by); } //webhook $module ='New Invoice Payment'; $webhook= Utility::webhookSetting($module,$invoice->created_by); if($webhook) { $parameter = json_encode($invoice_payment); $status = Utility::WebhookCall($webhook['url'],$parameter,$webhook['method']); if($status == true) { return redirect()->route('invoice.link.copy', \Crypt::encrypt($invoice->id))->with('error', __('Transaction has been failed.')); } else { return redirect()->back()->with('error', __('Webhook call failed.')); } } return redirect()->route('invoice.link.copy', \Crypt::encrypt($invoice->id))->with('success', __('Invoice paid Successfully!')); } else { return redirect()->route('invoice.link.copy', \Crypt::encrypt($invoice->id))->with('error', __('Transaction fail')); } } }