Voucherify phase 2

This commit is contained in:
Dillon
2023-07-17 21:29:19 +08:00
parent 46ae50b569
commit c7927847c4
162 changed files with 6063 additions and 298 deletions
@@ -0,0 +1,59 @@
<?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;
}
}
}