Files
exchange-2.0/app/Classes/Modules/Vouchers/Services/Voucherify/CreatesVoucherifyVoucher.php
T

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->toIso8601String(),
"expiration_date" => $expirationDate->toIso8601String()
]);
return $result;
} catch (\Voucherify\ClientException $e) {
Log::error('CreatesVoucherifyVoucher '.$e);
return null;
}
}
}