mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/exchange-2.0.git
synced 2026-08-19 04:23:55 +00:00
Voucherify phase 2
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user