mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/exchange-2.0.git
synced 2026-08-19 04:23:55 +00:00
60 lines
1.5 KiB
PHP
60 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Classes\Modules\Vouchers\Services\Voucherify;
|
|
|
|
use App\Models\User;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Carbon\Carbon;
|
|
|
|
class CreatesVoucherifyVoucher
|
|
{
|
|
|
|
/** @var VoucherifyClient */
|
|
private $voucherifyClient;
|
|
|
|
|
|
/**
|
|
* CreatesVoucherifyVoucher constructor.
|
|
*
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->voucherifyClient = createVoucherifyClient();
|
|
}
|
|
|
|
/**
|
|
* @param User $user
|
|
* @param int $amount
|
|
* @return null|object
|
|
* @throws \Voucherify\ClientException
|
|
*/
|
|
public function execute(User $user, int $amount)
|
|
{
|
|
$startDate = Carbon::now();
|
|
$expirationDate = $startDate->copy()->addMonths(12)->endOfDay();
|
|
try {
|
|
$result = $this->voucherifyClient->vouchers->create([
|
|
"code" => Str::random(10),
|
|
"type" => "DISCOUNT_VOUCHER",
|
|
"discount" => [
|
|
"type" => "AMOUNT",
|
|
"amount_off" => $amount * 100,
|
|
],
|
|
"redemption" => [
|
|
"quantity" => 1
|
|
],
|
|
"metadata" => [
|
|
"email" => $user->email
|
|
],
|
|
"start_date" => $startDate->format('Y-m-d H:i:s'),
|
|
"expiration_date" => $expirationDate->format('Y-m-d H:i:s')
|
|
]);
|
|
return $result;
|
|
} catch (\Voucherify\ClientException $e) {
|
|
Log::error($e);
|
|
return null;
|
|
}
|
|
}
|
|
}
|