mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/exchange-2.0.git
synced 2026-08-19 04:23:55 +00:00
71 lines
2.4 KiB
PHP
71 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Classes\Modules\Vouchers\Services\Voucherify;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
use App\Classes\Modules\Vouchers\DataTransferObjects\ValidateVoucherifyVoucherObject;
|
|
use App\Classes\General\Helper;
|
|
|
|
class ValidatesVoucherifyVoucher
|
|
{
|
|
|
|
/** @var VoucherifyClient */
|
|
private $voucherifyClient;
|
|
|
|
|
|
/**
|
|
* ValidatesVoucherifyVoucher constructor.
|
|
*
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->voucherifyClient = createVoucherifyClient();
|
|
}
|
|
|
|
/**
|
|
* @param ValidateVoucherifyVoucherObject $validateVoucherifyVoucherObject
|
|
* @return null|object
|
|
* @throws \Voucherify\ClientException
|
|
*/
|
|
public function execute(ValidateVoucherifyVoucherObject $validateVoucherifyVoucherObject)
|
|
{
|
|
try {
|
|
$validateVoucherObj = [
|
|
"customer" => [
|
|
"source_id" => $validateVoucherifyVoucherObject->getUser()->id,
|
|
"name" => $validateVoucherifyVoucherObject->getUser()->name,
|
|
"email" => $validateVoucherifyVoucherObject->getUser()->email,
|
|
"metadata" => [
|
|
"exchange_company_id" => $validateVoucherifyVoucherObject->getCompanyId(),
|
|
"exchange_user_id" => $validateVoucherifyVoucherObject->getUser()->id
|
|
]
|
|
]
|
|
];
|
|
if ($validateVoucherifyVoucherObject->getAmount()) {
|
|
$validateVoucherObj['order'] = [
|
|
"amount" => $validateVoucherifyVoucherObject->getAmount() * 100 //converting it to cents
|
|
];
|
|
}
|
|
|
|
$result = $this->voucherifyClient->validations->validateVoucher($validateVoucherifyVoucherObject->getVoucherCode(), $validateVoucherObj);
|
|
|
|
if (isset($result->metadata) && isset($result->metadata->email)) {
|
|
if($validateVoucherifyVoucherObject->getUser()->email != $result->metadata->email){
|
|
$result->reason = 'Invalid Code';
|
|
}
|
|
}
|
|
|
|
if (isset($result->reason)) {
|
|
Helper::debugLogger('ValidatesVoucherifyVoucher error: '. $result->reason);
|
|
$result->reason = 'Invalid Code';
|
|
}
|
|
|
|
return $result;
|
|
} catch (\Voucherify\ClientException $e) {
|
|
// throw $e;
|
|
Log::error($e);
|
|
return null;
|
|
}
|
|
}
|
|
}
|