Search

I need to be able to trigger several different emails to different recipients based on the PayPal Payments IPN Event. I would also like to include data within the email from the original invoice section linked via ‘txn_id’. Could I include a data source on the page and then filter it based on Pay Pal response? Can then use ETM or ETM to send my emails?

I have had a read through this post http://getsymphony.com/discuss/thread/64323/3/#position-59 but this doesn’t mention grabbing data from another data source just the Pay Pal response. Does anyone have any code examples, or a concrete workflow I can follow?

Should I be looking to use ETF or ETM?

Thanks for helping,

Andy

Can anyone help advise with my question above please.

The "Save IPN data" event in the extension sits outside the normal event save flow, so you can't hook into the delegates as you normally would.

My advice would be to either modify the event.paypal_payments_ipn.php file and add the callback you need, or simply create another custom event that replicates the checks in the IPN event and then calls the ETM extension using something along the lines of this code example.

Thanks @Makenosound, I have started trying to tweak the IPN Event. I have tried using the sample code above to send a basic empty email using no Data Sources, the event still works but no email is sent. My code currently looks like this:

require_once(EXTENSIONS . '/email_template_manager/lib/class.emailtemplatemanager.php');
$template = EmailTemplateManager::load('pay-pal-ipn');
$template->parseProperties();
$properties = $template->getParsedProperties();
$output = $template->render();

My email template is 'Pay Pal IPN' I have tried using this string and handle?

@muthahubbard, there are two ways of getting this to work. The first way is to simply add the filter to the event by including

        public $eParamFILTERS = array(
            'etm-TEMPLATE_HANDLE_HERE'
        );

in the top your IPN Event. This includes the ETM in the event as if it was a standard symphony event. If this does not work, or if you need more control, you should indeed modify the event a bit further.

Is the code you posted all the code you have? The piece you showed here does not do anything with the template that is rendered.

To actually send the email you will need to use the email API of Symphony.

As a starting point, try to add this after the code you posted:

$email = Email::create();
$email->subject = 'Your Subject';
$email->text_plain = $output['plain'];
$email->text_html = $content['html'];
$email->recipients = array('name'=>'example@example.com');
$email->send();

Ofcourse, you will have to replace the subject with your own subject, the recipient(s) with your own recipients etc, but this should give you an impression of how its done.

Does that clarify things a bit, or am I going too fast?

Thanks for helping @creativedutchmen. I have tried adding the filter previously to call the ETM but this does not appear to fire / send a email.

Regarding the code above I posted I have been following this post - http://getsymphony.com/discuss/thread/64323/3/#position-59

The alternative code sample above I guess just sends a native email and not via the ETM? Ultimately I need to be able to include data from several data sources and send different emails to different recipients.

As always really appreciate everyone's help.

Hi all, still looking for some help hooking ETM into PayPal IPN event?

Hi,

I am also having trouble with the same issue. While I can send a plain email from the IPN script it breaks whenever I try and include the following lines:

$template->parseProperties();
$properties = $template->getParsedProperties();
$content = $template->render();

The first issue I come across appears to be with line 72 in class.emailtemplate.php:

$dom->loadXML($this->getXML());

I would really appreciate any help and possibly a full working example as I have read through all associated posts and tried to figure it out but so far haven't got enough knowledge of Symphony to get it working.

Hi,

As an update to the above, I have found that the following code works:

require_once(EXTENSIONS . '/email_template_manager/lib/class.emailtemplatemanager.php');

$email = Email::create();

$template = EmailTemplateManager::load('members');
$template->addParams(array("etm-entry-id" => $entry_id));
$xml = $template->processDatasources();
$template->setXML($xml->generate());
$template->recipients = $emailaddr;
$template->parseProperties();
$properties = $template->getParsedProperties();
$content = $template->render();

if(!empty($content['subject'])){
    $email->subject = $content['subject'];
}

if(isset($content['reply-to-name'])){
    $email->reply_to_name = $content['reply-to-name'];
}

if(isset($content['reply-to-email-address'])){
    $email->reply_to_email_address = $content['reply-to-email-address'];
}

if(isset($content['plain']))
    $email->text_plain = $content['plain'];

if(isset($content['html']))
    $email->text_html = $content['html'];

$email->recipients = array($emailaddr);

$email->send();

You just need to change the following parameter to the handle for your email:

$template = EmailTemplateManager::load('members');

and also you need to set the $emailaddr variable with the email address passed in the $_POST.

