AccountDetail.php
99 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\PaymentGateways\Stripe\Repositories; |
| 4 | |
| 5 | use Give\Framework\Exceptions\Primitives\InvalidArgumentException; |
| 6 | use Give\PaymentGateways\Exceptions\InvalidPropertyName; |
| 7 | use Give\PaymentGateways\Stripe\Models\AccountDetail as AccountDetailModel; |
| 8 | |
| 9 | /** |
| 10 | * Class AccountDetail |
| 11 | * |
| 12 | * @package Give\PaymentGateways\Stripe\Repository |
| 13 | * @since 2.10.2 |
| 14 | */ |
| 15 | class AccountDetail |
| 16 | { |
| 17 | /** |
| 18 | * Return Stripe account id for donation form. |
| 19 | * |
| 20 | * @since 2.10.2 |
| 21 | * |
| 22 | * @param int $formId |
| 23 | * |
| 24 | * @return AccountDetailModel |
| 25 | * @throws InvalidPropertyName |
| 26 | */ |
| 27 | public function getDonationFormStripeAccountId($formId) |
| 28 | { |
| 29 | $formHasStripeAccount = give_is_setting_enabled(give_get_meta($formId, 'give_stripe_per_form_accounts', true)); |
| 30 | if ($formId > 0 && $formHasStripeAccount) { |
| 31 | // Return default Stripe account of the form, if enabled. |
| 32 | $accountId = give_get_meta($formId, '_give_stripe_default_account', true); |
| 33 | } else { |
| 34 | // Global Stripe account. |
| 35 | $accountId = give_get_option('_give_stripe_default_account', ''); |
| 36 | } |
| 37 | |
| 38 | return $this->getAccountDetail($accountId); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Get account detail by Stripe account id. |
| 43 | * |
| 44 | * @since 2.10.2 |
| 45 | * |
| 46 | * @param string $accountId |
| 47 | * |
| 48 | * @return AccountDetailModel |
| 49 | * @throws InvalidPropertyName |
| 50 | */ |
| 51 | public function getAccountDetail($accountId) |
| 52 | { |
| 53 | $accountDetail = array_filter( |
| 54 | give_stripe_get_all_accounts(), |
| 55 | static function ($data) use ($accountId) { |
| 56 | return $data['account_id'] === $accountId; |
| 57 | } |
| 58 | ); |
| 59 | |
| 60 | $accountDetail = $accountDetail ? current($accountDetail) : $accountDetail; |
| 61 | |
| 62 | return new AccountDetailModel($accountDetail); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Get account detail by Stripe account slug. |
| 67 | * |
| 68 | * @since 2.13.3 |
| 69 | * |
| 70 | * @param string $accountSlug |
| 71 | * |
| 72 | * @return AccountDetailModel |
| 73 | * @throws InvalidArgumentException|InvalidPropertyName |
| 74 | */ |
| 75 | public function getAccountDetailBySlug($accountSlug) |
| 76 | { |
| 77 | $accountDetail = array_filter( |
| 78 | give_stripe_get_all_accounts(), |
| 79 | static function ($data) use ($accountSlug) { |
| 80 | return $data['account_slug'] === $accountSlug; |
| 81 | } |
| 82 | ); |
| 83 | |
| 84 | if ( ! $accountDetail) { |
| 85 | throw new InvalidArgumentException( |
| 86 | sprintf( |
| 87 | 'Stripe account with %s account slug does not exist', |
| 88 | $accountSlug |
| 89 | ) |
| 90 | ); |
| 91 | } |
| 92 | |
| 93 | $accountDetail = current($accountDetail); |
| 94 | |
| 95 | return new AccountDetailModel($accountDetail); |
| 96 | } |
| 97 | |
| 98 | } |
| 99 |