diff --git a/booking.helper.inc b/booking.helper.inc index e74d947..5177002 100644 --- a/booking.helper.inc +++ b/booking.helper.inc @@ -1096,6 +1096,95 @@ function _booking_amount_owing($person, $amount_paid = 0, $include_paypal_fees = return number_format($amount_owing, 2, '.', ''); } +/** + * Function to handle any post-payment notification emails for either paypal or manual payment + * still a WIP + * + * @param $nid - the node ID relating to this person + * @param $person - the populated node object representing this person including related price information + * @param $balance_payment - boolean to indicate if this transaction was to complete a payment + * @return nothing + */ +function _booking_postpayment_trigger($nid, $person, $balance_payment) +{ + global $event; + + //If there is no outstanding balance, send a payment complete email + $amount_owing = _booking_amount_owing($person); + //if ($total >= $person->booking_total_pay_reqd) + if ($amount_owing == 0) + { + //set the payment complete flag + db_update('booking_person') + ->fields(array( + 'booking_payment_complete' => 'Y', + )) + ->condition('nid', $nid) + ->execute(); + + //this should always be a balance payment type email, since that tells the user that their payment is completed + _booking_registration_email($nid, TRUE); + + //if we are combining payments, and this person has a linked spouse + if ((variable_get('booking_enable_combined_pricing', 0) == 1) && ($person->booking_partner_id > 0)) + { + //check spouse booking_status and payment info + $spouse = db_select('booking_person', 'p') + ->condition('p.nid', $person->booking_partner_id,'=') + ->fields('p', array('booking_amount_paid', 'booking_status', 'booking_total_pay_reqd')) + ->execute() + ->fetchObject(); + + //update the spouse's status from "not paid" if required + $spouse_status = $spouse->booking_status == 0 ? 1 : $spouse->booking_status; + watchdog('booking', 'Setting status for spouse id !id to !new from !status', array('!id' => $person->booking_partner_id, + '!new' => $spouse_status, '!status' => $spouse->booking_status)); + + //set the spouse's payment required to be zero or equal to their previous payment total + $spouse_new_amount_reqd = $spouse->booking_amount_paid > 0 ? $spouse->booking_amount_paid : 0; + watchdog('booking', 'Setting amount owing for spouse id !id to !new from !old.', array('!id' => $person->booking_partner_id, + '!new' => $spouse_new_amount_reqd, '!old' => $spouse->booking_total_pay_reqd)); + + //update the database for this person + db_update('booking_person') + ->fields(array( + 'booking_total_pay_reqd' => $spouse_new_amount_reqd, + 'booking_status' => $spouse_status, + 'booking_payment_complete' => 'Y', + )) + ->condition('nid', $person->booking_partner_id) + ->execute(); + + //send an email to the spouse + _booking_registration_email($person->booking_partner_id, TRUE); + } + /* + elseif (variable_get('booking_enable_combined_pricing', 0) == 1) + { + watchdog('booking', 'Combined pricing is enabled, but this person has a partner id of !id.', array('!id' => $person->booking_partner_id)); + } //end spouse check + */ + } + //if this was an initial payment we might need to send a notification + elseif ($balance_payment == FALSE) + { + //send a notification email if we didn't automatically send one earlier + if (variable_get('booking_auto_confirm_email', 0) == 0) + { + _booking_registration_email($nid, FALSE); + } + } + else //this person still has an outstanding balance so just send a confirmation email + { + watchdog('booking', 'This balance payment of !payment was insufficient for !id to completely pay the total outstanding of !outstanding.', + array('!id' => $nid, '!payment' => $data['mc_gross'], '!outstanding' => $amount_owing)); + //send the person an email thanking them for their partial payment + _booking_partialbalance_payment_email($nid); + //TODO: create an email specifically for partial-balance payments + //_booking_registration_email($nid, $balance_payment); + } +} + /** * Function to process the amount to refund a person when they withdraw their registration * diff --git a/booking.paypal.inc b/booking.paypal.inc index 85fce7a..ce8b645 100644 --- a/booking.paypal.inc +++ b/booking.paypal.inc @@ -58,15 +58,31 @@ function booking_paypal_ipn() { //verify the notification with paypal if(!_booking_paypal_ipn_verify($ipn)) { - watchdog('booking_paypal', 'Payment verification did not succeed. Retrying.'); - if(!_booking_paypal_ipn_verify($ipn)) { - return; - } else { - watchdog('booking_paypal', 'Payment verification succeeded on second attempt.'); + watchdog('booking_paypal', 'Payment verification did not succeed. Retrying...'); + + //retry the attempt to verify the payment 5 times, sleeping in between each attempt + $continue = TRUE; + $i = 0; + while ($continue && $i < 5) + { + if(!_booking_paypal_ipn_verify($ipn)) { + watchdog('booking_paypal', 'Payment verification did not succeed on attempt $i.'); + $i++; + sleep(3); + } else { + $continue = FALSE; + watchdog('booking_paypal', 'Payment verification succeeded on attempt $i.'); + } + } + + //exited the loop above with an unsuccessful verification, so return from the function now + if ($continue) + { + watchdog('booking_paypal', 'Payment verification was unsuccessful.'); + return; } } - - //if we're still exectuing then the verification was successful + //verification was successful if we reach this point /* if ($ipn['payment_status'] != 'Pending' && variable_get('booking_paypal_sandbox', 0) == 1) { @@ -147,14 +163,6 @@ function _booking_process_payment($data) { ->execute(); //Get the person's info - /* - $payment = db_select('booking_person', 'p') - ->condition('p.nid', $nid, '=') - ->fields('p', array('booking_amount_paid', 'booking_status', 'booking_total_pay_reqd', 'booking_partner_id')) - ->execute() - ->fetchObject(); - */ - $person = node_load($nid); //check if we found a person matching this payment @@ -201,7 +209,11 @@ function _booking_process_payment($data) { )) ->condition('nid', $nid) ->execute(); - + + //replace the code below with a call to the helper function + _booking_postpayment_trigger($nid, $person, $balance_payment); + + /* //If there is no outstanding balance, send a payment complete email $amount_owing = _booking_amount_owing($person); //if ($total >= $person->booking_total_pay_reqd) @@ -251,12 +263,6 @@ function _booking_process_payment($data) { //send an email to the spouse _booking_registration_email($person->booking_partner_id, TRUE); } - /* - elseif (variable_get('booking_enable_combined_pricing', 0) == 1) - { - watchdog('booking', 'Combined pricing is enabled, but this person has a partner id of !id.', array('!id' => $person->booking_partner_id)); - } //end spouse check - */ } //if this was an initial payment we might need to send a notification elseif ($balance_payment == FALSE) @@ -275,7 +281,9 @@ function _booking_process_payment($data) { _booking_partialbalance_payment_email($nid); //TODO: create an email specifically for partial-balance payments //_booking_registration_email($nid, $balance_payment); - } + } + + */ } else //couldn't find a matching nid for this invoice {