AccountDetail.php
166 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\PaymentGateways\Stripe\Models; |
| 4 | |
| 5 | use Exception; |
| 6 | use Give\Helpers\ArrayDataSet; |
| 7 | use Give\PaymentGateways\Exceptions\InvalidPropertyName; |
| 8 | use Give\PaymentGateways\Stripe\Traits\HasStripeStatementDescriptorText; |
| 9 | |
| 10 | /** |
| 11 | * Class AccountDetail |
| 12 | * |
| 13 | * @package Give\PaymentGateways\Stripe\Models |
| 14 | * @since 2.10.2 |
| 15 | * |
| 16 | * @property-read string $type |
| 17 | * @property-read string $accountName |
| 18 | * @property-read string $accountSlug |
| 19 | * @property-read string $accountEmail |
| 20 | * @property-read string $accountCountry |
| 21 | * @property-read string $accountId |
| 22 | * @property-read string $liveSecretKey |
| 23 | * @property-read string $livePublishableKey |
| 24 | * @property-read string $testSecretKey |
| 25 | * @property-read string $testPublishableKey |
| 26 | * @property-read string $statementDescriptor |
| 27 | */ |
| 28 | class AccountDetail |
| 29 | { |
| 30 | use HasStripeStatementDescriptorText; |
| 31 | |
| 32 | protected $args; |
| 33 | protected $propertiesArgs; |
| 34 | protected $requiredArgs = [ |
| 35 | 'type', |
| 36 | 'account_name', |
| 37 | 'account_slug', |
| 38 | 'account_email', |
| 39 | 'account_country', |
| 40 | 'account_id', |
| 41 | 'live_secret_key', |
| 42 | 'live_publishable_key', |
| 43 | 'test_secret_key', |
| 44 | 'test_publishable_key', |
| 45 | 'statement_descriptor', |
| 46 | ]; |
| 47 | |
| 48 | /** |
| 49 | * AccountDetail constructor. |
| 50 | * |
| 51 | * @since 2.10.2 |
| 52 | * |
| 53 | * @param array $args |
| 54 | * |
| 55 | * @throws InvalidPropertyName |
| 56 | */ |
| 57 | final public function __construct(array $args) |
| 58 | { |
| 59 | $this->args = $args; |
| 60 | $args = $this->addSupportFormNewStatementDescriptorParam($args); |
| 61 | $this->propertiesArgs = ArrayDataSet::camelCaseKeys($args); |
| 62 | $this->validate($args); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * @since 2.13.0 |
| 67 | * @throws InvalidPropertyName |
| 68 | */ |
| 69 | public static function fromArray($array) |
| 70 | { |
| 71 | return new static($array); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @since 2.13.0 |
| 76 | * |
| 77 | * @return array |
| 78 | */ |
| 79 | public function toArray() |
| 80 | { |
| 81 | return [ |
| 82 | 'type' => $this->type, |
| 83 | 'account_id' => $this->accountId, |
| 84 | 'account_slug' => $this->accountSlug, |
| 85 | 'account_name' => $this->accountName, |
| 86 | 'account_country' => $this->accountCountry, |
| 87 | 'account_email' => $this->accountEmail, |
| 88 | 'live_secret_key' => $this->liveSecretKey, |
| 89 | 'test_secret_key' => $this->testSecretKey, |
| 90 | 'live_publishable_key' => $this->livePublishableKey, |
| 91 | 'test_publishable_key' => $this->testPublishableKey, |
| 92 | 'statement_descriptor' => $this->statementDescriptor, |
| 93 | ]; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @since 2.10.2 |
| 98 | * |
| 99 | * @param string $key |
| 100 | * |
| 101 | * @return mixed |
| 102 | * @throws InvalidPropertyName |
| 103 | */ |
| 104 | public function __get($key) |
| 105 | { |
| 106 | if ( ! array_key_exists($key, $this->propertiesArgs)) { |
| 107 | throw new InvalidPropertyName( |
| 108 | sprintf( |
| 109 | '$1%s property does not exist in %2$s class', |
| 110 | $key, |
| 111 | __CLASS__ |
| 112 | ) |
| 113 | ); |
| 114 | } |
| 115 | |
| 116 | return $this->propertiesArgs[$key]; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Validate array format. |
| 121 | * |
| 122 | * @since 2.10.2 |
| 123 | * |
| 124 | * @param array $array |
| 125 | * |
| 126 | * @throws InvalidPropertyName |
| 127 | */ |
| 128 | private function validate($array) |
| 129 | { |
| 130 | if (array_diff($this->requiredArgs, array_keys($array))) { |
| 131 | throw new InvalidPropertyName( |
| 132 | sprintf( |
| 133 | 'To create a %1$s object, please provide valid: %2$s', |
| 134 | __CLASS__, |
| 135 | implode(' , ', $this->requiredArgs) |
| 136 | ) |
| 137 | ); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * We decided to define statement descriptor per stripe account. |
| 143 | * Statement descriptor default text for each account will be set to blog title. |
| 144 | * @see : https://github.com/impress-org/givewp/issues/6021 |
| 145 | * |
| 146 | * @since 2.19.0 |
| 147 | * @since 2.19.1 Use old stripe statement descriptor requirements to filter text. |
| 148 | * https://github.com/impress-org/givewp/pull/6269 |
| 149 | * @deprecated |
| 150 | * |
| 151 | * @param array $args |
| 152 | * |
| 153 | * @return array |
| 154 | */ |
| 155 | private function addSupportFormNewStatementDescriptorParam($args) |
| 156 | { |
| 157 | $propertyName = 'statement_descriptor'; |
| 158 | if ( ! array_key_exists($propertyName, $args) || empty($args[$propertyName])) { |
| 159 | $statementDescriptor = give_get_option('stripe_statement_descriptor', get_bloginfo('name')); |
| 160 | $args[$propertyName] = $this->filterOldStatementDescriptor($statementDescriptor); |
| 161 | } |
| 162 | |
| 163 | return $args; |
| 164 | } |
| 165 | } |
| 166 |