AccountDetail.php
91 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 | * Return Stripe account id for donation form. |
| 18 | * |
| 19 | * @param int $formId |
| 20 | * |
| 21 | * @return AccountDetailModel |
| 22 | * @throws InvalidPropertyName |
| 23 | * @since 2.10.2 |
| 24 | */ |
| 25 | public function getDonationFormStripeAccountId( $formId ) { |
| 26 | $formHasStripeAccount = give_is_setting_enabled( give_get_meta( $formId, 'give_stripe_per_form_accounts', true ) ); |
| 27 | if ( $formId > 0 && $formHasStripeAccount ) { |
| 28 | // Return default Stripe account of the form, if enabled. |
| 29 | $accountId = give_get_meta( $formId, '_give_stripe_default_account', true ); |
| 30 | } else { |
| 31 | // Global Stripe account. |
| 32 | $accountId = give_get_option( '_give_stripe_default_account', '' ); |
| 33 | } |
| 34 | |
| 35 | return $this->getAccountDetail( $accountId ); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Get account detail by Stripe account id. |
| 40 | * |
| 41 | * @param string $accountId |
| 42 | * |
| 43 | * @return AccountDetailModel |
| 44 | * @throws InvalidPropertyName |
| 45 | * @since 2.10.2 |
| 46 | */ |
| 47 | public function getAccountDetail( $accountId ) { |
| 48 | $accountDetail = array_filter( |
| 49 | give_stripe_get_all_accounts(), |
| 50 | static function ( $data ) use ( $accountId ) { |
| 51 | return $data['account_id'] === $accountId; |
| 52 | } |
| 53 | ); |
| 54 | |
| 55 | $accountDetail = $accountDetail ? current( $accountDetail ) : $accountDetail; |
| 56 | return new AccountDetailModel( $accountDetail ); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Get account detail by Stripe account slug. |
| 61 | * |
| 62 | * @since 2.13.3 |
| 63 | * |
| 64 | * @param string $accountSlug |
| 65 | * |
| 66 | * @return AccountDetailModel |
| 67 | * @throws InvalidArgumentException|InvalidPropertyName |
| 68 | */ |
| 69 | public function getAccountDetailBySlug( $accountSlug ) { |
| 70 | $accountDetail = array_filter( |
| 71 | give_stripe_get_all_accounts(), |
| 72 | static function ( $data ) use ( $accountSlug ) { |
| 73 | return $data['account_slug'] === $accountSlug; |
| 74 | } |
| 75 | ); |
| 76 | |
| 77 | if ( ! $accountDetail ) { |
| 78 | throw new InvalidArgumentException( |
| 79 | sprintf( |
| 80 | 'Stripe account with %s account slug does not exist', |
| 81 | $accountSlug |
| 82 | ) |
| 83 | ); |
| 84 | } |
| 85 | |
| 86 | $accountDetail = current( $accountDetail ); |
| 87 | return new AccountDetailModel( $accountDetail ); |
| 88 | } |
| 89 | |
| 90 | } |
| 91 |