mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/exchange-2.0.git
synced 2026-08-19 04:23:55 +00:00
65 lines
1.7 KiB
PHP
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;
|
|
}
|
|
}
|
|
}
|