Author Topic: Payment Thank You Message  (Read 647 times)

Offline theitd

  • Jr. Member
  • **
  • Posts: 13
    • View Profile
Payment Thank You Message
« on: 13/July/2012, 09:31:00 AM »
Is there a facility to send a 'thank you for your payment' email, automatically, once an invoice is marked as paid?

Online netshine

  • Administrator
  • Hero Member
  • *****
  • Posts: 4,952
    • View Profile
Re: Payment Thank You Message
« Reply #1 on: 13/July/2012, 09:35:52 AM »
Not built-in, no, but most Payment Service Providers send a confirmation e-mail when someone makes a payment. Also, you could use the payment_received event folder to write a custom script which sends an email. This goes beyond the scope of support and requires PHP development skills (it would be a little tricky to find the client's email address - you would have to pick up the document ID from the #__nbill_gateway_tx table (primary key is the $g_tx_id which is passed to that event), then look up the entity ID from the document table, then find the appropriate record from the contact table using the primary contact ID, and get the e-mail address from there - this can all be done in a single query using inner joins).

Offline theitd

  • Jr. Member
  • **
  • Posts: 13
    • View Profile
Re: Payment Thank You Message
« Reply #2 on: 13/July/2012, 09:48:46 AM »
Thanks for the quick reply.

I can see a similar 'hook' in the events folder hierarchy, in the order_status_updated folder (google_event.php). Will the contents of events_folder /payment_received be processed as long as it contains a php file?

Is there any documentation available?

Online netshine

  • Administrator
  • Hero Member
  • *****
  • Posts: 4,952
    • View Profile
Re: Payment Thank You Message
« Reply #3 on: 13/July/2012, 10:07:45 AM »
That google_event.php file should only be there if you installed the google checkout gateway (which is no longer supported). If you are not using google checkout, you should delete that file.

More information about events is available here: http://www.nbill.co.uk/help/index.php?page=events.html

Offline theitd

  • Jr. Member
  • **
  • Posts: 13
    • View Profile
Re: Payment Thank You Message
« Reply #4 on: 13/July/2012, 10:31:29 AM »
Thanks very much. I'll take a look and let you know.

Offline theitd

  • Jr. Member
  • **
  • Posts: 13
    • View Profile
Re: Payment Thank You Message
« Reply #5 on: 13/July/2012, 06:49:16 PM »
I've pinched a lot of code from other places and managed to write (bodge) the following. Placing the file in the 'income_created' event folder, means it fires the thank_you email when marked as paid in the back-end.

Code: [Select]
<?php
//Ensure this file has been reached through a valid entry point (not always necessary eg. for class files, but included on every file to be safe!)
(defined('_VALID_MOS') || defined('_JEXEC') || defined('NBILL_VALID_NBF')) or die('Access Denied.');

$nb_database nbf_cms::$interop->database;
$trans_id $params['id'];

//Query used to match up the transaction ID to the primary email address
$sql="SELECT c.first_name, c.last_name, CONCAT_WS(' ',c. first_name,c. last_name) as name, c.email_address, c.user_id , e.id as client_id, d.document_no as record_name, d.id as record FROM `#__nbill_contact` c INNER JOIN `#__nbill_entity` e ON e.primary_contact_id = c.id INNER JOIN `#__nbill_document` d ON e.id = d.entity_id INNER JOIN `#__nbill_transaction` t ON t.document_ids = d.id WHERE t.id = '" nbf_common::get_param($params,'id') . "'"

$nb_database->setQuery($sql);
$rows $nb_database->loadObjectList();
if(
count($rows)<1){
return;
}
 
$row=$rows[0];

//Define the client specific variables
$email_address  = $row->email_address;
$email_body  = "";
$client_id = $row->client_id;
$orig_record_id =       $row->record;
$orig_record_name = $row->record_name;
$contact_name = $row->name;

//Initialise
$admin_email  "accounts@example.com"//Update with the admin address
$admin_name  "Vendor or Company"//Update with your own
$email_subject  = "Payment Received for " $orig_record_name " - Thank you.";
$email_body  .= "<body><html>";
$email_body  .= "<p>Dear " $contact_name "</p>\n\n";
$email_body  .= "<p>Your recent payment for the invoice " $orig_record_name " has been received.</p><p>Thank you!</p>\n";
$email_body  .= "<p>Best Regards,</p>";
$email_body  .= nbf_cms::$interop->site_name;
$email_body  .= "</body></html>";
$html  true;
$attachments = null;

//Send the HTML email
$mailsent nbf_cms::$interop->send_email($admin_email$admin_name$email_address$email_subject$email_body$html 0nullnull$attachments);

$status ""//Empty string = unknown error (OK = ok, other = other error message)

//Check to make sure the mail is sent
if (!$mailsent)
{
$failure true;
$status nbf_globals::$message;
}
else
{
    
$status "OK";
}

//Insert entry in email log
$sql "INSERT INTO #__nbill_email_log (`type`, `entity_id`, `document_id`, `from`, `to`, `timestamp`, `status`, `subject`, `message`, `html`) VALUES ('IN', " intval($client_id) . ", " intval($orig_record_id) . ", '" $admin_email "', '" $email_address "', " nbf_common::nb_time() . ", '" $nb_database->getEscaped($status) . "', '" $nb_database->getEscaped($email_subject) . "', '" $nb_database->getEscaped($email_body) . "', " . ($html 0) . ")";

$nb_database->setQuery($sql);
$nb_database->query();

?>

Hope someone might find this useful - and thanks again for the helpful replies.
« Last Edit: 13/July/2012, 07:00:04 PM by theitd »

Online netshine

  • Administrator
  • Hero Member
  • *****
  • Posts: 4,952
    • View Profile
Re: Payment Thank You Message
« Reply #6 on: 14/July/2012, 08:07:20 AM »
Just be aware that it is possible for income records to be created without referencing an invoice (eg. if an administrator adds an income record for bank interest), and that invoices do not necessarily have to be associated with a client, and that clients don't necessarily have to have an email address.

Offline theitd

  • Jr. Member
  • **
  • Posts: 13
    • View Profile
Re: Payment Thank You Message
« Reply #7 on: 16/July/2012, 09:05:57 AM »
Sure. I'll make a few changes and check back.

Is there a better event folder I should place it in? I tried in payment_received, but this didn't fire when updating the invoice in the back-end.

Online netshine

  • Administrator
  • Hero Member
  • *****
  • Posts: 4,952
    • View Profile
Re: Payment Thank You Message
« Reply #8 on: 16/July/2012, 09:14:50 AM »
income_created is the only one that gets called when invoices are marked as paid manually as well as when they are paid automatically, but if you only want your code to run for paid invoices associated with an email address, you have to include appropriate conditional statements so that it will not cause problems for other situations.

 

anything