| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Modules\Payments\PaymentMethods\Stripe; |
| 4 |
|
| 5 |
if (!defined('ABSPATH')) { |
| 6 |
exit; // Exit if accessed directly. |
| 7 |
} |
| 8 |
|
| 9 |
use FluentForm\Framework\Helpers\ArrayHelper; |
| 10 |
use FluentForm\App\Modules\Payments\Components\PaymentMethods; |
| 11 |
use FluentForm\App\Modules\Payments\PaymentMethods\Stripe\API\ApiRequest; |
| 12 |
use FluentForm\App\Modules\Payments\PaymentMethods\Stripe\API\StripeListener; |
| 13 |
use FluentForm\App\Modules\Payments\PaymentMethods\Stripe\API\Account; |
| 14 |
|
| 15 |
class StripeHandler |
| 16 |
{ |
| 17 |
protected $key = 'stripe'; |
| 18 |
|
| 19 |
public function init() |
| 20 |
{ |
| 21 |
add_filter('fluentform/payment_settings_' . $this->key, function () { |
| 22 |
$settings = StripeSettings::getSettings(); |
| 23 |
|
| 24 |
if ($settings['test_secret_key']) { |
| 25 |
$settings['test_secret_key'] = 'ENCRYPTED_KEY'; |
| 26 |
} |
| 27 |
|
| 28 |
if ($settings['live_secret_key']) { |
| 29 |
$settings['live_secret_key'] = 'ENCRYPTED_KEY'; |
| 30 |
} |
| 31 |
|
| 32 |
return $settings; |
| 33 |
|
| 34 |
}); |
| 35 |
|
| 36 |
add_filter('fluentform/payment_method_settings_validation_' . $this->key, array($this, 'validateSettings'), 10, 2); |
| 37 |
|
| 38 |
add_filter('fluentform/payment_method_settings_save_' . $this->key, array($this, 'sanitizeGlobalSettings'), 10, 1); |
| 39 |
|
| 40 |
if (!$this->isEnabled()) { |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
add_filter('fluentform/available_payment_methods', array($this, 'pushPaymentMethodToForm'), 10, 1); |
| 45 |
|
| 46 |
add_action('fluentform/rendering_payment_method_' . $this->key, array($this, 'enqueueAssets')); |
| 47 |
|
| 48 |
add_filter('fluentform/transaction_data_' . $this->key, array($this, 'modifyTransaction'), 10, 1); |
| 49 |
|
| 50 |
add_action('fluentform/ipn_endpoint_' . $this->key, function () { |
| 51 |
(new StripeListener())->verifyIPN(); |
| 52 |
}); |
| 53 |
|
| 54 |
add_action('fluentform/process_payment_stripe', [$this, 'routeStripeProcessor'], 10, 6); |
| 55 |
|
| 56 |
add_filter('fluentform/payment_manager_class_' . $this->key, function ($class) { |
| 57 |
return new PaymentManager(); |
| 58 |
}); |
| 59 |
|
| 60 |
(new StripeProcessor())->init(); |
| 61 |
(new StripeInlineProcessor())->init(); |
| 62 |
} |
| 63 |
|
| 64 |
public function routeStripeProcessor($submissionId, $submissionData, $form, $methodSettings, $hasSubscriptions, $totalPayable = 0) |
| 65 |
{ |
| 66 |
$processor = ArrayHelper::get($methodSettings, 'settings.embedded_checkout.value') === 'yes' ? 'inline' : 'hosted'; |
| 67 |
|
| 68 |
do_action_deprecated( |
| 69 |
'fluentform_process_payment_stripe_' . $processor, |
| 70 |
[ |
| 71 |
$submissionId, |
| 72 |
$submissionData, |
| 73 |
$form, |
| 74 |
$methodSettings, |
| 75 |
$hasSubscriptions, |
| 76 |
$totalPayable |
| 77 |
], |
| 78 |
FLUENTFORM_FRAMEWORK_UPGRADE, |
| 79 |
'fluentform/process_payment_stripe_' . $processor, |
| 80 |
'Use fluentform/process_payment_stripe_' . $processor . ' instead of fluentform_process_payment_stripe_' . $processor |
| 81 |
); |
| 82 |
|
| 83 |
do_action('fluentform/process_payment_stripe_' . $processor, $submissionId, $submissionData, $form, $methodSettings, $hasSubscriptions, $totalPayable); |
| 84 |
} |
| 85 |
|
| 86 |
public function pushPaymentMethodToForm($methods) |
| 87 |
{ |
| 88 |
if (!$this->isEnabled()) { |
| 89 |
return $methods; |
| 90 |
} |
| 91 |
|
| 92 |
$methods[$this->key] = [ |
| 93 |
'title' => __('Credit/Debit Card (Stripe)', 'fluentform'), |
| 94 |
'enabled' => 'yes', |
| 95 |
'method_value' => $this->key, |
| 96 |
'settings' => [ |
| 97 |
'option_label' => [ |
| 98 |
'type' => 'text', |
| 99 |
'template' => 'inputText', |
| 100 |
'value' => 'Pay with Card (Stripe)', |
| 101 |
'label' => __('Method Label', 'fluentform') |
| 102 |
], |
| 103 |
'embedded_checkout' => [ |
| 104 |
'type' => 'checkbox', |
| 105 |
'template' => 'inputYesNoCheckbox', |
| 106 |
'value' => 'yes', |
| 107 |
'label' => __('Embedded Checkout', 'fluentform') |
| 108 |
], |
| 109 |
'require_billing_info' => [ |
| 110 |
'type' => 'checkbox', |
| 111 |
'template' => 'inputYesNoCheckbox', |
| 112 |
'value' => 'no', |
| 113 |
'label' => __('Require Billing info', 'fluentform'), |
| 114 |
'dependency' => array( |
| 115 |
'depends_on' => 'embedded_checkout/value', |
| 116 |
'value' => 'yes', |
| 117 |
'operator' => '!=' |
| 118 |
) |
| 119 |
], |
| 120 |
'require_shipping_info' => [ |
| 121 |
'type' => 'checkbox', |
| 122 |
'template' => 'inputYesNoCheckbox', |
| 123 |
'value' => 'no', |
| 124 |
'label' => __('Collect Shipping Info', 'fluentform'), |
| 125 |
'dependency' => array( |
| 126 |
'depends_on' => 'embedded_checkout/value', |
| 127 |
'value' => 'yes', |
| 128 |
'operator' => '!=' |
| 129 |
) |
| 130 |
], |
| 131 |
'verify_zip_code' => [ |
| 132 |
'type' => 'checkbox', |
| 133 |
'template' => 'inputYesNoCheckbox', |
| 134 |
'value' => 'no', |
| 135 |
'label' => __('Verify Zip/Postal Code', 'fluentform') |
| 136 |
], |
| 137 |
] |
| 138 |
]; |
| 139 |
|
| 140 |
return $methods; |
| 141 |
} |
| 142 |
|
| 143 |
public function enqueueAssets() |
| 144 |
{ |
| 145 |
wp_enqueue_script('stripe_elements', 'https://js.stripe.com/v3/', array('jquery'), '3.0', true); |
| 146 |
} |
| 147 |
|
| 148 |
public function validateSettings($errors, $settings) |
| 149 |
{ |
| 150 |
if (ArrayHelper::get($settings, 'is_active') != 'yes') { |
| 151 |
return []; |
| 152 |
} |
| 153 |
|
| 154 |
$mode = $settings['payment_mode']; |
| 155 |
|
| 156 |
if (empty($settings[$mode . '_publishable_key'])) { |
| 157 |
/* translators: %s is the mode (Live/Test) */ |
| 158 |
$errors[$mode . '_publishable_key'] = sprintf(__('%s Publishable Key is required', 'fluentform'), ucfirst($mode)); |
| 159 |
} |
| 160 |
|
| 161 |
if (empty($settings[$mode . '_secret_key'])) { |
| 162 |
/* translators: %s is the mode (Live/Test) */ |
| 163 |
$errors[$mode . '_secret_key'] = sprintf(__('%s Secret Key is required', 'fluentform'), ucfirst($mode)); |
| 164 |
} |
| 165 |
|
| 166 |
if (ArrayHelper::get($settings, 'provider') === 'connect' && count($errors)) { |
| 167 |
$errors = [ |
| 168 |
'connect' => __('Connect with Stripe was not successful. Please try again!', 'fluentform') |
| 169 |
]; |
| 170 |
} |
| 171 |
|
| 172 |
return $errors; |
| 173 |
} |
| 174 |
|
| 175 |
public function modifyTransaction($transaction) |
| 176 |
{ |
| 177 |
if ($transaction->charge_id) { |
| 178 |
$urlBase = 'https://dashboard.stripe.com/'; |
| 179 |
if ($transaction->payment_mode != 'live') { |
| 180 |
$urlBase .= 'test/'; |
| 181 |
} |
| 182 |
$transaction->action_url = $urlBase . 'payments/' . $transaction->charge_id; |
| 183 |
} |
| 184 |
|
| 185 |
if ($transaction->status == 'requires_capture') { |
| 186 |
$transaction->additional_note = sprintf( |
| 187 |
/* translators: %s is the URL to capture payment in Stripe dashboard */ |
| 188 |
__('<b>Action Required: </b> The payment has been authorized but not captured yet. Please <a target="_blank" rel="noopener noreferrer" href="%s">Click here</a> to capture this payment in stripe.com', 'fluentform'), |
| 189 |
esc_url($transaction->action_url) |
| 190 |
); |
| 191 |
} |
| 192 |
|
| 193 |
return $transaction; |
| 194 |
} |
| 195 |
|
| 196 |
public function isEnabled() |
| 197 |
{ |
| 198 |
$settings = StripeSettings::getSettings(false); |
| 199 |
return $settings['is_active'] == 'yes'; |
| 200 |
} |
| 201 |
|
| 202 |
public function sanitizeGlobalSettings($settings) |
| 203 |
{ |
| 204 |
if ($settings['is_active'] != 'yes') { |
| 205 |
return [ |
| 206 |
'test_publishable_key' => '', |
| 207 |
'test_secret_key' => '', |
| 208 |
'live_publishable_key' => '', |
| 209 |
'live_secret_key' => '', |
| 210 |
'payment_mode' => 'test', |
| 211 |
'is_active' => 'no', |
| 212 |
'provider' => 'connect' // api_keys |
| 213 |
]; |
| 214 |
} |
| 215 |
|
| 216 |
$prevSettings = StripeSettings::getSettings(true); |
| 217 |
|
| 218 |
$accountId = ArrayHelper::get($prevSettings, 'test_account_id'); |
| 219 |
$token = ArrayHelper::get($prevSettings, 'test_secret_key'); |
| 220 |
if (ArrayHelper::get($prevSettings, 'payment_mode') === 'live') { |
| 221 |
$accountId = ArrayHelper::get($prevSettings, 'live_account_id'); |
| 222 |
$token = ArrayHelper::get($prevSettings, 'live_secret_key'); |
| 223 |
} |
| 224 |
|
| 225 |
// Fetch connected account details |
| 226 |
$connectedAccountDetails = Account::retrive($accountId, $token); |
| 227 |
if ($connectedAccountDetails && isset($connectedAccountDetails->country)) { |
| 228 |
$settings['connected_account_country'] = $connectedAccountDetails->country; |
| 229 |
} |
| 230 |
|
| 231 |
// Fetch platform account details |
| 232 |
$platformAccountDetails = Account::retrive('', $token); |
| 233 |
if ($platformAccountDetails && isset($platformAccountDetails->country)) { |
| 234 |
$settings['platform_account_country'] = $platformAccountDetails->country; |
| 235 |
} |
| 236 |
|
| 237 |
if ($settings['test_secret_key'] == 'ENCRYPTED_KEY') { |
| 238 |
$settings['test_secret_key'] = $prevSettings['test_secret_key']; |
| 239 |
} |
| 240 |
|
| 241 |
if ($settings['live_secret_key'] == 'ENCRYPTED_KEY') { |
| 242 |
$settings['live_secret_key'] = $prevSettings['live_secret_key']; |
| 243 |
} |
| 244 |
|
| 245 |
return StripeSettings::encryptKeys($settings); |
| 246 |
} |
| 247 |
|
| 248 |
} |
| 249 |
|