| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Modules\Payments\PaymentMethods\Stripe; |
| 4 |
|
| 5 |
use FluentForm\App\Helpers\Helper; |
| 6 |
use FluentForm\Framework\Helpers\ArrayHelper; |
| 7 |
use FluentForm\App\Modules\Payments\PaymentMethods\Stripe\API\Account; |
| 8 |
|
| 9 |
if (!defined('ABSPATH')) { |
| 10 |
exit; // Exit if accessed directly. |
| 11 |
} |
| 12 |
|
| 13 |
class ConnectConfig |
| 14 |
{ |
| 15 |
private static $connectBase = 'https://apiv2.wpmanageninja.com/fluentform/'; |
| 16 |
|
| 17 |
public static function getConnectConfig() |
| 18 |
{ |
| 19 |
$configBase = self::$connectBase . 'stripe-connect'; |
| 20 |
$hash = md5(site_url() . wp_generate_uuid4() . time()); |
| 21 |
|
| 22 |
$liveArgs = [ |
| 23 |
'url_base' => rawurlencode(admin_url('admin.php?page=fluent_forms_settings')), |
| 24 |
'mode' => 'live', |
| 25 |
'hash' => $hash |
| 26 |
]; |
| 27 |
|
| 28 |
$testArgs = [ |
| 29 |
'url_base' => rawurlencode(admin_url('admin.php?page=fluent_forms_settings')), |
| 30 |
'mode' => 'test', |
| 31 |
'hash' => $hash |
| 32 |
]; |
| 33 |
|
| 34 |
$settings = StripeSettings::getSettings(); |
| 35 |
|
| 36 |
$data = [ |
| 37 |
'connect_config' => [ |
| 38 |
'live_redirect' => add_query_arg($liveArgs, $configBase), |
| 39 |
'test_redirect' => add_query_arg($testArgs, $configBase), |
| 40 |
'image_url' => fluentformMix('img/payment/stripe-connect.png'), |
| 41 |
'should_apply_application_fee' => !Helper::hasPro(), |
| 42 |
], |
| 43 |
'test_account' => self::getAccountInfo($settings, 'test'), |
| 44 |
'live_account' => self::getAccountInfo($settings, 'live') |
| 45 |
]; |
| 46 |
|
| 47 |
if ($settings['test_secret_key']) { |
| 48 |
$settings['test_secret_key'] = 'ENCRYPTED_KEY'; |
| 49 |
} |
| 50 |
|
| 51 |
if ($settings['live_secret_key']) { |
| 52 |
$settings['live_secret_key'] = 'ENCRYPTED_KEY'; |
| 53 |
} |
| 54 |
|
| 55 |
$data['settings'] = $settings; |
| 56 |
|
| 57 |
return $data; |
| 58 |
} |
| 59 |
|
| 60 |
public static function verifyAuthorizeSuccess($data) |
| 61 |
{ |
| 62 |
$response = wp_remote_post(self::$connectBase . 'stripe-verify-code', [ |
| 63 |
'method' => 'POST', |
| 64 |
'timeout' => 45, |
| 65 |
'redirection' => 5, |
| 66 |
'httpversion' => '1.0', |
| 67 |
'sslverify' => false, |
| 68 |
'blocking' => true, |
| 69 |
'headers' => array(), |
| 70 |
'body' => $data, |
| 71 |
'cookies' => array() |
| 72 |
]); |
| 73 |
|
| 74 |
if (is_wp_error($response)) { |
| 75 |
$message = $response->get_error_message(); |
| 76 |
echo '<div class="ff_message ff_message_error">' . esc_html($message) . '</div>'; |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
$response = json_decode(wp_remote_retrieve_body($response), true); |
| 81 |
|
| 82 |
if (empty($response['stripe_user_id'])) { |
| 83 |
$message = ArrayHelper::get($response, 'message'); |
| 84 |
if (!$message) { |
| 85 |
$message = __('Invalid Stripe Request. Please configure stripe payment gateway again', 'fluentform'); |
| 86 |
} |
| 87 |
echo '<div class="ff_message ff_message_error">' . esc_html($message) . '</div>'; |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
$settings = StripeSettings::getSettings(); |
| 92 |
$settings['provider'] = 'connect'; |
| 93 |
|
| 94 |
$settings['is_active'] = 'yes'; |
| 95 |
|
| 96 |
if (!empty($response['livemode'])) { |
| 97 |
$settings['payment_mode'] = 'live'; |
| 98 |
$settings['live_account_id'] = $response['stripe_user_id']; |
| 99 |
$settings['live_publishable_key'] = $response['stripe_publishable_key']; |
| 100 |
$settings['live_secret_key'] = $response['access_token']; |
| 101 |
} else { |
| 102 |
$settings['payment_mode'] = 'test'; |
| 103 |
$settings['test_account_id'] = $response['stripe_user_id']; |
| 104 |
$settings['test_publishable_key'] = $response['stripe_publishable_key']; |
| 105 |
$settings['test_secret_key'] = $response['access_token']; |
| 106 |
} |
| 107 |
|
| 108 |
StripeSettings::updateSettings($settings); |
| 109 |
|
| 110 |
?> |
| 111 |
<script type="text/javascript"> |
| 112 |
window.location = "<?php echo esc_url(admin_url('admin.php?page=fluent_forms_settings#payments/payment_methods')); ?>" |
| 113 |
</script> |
| 114 |
<?php |
| 115 |
|
| 116 |
} |
| 117 |
|
| 118 |
private static function getAccountInfo($settings, $mode) |
| 119 |
{ |
| 120 |
|
| 121 |
if ($settings['is_active'] != 'yes') { |
| 122 |
return false; |
| 123 |
} |
| 124 |
|
| 125 |
if ($settings['provider'] != 'connect') { |
| 126 |
return false; |
| 127 |
} |
| 128 |
|
| 129 |
$apiKey = $settings[$mode . '_secret_key']; |
| 130 |
|
| 131 |
$accountId = ArrayHelper::get($settings, $mode . '_account_id'); |
| 132 |
|
| 133 |
if (!$accountId) { |
| 134 |
return false; |
| 135 |
} |
| 136 |
|
| 137 |
$account = Account::retrive($accountId, $apiKey); |
| 138 |
|
| 139 |
if (is_wp_error($account)) { |
| 140 |
return [ |
| 141 |
'error' => $account->get_error_message() |
| 142 |
]; |
| 143 |
} |
| 144 |
|
| 145 |
// Find the email. |
| 146 |
$email = isset($account->email) |
| 147 |
? esc_html($account->email) |
| 148 |
: ''; |
| 149 |
|
| 150 |
// Find a Display Name. |
| 151 |
$display_name = isset($account->display_name) |
| 152 |
? esc_html($account->display_name) |
| 153 |
: ''; |
| 154 |
|
| 155 |
if ( |
| 156 |
empty($display_name) && |
| 157 |
isset($account->settings) && |
| 158 |
isset($account->settings->dashboard) && |
| 159 |
isset($account->settings->dashboard->display_name) |
| 160 |
) { |
| 161 |
$display_name = esc_html($account->settings->dashboard->display_name); |
| 162 |
} |
| 163 |
|
| 164 |
if (empty($display_name)) { |
| 165 |
return [ |
| 166 |
'error' => __('Unable to find connected display name', 'fluentform') |
| 167 |
]; |
| 168 |
} |
| 169 |
|
| 170 |
return [ |
| 171 |
'account_id' => $accountId, |
| 172 |
'display_name' => $display_name, |
| 173 |
'email' => $email |
| 174 |
]; |
| 175 |
|
| 176 |
} |
| 177 |
|
| 178 |
public static function disconnect($data, $sendResponse = false) |
| 179 |
{ |
| 180 |
$mode = ArrayHelper::get($data, 'mode'); |
| 181 |
$stripeSettings = StripeSettings::getSettings(); |
| 182 |
|
| 183 |
if($stripeSettings['is_active'] != 'yes') { |
| 184 |
if($sendResponse) { |
| 185 |
wp_send_json_error([ |
| 186 |
'message' => __('Stripe mode is not active', 'fluentform') |
| 187 |
], 423); |
| 188 |
} |
| 189 |
return false; |
| 190 |
} |
| 191 |
|
| 192 |
if(empty($stripeSettings[$mode.'_account_id'])) { |
| 193 |
if($sendResponse) { |
| 194 |
wp_send_json_error([ |
| 195 |
'message' => __('Selected Account does not exist', 'fluentform') |
| 196 |
], 423); |
| 197 |
} |
| 198 |
return false; |
| 199 |
} |
| 200 |
|
| 201 |
$stripeSettings[$mode.'_account_id'] = ''; |
| 202 |
$stripeSettings[$mode.'_publishable_key'] = ''; |
| 203 |
$stripeSettings[$mode.'_secret_key'] = ''; |
| 204 |
|
| 205 |
if($mode == 'live') { |
| 206 |
$alternateMode = 'test'; |
| 207 |
} else { |
| 208 |
$alternateMode = 'live'; |
| 209 |
} |
| 210 |
|
| 211 |
if(empty($stripeSettings[$alternateMode.'_account_id'])) { |
| 212 |
$stripeSettings['is_active'] = 'no'; |
| 213 |
$stripeSettings['payment_mode'] = 'test'; |
| 214 |
} else { |
| 215 |
$stripeSettings['payment_mode'] = $alternateMode; |
| 216 |
} |
| 217 |
|
| 218 |
StripeSettings::updateSettings($stripeSettings); |
| 219 |
|
| 220 |
if($sendResponse) { |
| 221 |
wp_send_json_success([ |
| 222 |
'message' => __('Stripe settings has been disconnected', 'fluentform'), |
| 223 |
'settings' => $stripeSettings |
| 224 |
], 200); |
| 225 |
} |
| 226 |
|
| 227 |
return true; |
| 228 |
} |
| 229 |
} |
| 230 |
|