1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This software may be modified and distributed under the terms |
7
|
|
|
* of the MIT license. See the LICENSE file for details. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Shapin\Stripe\Configuration; |
11
|
|
|
|
12
|
|
|
use Shapin\Stripe\Model\Coupon\Coupon; |
13
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
14
|
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface; |
15
|
|
|
|
16
|
|
|
class CouponCreate implements ConfigurationInterface |
17
|
|
|
{ |
18
|
4 |
|
public function getConfigTreeBuilder() |
19
|
|
|
{ |
20
|
4 |
|
$treeBuilder = new TreeBuilder('shapin_stripe_coupon_create'); |
21
|
4 |
|
$rootNode = $treeBuilder->getRootNode(); |
22
|
|
|
|
23
|
|
|
$rootNode |
24
|
4 |
|
->children() |
25
|
4 |
|
->scalarNode('id') |
26
|
4 |
|
->info('Unique string of your choice that will be used to identify this coupon when applying it to a customer. This is often a specific code you’ll give to your customer to use when signing up (e.g., FALL25OFF). If you don’t want to specify a particular code, you can leave the ID blank and we’ll generate a random code for you.') |
27
|
4 |
|
->end() |
28
|
4 |
|
->enumNode('duration') |
29
|
4 |
|
->isRequired() |
30
|
4 |
|
->values([Coupon::DURATION_FOREVER, Coupon::DURATION_ONCE, Coupon::DURATION_REPEATING]) |
31
|
4 |
|
->info('Specifies how long the discount will be in effect. Can be forever, once, or repeating.') |
32
|
4 |
|
->end() |
33
|
4 |
|
->integerNode('amount_off') |
34
|
4 |
|
->info('A positive integer representing the amount to subtract from an invoice total (required if percent_off is not passed).') |
35
|
4 |
|
->end() |
36
|
4 |
|
->scalarNode('currency') |
37
|
4 |
|
->info('Three-letter ISO code for the currency of the amount_off parameter (required if amount_off is passed).') |
38
|
4 |
|
->end() |
39
|
4 |
|
->integerNode('duration_in_months') |
40
|
4 |
|
->info('Required only if duration is repeating, in which case it must be a positive integer that specifies the number of months the discount will be in effect.') |
41
|
4 |
|
->end() |
42
|
4 |
|
->integerNode('max_redemptions') |
43
|
4 |
|
->info('A positive integer specifying the number of times the coupon can be redeemed before it’s no longer valid. For example, you might have a 50% off coupon that the first 20 readers of your blog can use.') |
44
|
4 |
|
->end() |
45
|
4 |
|
->arrayNode('metadata') |
46
|
4 |
|
->scalarPrototype()->end() |
47
|
4 |
|
->info('Set of key-value pairs that you can attach to an object. This can be useful for storing additional information about the object in a structured format. Individual keys can be unset by posting an empty value to them. All keys can be unset by posting an empty value to metadata.') |
48
|
4 |
|
->end() |
49
|
4 |
|
->scalarNode('name') |
50
|
4 |
|
->info('Name of the coupon displayed to customers on, for instance invoices, or receipts. By default the id is shown if name is not set.') |
51
|
4 |
|
->end() |
52
|
4 |
|
->floatNode('percent_off') |
53
|
4 |
|
->info('A positive float larger than 0, and smaller or equal to 100, that represents the discount the coupon will apply (required if amount_off is not passed).') |
54
|
4 |
|
->end() |
55
|
4 |
|
->integerNode('redeem_by') |
56
|
4 |
|
->info('Unix timestamp specifying the last time at which the coupon can be redeemed. After the redeem_by date, the coupon can no longer be applied to new customers.') |
57
|
4 |
|
->end() |
58
|
4 |
|
->end() |
59
|
|
|
; |
60
|
|
|
|
61
|
|
|
$rootNode |
62
|
4 |
|
->validate() |
63
|
|
|
->ifTrue(function ($v) { |
64
|
4 |
|
return Coupon::DURATION_REPEATING === $v['duration'] && !isset($v['duration_in_months']); |
65
|
4 |
|
}) |
66
|
4 |
|
->thenInvalid('"duration_in_months" is required when using "repeating" duration.') |
67
|
4 |
|
->end() |
68
|
4 |
|
->validate() |
69
|
|
|
->ifTrue(function ($v) { |
70
|
3 |
|
return isset($v['amount_off']) && !isset($v['currency']); |
71
|
4 |
|
}) |
72
|
4 |
|
->thenInvalid('"currency" is required when specifying "amount_off".') |
73
|
4 |
|
->end() |
74
|
4 |
|
->validate() |
75
|
|
|
->ifTrue(function ($v) { |
76
|
2 |
|
return !isset($v['amount_off']) && !isset($v['percent_off']); |
77
|
4 |
|
}) |
78
|
4 |
|
->thenInvalid('You must specify either an "amount_off" or an "percent_off".') |
79
|
4 |
|
->end() |
80
|
|
|
; |
81
|
|
|
|
82
|
4 |
|
return $treeBuilder; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|