WebhookHandlers
4 months ago
APIClass.php
3 years ago
Helpers.php
1 month ago
PaymentHelpers.php
3 months ago
Stripe.php
3 months ago
WebhookHelpers.php
8 months ago
index.php
3 years ago
Helpers.php
303 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\Membership\PaymentMethods\Stripe; |
| 4 | |
| 5 | use WP_Error; |
| 6 | |
| 7 | class Helpers |
| 8 | { |
| 9 | public static function check_keys_exist() |
| 10 | { |
| 11 | $secret_key = self::get_secret_key(); |
| 12 | $publishable_key = self::get_publishable_key(); |
| 13 | |
| 14 | if ( ! empty($secret_key) && ! empty($publishable_key)) { |
| 15 | return true; |
| 16 | } |
| 17 | |
| 18 | return false; |
| 19 | } |
| 20 | |
| 21 | public static function get_publishable_key() |
| 22 | { |
| 23 | $mode = ppress_is_test_mode() ? 'test' : 'live'; |
| 24 | |
| 25 | return ppress_var( |
| 26 | get_option(PPRESS_PAYMENT_METHODS_OPTION_NAME, []), |
| 27 | 'stripe_' . $mode . '_publishable_key', |
| 28 | false, |
| 29 | true |
| 30 | ); |
| 31 | } |
| 32 | |
| 33 | public static function get_secret_key() |
| 34 | { |
| 35 | $mode = ppress_is_test_mode() ? 'test' : 'live'; |
| 36 | |
| 37 | return ppress_var( |
| 38 | get_option(PPRESS_PAYMENT_METHODS_OPTION_NAME, []), |
| 39 | 'stripe_' . $mode . '_secret_key', |
| 40 | false, |
| 41 | true |
| 42 | ); |
| 43 | } |
| 44 | |
| 45 | public static function get_webhook_secret() |
| 46 | { |
| 47 | $mode = ppress_is_test_mode() ? 'test' : 'live'; |
| 48 | |
| 49 | return ppress_var( |
| 50 | get_option(PPRESS_PAYMENT_METHODS_OPTION_NAME, []), |
| 51 | 'stripe_' . $mode . '_webhook_secret', |
| 52 | false, |
| 53 | true |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | public static function get_account_user_id() |
| 58 | { |
| 59 | $mode = ppress_is_test_mode() ? 'test' : 'live'; |
| 60 | |
| 61 | return ppress_var( |
| 62 | get_option(PPRESS_PAYMENT_METHODS_OPTION_NAME, []), |
| 63 | 'stripe_' . $mode . '_user_id', |
| 64 | false, |
| 65 | true |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | public static function get_connect_url($redirect_url = '') |
| 70 | { |
| 71 | if (empty($redirect_url)) { |
| 72 | $redirect_url = PPRESS_SETTINGS_SETTING_GENERAL_PAGE; |
| 73 | } |
| 74 | |
| 75 | return add_query_arg( |
| 76 | [ |
| 77 | 'mode' => ppress_is_test_mode() ? 'test' : 'live', |
| 78 | 'ppnonce' => wp_create_nonce('ppress_stripe_auth'), |
| 79 | 'redirect_url' => urlencode($redirect_url), |
| 80 | ], |
| 81 | 'https://auth.profilepress.com/stripe' |
| 82 | ); |
| 83 | } |
| 84 | |
| 85 | public static function get_disconnect_url($admin_url) |
| 86 | { |
| 87 | return add_query_arg( |
| 88 | array( |
| 89 | 'ppress-stripe-disconnect' => 'true', |
| 90 | 'ppnonce' => wp_create_nonce('ppress_stripe_disconnect') |
| 91 | ), |
| 92 | $admin_url |
| 93 | ); |
| 94 | } |
| 95 | |
| 96 | public static function get_connect_button($redirect_url = '') |
| 97 | { |
| 98 | $url = self::get_connect_url($redirect_url); |
| 99 | |
| 100 | ob_start(); |
| 101 | ?> |
| 102 | <a href="<?php echo esc_url($url); ?>" aria-label="<?php echo esc_attr__('Connect with Stripe', 'wp-user-avatar'); ?>" class="ppress-stripe-connect"> |
| 103 | <span><?php esc_html_e('Connect with', 'wp-user-avatar'); ?></span> |
| 104 | <svg width="49" height="20" fill="none" xmlns="http://www.w3.org/2000/svg"> |
| 105 | <path fill-rule="evenodd" clip-rule="evenodd" d="M48.4718 10.3338c0-3.41791-1.6696-6.11484-4.8607-6.11484-3.2045 0-5.1434 2.69693-5.1434 6.08814 0 4.0187 2.289 6.048 5.5743 6.048 1.6023 0 2.8141-.3604 3.7296-.8678v-2.6702c-.9155.4539-1.9658.7343-3.2987.7343-1.3061 0-2.464-.4539-2.6121-2.0294h6.5841c0-.1735.0269-.8678.0269-1.1882Zm-6.6514-1.26838c0-1.50868.929-2.13618 1.7773-2.13618.8213 0 1.6965.6275 1.6965 2.13618h-3.4738Zm-8.5499-4.84646c-1.3195 0-2.1678.61415-2.639 1.04139l-.1751-.82777h-2.9621V20l3.3661-.7076.0134-3.7784c.4847.3471 1.1984.8411 2.3832.8411 2.4102 0 4.6048-1.9225 4.6048-6.1548-.0134-3.87186-2.235-5.98134-4.5913-5.98134Zm-.8079 9.19894c-.7944 0-1.2656-.2804-1.5888-.6275l-.0134-4.95328c.35-.38719.8348-.65421 1.6022-.65421 1.2253 0 2.0735 1.36182 2.0735 3.11079 0 1.7891-.8347 3.1242-2.0735 3.1242Zm-9.6001-9.98666 3.3796-.72096V0l-3.3796.70761v2.72363Zm0 1.01469h3.3796V16.1282h-3.3796V4.44593Zm-3.6219.98798-.2154-.98798h-2.9083V16.1282h3.3661V8.21095c.7944-1.02804 2.1408-.84112 2.5582-.69426V4.44593c-.4309-.16022-2.0062-.45394-2.8006.98798Zm-6.7322-3.88518-3.2853.69426-.01346 10.69421c0 1.976 1.49456 3.4313 3.48726 3.4313 1.1041 0 1.912-.2003 2.3563-.4406v-2.7103c-.4309.1736-2.5583.7877-2.5583-1.1882V7.28972h2.5583V4.44593h-2.5583l.0135-2.8972ZM3.40649 7.83712c0-.5207.43086-.72096 1.14447-.72096 1.0233 0 2.31588.30707 3.33917.85447V4.83311c-1.11755-.44059-2.22162-.61415-3.33917-.61415C1.81769 4.21896 0 5.63418 0 7.99733c0 3.68487 5.11647 3.09747 5.11647 4.68627 0 .6141-.53858.8144-1.29258.8144-1.11755 0-2.54477-.4539-3.675782-1.0681v3.1776c1.252192.534 2.517842.761 3.675782.761 2.80059 0 4.72599-1.3752 4.72599-3.765-.01346-3.97867-5.14339-3.27106-5.14339-4.76638Z" fill="#fff"/> |
| 106 | </svg> |
| 107 | </a> |
| 108 | |
| 109 | <style> |
| 110 | .ppress-stripe-connect { |
| 111 | color: #fff !important; |
| 112 | font-size: 15px; |
| 113 | font-weight: bold; |
| 114 | text-decoration: none !important; |
| 115 | line-height: 1; |
| 116 | background-color: #635bff; |
| 117 | border-radius: 3px; |
| 118 | padding: 10px 20px; |
| 119 | display: inline-flex; |
| 120 | align-items: center; |
| 121 | } |
| 122 | |
| 123 | .ppress-stripe-connect:focus, |
| 124 | .ppress-stripe-connect:hover { |
| 125 | color: #fff; |
| 126 | background-color: #0a2540; |
| 127 | } |
| 128 | |
| 129 | .ppress-stripe-connect:focus { |
| 130 | outline: 0; |
| 131 | box-shadow: inset 0 0 0 1px #fff, 0 0 0 1.5px #0a2540; |
| 132 | } |
| 133 | |
| 134 | .ppress-stripe-connect svg { |
| 135 | margin-left: 5px; |
| 136 | } |
| 137 | </style> |
| 138 | |
| 139 | <?php |
| 140 | return ob_get_clean(); |
| 141 | } |
| 142 | |
| 143 | public static function get_account_information($redirect_url) |
| 144 | { |
| 145 | $unknown_error = esc_html__('Unable to retrieve account information.', 'wp-user-avatar'); |
| 146 | |
| 147 | $connect = sprintf( |
| 148 | '<div style="margin-top: 8px;">%s</div>', |
| 149 | Helpers::get_connect_button($redirect_url) |
| 150 | ); |
| 151 | |
| 152 | $access_string = __('You cannot manage this account in Stripe.', 'wp-user-avatar'); |
| 153 | |
| 154 | $dev_account_error = sprintf( |
| 155 | /* translators: %1$s Opening strong tag, do not translate. %2$s Closing anchor tag, do not translate. */ |
| 156 | __( |
| 157 | 'You are currently connected to a %1$stemporary%2$s Stripe account, which can only be used for testing purposes.', |
| 158 | 'wp-user-avatar' |
| 159 | ), |
| 160 | '<strong>', |
| 161 | '</strong>' |
| 162 | ) . ' ' . $access_string; |
| 163 | |
| 164 | $account_id = Helpers::get_account_user_id(); |
| 165 | |
| 166 | $secret_key = Helpers::get_secret_key(); |
| 167 | $publishable_key = Helpers::get_publishable_key(); |
| 168 | $key_errors = new WP_Error(); |
| 169 | |
| 170 | // Publishable Key being used for Secret Key. |
| 171 | if ('pk_' === substr($secret_key, 0, 3)) { |
| 172 | $key_errors->add( |
| 173 | 'ppress_stripe_sk_mismatch', |
| 174 | __( |
| 175 | 'Invalid Secret Key. Secret Key should begin with <code>sk_</code>.', |
| 176 | 'wp-user-avatar' |
| 177 | ) |
| 178 | ); |
| 179 | } |
| 180 | |
| 181 | // Secret Key being used for Publishable Key. |
| 182 | if ('sk_' === substr($publishable_key, 0, 3)) { |
| 183 | $key_errors->add( |
| 184 | 'ppress_stripe_pk_mismatch', |
| 185 | __( |
| 186 | 'Invalid Publishable Key. Publishable Key should begin with <code>pk_</code>.', |
| 187 | 'wp-user-avatar' |
| 188 | ) |
| 189 | ); |
| 190 | } |
| 191 | |
| 192 | if (ppress_is_test_mode()) { |
| 193 | // Live Mode Publishable Key used in Test Mode Publishable Key. |
| 194 | if ('pk_live_' === substr($publishable_key, 0, 8)) { |
| 195 | $key_errors->add( |
| 196 | 'ppress_stripe_pk_mode_mismatch', |
| 197 | __( |
| 198 | 'Invalid Publishable Key for current mode. Publishable Key should begin with <code>pk_test_</code>.', |
| 199 | 'wp-user-avatar' |
| 200 | ) |
| 201 | ); |
| 202 | } |
| 203 | |
| 204 | // Live Mode Secret Key used in Test Mode Secret Key. |
| 205 | if ('sk_live_' === substr($secret_key, 0, 8)) { |
| 206 | $key_errors->add( |
| 207 | 'ppress_stripe_sk_mode_mismatch', |
| 208 | __( |
| 209 | 'Invalid Secret Key for current mode. Secret Key should begin with <code>sk_test_</code>.', |
| 210 | 'wp-user-avatar' |
| 211 | ) |
| 212 | ); |
| 213 | } |
| 214 | } else { |
| 215 | // Test Mode Secret Key used in Live Mode Secret Key. |
| 216 | if ('pk_test_' === substr($publishable_key, 0, 8)) { |
| 217 | $key_errors->add( |
| 218 | 'ppress_stripe_pk_mode_mismatch', |
| 219 | __( |
| 220 | 'Invalid Publishable Key for current mode. Publishable Key should begin with <code>pk_live_</code>.', |
| 221 | 'wp-user-avatar' |
| 222 | ) |
| 223 | ); |
| 224 | } |
| 225 | |
| 226 | // Test Mode Secret Key used in Live Mode Secret Key. |
| 227 | if ('sk_test_' === substr($secret_key, 0, 8)) { |
| 228 | $key_errors->add( |
| 229 | 'ppress_stripe_sk_mode_mismatch', |
| 230 | __( |
| 231 | 'Invalid Secret Key for current mode. Secret Key should begin with <code>sk_live_</code>.', |
| 232 | 'wp-user-avatar' |
| 233 | ) |
| 234 | ); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | if ( ! empty($key_errors->errors)) { |
| 239 | return sprintf( |
| 240 | '<span style="color: red;">%s</span> %s %s', |
| 241 | $key_errors->get_error_message(), |
| 242 | __( |
| 243 | 'If you have manually modified these values after connecting your account, please reconnect below or update your API keys manually.', |
| 244 | 'wp-user-avatar' |
| 245 | ), |
| 246 | $connect |
| 247 | ); |
| 248 | } |
| 249 | |
| 250 | // Stripe Connect. |
| 251 | if ( ! empty($account_id)) { |
| 252 | |
| 253 | try { |
| 254 | |
| 255 | $account = (new APIClass())->get_account($account_id); |
| 256 | |
| 257 | if ( ! ppress_get_payment_method_setting('stripe_connect_account_country')) { |
| 258 | ppress_update_payment_method_setting('stripe_connect_account_country', $account['country']); |
| 259 | } |
| 260 | |
| 261 | $email = isset($account['email']) ? $account['email'] : ''; |
| 262 | $display_name = isset($account['display_name']) ? $account['display_name'] : ''; |
| 263 | |
| 264 | if (empty($display_name)) { |
| 265 | if (isset($account['settings']['dashboard']['display_name'])) { |
| 266 | $display_name = $account['settings']['dashboard']['display_name']; |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | if (empty($email) && empty($display_name)) { |
| 271 | return $dev_account_error; |
| 272 | } |
| 273 | |
| 274 | if ( ! empty($display_name)) { |
| 275 | $display_name = '<strong>' . $display_name . '</strong>'; |
| 276 | } |
| 277 | |
| 278 | if ( ! empty($email) || ! empty($display_name)) { |
| 279 | $email_string = ! empty($email) ? $email . ' — ' : ''; |
| 280 | $email = '<br>' . $email_string . esc_html__('Administrator (Owner)', 'wp-user-avatar'); |
| 281 | } |
| 282 | |
| 283 | $message = $display_name . $email; |
| 284 | |
| 285 | $fee_message = (PaymentHelpers::has_application_fee()) |
| 286 | ? '</p><p>' . sprintf( |
| 287 | esc_html__( |
| 288 | 'Pay as you go pricing: 2%% per-transaction fee + Stripe fees. Remove the 2%% fee by %supgrading to premium%s.', |
| 289 | 'wp-user-avatar' |
| 290 | ), |
| 291 | '<a target="_blank" href="https://profilepress.com/pricing/?utm_source=wp_dashboard&utm_medium=upgrade&utm_campaign=stripe-gateway-method">', |
| 292 | '</a>' |
| 293 | ) |
| 294 | : ''; |
| 295 | |
| 296 | return apply_filters('ppress_stripe_connect_account_message', $message . $fee_message); |
| 297 | |
| 298 | } catch (\Exception $e) { |
| 299 | return $unknown_error; |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | } |