Server : Apache System : Linux 145.162.205.92.host.secureserver.net 5.14.0-611.45.1.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Apr 1 05:56:53 EDT 2026 x86_64 User : tradze ( 1001) PHP Version : 8.1.34 Disable Function : NONE Directory : /home/tradze/public_html/app/Modules/Vouchers/Http/Controllers/Frontend/ |
<?php
namespace App\Modules\Vouchers\Http\Controllers\Frontend;
use App\Http\Controllers\SiteController;
use App\Modules\Banners\Repositories\BannersRepo;
use App\Modules\Schedules\Http\Requests\CardPayRequest;
use App\Modules\Services\Models\ServiceDuration;
use App\Modules\Users\Models\UserBillingAddress;
use App\Modules\Vouchers\Models\VoucherOffer;
use App\Modules\Vouchers\Repositories\VoucherRepository;
use App\User;
use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Storage;
use Illuminate\Http\Request;
use Laracasts\Flash\Flash;
use Omnipay\Common\CreditCard;
use Omnipay\Omnipay;
class VoucherOfferController extends SiteController
{
/**
* The book repository instance
*
* @var BookingRepository
*/
protected $voucher;
/**
* Create a new controller instance.
*/
public function __construct(BannersRepo $banners, VoucherRepository $voucher)
{
parent::__construct($banners);
$this->voucher = $voucher;
}
/**
* Checkout page
*/
public function buy_offer($slugString,$option)
{
$data = $this->getOfferOption($slugString,$option);
//if offer is inative or does not exists, return page 404
if (!$data['offer'])
return abort(404,'page not found');
//create data array
$this->data = [
'meta_title' => 'Buy offer',
'meta_description' => '',
'meta_keywords' => '',
'details' => $data,
'user' => \Auth::user(),
];
//render page
return view('vouchers::frontend_new.offers.buyoffer',$this->data);
}
/**
* Pay method
* @param Request $request
*/
public function pay(CardPayRequest $request)
{
$data = $this->getOfferOption($request->offer,$request->option);
$data['input'] = $request->input();
if (!$data['offer'] || !$data['service']){
return abort(404,'Error! The service or offer does not exists. Try again!');
}
$total = $data['total_price'];
$user = User::find(Auth::user()->id);
$token = $request->input('payment-method-nonce');
$gateway = Omnipay::create('SagePay\Direct');
$gateway->setVendor(env('SAGEPAY_VENDOR_NAME'));
$gateway->setTestMode(env('SAGEPAY_TEST_MODE'));
$user = User::find(Auth::user()->id);
$billingAddress = $user->address()->orderBy('is_main','desc')->first();
try {
$card = new CreditCard([
'firstName' => $user->profile->first_name,
'lastName' => $user->profile->last_name,
'number' => $request->card_number, //4462000000000003
'expiryMonth' => $request->expiry_month, //6
'expiryYear' => $request->expiry_year, //2030
'cvv' => $request->card_cvv, //123
//billing
'billingAddress1' => $request->billing_address?: env('SAGEPAY_BILLING_ADDR1'),
'billingCity' => $request->billing_county?: env('SAGEPAY_BILLING_CITY'),
'billingPostcode' => $request->billing_postcode?: env('SAGEPAY_BILLING_POSTCODE'),
'billingCountry' => $request->billing_country?:'GB',
'billingPhone' => $user->profile->mobile_number,
//shipping
'shippingAddress1' => $billingAddress?$billingAddress->address : env('SAGEPAY_BILLING_ADDR1'),
'shippingState' => $billingAddress?$billingAddress->county : env('SAGEPAY_BILLING_CITY'),
'shippingCity' => $billingAddress?$billingAddress->county : env('SAGEPAY_BILLING_CITY'),
'shippingPostcode' => $billingAddress?$billingAddress->postcode : env('SAGEPAY_BILLING_POSTCODE'),
'shippingCountry' => 'GB',
'shippingPhone' => $user->profile->mobile_number,
]);
}
catch(\Exception $e){
flash(strtoupper($e->getMessage()), 'danger');
return redirect(route('offers.buy.page',['offer'=>$data['offer']->slug,'option'=>$data['service']->id]));
}
$transactionId = time().'-'.$user->id.'-'.str_slug($data['offer']->name,'');
//add to the request
$request->transaction_id = $transactionId;
$data['input']['transaction_id'] = $transactionId;
try{
$response = $gateway->purchase(array(
'amount' => $total,
'currency' => env('SAGEPAY_CCY'),
'card' => $card,
'notifyUrl' => route('offers.buy.page',['offer'=>$data['offer']->slug,'option'=>$data['service']->id]),
'redirectUrl' => route('offers.buy.page',['offer'=>$data['offer']->slug,'option'=>$data['service']->id]),
'transactionId' => $transactionId,
'description' => 'Offer: '.$data['offer']->name,
'billingCountry' => 'GB',
))->send();
}
catch(\Exception $e){
flash(strtoupper($e->getMessage()), 'danger');
return redirect(route('offers.buy.page',['offer'=>$data['offer']->slug,'option'=>$data['service']->id]));
}
//if the payment is with successs
if ($response->isSuccessful()) {
//generate voucher code
$this->voucher->new_offer_voucher($response,$data,Auth::user()->id);
//save billing address
$billingAddrObj = UserBillingAddress::firstOrCreate([
'address'=>$request->billing_address,
'county'=>$request->billing_county,
'postcode'=>$request->billing_postcode,
'user_id' => \Auth::user()->id
]);
//redirect to success page
return redirect(route('offers.buy.paysuccess'));
}
elseif ($response->isRedirect()){
return redirect($response->redirect());
}
else{
flash(strtoupper($response->getMessage()), 'danger');
return redirect(route('offers.buy.page',['offer'=>$data['offer']->slug,'option'=>$data['service']->id]));
}
}
/**
* Pay method
* @param Request $request
*/
public function pay_old(CardPayRequest $request)
{
$data = $this->getOfferOption($request->offer,$request->option);
if (!$data['offer'] || !$data['service']){
return abort(404,'Error! The service or offer does not exists. Try again!');
}
$total = $data['total_price'];
$user = User::find(Auth::user()->id);
$token = $request->input('payment-method-nonce');
//if user has no plans, create one
if (!$user->braintree_id){
try{
$user->newSubscription('single-charge', env(BRAINTREE_PLAN,'qrjb'))->create($token,[
'email'=>$user->email,
'phone'=>$user->profile->mobile_phone,
]);
$name = "{$data['offer']->name}";
$response = $user->invoiceFor($name,$total);
//generate voucher code
$this->voucher->new_offer_voucher($response,$data,Auth::user()->id);
}
catch (\Exception $e){
flash(strtoupper(trans('scheduless::payment.incorrect_card_details')), 'danger');
return redirect(route('offers.buy.page',['offer'=>$data['offer']->slug,'option'=>$data['service']->id]));
}
}
else{
//update token for each payment
try{
$user->updateCard($token);
$name = "{$data['offer']->name}";
$response = $user->invoiceFor($name,$total);
//generate voucher code
$this->voucher->new_offer_voucher($response,$data,Auth::user()->id);
}
catch (\Exception $e){
$message = $e->getMessage();
$message = str_ireplace('braintree','Tradze',$message);
flash(strtoupper($message), 'danger');
return redirect(route('offers.buy.page',['offer'=>$data['offer']->slug,'option'=>$data['service']->id]));
}
}
// //change user amount
// try {
// $name = "{$data['offer']->name}";
// $response = $user->invoiceFor($name,$total);
//
// //generate voucher code
// $this->voucher->new_offer_voucher($response,$data,Auth::user()->id);
//
// } catch (\Exception $e) {
//
// flash(strtoupper($e->getMessage()), 'danger');
// return redirect(route('offers.buy.page',['offer'=>$data['offer']->slug,'option'=>$data['service']->id]));
// }
//redirect to success page
return redirect(route('offers.buy.paysuccess'));
}
/**
* Checkout success payment page
*/
public function paysuccess()
{
$this->data = [
'meta_title' => 'Voucher generated',
'meta_description' => '',
'meta_keywords' => '',
];
//render page
return view('vouchers::frontend_new.offers.checkoutsuccess',$this->data);
}
/**
* Get offer an option info
* @param $offer
* @param $option
* @return array
*/
protected function getOfferOption($slugString,$option)
{
$data = ['offer'=>null];
$offer = VoucherOffer::where('is_active',1)->where('slug',$slugString)->first();
if (!$offer)
return $data;
$service_duration = $offer->duration_options()->where('service_duration_id',$option)->first();
if (!$service_duration)
return $data;
$price = $offer->buy * $service_duration->price;
$amount_saved = $offer->bonus * $service_duration->price;
$price_wh_discount = ($offer->buy+$offer->bonus) * $service_duration->price;
$data = [
'offer'=>$offer,
'service'=>$service_duration,
'amount_saved' => $amount_saved,
'single_price' => $service_duration->price,
'total_price' => $price,
'noOftreatments' => $offer->buy+$offer->bonus,
'price_wh_discount' => $price_wh_discount,
];
//return data
return $data;
}
}