DisconnectStripeAccountController.php
4 years ago
GetStripeAccountDetailsController.php
4 years ago
NewStripeAccountOnBoardingController.php
4 years ago
SetDefaultStripeAccountController.php
4 years ago
NewStripeAccountOnBoardingController.php
150 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\PaymentGateways\Stripe\Controllers; |
| 4 | |
| 5 | use Give\PaymentGateways\Stripe\DataTransferObjects\NewStripeAccountOnBoardingDto; |
| 6 | use Give\PaymentGateways\Stripe\Models\AccountDetail as AccountDetailModel; |
| 7 | use Give\PaymentGateways\Stripe\Repositories\Settings; |
| 8 | use Give_Admin_Settings; |
| 9 | use Stripe\Stripe; |
| 10 | |
| 11 | /** |
| 12 | * Class NewStripeAccountOnBoardingController |
| 13 | * @package Give\PaymentGateways\Stripe\Controllers |
| 14 | * |
| 15 | * @since 2.13.0 |
| 16 | */ |
| 17 | class NewStripeAccountOnBoardingController |
| 18 | { |
| 19 | /** |
| 20 | * @var Settings |
| 21 | */ |
| 22 | private $settings; |
| 23 | |
| 24 | /** |
| 25 | * @param Settings $settings |
| 26 | */ |
| 27 | public function __construct(Settings $settings) |
| 28 | { |
| 29 | $this->settings = $settings; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * @since 2.13.0 |
| 34 | */ |
| 35 | public function __invoke() |
| 36 | { |
| 37 | if (!current_user_can('manage_give_settings')) { |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | $requestedData = NewStripeAccountOnBoardingDto::fromArray(give_clean($_GET)); |
| 42 | |
| 43 | if (!$requestedData->hasValidateData()) { |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | $stripe_accounts = give_stripe_get_all_accounts(); |
| 48 | $secret_key = !give_is_test_mode() ? $requestedData->stripeAccessToken : $requestedData->stripeAccessTokenTest; |
| 49 | |
| 50 | Stripe::setApiKey($secret_key); |
| 51 | |
| 52 | // Get Account Details. |
| 53 | $account_details = give_stripe_get_account_details($requestedData->stripeUserId); |
| 54 | |
| 55 | // Setup Account Details for Connected Stripe Accounts. |
| 56 | if (empty($account_details->id)) { |
| 57 | Give_Admin_Settings::add_error( |
| 58 | 'give-stripe-account-id-fetching-error', |
| 59 | sprintf( |
| 60 | '<strong>%1$s</strong> %2$s', |
| 61 | esc_html__('Stripe Error:', 'give'), |
| 62 | esc_html__( |
| 63 | 'We are unable to connect your Stripe account. Please contact the support team for assistance.', |
| 64 | 'give' |
| 65 | ) |
| 66 | ) |
| 67 | ); |
| 68 | |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | $account_name = !empty($account_details->business_profile->name) ? |
| 73 | $account_details->business_profile->name : |
| 74 | $account_details->settings->dashboard->display_name; |
| 75 | $account_slug = $account_details->id; |
| 76 | $account_email = $account_details->email; |
| 77 | $account_country = $account_details->country; |
| 78 | |
| 79 | // Set first Stripe account as default. |
| 80 | if (!$stripe_accounts) { |
| 81 | give_update_option('_give_stripe_default_account', $account_slug); |
| 82 | } |
| 83 | |
| 84 | try { |
| 85 | $accountDetailModel = AccountDetailModel::fromArray( |
| 86 | [ |
| 87 | 'type' => 'connect', |
| 88 | 'account_name' => $account_name, |
| 89 | 'account_slug' => $account_slug, |
| 90 | 'account_email' => $account_email, |
| 91 | 'account_country' => $account_country, |
| 92 | 'account_id' => $requestedData->stripeUserId, |
| 93 | 'live_secret_key' => $requestedData->stripeAccessToken, |
| 94 | 'test_secret_key' => $requestedData->stripeAccessTokenTest, |
| 95 | 'live_publishable_key' => $requestedData->stripePublishableKey, |
| 96 | 'test_publishable_key' => $requestedData->stripePublishableKeyTest, |
| 97 | 'statement_descriptor' => $account_details->settings->payments->statement_descriptor, |
| 98 | ] |
| 99 | ); |
| 100 | |
| 101 | $this->settings->addNewStripeAccount($accountDetailModel); |
| 102 | |
| 103 | if ($requestedData->formId) { |
| 104 | if (!Settings::getDefaultStripeAccountSlugForDonationForm($requestedData->formId)) { |
| 105 | Settings::setDefaultStripeAccountSlugForDonationForm( |
| 106 | $requestedData->formId, |
| 107 | $accountDetailModel->accountSlug |
| 108 | ); |
| 109 | } |
| 110 | |
| 111 | give()->form_meta->update_meta( |
| 112 | $requestedData->formId, |
| 113 | 'give_stripe_per_form_accounts', |
| 114 | 'enabled' |
| 115 | ); |
| 116 | } |
| 117 | |
| 118 | wp_redirect( |
| 119 | esc_url_raw( |
| 120 | add_query_arg( |
| 121 | ['stripe_account' => 'connected'], |
| 122 | $requestedData->formId ? |
| 123 | admin_url( |
| 124 | "post.php?post=$requestedData->formId&action=edit&give_tab=stripe_form_account_options" |
| 125 | ) : |
| 126 | admin_url( |
| 127 | 'edit.php?post_type=give_forms&page=give-settings&tab=gateways§ion=stripe-settings' |
| 128 | ) |
| 129 | ) |
| 130 | ) |
| 131 | ); |
| 132 | exit(); |
| 133 | } catch (\Exception $e) { |
| 134 | Give_Admin_Settings::add_error( |
| 135 | 'give-stripe-account-on-boarding-error', |
| 136 | sprintf( |
| 137 | '<strong>%1$s</strong> %2$s', |
| 138 | esc_html__('Stripe Error:', 'give'), |
| 139 | esc_html__( |
| 140 | 'We are unable to connect your Stripe account. Please contact the support team for assistance.', |
| 141 | 'give' |
| 142 | ) |
| 143 | ) |
| 144 | ); |
| 145 | |
| 146 | return; |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 |