-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdgx-donate-paypalstd.php
497 lines (404 loc) · 19.2 KB
/
dgx-donate-paypalstd.php
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
<?php
/*
Seamless Donations by David Gewirtz, adopted from Allen Snook
Lab Notes: http://zatzlabs.com/lab-notes/
Plugin Page: http://zatzlabs.com/seamless-donations/
Contact: http://zatzlabs.com/contact-us/
Copyright (c) 2015 by David Gewirtz
*/
// Exit if .php file accessed directly
if ( !defined( 'ABSPATH' ) ) exit;
/* PayPal Website Payments Standard Module for Seamless Donations */
define( 'DGXDONATEPAYPALSTD', 'DGXDONATEPAYPALSTD' );
/******************************************************************************************************/
function dgx_donate_paypalstd_init () {
// The showing and saving of settings uses actions
add_action ( 'dgx_donate_show_settings_forms', 'dgx_donate_show_paypalstd_settings_form' );
add_action ( 'dgx_donate_save_settings_forms', 'dgx_donate_save_paypalstd_settings_form' );
// The donation form content uses a filter since it must return the form to the caller
add_filter ( 'dgx_donate_donation_form', 'dgx_donate_show_paypalstd_donation_form' );
}
add_action ( 'init', 'dgx_donate_paypalstd_init' );
/******************************************************************************************************/
function dgx_donate_paypalstd_enqueue_scripts () {
$load_in_footer = ( 'true' == get_option ( 'dgx_donate_scripts_in_footer' ) );
wp_enqueue_script ( 'jquery' );
$script_url = plugins_url ( '/js/paypalstd-script.js', __FILE__ );
wp_enqueue_script ( 'dgx_donate_paypalstd_script', $script_url, array( 'jquery' ), false, $load_in_footer );
}
add_action ( 'wp_enqueue_scripts', 'dgx_donate_paypalstd_enqueue_scripts' );
/******************************************************************************************************/
function dgx_donate_show_paypalstd_settings_form () {
// First, show our radio button
$paymentGateway = get_option ( 'dgx_donate_payment_gateway' );
if( $paymentGateway == DGXDONATEPAYPALSTD ) {
$checked = "checked";
} else {
$checked = "";
}
echo "<p class=\"dgxdonategatewayname\"><input type=\"radio\" name=\"paymentgateway\" value=\"";
echo DGXDONATEPAYPALSTD;
echo "\" $checked /> <b>PayPal Standard</b></p>";
// Now show our form content
$checkSandbox = "";
$checkLive = "";
$payPalServer = get_option ( 'dgx_donate_paypal_server' );
if( strcasecmp ( $payPalServer, "SANDBOX" ) == 0 ) {
$checkSandbox = "checked";
} else {
$checkLive = "checked";
}
$payPalEmail = get_option ( 'dgx_donate_paypal_email' );
echo "<div class=\"form-field\">\n";
echo "<label for='paypalemail'>" . esc_html__ ( 'PayPal Email Address', 'seamless-donations' ) . "</label><br/>\n";
echo "<input type=\"text\" name=\"paypalemail\" value=\"$payPalEmail\" />\n";
echo "<p class='description'>" .
esc_html__ ( 'The email address at which to receive payments.', 'seamless-donations' ) . "</p>\n";
echo "</div> <!-- form-field --> \n";
echo "<p>" . esc_html__ ( 'Mode:', 'seamless-donations' ) . " \n";
echo "<input type='radio' name='paypalserver' value='SANDBOX' $checkSandbox /> " .
esc_html__ ( 'Sandbox (Test Server)', 'seamless-donations' ) . " ";
echo "<input type='radio' name='paypalserver' value='LIVE' $checkLive /> " .
esc_html__ ( 'Live (Production Server)', 'seamless-donations' ) . "</p>";
echo "<p>" . __ ( 'IPN URL', 'seamless-donations' ) . "<p>";
$notify_url = plugins_url ( '/pay/paypalstd/ipn.php', __FILE__ );
echo "<pre>$notify_url</pre>";
}
/******************************************************************************************************/
function dgx_donate_save_paypalstd_settings_form () {
$paymentGateway = "";
$payPalServer = "";
$payPalEmail = "";
if( isset( $_POST['paymentgateway'] ) ) {
$paymentGateway = $_POST['paymentgateway'];
}
if( isset( $_POST['paypalserver'] ) ) {
$payPalServer = $_POST['paypalserver'];
}
if( isset( $_POST['paypalemail'] ) ) {
$payPalEmail = $_POST['paypalemail'];
}
// If they set the paymentGateway, record the setting
// It is OK for all gateways to do this (so at least one does)
if( ! empty( $paymentGateway ) ) {
update_option ( 'dgx_donate_payment_gateway', $paymentGateway );
}
// If they set the paypalemail, record the setting
if( ! empty( $payPalEmail ) ) {
$payPalEmail = trim ( $payPalEmail );
if( is_email ( $payPalEmail ) ) {
update_option ( 'dgx_donate_paypal_email', $payPalEmail );
}
}
// If they set the paypal server type (sandbox or live), record the setting
if( ! empty( $payPalServer ) ) {
update_option ( 'dgx_donate_paypal_server', $payPalServer );
}
}
/******************************************************************************************************/
function dgx_donate_show_paypalstd_donation_form ( $content ) {
// If we are the actively selected gateway
$paymentGateway = get_option ( 'dgx_donate_payment_gateway' );
if( $paymentGateway == DGXDONATEPAYPALSTD ) {
// Open the form
$content .= "<!-- SD " . dgx_donate_get_version () . " legacy mode -->";
$content .= "<form id=\"dgx-donate-form\" method=\"post\" onsubmit=\"return DgxDonateDoCheckout();\" >";
// Save the session ID as a hidden input
$session_id = 'dgxdonate_' . substr ( session_id (), 0, 10 ) . '_' . time ();
$content .= "<input type=\"hidden\" name=\"_dgx_donate_session_id\" value=\"$session_id\" />";
// Start the outermost container
$content .= "<div id=\"dgx-donate-container\">\n";
// Pick and choose the built in sections this gateway supports
$content = dgx_donate_paypalstd_warning_section ( $content );
$content = dgx_donate_get_donation_section ( $content );
$content = dgx_donate_get_tribute_section ( $content );
$content = dgx_donate_get_employer_section ( $content );
$content = dgx_donate_get_donor_section ( $content );
$content = dgx_donate_get_billing_section ( $content );
$content = dgx_donate_paypalstd_payment_section ( $content );
// Close the outermost container
$content .= "</div>\n";
// Close the form
$content .= "</form>\n";
$content .= dgx_donate_paypalstd_get_hidden_form ();
}
return $content;
}
/******************************************************************************************************/
function dgx_donate_paypalstd_get_current_url () {
if( isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] == 'on' ) {
$http = 'https';
} else {
$http = 'http';
}
$currentUrl = $http . '://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
return $currentUrl;
}
/******************************************************************************************************/
function dgx_donate_paypalstd_get_hidden_form () {
seamless_donations_debug_log ( "Inside dgx_donate_paypalstd_get_hidden_form" );
$paypalEmail = get_option ( 'dgx_donate_paypal_email' );
$payPalServer = get_option ( 'dgx_donate_paypal_server' );
if( $payPalServer == "SANDBOX" ) {
$formAction = "https://www.sandbox.paypal.com/cgi-bin/webscr";
} else {
$formAction = "https://www.paypal.com/cgi-bin/webscr";
}
$notifyUrl = plugins_url ( '/pay/paypalstd/ipn.php', __FILE__ );
$sessionID = session_id ();
$successUrl = dgx_donate_paypalstd_get_current_url ();
if( strpos ( $successUrl, "?" ) === false ) {
$successUrl .= "?";
} else {
$successUrl .= "&";
}
$successUrl .= "thanks=1&sessionid=";
$successUrl .= "$sessionID";
$currency_code = get_option ( 'dgx_donate_currency' );
$item_name = apply_filters ( 'dgx_donate_item_name', __ ( 'Donation', 'seamless-donations' ) );
$output = "";
$output .= "<form id='dgx-donate-hidden-form' action='" . esc_attr ( $formAction ) .
"' method='post' target='_top' >";
$output .= "<input type='hidden' name='cmd' value='_donations' />";
$output .= "<input type='hidden' name='business' value='" . esc_attr ( $paypalEmail ) . "' />";
$output .= "<input type='hidden' name='return' value='" . esc_attr ( $successUrl ) . "' />";
$output .= "<input type='hidden' name='first_name' value='' /> ";
$output .= "<input type='hidden' name='last_name' value='' />";
$output .= "<input type='hidden' name='address1' value='' />";
$output .= "<input type='hidden' name='address2' value='' />";
$output .= "<input type='hidden' name='city' value='' />";
$output .= "<input type='hidden' name='state' value='' />"; // removed if country not US or Canada
$output .= "<input type='hidden' name='zip' value='' />";
$output .= "<input type='hidden' name='country' value='' />";
$output .= "<input type='hidden' name='email' value='' />";
$output .= "<input type='hidden' name='custom' value='' />";
$output .= "<input type='hidden' name='notify_url' value='" . esc_attr ( $notifyUrl ) . "' />";
$output .= "<input type='hidden' name='item_name' value='" . esc_attr ( $item_name ) . "' />";
$output .= "<input type='hidden' name='amount' value='1.00' />";
$output .= "<input type='hidden' name='quantity' value='1' />";
$output .= "<input type='hidden' name='currency_code' value='" . esc_attr ( $currency_code ) . "' />";
$output .= "<input type='hidden' name='no_note' value='1' />";
$output .= "<input type='hidden' name='src' value='1' />"; // removed when not repeating
$output .= "<input type='hidden' name='p3' value='1' />"; // removed when not repeating
$output .= "<input type='hidden' name='t3' value='1' />"; // removed when not repeating
$output .= "<input type='hidden' name='a3' value='1' />"; // removed when not repeating
$output .= "</form>";
seamless_donations_debug_log ( "Exiting dgx_donate_paypalstd_get_hidden_form" );
return $output;
}
/******************************************************************************************************/
function dgx_donate_paypalstd_warning_section ( $formContent ) {
// Display any setup warnings we need to display here (e.g. running in test mode)
$payPalServer = get_option ( 'dgx_donate_paypal_server' );
if( $payPalServer == "SANDBOX" ) {
$formContent .= "<div class='dgx-donate-form-section' id='dgx-donate-form-sandbox-warning'>";
$formContent .= "<p>";
$formContent .= esc_html__ (
"Warning - Seamless Donations is currently configured to use the sandbox test server.",
'seamless-donations' );
$formContent .= "</p>";
$formContent .= "</div>";
}
// Echo a NOSCRIPT warning
$formContent .= "<noscript>";
$formContent .= "<div class='dgx-donate-form-section' id='dgx-donate-form-noscript-warning'>";
$formContent
.=
"<p>" . esc_html__ ( "Warning: To make a donation, you must first enable JavaScript.", 'seamless-donations' ) .
"</p>";
$formContent .= "</div>";
$formContent .= "</noscript>";
return $formContent;
}
/******************************************************************************************************/
function dgx_donate_paypalstd_payment_section ( $form_content ) {
// Show the button that kicks it all off
$processing_image_url = plugins_url ( '/images/ajax-loader.gif', __FILE__ );
$button_image_url = plugins_url ( '/images/paypal_btn_donate_lg.gif', __FILE__ );
$disabled_button_image_url = plugins_url ( '/images/paypal_btn_donate_lg_disabled.gif', __FILE__ );
$section = "<div class='dgx-donate-form-section' id='dgx-donate-form-payment-section'>"
. "<p>"
. "<input class='dgx-donate-pay-enabled' type='image' src='" . esc_url ( $button_image_url ) .
"' value='" . esc_attr__ ( 'Donate Now', 'seamless-donations' ) . "'/>"
. "<img class='dgx-donate-pay-disabled' src='" . esc_url ( $disabled_button_image_url ) . "' />"
. "<img class='dgx-donate-busy' src='" . esc_url ( $processing_image_url ) . "' />"
. "</p>"
. "<p class='dgx-donate-error-msg'></p>"
. "</div>\n";
$form_content .= $section;
return $form_content;
}
/******************************************************************************************************/
function dgx_donate_paypalstd_detail () {
echo "<p>TODO: dgx_donate_paypalstd_detail</p>";
}
/******************************************************************************************************/
function dgx_donate_paypalstd_ajax_checkout () {
// Log
dgx_donate_debug_log ( '----------------------------------------' );
dgx_donate_debug_log ( 'DONATION TRANSACTION STARTED' );
dgx_donate_debug_log ( 'Test mode: B' );
$php_version = phpversion ();
dgx_donate_debug_log ( "PHP Version: $php_version" );
dgx_donate_debug_log ( "Seamless Donations Version: " . dgx_donate_get_version () );
dgx_donate_debug_log ( "User browser: " . seamless_donations_get_browser_name() );
dgx_donate_debug_log ( 'IPN: ' . plugins_url ( '/pay/paypalstd/ipn.php', __FILE__ ) );
$nonce = $_POST['nonce'];
if( ! wp_verify_nonce ( $nonce, 'dgx-donate-nonce' ) ) {
dgx_donate_debug_log ( 'Payment process nonce validation failure.' );
die( 'Access Denied.' );
} else {
dgx_donate_debug_log ( "Payment process nonce $nonce validated." );
}
$sd4_mode = get_option ( 'dgx_donate_start_in_sd4_mode' );
$session_id = $_POST['sessionID'];
dgx_donate_debug_log ( "Session ID retrieved from _POST: $session_id" );
// now attempt to retrieve session data to see if it already exists (which would trigger an error)
if( $sd4_mode == false ) {
// use the old transient system
$session_data = get_transient ( $session_id );
dgx_donate_debug_log ( 'Looking for pre-existing session data (legacy transient mode): ' . $session_id );
} else {
// use the new guid/audit db system
$session_data = seamless_donations_get_audit_option ( $session_id );
dgx_donate_debug_log ( 'Looking for pre-existing session data (guid/audit db mode): ' . $session_id );
}
if( $session_data !== false ) {
dgx_donate_debug_log ( 'Session data already exists, returning false' );
die();
} else {
dgx_donate_debug_log ( 'Duplicate session data not found. Payment process data assembly can proceed.' );
// all of this no longer necessary for transfer to PayPal, just for storage in local audit table
$referringUrl = $_POST['referringUrl'];
$donationAmount = $_POST['donationAmount'];
$userAmount = $_POST['userAmount'];
$repeating = $_POST['repeating'];
$designated = $_POST['designated'];
$designatedFund = $_POST['designatedFund'];
$tributeGift = $_POST['tributeGift'];
$memorialGift = $_POST['memorialGift'];
$honoreeName = $_POST['honoreeName'];
$honorByEmail = $_POST['honorByEmail'];
$honoreeEmail = $_POST['honoreeEmail'];
$honoreeAddress = $_POST['honoreeAddress'];
$honoreeCity = $_POST['honoreeCity'];
$honoreeState = $_POST['honoreeState'];
$honoreeProvince = $_POST['honoreeProvince'];
$honoreeCountry = $_POST['honoreeCountry'];
if( 'US' == $honoreeCountry ) {
$honoreeProvince = '';
} else if( 'CA' == $honoreeCountry ) {
$honoreeState = '';
} else {
$honoreeState = '';
$honoreeProvince = '';
}
$honoreeZip = $_POST['honoreeZip'];
$honoreeEmailName = $_POST['honoreeEmailName'];
$honoreePostName = $_POST['honoreePostName'];
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$addToMailingList = $_POST['addToMailingList'];
$address = $_POST['address'];
$address2 = $_POST['address2'];
$city = $_POST['city'];
$state = $_POST['state'];
$province = $_POST['province'];
$country = $_POST['country'];
if( 'US' == $country ) {
$province = '';
} else if( 'CA' == $country ) {
$state = '';
} else {
$state = '';
$province = '';
}
$zip = $_POST['zip'];
$increaseToCover = $_POST['increaseToCover'];
$anonymous = $_POST['anonymous'];
$employerMatch = $_POST['employerMatch'];
$employerName = $_POST['employerName'];
$occupation = $_POST['occupation'];
$ukGiftAid = $_POST['ukGiftAid'];
// Resolve the donation amount
if( strcasecmp ( $donationAmount, "OTHER" ) == 0 ) {
$amount = floatval ( $userAmount );
} else {
$amount = floatval ( $donationAmount );
}
if( $amount < 1.00 ) {
$amount = 1.00;
}
// Repack the POST
$post_data = array();
$post_data['REFERRINGURL'] = $referringUrl;
$post_data['SESSIONID'] = $session_id;
$post_data['AMOUNT'] = $amount;
$post_data['REPEATING'] = $repeating;
$post_data['DESIGNATED'] = $designated;
$post_data['DESIGNATEDFUND'] = $designatedFund;
$post_data['TRIBUTEGIFT'] = $tributeGift;
$post_data['MEMORIALGIFT'] = $memorialGift;
$post_data['HONOREENAME'] = $honoreeName;
$post_data['HONORBYEMAIL'] = $honorByEmail;
$post_data['HONOREEEMAIL'] = $honoreeEmail;
$post_data['HONOREEADDRESS'] = $honoreeAddress;
$post_data['HONOREECITY'] = $honoreeCity;
$post_data['HONOREESTATE'] = $honoreeState;
$post_data['HONOREEPROVINCE'] = $honoreeProvince;
$post_data['HONOREECOUNTRY'] = $honoreeCountry;
$post_data['HONOREEZIP'] = $honoreeZip;
$post_data['HONOREEEMAILNAME'] = $honoreeEmailName;
$post_data['HONOREEPOSTNAME'] = $honoreePostName;
$post_data['FIRSTNAME'] = $firstName;
$post_data['LASTNAME'] = $lastName;
$post_data['PHONE'] = $phone;
$post_data['EMAIL'] = $email;
$post_data['ADDTOMAILINGLIST'] = $addToMailingList;
$post_data['ADDRESS'] = $address;
$post_data['ADDRESS2'] = $address2;
$post_data['CITY'] = $city;
$post_data['STATE'] = $state;
$post_data['PROVINCE'] = $province;
$post_data['COUNTRY'] = $country;
$post_data['ZIP'] = $zip;
$post_data['INCREASETOCOVER'] = $increaseToCover;
$post_data['ANONYMOUS'] = $anonymous;
$post_data['PAYMENTMETHOD'] = "PayPal";
$post_data['EMPLOYERMATCH'] = $employerMatch;
$post_data['EMPLOYERNAME'] = $employerName;
$post_data['OCCUPATION'] = $occupation;
$post_data['UKGIFTAID'] = $ukGiftAid;
$post_data['SDVERSION'] = dgx_donate_get_version ();
// Sanitize the data (remove leading, trailing spaces quotes, brackets)
foreach( $post_data as $key => $value ) {
$temp = trim ( $value );
$temp = str_replace ( "\"", "", $temp );
$temp = strip_tags ( $temp );
$post_data[ $key ] = $temp;
}
if( $sd4_mode == false ) {
// Save it all in a transient
$transient_token = $post_data['SESSIONID'];
set_transient ( $transient_token, $post_data, 7 * 24 * 60 * 60 ); // 7 days
dgx_donate_debug_log ( 'Saving transaction data using legacy mode' );
} else {
seamless_donations_update_audit_option ( $session_id, $post_data );
dgx_donate_debug_log ( 'Saving transaction data using guid/audit db mode' );
}
// more log data
dgx_donate_debug_log ( 'Name: ' . $post_data['FIRSTNAME'] . ' ' . $post_data['LASTNAME'] );
dgx_donate_debug_log ( 'Amount: ' . $post_data['AMOUNT'] );
dgx_donate_debug_log ( "Preparation complete. It is now up to PayPal to return data via IPN." );
// Return success to AJAX caller as " code | message "
// A return code of 0 indicates success, and the returnMessage is ignored
// A return code of 1 indicates failure, and the returnMessage contains the error message
$returnMessage = "0|SUCCESS";
echo $returnMessage;
die(); // this is required to return a proper result
}
}
add_action ( 'wp_ajax_dgx_donate_paypalstd_ajax_checkout', 'dgx_donate_paypalstd_ajax_checkout' );
add_action ( 'wp_ajax_nopriv_dgx_donate_paypalstd_ajax_checkout', 'dgx_donate_paypalstd_ajax_checkout' );