unioil-loyalty-app/app/Services/PaymentService.php

248 lines
8.5 KiB
PHP

<?php
namespace App\Services;
use Response;
use Schema;
use Hash;
use App\Libraries\ListHelper;
use Illuminate\Http\Request;
use App\Contracts\PaymentResourceInterface;
use App\Payment;
use App\PaymentsItems;
use App\Helpers\HttpStatusCode;
use App\Helpers\CurrentUserHelper;
use App\Libraries\UuidHelper;
use App\Libraries\StaticContents;
class PaymentService implements PaymentResourceInterface
{
public $payments;
public $items;
public $payment_id;
public function report_topUp($params, $export = false)
{
$pagination = ListHelper::validatePagination($params['page_size'],$params['page']);
$list = Payment::with(['loyaltyCard'])
->where('payments.is_active','=',1)
->where('payments.cyware_synced','=',1)
->where('station_id',StaticContents::app_station_id());
if($params['date_start'] != null && $params['date_end'] != null)
{
$list = $list->where('paid_at','>=',date('Y-m-d',strtotime($params['date_start'])).' 00:00:00')
->where('paid_at','<=',date('Y-m-d',strtotime($params['date_end'])).' 23:59:59');
}
if($params['search'] != null)
{
$list = $list->whereHas('loyaltyCard', function ($query) use ($params) {
$query->where('card_number', 'LIKE', '%'.$params['search'].'%');
});
}
$sorting = $params['sorting'];
if(count($sorting) > 0)
{
$list = $list->sort($sorting['field'],$sorting['sort_order']);
}
if($export)
return $list->get();
else
return $list->paginate($params['page_size']);
}
public function get_by_trans_num($trans_num)
{
$data = Payment::with(['ratings','items'])
->where('trans_num',$trans_num);
return $data->first();
}
public function store_non_app($data,$member_data)
{
$this->payments = new Payment();
$uuid = new UuidHelper;
$this->payments->payment_uuid = $uuid->generate_uuid1();
$this->payments->lcard_id = $member_data->lcard_id;
$this->payments->station_id = $data['station_id'];
$this->payments->firstname = $member_data->personalDetails->firstname;
$this->payments->lastname = $member_data->personalDetails->lastname;
$this->payments->payer_email = $member_data->email;
$this->payments->trans_num = $data['trans_num'];
// $this->payments->item_name = $data['item_name'];
// $this->payments->item_code = $data['item_code'];
$this->payments->amount = $data['total_amount'];
$this->payments->points = $data['points'];
$this->payments->entry_type_code = $data['entry_type_code'];
$this->payments->entry_type_desc = $data['entry_type_desc'];
// $this->payments->quantity = $data['quantity'];
$this->payments->created_by = $member_data->lcard_id;
$this->payments->payer_id = 0;
$this->payments->business = '';
$this->payments->verify_sign = '';
$this->payments->receiver_email = '';
if($this->payments->save())
{
$payment_id = $this->payments->payment_id;
foreach ($data['items'] as $k => $i) {
$this->items = new PaymentsItems();
$this->items->payment_id = $payment_id;
$this->items->item_name = $i['item'];
$this->items->quantity = $i['quantity'];
$this->items->price = $i['price'];
$this->items->created_at = date('Y-m-d H:i:s');
$this->items->save();
}
return $payment_id;
}
else
return false;
}
public function store($data,$member_data)
{
$this->payments = new Payment();
$uuid = new UuidHelper;
$this->payments->payment_uuid = $uuid->generate_uuid1();
$this->payments->lcard_id = $member_data->lcard_id;
$this->payments->station_id = StaticContents::app_station_id();
$this->payments->trans_num = $data['trans_num'];
// $this->payments->item_name = $data['item_name'];
// $this->payments->item_code = $data['item_code'];
$this->payments->amount = $data['amount'];
$this->payments->points = 0; // cannot be known
$this->payments->entry_type_code = $data['entry_type_code'];
$this->payments->entry_type_desc = $data['entry_type_desc'];
// $this->payments->quantity = 1;
$this->payments->created_by = $member_data->lcard_id;
$this->payments->paypal_id = $data['paypal_id'];
$this->payments->other = $data['other'];
$this->payments->status = 0;
// details to be updated after successful payment from paypal
$this->payments->payer_email = '';
$this->payments->payer_id = 0;
$this->payments->business = '';
$this->payments->verify_sign = '';
$this->payments->receiver_email = '';
$this->payments->firstname = '';
$this->payments->lastname = '';
if($this->payments->save())
{
$payment_id = $this->payments->payment_id;
foreach ($data['items'] as $k => $i) {
$this->items = new PaymentsItems();
$this->items->payment_id = $payment_id;
$this->items->item_name = $i['item'];
$this->items->quantity = $i['quantity'];
$this->items->price = $i['price'];
$this->items->created_at = date('Y-m-d H:i:s');
$this->items->save();
}
return $payment_id;
}
else
return false;
}
public function count_between($lcard_id, $from, $to)
{
$count = Payment::where('is_active',1)
->where('lcard_id',$lcard_id)
->where('paid_at','>=',$from)
->where('paid_at','<=',$to)
->count();
return $count;
}
public function count_trans_month()
{
$count = Payment::where('is_active',1)
->where('created_at','>=',date('Y-m-01').' 00:00:00')
->where('created_at','<=',date('Y-m-t').' 23:59:59')
->count();
return $count;
}
public function failed_transaction($paypal_id,$other)
{
$this->payments = Payment::where('paypal_id',$paypal_id)
->where('is_active',1)
->first();
if($this->payments->status != 1)
{
$this->payments->status = 2;
$this->payments->other = serialize($other);
if($this->payments->save())
return true;
else
return false;
}
}
public function success_transaction($trans_num, $data)
{
$this->payments = Payment::where('trans_num',$trans_num)
->where('is_active',1)
->first();
$this->payments->status = 1;
$this->payments->other = serialize($data['other']);
$this->payments->firstname = $data['firstname'];
$this->payments->lastname = $data['lastname'];
$this->payments->payer_email = $data['payer_email'];
$this->payments->receiver_email = $data['receiver_email'];
$this->payments->paypal_trans_num = $data['paypal_trans_num'];
$this->payments->paid_at = $data['paid_at'];
if($this->payments->save())
return true;
else
return false;
}
public function cyware_synced($id, $points)
{
$this->payments = Payment::where('payment_id',$id)
->first();
$this->payments->cyware_synced = 1;
$this->payments->points = $points;
if($this->payments->save())
return true;
else
return false;
}
}