PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.16.9
GiveWP – Donation Plugin and Fundraising Platform v4.16.9
4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 All 255 releases
give / src / PaymentGateways / Stripe / Repositories / Settings.php

Settings.php in GiveWP – Donation Plugin and Fundraising Platform 4.16.9, at src/PaymentGateways/Stripe/Repositories/Settings.php

207 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\Exceptions\DuplicateStripeAccountName;
8 use Give\PaymentGateways\Stripe\Exceptions\StripeAccountAlreadyConnected;
9 use Give\PaymentGateways\Stripe\Models\AccountDetail as AccountDetailModel;
10
11 use Stripe\Account;
12
13 use function esc_html__;
14 use function give_stripe_get_all_accounts;
15 use function give_update_option;
16
17 /**
18 * Class Settings
19 * @package GiveStripe\Settings\Repositories
20 *
21 * @since 2.13.0
22 */
23 class Settings
24 {
25 /**
26 * @since 2.13.0
27 * @return array
28 */
29 public function getAllStripeAccounts()
30 {
31 return give_stripe_get_all_accounts();
32 }
33
34 /**
35 * @since 2.19.0
36 *
37 * @param string $stripeAccountId
38 *
39 * @return AccountDetailModel
40 * @throws InvalidPropertyName|InvalidArgumentException
41 */
42 public function getStripeAccountById($stripeAccountId)
43 {
44 $stripeAccounts = give_stripe_get_all_accounts();
45
46 if (array_key_exists($stripeAccountId, $stripeAccounts)) {
47 return AccountDetailModel::fromArray($stripeAccounts[$stripeAccountId]);
48 }
49
50
51 throw new InvalidArgumentException( esc_html__( 'Invalid Stripe account id.', 'give') );
52 }
53
54 /**
55 * @since 2.13.0
56 *
57 * @param int $formId
58 *
59 * @return bool|mixed|string
60 */
61 public static function getDefaultStripeAccountSlugForDonationForm($formId)
62 {
63 return give()->form_meta->get_meta($formId, '_give_stripe_default_account', true);
64 }
65
66 /**
67 * @since 2.13.0
68 *
69 * @param int $formId
70 * @param $stripeAccountSlug
71 *
72 * @return bool
73 */
74 public static function setDefaultStripeAccountSlugForDonationForm($formId, $stripeAccountSlug)
75 {
76 return give()->form_meta->update_meta($formId, '_give_stripe_default_account', $stripeAccountSlug);
77 }
78
79 /**
80 * @since 2.13.0
81 * @return string
82 */
83 public function getDefaultStripeAccountSlug()
84 {
85 return give_stripe_get_default_account_slug();
86 }
87
88 /**
89 * @since 2.13.0
90 * @return bool
91 */
92 public function hasDefaultGlobalStripeAccountSlug()
93 {
94 return (bool)$this->getDefaultStripeAccountSlug();
95 }
96
97 /**
98 * @since 2.13.0
99 *
100 * @param string $accountSlug
101 *
102 * @return bool
103 */
104 public function setDefaultGlobalStripeAccountSlug($accountSlug)
105 {
106 return give_update_option('_give_stripe_default_account', $accountSlug);
107 }
108
109 /**
110 * @since 2.13.0
111 * @throws StripeAccountAlreadyConnected
112 * @throws DuplicateStripeAccountName|InvalidPropertyName
113 */
114 public function addNewStripeAccount(AccountDetailModel $stripeAccount)
115 {
116 $allAccounts = give_stripe_get_all_accounts();
117 $accountSlug = $stripeAccount->accountSlug;
118
119 if (!$this->isUniqueAccountName($stripeAccount->accountName, $allAccounts)) {
120 throw new DuplicateStripeAccountName(esc_html__('Stripe account already exist with same name.', 'give'));
121 }
122
123 if (
124 array_key_exists($accountSlug, $allAccounts) ||
125 $this->isAccountAlreadyConnected($stripeAccount, $allAccounts)
126 ) {
127 throw new StripeAccountAlreadyConnected(esc_html__('Stripe account already connected', 'give'));
128 }
129
130 $allAccounts[$accountSlug] = $stripeAccount->toArray();
131
132 return give_update_option('_give_stripe_get_all_accounts', $allAccounts);
133 }
134
135 /**
136 * @since 2.13.0
137 */
138 public function updateStripeAccount(AccountDetailModel $stripeAccount)
139 {
140 $allAccounts = give_stripe_get_all_accounts();
141 $accountSlug = $stripeAccount->accountSlug;
142
143 if (array_key_exists($accountSlug, $allAccounts)) {
144 $accountDetails = $stripeAccount->toArray();
145
146 // account_id, account_slug are unique value which used to reference to connect stripe account.
147 // They can not be renamed.
148 unset($accountDetails['account_id'], $accountDetails['account_slug']);
149
150 $allAccounts[$accountSlug] = $stripeAccount->toArray();
151
152 return give_update_option('_give_stripe_get_all_accounts', $allAccounts);
153 }
154
155 return false;
156 }
157
158 /**
159 * @since 2.13.0
160 *
161 * @param AccountDetailModel $stripeAccount
162 * @param array $allAccounts
163 *
164 * @return bool
165 * @throws InvalidPropertyName
166 */
167 public function isAccountAlreadyConnected(AccountDetailModel $stripeAccount, $allAccounts)
168 {
169 foreach ($allAccounts as $account) {
170 $savedStripeAccount = AccountDetailModel::fromArray($account);
171
172 if (
173 $savedStripeAccount->liveSecretKey === $stripeAccount->liveSecretKey &&
174 $savedStripeAccount->livePublishableKey === $stripeAccount->livePublishableKey &&
175 $savedStripeAccount->testSecretKey === $stripeAccount->testSecretKey &&
176 $savedStripeAccount->testPublishableKey === $stripeAccount->testPublishableKey
177 ) {
178 return true;
179 }
180 }
181
182 return false;
183 }
184
185 /**
186 * @since 2.13.0
187 *
188 * @param string $stripeAccountName
189 * @param array $allAccounts
190 *
191 * @return bool
192 * @throws InvalidPropertyName
193 */
194 public function isUniqueAccountName($stripeAccountName, $allAccounts)
195 {
196 foreach ($allAccounts as $account) {
197 $savedStripeAccount = AccountDetailModel::fromArray($account);
198
199 if ($savedStripeAccount->accountName === $stripeAccountName) {
200 return false;
201 }
202 }
203
204 return true;
205 }
206 }
207