HasStripeStatementDescriptorText.php
48 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\PaymentGateways\Stripe\Traits; |
| 4 | |
| 5 | use Give\Framework\Exceptions\Primitives\InvalidArgumentException; |
| 6 | |
| 7 | /** |
| 8 | * @since 2.19.0 |
| 9 | */ |
| 10 | trait HasStripeStatementDescriptorText |
| 11 | { |
| 12 | /** |
| 13 | * Return filtered stripe statement descriptor text. |
| 14 | * Check Stripe statement descriptor requirements: https://stripe.com/docs/statement-descriptors#requirements |
| 15 | * |
| 16 | * @since 2.19.0 |
| 17 | * |
| 18 | * @param string $statementDescriptor |
| 19 | */ |
| 20 | protected function validateStatementDescriptor($statementDescriptor) |
| 21 | { |
| 22 | $maxLength = 22; |
| 23 | $minLength = 5; |
| 24 | $unsupportedCharacters = ['<', '>', '"', '\\', '\'', '*']; // Stripe reserve keywords. |
| 25 | |
| 26 | if ($minLength > strlen($statementDescriptor) || $maxLength < strlen($statementDescriptor)) { |
| 27 | throw new InvalidArgumentException( |
| 28 | esc_html__('Stripe statement descriptor text should contain between 5 - 22 letters, inclusive.', 'give') |
| 29 | ); |
| 30 | } |
| 31 | |
| 32 | if (is_numeric($statementDescriptor)) { |
| 33 | throw new InvalidArgumentException( |
| 34 | esc_html__('Stripe statement descriptor text should contain at least one letter.', 'give') |
| 35 | ); |
| 36 | } |
| 37 | |
| 38 | if (array_intersect($unsupportedCharacters, str_split($statementDescriptor))) { |
| 39 | throw new InvalidArgumentException( |
| 40 | __( |
| 41 | 'Stripe statement descriptor text should not contain any of the special characters <code>< > \ \' " *</code>.', |
| 42 | 'give' |
| 43 | ) |
| 44 | ); |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 |