1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
<?php /** * Stripe module main file * * @link https://contactform7.com/stripe-integration/ */
wpcf7_include_module_file( 'stripe/service.php' ); wpcf7_include_module_file( 'stripe/api.php' );
add_action( 'wpcf7_init', 'wpcf7_stripe_register_service', 10, 0 );
/** * Registers the Stripe service. */ function wpcf7_stripe_register_service() { $integration = WPCF7_Integration::get_instance();
$integration->add_service( 'stripe', WPCF7_Stripe::get_instance() ); }
add_action( 'wpcf7_enqueue_scripts', 'wpcf7_stripe_enqueue_scripts', 10, 0 );
/** * Enqueues scripts and styles for the Stripe module. */ function wpcf7_stripe_enqueue_scripts() { $service = WPCF7_Stripe::get_instance();
if ( ! $service->is_active() ) { return; }
wp_enqueue_style( 'wpcf7-stripe', wpcf7_plugin_url( 'modules/stripe/style.css' ), array(), WPCF7_VERSION, 'all' );
wp_enqueue_script( 'stripe', 'https://js.stripe.com/v3/', array(), null );
$assets = array();
$asset_file = wpcf7_plugin_path( 'modules/stripe/index.asset.php' );
if ( file_exists( $asset_file ) ) { $assets = include( $asset_file ); }
$assets = wp_parse_args( $assets, array( 'src' => wpcf7_plugin_url( 'modules/stripe/index.js' ), 'dependencies' => array( 'wp-polyfill', ), 'version' => WPCF7_VERSION, ) );
wp_enqueue_script( 'wpcf7-stripe', $assets['src'], array_merge( array( 'contact-form-7', 'stripe', ), $assets['dependencies'] ), $assets['version'], true );
$api_keys = $service->get_api_keys();
if ( $api_keys['publishable'] ) { wp_localize_script( 'wpcf7-stripe', 'wpcf7_stripe', array( 'publishable_key' => $api_keys['publishable'], ) ); } }
add_filter( 'wpcf7_skip_spam_check', 'wpcf7_stripe_skip_spam_check', 10, 2 );
/** * Skips the spam check if it is not necessary. * * @return bool True if the spam check is not necessary. */ function wpcf7_stripe_skip_spam_check( $skip_spam_check, $submission ) { $service = WPCF7_Stripe::get_instance();
if ( ! $service->is_active() ) { return $skip_spam_check; }
if ( ! empty( $_POST['_wpcf7_stripe_payment_intent'] ) ) { $pi_id = trim( $_POST['_wpcf7_stripe_payment_intent'] ); $payment_intent = $service->api()->retrieve_payment_intent( $pi_id );
if ( isset( $payment_intent['status'] ) and ( 'succeeded' === $payment_intent['status'] ) ) { $submission->payment_intent = $pi_id; } }
if ( ! empty( $submission->payment_intent ) and $submission->verify_posted_data_hash() ) { $skip_spam_check = true; }
return $skip_spam_check; }
add_action( 'wpcf7_before_send_mail', 'wpcf7_stripe_before_send_mail', 10, 3 );
/** * Creates Stripe's Payment Intent. */ function wpcf7_stripe_before_send_mail( $contact_form, &$abort, $submission ) { $service = WPCF7_Stripe::get_instance();
if ( ! $service->is_active() ) { return; }
$tags = $contact_form->scan_form_tags( array( 'type' => 'stripe' ) );
if ( ! $tags ) { return; }
if ( ! empty( $submission->payment_intent ) ) { return; }
$tag = $tags[0]; $amount = $tag->get_option( 'amount', 'int', true ); $currency = $tag->get_option( 'currency', '[a-zA-Z]{3}', true );
$payment_intent_params = apply_filters( 'wpcf7_stripe_payment_intent_parameters', array( 'amount' => $amount ? absint( $amount ) : null, 'currency' => $currency ? strtolower( $currency ) : null, 'receipt_email' => $submission->get_posted_data( 'your-email' ), ) );
$payment_intent = $service->api()->create_payment_intent( $payment_intent_params );
if ( $payment_intent ) { $submission->add_result_props( array( 'stripe' => array( 'payment_intent' => array( 'id' => $payment_intent['id'], 'client_secret' => $payment_intent['client_secret'], ), ), ) );
$submission->set_status( 'payment_required' );
$submission->set_response( __( "Payment is required. Please pay by credit card.", 'contact-form-7' ) ); }
$abort = true; }
/** * Returns payment link URL. * * @param string $pi_id Payment Intent ID. * @return string The URL. */ function wpcf7_stripe_get_payment_link( $pi_id ) { return sprintf( 'https://dashboard.stripe.com/payments/%s', urlencode( $pi_id ) ); }
add_filter( 'wpcf7_special_mail_tags', 'wpcf7_stripe_smt', 10, 4 );
/** * Registers the [_stripe_payment_link] special mail-tag. */ function wpcf7_stripe_smt( $output, $tag_name, $html, $mail_tag = null ) { if ( '_stripe_payment_link' === $tag_name ) { $submission = WPCF7_Submission::get_instance();
if ( ! empty( $submission->payment_intent ) ) { $output = wpcf7_stripe_get_payment_link( $submission->payment_intent ); } }
return $output; }
add_filter( 'wpcf7_flamingo_inbound_message_parameters', 'wpcf7_stripe_add_flamingo_inbound_message_params', 10, 1 );
/** * Adds Stripe-related meta data to Flamingo Inbound Message parameters. */ function wpcf7_stripe_add_flamingo_inbound_message_params( $args ) { $submission = WPCF7_Submission::get_instance();
if ( empty( $submission->payment_intent ) ) { return $args; }
$pi_link = wpcf7_stripe_get_payment_link( $submission->payment_intent );
$meta = (array) $args['meta'];
$meta['stripe_payment_link'] = $pi_link;
$args['meta'] = $meta;
return $args; }
add_action( 'wpcf7_init', 'wpcf7_add_form_tag_stripe', 10, 0 );
/** * Registers the stripe form-tag handler. */ function wpcf7_add_form_tag_stripe() { wpcf7_add_form_tag( 'stripe', 'wpcf7_stripe_form_tag_handler', array( 'display-block' => true, 'singular' => true, ) ); }
/** * Defines the stripe form-tag handler. * * @return string HTML content that replaces a stripe form-tag. */ function wpcf7_stripe_form_tag_handler( $tag ) { $card_element = sprintf( '<div %s></div>', wpcf7_format_atts( array( 'class' => 'card-element wpcf7-form-control', 'aria-invalid' => 'false', ) ) );
$card_element = sprintf( '<div class="wpcf7-form-control-wrap hidden">%s</div>', $card_element );
$button_1_label = __( 'Proceed to checkout', 'contact-form-7' );
if ( isset( $tag->values[0] ) ) { $button_1_label = trim( $tag->values[0] ); }
$button_1 = sprintf( '<button %1$s>%2$s</button>', wpcf7_format_atts( array( 'type' => 'submit', 'class' => 'first', ) ), esc_html( $button_1_label ) );
$button_2_label = __( 'Complete payment', 'contact-form-7' );
if ( isset( $tag->values[1] ) ) { $button_2_label = trim( $tag->values[1] ); }
$button_2 = sprintf( '<button %1$s>%2$s</button>', wpcf7_format_atts( array( 'type' => 'button', 'class' => 'second hidden', ) ), esc_html( $button_2_label ) );
$buttons = sprintf( '<span class="buttons has-spinner">%1$s %2$s</span>', $button_1, $button_2 );
return sprintf( '<div class="wpcf7-stripe">%1$s %2$s %3$s</div>', $card_element, $buttons, '<input type="hidden" name="_wpcf7_stripe_payment_intent" value="" />' ); }
|