Hope this helps anyone else having this issue.

Thanks

Hi @hdPhoenix I just got really excited seeing your post. I have tried using the code above but get a PHP error:

PHP Fatal error: Call to a member function addParams() on a non-object

Any help would really be appreciated.

I have fixed the error by deleting and re-creating the email template.

My last issue appears to trying to use a email address from the data sources rather than the pay pal response. I can use xpath to select the email address and addd to the subject but I cant seem to use the email address, returns the xpath string.

That is strange, can you post the query you are using to select your email address?

Solved. I was trying to access the value in PHP before the xpath had been processed.

Updated PHP to:

$properties = $template->getParsedProperties();

$emailaddr = $properties['recipients'][0];

Very happy to finally have this working :)

@muthahubbard it would be great to see your full working code. I too have been trying to do this for some time without success! Would you mind posting the full solution? Thanks

^ Seconded. It would be pretty helpful to see the event in it's entirity, as I am following the above but having the same problems - I cannot get an email to fire from this event at all.

Here's my working copy that I have been using.

<?php
require_once(EXTENSIONS . '/email_template_manager/lib/class.emailtemplatemanager.php');
Final Class eventPaypal_payments_ipn extends Event
{
    public static function about()
    {
        return array(
            'name' => 'PayPal Payments: Save IPN data',
            'author' => array(
                'name' => 'Max Wheeler',
                'website' => 'http://www.icelab.com.au',
                'email' => 'max@icelab.com.au'
            ),
            'version' => '1.0.4',
            'release-date' => '2011-05-01'
        );
    }
    public function __construct()
    {
        parent::__construct();
        $this->_driver = Symphony::ExtensionManager()->create('paypal_payments');
    }
    public function load()
    {
        if (!isset($_POST['verify_sign']))
            return NULL;
        $req = 'cmd=_notify-validate';
        foreach ($_POST as $key => $value) {
            if (get_magic_quotes_gpc()) {
                $_POST[$key] = stripslashes($value);
                $value       = stripslashes($value);
            }
            $value = urlencode($value);
            $req .= "&$key=$value";
        }
        $url = $this->_driver->_build_paypay_url(true);
        $ch  = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FAILONERROR, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 3);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
        $result = curl_exec($ch);
        curl_close($ch);
        if (strcmp($result, "VERIFIED") == 0 && is_array($_POST) && !empty($_POST))
            return $this->__trigger();
        return NULL;
    }
    public static function documentation()
    {
        $docs   = array();
        $docs[] = 'removed';
        return implode("n", $docs);
    }
    protected function __trigger()
    {
        $valid_variables    = array(
            'address',
            'address_city',
            'address_country',
            'address_country_code',
            'address_name',
            'address_state',
            'address_status',
            'address_street',
            'address_zip',
            'adjustment_reversal',
            'authorization',
            'auth_amount',
            'auth_exp',
            'auth_id',
            'auth_status',
            'business',
            'buyer-complaint',
            'chargeback',
            'chargeback_reimbursement',
            'chargeback_settlement',
            'charset',
            'confirmed',
            'contact_phone',
            'custom',
            'echeck',
            'exchange_rate',
            'first_name',
            'guarantee',
            'instant',
            'intl',
            'invoice',
            'item_name',
            'item_number',
            'last_name',
            'mc_currency',
            'mc_fee',
            'mc_gross',
            'mc_handling',
            'mc_shipping',
            'memo',
            'multi-currency',
            'notify_version',
            'order',
            'verify_sign',
            'other',
            'parent_txn_id',
            'payer_business_name',
            'payer_email',
            'payer_id',
            'payer_status',
            'paymentreview',
            'payment_date',
            'payment_fee',
            'payment_gross',
            'payment_status',
            'payment_type',
            'pending_reason',
            'protection_eligibility',
            'quantity',
            'reason_code',
            'receiver_email',
            'receiver_id',
            'refund',
            'remaining_settle',
            'residence_country',
            'settle_amount',
            'settle_currency',
            'shipping',
            'shipping_method',
            'tax',
            'test_ipn',
            'transaction_entity',
            'txn_id',
            'txn_type',
            'unconfirmed',
            'unilateral',
            'unverified',
            'upgrade',
            'verified',
            'verify'
        );
        $required_variables = array(
            'invoice',
            'payment_type',
            'payment_date',
            'payment_status',
            'address_status',
            'payer_status',
            'first_name',
            'last_name',
            'payer_email',
            'payer_id',
            'address_name',
            'address_country',
            'address_country_code',
            'address_zip',
            'address_state',
            'address_city',
            'address_street',
            'residence_country',
            'tax',
            'mc_currency',
            'mc_fee',
            'mc_gross',
            'txn_type',
            'txn_id',
            'notify_version',
            'verify_sign'
        );
        $matches            = array();
        foreach ($_POST as $key => $val) {
            if (in_array($key, $valid_variables))
                $matches[$key] = utf8_encode(General::sanitize($val));
        }
        $output = new XMLElement('paypal-payments-ipn');
        $log    = array();
        if (!empty($matches)) {
            foreach ($matches as $key => $val) {
                $output->appendChild(new XMLElement($key, $val));
                if (in_array($key, $required_variables)) {
                    if ($key == 'payment_date')
                        $val = strftime('%Y-%m-%d %H:%M:%S', strtotime($val));
                    $log[$key] = $val;
                }
            }
            $entry_id     = $log['invoice'];
            $entryManager = new EntryManager(Symphony::Engine());
            $fieldManager = new FieldManager(Symphony::Engine());
            $entries      = $entryManager->fetch($entry_id, null, null, null, null, null, false, true);
            if (count($entries) > 0) {
                $entry      = $entries[0];
                $section_id = $entry->get('section_id');
                $fields     = Symphony::Database()->fetch("
                        SELECT `id`, `label` FROM `sym_fields` WHERE `parent_section` = '$section_id'
                    ");
                foreach ($fields as $field) {
                    $label = $field['label'];
                    if (in_array($label, $valid_variables)) {
                        $value = $log[$label];
                        $entry->setData($field['id'], array(
                            'handle' => Lang::createHandle($value),
                            'value' => $value
                        ));
                    }
                }
                $entry->commit();
                $output->setAttribute('result', 'success');
                $output->appendChild(new XMLElement('message', 'PayPal data logged and reconciled.'));
            } else {
                $output->setAttribute('result', 'error');
                $output->appendChild(new XMLElement('message', 'No matching entry, could not reconcile payment data.'));
            }
            Symphony::Database()->query("
                    DELETE FROM
                        `sym_paypalpayments_logs`
                    WHERE
                        `txn_id` = '{$log['txn_id']}' AND
                        `payment_status` = '{$log['payment_status']}'
                ");
            Symphony::Database()->insert($log, 'sym_paypalpayments_logs');
            $email    = Email::create();
            $template = EmailTemplateManager::load('registration-confirm');
            $template->addParams(array(
                "etm-entry-id" => $entry_id
            ));
            $xml = $template->processDatasources();
            $template->setXML($xml->generate());
            $template->parseProperties();
            $properties = $template->getParsedProperties();
            $content    = $template->render();
            if (!empty($content['subject'])) {
                $email->subject = $content['subject'];
            }
            if (!empty($content['recipients'])) {
                $email->recipients = $content['recipients'];
            }
            if (isset($content['reply-to-name'])) {
                $email->reply_to_name = $content['reply-to-name'];
            }
            if (isset($content['reply-to-email-address'])) {
                $email->reply_to_email_address = $content['reply-to-email-address'];
            }
            if (isset($content['plain']))
                $email->text_plain = $content['plain'];
            if (isset($content['html']))
                $email->text_html = $content['html'];
            $email->send();
        }
        return $output;
    }
}

Thanks @koolaid that is really helpful. Please could you confirm which version of Symphony you are using this with.

I am currently on 2.3.2

I finally got round to trying this out and it works great in Sym 2.3.3. Thanks Mike.

Hi guys, wondering if anyone can help me. I have a similar set up to that mentioned above.

Basically I have paypal payments setup, this works great, I have used the code above that @koolaid kindly shared to link to email template manager. This too works great and sends an email.

However I am trying to include information from my database in the email, i.e. payment details. I have created a custom data source and linked this to my email template, which works when I hard code in the entry id - however when I try to access the parameter $etm-entry-id - I am not having any success.

Do you know of a way to filter a custom data source with the $etm-entry-id created in the code attached to the event.paypalpaymentsipn.php file;

$template->addParams(array(
                "etm-entry-id" => $entry_id
            ));

I have tried this in my custom datasource;

$etmentryid = Frontend::instance()->Page()->_param['etm-entry-id'];

but it doesn't work, if I change it to 'current-time' it brings out the correct value.

Any help would be greatly appreciated. Thank you.

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details