PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / trunk
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder vtrunk
6.2.15 6.2.14 6.2.13 6.2.12 6.2.10 6.2.11 6.2.9 6.2.8 6.2.7 6.2.6 6.2.5 6.2.4 6.2.3 6.2.2 3.6.22 3.6.31 3.6.40 3.6.41 3.6.42 3.6.50 3.6.51 3.6.60 3.6.61 3.6.62 3.6.64 All 197 releases
fluentform / app / Modules / Payments / PaymentMethods / Stripe / StripeHandler.php

StripeHandler.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder trunk, at app/Modules/Payments/PaymentMethods/Stripe/StripeHandler.php

263 lines 10.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 'enable_payment_element' => [
110 'type' => 'checkbox',
111 'template' => 'inputYesNoCheckbox',
112 'value' => 'no',
113 'label' => __('Enable Payment Element', 'fluentform'),
114 'help_text' => __('Replaces the card field with Stripe\'s Payment Element, which adds Apple Pay and Google Pay on supported devices. Apple Pay needs your site domain registered in Stripe under Settings > Payment method domains.', 'fluentform'),
115 'dependency' => array(
116 'depends_on' => 'embedded_checkout/value',
117 'value' => 'yes',
118 'operator' => '=='
119 )
120 ],
121 'require_billing_info' => [
122 'type' => 'checkbox',
123 'template' => 'inputYesNoCheckbox',
124 'value' => 'no',
125 'label' => __('Require Billing info', 'fluentform'),
126 'dependency' => array(
127 'depends_on' => 'embedded_checkout/value',
128 'value' => 'yes',
129 'operator' => '!='
130 )
131 ],
132 'require_shipping_info' => [
133 'type' => 'checkbox',
134 'template' => 'inputYesNoCheckbox',
135 'value' => 'no',
136 'label' => __('Collect Shipping Info', 'fluentform'),
137 'dependency' => array(
138 'depends_on' => 'embedded_checkout/value',
139 'value' => 'yes',
140 'operator' => '!='
141 )
142 ],
143 'verify_zip_code' => [
144 'type' => 'checkbox',
145 'template' => 'inputYesNoCheckbox',
146 'value' => 'no',
147 'label' => __('Verify Zip/Postal Code', 'fluentform')
148 ],
149 ]
150 ];
151
152 return $methods;
153 }
154
155 public function enqueueAssets()
156 {
157 wp_enqueue_script('stripe_elements', 'https://js.stripe.com/v3/', array('jquery'), '3.0', true);
158 }
159
160 public function validateSettings($errors, $settings)
161 {
162 if (ArrayHelper::get($settings, 'is_active') != 'yes') {
163 return [];
164 }
165
166 $mode = $settings['payment_mode'];
167
168 if (empty($settings[$mode . '_publishable_key'])) {
169 /* translators: %s is the mode (Live/Test) */
170 $errors[$mode . '_publishable_key'] = sprintf(__('%s Publishable Key is required', 'fluentform'), ucfirst($mode));
171 }
172
173 if (empty($settings[$mode . '_secret_key'])) {
174 /* translators: %s is the mode (Live/Test) */
175 $errors[$mode . '_secret_key'] = sprintf(__('%s Secret Key is required', 'fluentform'), ucfirst($mode));
176 }
177
178 if (ArrayHelper::get($settings, 'provider') === 'connect' && count($errors)) {
179 $errors = [
180 'connect' => __('Connect with Stripe was not successful. Please try again!', 'fluentform')
181 ];
182 }
183
184 return $errors;
185 }
186
187 public function modifyTransaction($transaction)
188 {
189 if ($transaction->charge_id) {
190 $urlBase = 'https://dashboard.stripe.com/';
191 if ($transaction->payment_mode != 'live') {
192 $urlBase .= 'test/';
193 }
194 $transaction->action_url = $urlBase . 'payments/' . $transaction->charge_id;
195 }
196
197 if ($transaction->status == 'requires_capture') {
198 $transaction->additional_note = sprintf(
199 /* translators: %s is the URL to capture payment in Stripe dashboard */
200 __('<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'),
201 esc_url($transaction->action_url)
202 );
203 }
204
205 return $transaction;
206 }
207
208 public function isEnabled()
209 {
210 $settings = StripeSettings::getSettings(false);
211 return $settings['is_active'] == 'yes';
212 }
213
214 public function sanitizeGlobalSettings($settings)
215 {
216 if ($settings['is_active'] != 'yes') {
217 return [
218 'test_publishable_key' => '',
219 'test_secret_key' => '',
220 'live_publishable_key' => '',
221 'live_secret_key' => '',
222 'payment_mode' => 'test',
223 'is_active' => 'no',
224 'provider' => 'connect' // api_keys
225 ];
226 }
227
228 $prevSettings = StripeSettings::getSettings(true);
229
230 $accountId = ArrayHelper::get($prevSettings, 'test_account_id');
231 $token = ArrayHelper::get($prevSettings, 'test_secret_key');
232 if (ArrayHelper::get($prevSettings, 'payment_mode') === 'live') {
233 $accountId = ArrayHelper::get($prevSettings, 'live_account_id');
234 $token = ArrayHelper::get($prevSettings, 'live_secret_key');
235 }
236
237 // Fetch connected account details
238 $connectedAccountDetails = Account::retrive($accountId, $token);
239 if ($connectedAccountDetails && isset($connectedAccountDetails->country)) {
240 $settings['connected_account_country'] = $connectedAccountDetails->country;
241 }
242
243 // Fetch platform account details
244 $platformAccountDetails = Account::retrive('', $token);
245 if ($platformAccountDetails && isset($platformAccountDetails->country)) {
246 $settings['platform_account_country'] = $platformAccountDetails->country;
247 }
248
249 if ($settings['test_secret_key'] == 'ENCRYPTED_KEY') {
250 $settings['test_secret_key'] = $prevSettings['test_secret_key'];
251 }
252
253 if ($settings['live_secret_key'] == 'ENCRYPTED_KEY') {
254 $settings['live_secret_key'] = $prevSettings['live_secret_key'];
255 }
256
257 $settings = StripeSettings::encryptKeys($settings);
258
259 return StripeSettings::preserveUnreadableKeys($settings);
260 }
261
262 }
263