HasStripeStatementDescriptorText.php
68 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 | /** |
| 49 | * Return filtered statement descriptor. |
| 50 | * This function should be used to filter statement description |
| 51 | * which was storing in stripe_statement_descriptor give setting prior to Giver 2.19. |
| 52 | * |
| 53 | * @since 2.19.1 |
| 54 | * @deprecated |
| 55 | * |
| 56 | * @param string $text |
| 57 | * |
| 58 | * @return false|string |
| 59 | */ |
| 60 | protected function filterOldStatementDescriptor($text) |
| 61 | { |
| 62 | $statementDescriptor = trim($text); |
| 63 | $unsupportedCharacters = ['<', '>', '"', '\'']; |
| 64 | $statementDescriptor = str_replace($unsupportedCharacters, '', $statementDescriptor); |
| 65 | return substr($statementDescriptor, 0, 22); |
| 66 | } |
| 67 | } |
| 68 |