PluginProbe
SureDonation – Donation Forms, Fundraising Campaigns & Donor Management / 0.0.1
SureDonation – Donation Forms, Fundraising Campaigns & Donor Management v0.0.1
1.6.1 1.6.0 1.5.1 1.5.0 1.4.0 1.3.0 trunk 0.0.1 1.0.0 1.1.0 1.1.1 1.1.2 1.2.0
suredonation / inc / ajax / donation-handler.php

donation-handler.php in SureDonation – Donation Forms, Fundraising Campaigns & Donor Management 0.0.1, at inc/ajax/donation-handler.php

185 lines 6.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * AJAX Donation Handler
4 *
5 * @package SureDonation
6 */
7
8 namespace SureDonation\Inc\Ajax;
9
10 use SureDonation\Inc\Database\Tables\Donations;
11 use SureDonation\Inc\Database\Tables\Donors;
12 use SureDonation\Inc\Emails\Email_Handler;
13 use SureDonation\Inc\Helper;
14 use SureDonation\Inc\Payments\Payment_Helper;
15 use SureDonation\Inc\Traits\Get_Instance;
16
17 // Exit if accessed directly.
18 if ( ! defined( 'ABSPATH' ) ) {
19 exit;
20 }
21
22 /**
23 * Donation_Handler class.
24 *
25 * @since 0.0.1
26 */
27 class Donation_Handler {
28 use Get_Instance;
29
30 /**
31 * Constructor.
32 *
33 * @since 0.0.1
34 */
35 public function __construct() {
36 add_action( 'wp_ajax_suredonation_submit_donation', [ $this, 'handle_donation_submission' ] );
37 add_action( 'wp_ajax_nopriv_suredonation_submit_donation', [ $this, 'handle_donation_submission' ] );
38 }
39
40 /**
41 * Handle donation form submission.
42 *
43 * @return void
44 * @since 0.0.1
45 */
46 public function handle_donation_submission() {
47 // First check if nonce exists before accessing any other POST data.
48 if ( ! isset( $_POST['suredonation_nonce'] ) ) {
49 wp_send_json_error( __( 'Security check failed', 'suredonation' ) );
50 }
51
52 // Sanitize nonce value.
53 $nonce = sanitize_text_field( wp_unslash( $_POST['suredonation_nonce'] ) );
54
55 // Now get values needed to determine nonce action.
56 $is_standalone = isset( $_POST['is_standalone'] ) && '1' === $_POST['is_standalone'];
57 $campaign_id = isset( $_POST['campaign_id'] ) ? intval( $_POST['campaign_id'] ) : 0;
58
59 // Verify nonce - different nonce for standalone vs campaign-linked forms.
60 $nonce_action = $is_standalone ? 'suredonation_donation_standalone' : 'suredonation_donation_' . $campaign_id;
61
62 if ( ! wp_verify_nonce( $nonce, $nonce_action ) ) {
63 wp_send_json_error( __( 'Security check failed', 'suredonation' ) );
64 }
65
66 // Validate campaign only if not standalone.
67 $campaign = null;
68 if ( ! $is_standalone ) {
69 if ( ! $campaign_id ) {
70 wp_send_json_error( __( 'Invalid campaign', 'suredonation' ) );
71 }
72
73 $campaign = get_post( $campaign_id );
74 if ( ! $campaign || SUREDONATION_POST_TYPE !== $campaign->post_type ) {
75 wp_send_json_error( __( 'Invalid campaign', 'suredonation' ) );
76 }
77 }
78
79 // Get form data.
80 $amount = isset( $_POST['amount'] ) ? floatval( $_POST['amount'] ) : 0;
81 $cover_fees = isset( $_POST['cover_fees'] ) ? true : false;
82 $is_anonymous = isset( $_POST['is_anonymous'] ) ? true : false;
83 $donor_name = $is_anonymous ? __( 'Anonymous', 'suredonation' ) : sanitize_text_field( wp_unslash( $_POST['donor_name'] ?? '' ) );
84 $donor_email = sanitize_email( wp_unslash( $_POST['donor_email'] ?? '' ) );
85 $donor_phone = sanitize_text_field( wp_unslash( $_POST['donor_phone'] ?? '' ) );
86 $donor_comment = sanitize_textarea_field( wp_unslash( $_POST['donor_comment'] ?? '' ) );
87
88 // Validate required fields.
89 if ( $amount <= 0 ) {
90 wp_send_json_error( __( 'Invalid donation amount', 'suredonation' ) );
91 }
92
93 if ( ! $is_anonymous ) {
94 if ( empty( $donor_name ) ) {
95 wp_send_json_error( __( 'Donor name is required', 'suredonation' ) );
96 }
97 if ( empty( $donor_email ) || ! is_email( $donor_email ) ) {
98 wp_send_json_error( __( 'Valid email address is required', 'suredonation' ) );
99 }
100 }
101
102 // Calculate fee and amounts.
103 $fee_percentage = 0.029; // 2.9%
104 $fee_fixed = 0.30;
105 $base_amount = $amount;
106 $fees_covered = 0;
107
108 if ( $cover_fees ) {
109 // Calculate the base amount from total (reverse calculation).
110 $base_amount = ( $amount - $fee_fixed ) / ( 1 + $fee_percentage );
111 $fees_covered = $amount - $base_amount;
112 }
113
114 // Get or create donor.
115 $donor_id = 0;
116 if ( ! empty( $donor_email ) ) {
117 $donor_id = Donors::get_or_create( $donor_email, $donor_name, $donor_phone );
118 }
119
120 // Get payment mode.
121 $payment_mode = 'live';
122 if ( class_exists( 'SureDonation\Inc\Payments\Payment_Helper' ) ) {
123 $payment_mode = Payment_Helper::get_payment_mode();
124 }
125
126 // Create donation in database.
127 $donation_id = Donations::add(
128 [
129 'campaign_id' => $campaign_id,
130 'donor_id' => $donor_id ? $donor_id : 0,
131 'amount' => number_format( $base_amount, 2, '.', '' ),
132 'fees_covered' => number_format( $fees_covered, 2, '.', '' ),
133 'currency' => Payment_Helper::get_currency(),
134 'gateway' => 'manual',
135 'payment_status' => 'pending',
136 'payment_mode' => $payment_mode,
137 'donor_name' => $donor_name,
138 'donor_email' => $donor_email,
139 'donor_phone' => $donor_phone,
140 'is_anonymous' => $is_anonymous ? 1 : 0,
141 'donation_type' => 'one-time',
142 'donor_comment' => $donor_comment,
143 'ip_address' => Helper::get_client_ip(),
144 'user_agent' => isset( $_SERVER['HTTP_USER_AGENT'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) : '',
145 'referer_url' => isset( $_SERVER['HTTP_REFERER'] ) ? esc_url_raw( wp_unslash( $_SERVER['HTTP_REFERER'] ) ) : '',
146 ]
147 );
148
149 if ( ! $donation_id ) {
150 wp_send_json_error( __( 'Failed to create donation', 'suredonation' ) );
151 }
152
153 // Note: Donation status will be updated by payment gateway webhooks or manual confirmation.
154
155 // Send donation confirmation email.
156 $donation_data = [
157 'id' => $donation_id,
158 'donor_name' => $donor_name,
159 'donor_email' => $donor_email,
160 'amount' => $base_amount,
161 'fees_covered' => $fees_covered,
162 'currency' => Payment_Helper::get_currency(),
163 ];
164
165 Email_Handler::send_donation_confirmation( $donation_id, $campaign_id, $donation_data );
166
167 // Get thank you message.
168 $thank_you_message = '';
169 if ( ! $is_standalone && $campaign_id ) {
170 $thank_you_message = Helper::get_campaign_meta_value( $campaign_id, 'thank_you_message', '' );
171 }
172 if ( empty( $thank_you_message ) ) {
173 $thank_you_message = esc_html__( 'Your generous contribution will make a real difference. A confirmation email has been sent to you.', 'suredonation' );
174 }
175
176 // Send success response.
177 wp_send_json_success(
178 [
179 'donation_id' => $donation_id,
180 'message' => wp_kses_post( Helper::get_string_value( $thank_you_message ) ),
181 ]
182 );
183 }
184 }
185