ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

How do I retrieve the entry_id from IPN

October 01, 2009 3:48pm

Subscribe [1]
  • #1 / Oct 01, 2009 3:48pm

    brankin

    150 posts

    I am trying to set up a way to change a custom field in an entry once an item has been purchased.  I used the ipn_handler extension framework from here and have added a query to it but am stuck on how to retrieve the entry_id from the IPN and then call that id in the query.

    Thanks for the help.
    Bryan

    Code I am using is here. Query is under “// do whatever you need to do here”

    <?php
    
    class Paypal_ipn_handler {
        var $settings = array();
        var $name            = 'Paypal IPN Handler';
        var $version         = '1.0.0';
        var $description     = 'Perform actions based on paypal events';
        var $settings_exist  = 'n';
        var $docs_url        = '';//'http://expressionengine.com';
        
        function Paypal_ipn_handler($settings = '') {
            $this->settings = $settings;
        }
            
        function evaluate_response($data, $result) {
            global $IN, $DSP, $LANG, $SESS, $PREFS, $OUT, $LOC, $FNS, $REGX, $LOG, $DB, $EXT;
            
            # continue if $EXT->end_script is false
            $EXT->end_script = FALSE;
            
            $paypal_account = ( ! $PREFS->ini('sc_paypal_account')) ? $PREFS->ini('webmaster_email') : $PREFS->ini('sc_paypal_account');
            
                    // good to have a record of what was received for testing purposes
            if($data->debug) {
                $dfile = fopen("/path/to/debug_ipn.txt", "a+"); // <- permissions are important here… 777 is nice and safe
                fwrite($dfile, "=============== " . date("F j, Y, g:i a") . " ===============\n");
                fwrite($dfile, "Response: " . $result . "\n\n");
                fwrite($dfile, "Data:\n" . print_r($data, true) . "\n\n");
                fclose($dfile);
            }
    
            // do whatever you need to do here
    
            $DB->query("UPDATE exp_weblogs_data SET my_column = 'Sale Pending' WHERE entry_id = '{ID PASSED BACK FROM PAYPAL'");
    
        }    
    
    
        # activate this extension
        function activate_extension() {
            global $DB;
    
            $DB->query($DB->insert_string('exp_extensions', array(
                'extension_id' => '',
                'class'        => "Paypal_ipn_handler",
                'method'       => "evaluate_response",
                'hook'         => "simple_commerce_evaluate_ipn_response",
                'settings'     => "",
                'priority'     => 3,
                'version'      => $this->version,
                'enabled'      => "y"
            )));
        }
        
        
        # update this extension
        function update_extension($current='') {
            global $DB;
    
            if ($current == '' OR $current == $this->version)
                {
                return FALSE;
                }
    
            if ($current < '1.0.1')
                {
                // Update to next version 1.0.1
                }
    
            if ($current < '1.0.2')
                {
                // Update to next version 1.0.2
                }
    
            $DB->query("UPDATE exp_extensions SET version = '" .
                $DB->escape_str($this->version) .
                "' WHERE class = 'Paypal_ipn_handler'");
        }
    
    
        # disable this extension
        function disable_extension() {
            global $DB;
            $DB->query("DELETE FROM exp_extensions WHERE class = 'Paypal_ipn_handler'");
        }
    }
  • #2 / Oct 01, 2009 4:08pm

    ender

    1644 posts

    the entry_id they purchased ought to be found in

    $data->post['item_number']

    I’d suggest going with

    $DB->escape_str($data->post['item_number'])

    if you need to use it in a query just on the off chance that it contains something you don’t expect.

  • #3 / Oct 01, 2009 4:26pm

    brankin

    150 posts

    Thanks for the reply.  So the query would look like this?

    $DB->query("UPDATE exp_weblogs_data SET field_id_22 = 'Sale Pending' WHERE entry_id = '"$DB->escape_str($data->post['item_number'])"'");

    Thanks,
    Bryan

  • #4 / Oct 01, 2009 4:32pm

    ender

    1644 posts

    $DB->query("UPDATE exp_weblogs_data SET field_id_22 = 'Sale Pending' WHERE entry_id = '" . $DB->escape_str($data->post['item_number']) . "'");

    . = string concatenation in PHP

  • #5 / Oct 01, 2009 4:37pm

    brankin

    150 posts

    Thanks,

    Not to familiar with PHP or SQL yet spent most of my time in design rather than development.

    One last thing.  Is there an easy way to test this?  I presume that inside a Sandbox account that you do not receive and IPN.  Would it be best to just try and buy a $1 item from myself?

    Thanks,
    Bryan

  • #6 / Oct 01, 2009 4:46pm

    ender

    1644 posts

    you receive IPNs from the sandbox, you just don’t receive email to a live account (just to the accounts within the developer area).

    you’ll still want to do integration testing with paypal when you go live.  just set up some $0.01 items when you need to do that.

  • #7 / Oct 01, 2009 4:50pm

    brankin

    150 posts

    Great. Thanks for all the help.  I heading to the “big city” for tonight but will test this tomorrow.

    Later,
    Bryan

  • #8 / Oct 02, 2009 5:57pm

    brankin

    150 posts

    Have done testing but cannot get it to work yet.  i placed an email generator in the mod.SCM file and definately am getting a ping. But oddly enough, though I am a logged in user it is not putting the purchases into the SCM.  I have a lot of custom Paypal variables but am not sure if this might causing the issue.  Have you ever run into this before?

    Thanks,
    Bryan

  • #9 / Oct 05, 2009 1:30pm

    brankin

    150 posts

    Alright I got it working.  had a ‘s’ in ‘ext_weblogs_data’, it needed to be ‘ext_weblog_data’.  Thanks a lot for your help.  I have noticed a couple other people needing the same solution.  Do you mind if I include your handler code in a tutorial I am going to write up when I get back from GA?

    Thanks again.
    Bryan

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases