1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CashierUtils\Console\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use Laravel\Cashier\Cashier; |
8
|
|
|
use Stripe\StripeClient; |
9
|
|
|
|
10
|
|
|
class CreatePromotionCodesCommand extends Command |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The name and signature of the console command. |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $signature = 'cashier-tools:api:create:promotion-codes |
18
|
|
|
{couponId : Coupon ID} |
19
|
|
|
{--P|pattern=$ : Pattern for creation} |
20
|
|
|
{--S|chars-count=8 : Random strings count} |
21
|
|
|
{--C|count=1 : Promo codes count} |
22
|
|
|
{--p_active= : Parameter active} |
23
|
|
|
{--p_customer= : Parameter customer} |
24
|
|
|
{--p_expires_at= : Parameter expires_at} |
25
|
|
|
{--p_max_redemptions= : Parameter max_redemptions} |
26
|
|
|
{--stripe-secret= : Stripe secret key} |
27
|
|
|
'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The console command description. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $description = " |
35
|
|
|
Create new promotion codes \n |
36
|
|
|
Example: \n |
37
|
|
|
php artisan cashier-tools:api:create:promotion-codes 4o73oLhX -P \"FREE12$\" -S 8 -C 2 --p_max_redemptions=1 >> codes.txt |
38
|
|
|
"; |
39
|
|
|
|
40
|
|
|
protected ?StripeClient $cachedStripeClient = null; |
41
|
|
|
|
42
|
1 |
|
public function handle() |
43
|
|
|
{ |
44
|
1 |
|
$createdCount = 0; |
45
|
1 |
|
for ($i = 1; $i <= $this->option('count'); $i++) { |
46
|
1 |
|
$code = $this->getCode(); |
47
|
1 |
|
if (empty($code)) { |
48
|
|
|
continue; |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
$data = [ |
52
|
1 |
|
'coupon' => $this->argument('couponId'), |
53
|
1 |
|
'code' => $code, |
54
|
1 |
|
]; |
55
|
1 |
|
if ($this->option('p_active')) { |
56
|
1 |
|
$data['active'] = !($this->option('p_active') === 'false'); |
57
|
|
|
} |
58
|
1 |
|
if ($this->option('p_customer')) { |
59
|
1 |
|
$data['customer'] = $this->option('p_customer'); |
60
|
|
|
} |
61
|
1 |
|
if ($this->option('p_expires_at')) { |
62
|
1 |
|
$data['expires_at'] = $this->option('p_expires_at'); |
63
|
|
|
} |
64
|
1 |
|
if ($this->option('p_max_redemptions')) { |
65
|
1 |
|
$data['max_redemptions'] = $this->option('p_max_redemptions'); |
66
|
|
|
} |
67
|
1 |
|
$promotionCode = $this->stripeClient()->promotionCodes->create($data); |
68
|
1 |
|
$this->info("Promotion code [{$promotionCode->code}]({$promotionCode->id}]) created."); |
69
|
1 |
|
$createdCount++; |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
$this->info("Totally created: {$createdCount} codes."); |
73
|
|
|
|
74
|
1 |
|
return 0; |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
public function getCode(): string |
78
|
|
|
{ |
79
|
1 |
|
$charsCount = 0; |
80
|
1 |
|
$option = $this->option('chars-count'); |
81
|
1 |
|
if ($option && is_numeric($option)) { |
82
|
1 |
|
$charsCount = (int)$option; |
83
|
|
|
} |
84
|
1 |
|
$pattern = '$'; |
85
|
1 |
|
$option = $this->option('pattern'); |
86
|
1 |
|
if (!empty($option) && is_string($option)) { |
87
|
1 |
|
$pattern = $option; |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
$code = Str::replace('$', Str::upper(Str::random($charsCount)), $pattern); |
91
|
|
|
|
92
|
1 |
|
if ($code) { |
93
|
1 |
|
$codes = $this->stripeClient()->promotionCodes->all([ |
94
|
1 |
|
'code' => $code, |
95
|
1 |
|
]); |
96
|
1 |
|
if (!$codes->isEmpty()) { |
97
|
|
|
$this->error("Code {$code} already exists."); |
98
|
|
|
|
99
|
|
|
return ''; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
1 |
|
return $code; |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
public function stripeClient(): StripeClient |
107
|
|
|
{ |
108
|
1 |
|
if ($this->cachedStripeClient) { |
109
|
1 |
|
return $this->cachedStripeClient; |
110
|
|
|
} |
111
|
|
|
|
112
|
1 |
|
$params = []; |
113
|
1 |
|
if ($key = $this->option('stripe-secret')) { |
114
|
|
|
$params['api_key'] = $key; |
115
|
|
|
} |
116
|
|
|
|
117
|
1 |
|
return $this->cachedStripeClient = app(Cashier::class)::stripe($params); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|