From 894ab1aba47a42d26c2617d80045625dadf94d87 Mon Sep 17 00:00:00 2001 From: Nathan Coad Date: Tue, 4 Mar 2014 16:21:10 +1100 Subject: [PATCH] Added partial payment option --- booking.balance.inc | 3 +- booking.confirm.inc | 3 +- booking.emails.inc | 41 +++++++++++++++++ booking.module | 10 +++++ booking.partialbalance.inc | 91 ++++++++++++++++++++++++++++++++++++++ booking.paypal.inc | 2 + booking.tokens.inc | 16 ++++++- 7 files changed, 163 insertions(+), 3 deletions(-) create mode 100644 booking.partialbalance.inc diff --git a/booking.balance.inc b/booking.balance.inc index fea8c6f..bdca1fb 100644 --- a/booking.balance.inc +++ b/booking.balance.inc @@ -14,7 +14,8 @@ function booking_balance_page() { //verify that arg(1) is a uuid if (! preg_match('/^[0-9A-Fa-f\-]+$/', arg(1))) { //parameter from url is not what we were expecting - drupal_set_message("Error: Invalid session ID supplied. Please use the contact us form to let us know.", 'error', FALSE); + drupal_set_message("Error: Invalid session ID supplied to the balance payment page. Please use the contact us form to let us know.", 'error', FALSE); + drupal_goto(''); return ""; } diff --git a/booking.confirm.inc b/booking.confirm.inc index 6c4e199..280e634 100644 --- a/booking.confirm.inc +++ b/booking.confirm.inc @@ -14,7 +14,8 @@ function booking_confirm_page() { //verify that arg(1) is a uuid if (! preg_match('/^[0-9A-Fa-f\-]+$/', arg(1))) { //parameter from url is not what we were expecting - drupal_set_message("Error: Invalid session ID supplied. Please use the contact us form to let us know.", 'error', FALSE); + drupal_set_message("Error: Invalid session ID supplied to the registration confirmation page. Please use the contact us form to let us know.", 'error', FALSE); + drupal_goto(''); return ""; } diff --git a/booking.emails.inc b/booking.emails.inc index 660d03a..d85281f 100644 --- a/booking.emails.inc +++ b/booking.emails.inc @@ -164,6 +164,47 @@ function _booking_balance_payment_email($nid) $params['headers']['Bcc'] = "it@coadcorp.com"; } +/** + * Function to generate email to be sent to the registrant thanking them for a partial balance payment + * + * @param $nid - the registration node + */ +function _booking_partialbalance_payment_email($nid) +{ + global $event; + global $user; + $language = user_preferred_language($user); + + //load the node matching this id + $node = node_load($nid); + $tokens = booking_define_personspecific_tokens($node); + + if ($tokens['payment-required'] <= 0) + { + watchdog('booking', "Not sending amount owing email, since this person doesn't owe any money: @info", array('@info' => var_export($node, TRUE))); + return; + } + + //calculate the from email address + $from = t('!event Registrations ', array('!event' => $event->booking_eventname, + '!email' => variable_get('booking_contact_email', variable_get('site_mail', ini_get('sendmail_from'))) + )); + + //calculate the remaining parameters + $to = $node->booking_email; + $subject = t('!event Partial Payment Required', array('!event' => $event->booking_eventname)); + $params['subject'] = $subject; + $params['headers']['Bcc'] = "it@coadcorp.com, " . variable_get('booking_notify_email', variable_get('site_mail', ini_get('sendmail_from'))); + + //TODO: Married couple version of this + $params['body'] = token_replace(variable_get('booking_email_partialpayment_received_text'), $tokens); + + //send the email + drupal_mail('booking', 'registration_mail_bal_outstanding', $to, $language, $params, $from); + $params['headers']['Bcc'] = "it@coadcorp.com"; +} + + /** * Function to generate email to be sent to the registrant based on custom email template configured in text tokens page * diff --git a/booking.module b/booking.module index e3d9cdb..4305aa4 100644 --- a/booking.module +++ b/booking.module @@ -33,6 +33,8 @@ module_load_include('inc', 'booking', 'booking.register'); module_load_include('inc', 'booking', 'booking.confirm'); // Load the include that contains the registration balance payment page module_load_include('inc', 'booking', 'booking.balance'); +// Load the include that contains the registration partial balance payment page +module_load_include('inc', 'booking', 'booking.partialbalance'); // Load the include that contains the registration reports pages module_load_include('inc', 'booking', 'booking.reports'); // Load the include for the admin pages @@ -222,6 +224,14 @@ function booking_menu() { 'type' => MENU_CALLBACK, ); + $items['partpay/%/%'] = array( + 'title' => 'Registration Partial Payment', + 'page callback' => 'booking_partial_balance_page', + 'page arguments' => array(4), //include the temporary id and the amount + 'access arguments' => array('access booking form'), + 'type' => MENU_CALLBACK, + ); + $items['travel/%'] = array( 'title' => 'Travel Details Page', 'page callback' => 'booking_travel_page', diff --git a/booking.partialbalance.inc b/booking.partialbalance.inc new file mode 100644 index 0000000..6886d43 --- /dev/null +++ b/booking.partialbalance.inc @@ -0,0 +1,91 @@ +'); + return ""; + } + + //verify that arg(2) is a number + if (! preg_match('/^[0-9]+$/', $partial_amount)) { + drupal_set_message("Error: Invalid payment amount supplied to the partial payment page. Please use the contact us form to let us know.", 'error', FALSE); + drupal_goto(''); + return ""; + } + + + //TODO: if the person says they're married, query to see if they're listed partner has already registered and paid + //if they have, then let the new registration person know their partner has already paid, and send them the confirmation email + + //work out the node id from the session id + $query = db_select('booking_person', 'p'); + $query->condition('p.booking_tempid', $temp_id, '=') + ->fields('p', array('nid')); + $person = $query->execute() + ->fetchObject(); + + if ($person) + { + //load all the fields + $node = node_load($person->nid); + //calculate the invoice ID + $invoiceid = $person->nid . '_bal' . REQUEST_TIME . '_' . $node->booking_lastname . '-' . $node->booking_firstname; + //maximum length for invoice id in paypal is 127 characters so truncate if necessary + $invoiceid = substr($invoiceid, 0, 126); + //there was no matching session id in the database + } else { + drupal_set_message("Unable to find matching session ID " . $temp_id, 'error', FALSE); + drupal_goto(''); + return ""; + } + + //populate tokens + $tokens = booking_define_personspecific_tokens($node); + + //check they're not trying to pay too much + if ($partial_amount > $tokens['paypal-total-amount']) + { + //just use the normal amount instead + $partial_amount = $tokens['paypal-total-amount']; + } + + //generate the paypal form + $tokens['paypal-total-form'] = _booking_paypal_form($node, $invoiceid, $partial_amount, "Pay $" . $partial_amount); + + //If the amount outstanding is zero, then display information to that effect. + if ($tokens['paypal-total-amount'] == 0) + { + $output = token_replace(variable_get('booking_regn_balance_page_paid'), $tokens); + } + else + { + $output = token_replace(variable_get('booking_regn_partial_balance_page'), $tokens); + //optional additional text for married people + if ($node->booking_married == 'Y') + $output .= token_replace(variable_get('booking_regn_balance_married_text'), $tokens); + } + + //put all the bits together + $return_array[] = array('paragraph' => array('#type' => 'markup', '#markup' => $output)); + //$return_array[] = array('form' => $paypal_form); + + //return the form + return $return_array; + +} + diff --git a/booking.paypal.inc b/booking.paypal.inc index 661fd76..69dc0de 100644 --- a/booking.paypal.inc +++ b/booking.paypal.inc @@ -255,6 +255,8 @@ function _booking_process_payment($data) { { 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); } diff --git a/booking.tokens.inc b/booking.tokens.inc index c688128..6267b70 100644 --- a/booking.tokens.inc +++ b/booking.tokens.inc @@ -233,7 +233,13 @@ $booking_registration_intro_text = variable_get('booking_registration_intro_text //'#format' => 'full_html', //'#default_value' => $booking_regn_balance_married_text['value'], ); - + $form['balance']['booking_regn_partial_balance_page'] = array( + '#title' => t('Text to use on the partial balance payment page.'), + '#type' => 'textarea', + '#description' => t(''), + '#default_value' => variable_get('booking_regn_partial_balance_page', ''), + ); + /*People reporting pages*/ $form['people_lists'] = array( '#type' => 'fieldset', @@ -335,6 +341,13 @@ $booking_registration_intro_text = variable_get('booking_registration_intro_text '#description' => t(''), '#default_value' => variable_get('booking_email_regn_complete_text', $booking_email_regn_complete_text), ); + $form['emails']['booking_email_partialpayment_received_text'] = array( + '#title' => t('Email text to send a person thanking them for their partial payment'), + '#type' => 'textarea', + //'#format' => 'full_html', + '#description' => t(''), + '#default_value' => variable_get('booking_email_partialpayment_received_text', ''), + ); $form['emails']['booking_email_waitinglist_text'] = array( '#title' => t('Email text to indicate a person has registered but is on the waiting list'), '#type' => 'textarea', @@ -355,6 +368,7 @@ $booking_registration_intro_text = variable_get('booking_registration_intro_text '#description' => t(''), '#default_value' => variable_get('booking_email_paymentoutstanding_text', $booking_email_paymentoutstanding_text), ); + $form['emails']['booking_email_paymentoutstanding_married_text'] = array( '#title' => t('Email text to send a married couple reminding them of how much they both owe (only applies when combined pricing enabled)'), '#type' => 'textarea',