Files
exchange-2.0/app/Classes/Modules/Vouchers/Services/ValidatesVoucher.php
T
2023-05-30 22:02:09 +08:00

65 lines
1.7 KiB
PHP

<?php
namespace App\Classes\Modules\Vouchers\Services;
use ErrorException;
use Illuminate\Support\Facades\Log;
class ValidatesVoucher
{
/** @var VoucherifyClient */
private $voucherifyClient;
/**
* ValidatesVoucher constructor.
*
*/
public function __construct()
{
$this->voucherifyClient = createVoucherifyClient();
}
/**
* @param string $voucherCode
* @param float $amount
* @param object $employee
* @return null|object
* @throws ErrorException
*/
public function execute(string $voucherCode, ?float $amount, ?object $employee)
{
try {
if($amount){
$result = $this->voucherifyClient->validations->validateVoucher($voucherCode, [
"customer" => [
// "id" => $employee->id,
"name" => $employee->name,
"email" => $employee->email,
],
"order" => [
"amount" => $amount * 100 //converting it to cents
]
]);
}
else{
$res = $this->voucherifyClient->validations->validateVoucher($voucherCode);
$result = [
"valid" => $res->valid,
"code" => $res->code
];
if (isset($res->reason)) {
$result["reason"] = $res->reason;
}
}
return $result;
} catch (\Voucherify\ClientException $e) {
// throw $e;
Log::error('ValidatesVoucher error:' . $e);
return null;
}
}
}