0

I'm developing payment gateway for the BrilliantRetail and I need to create IPN callback script which will change BR orders statuses and return response data to the IPN-gateway.

So I'm trying to find a solution to get EE instance but produce no output after calling.

My version of EE is 2.3.1, but I hope to find universal solution for other version.

I have tried a very-very rough way, to put IPN-callback script into the webroot, include index.php and catch output buffer.

ob_start();
require_once( 'index.php' );
ob_end_clean();
$ee = get_instance();

I don't want to use this solution. I must be sure there are no random output will be returned and no templates will be used at all.

Is there are any ways to get EE instance in than manner by using hooks or something?

Thanks.

4

Something is way off about your approach. You should not need the ob_start() methods if you were to build this properly. If you need a callback method, what you really want to look into is a using a module for a "controller". (For more info on a controller, refer to CodeIgniter's MVC pattern.)

So could create an ACT url (http://yourdomain.com/ACT=X) and this will be your callback. This will be called before the TMPL object is instantiated, and therefor you can more easily fetch the global instance.

There are two ways that are commonly used to grab the instance in the controller (EE 2.5.5 and before).

$this->EE =& get_instance();

And in 2.6+ it's simply:

ee();
| improve this answer | |
  • how can I create new action handler using BrilliantRetail payment gateway only? I would prefer to stay in BR bound only and don't create another one module. – Artem Oliynyk May 14 '13 at 11:17
  • Have you asked David Dexter of BR? I am not sure, but I am sure he will tell you. I was speaking more generally, but with your latest comment I see more of what you are trying to do and I would definitely ask David about the best approach. – Justin Kimbrell May 14 '13 at 14:34
  • Thank you Justin. I have found solution with PHP templates. – Artem Oliynyk May 15 '13 at 17:56
2

I just want to point out that BrilliantRetail has methods for handling IPN callbacks when creating custom gateways. I would consider using the core methods instead of putting logic in templates or files outside of the ExpressionEngine system:

http://docs.brilliantretail.com/3-extending-brilliantretail/payment-gateways/index.html#ipn-methods

The PayPal Standard gateway included in the system/expressionengine/brilliant_retail/core/gateways folder would work as an example using IPN methods.

Best, David

| improve this answer | |
1

Have you tried?

$ee =& get_instance();
| improve this answer | |
0

I have spend some extra time with this issue and have found a very handily solution: using PHP templates stored in DB to get current EE instance and create callback processing script.

As I needed this script for BrilliantRetail payment gateway module ( based upon Brilliant_retail_gateway ) main idea is to create PHP-template on module install and remove it on module deleting.

I decided to use custom template group to avoid template groups mess-up.

I'll describe all necessary action for this solution below.

First of all: manually create a template group and template itself for testing ( in Design > Template Management ) with such parameters:

  • Type — Web Page
  • Enable Caching — No
  • Allow PHP — Yes
  • PHP Parsing Stage — Output

For this example I will use tpl_group_name for the group name, and tpl_name for the template name

Turn on file synchronization and save template to the file. For now, you can create write pure PHP code to the template.

IMPORTANT: Your template file must have PHP close tag ( ?> ), to avoid template parsing error.

You can execute your template-script by opening URL like: http://example.com/tpl_group_name/tpl_name

Template will be stored in system/templates/default_site/tpl_group_name.group/tpl_name.html

Directory default_site is depend on your system configuration and can be different

You can work with EE instance in your template via

$this->EE

After script codding was finished template-script must be packed and placed into the BR Payment module.

I decided to compress file content with gzip then after apply base64 encoding to get plaint text, which allows me to insert compressed content to the file as a string variable.

Finally, you can manipulate your template using template_model class. Code below demonstrate how to add PHP-template and group to the DB.

// Adding group
$this->EE->load->model('template_model');
$grp_data = array(
    'group_name'        => 'tpl_group_name',
    'is_site_default'   => 'n', // do not replace default group
    'site_id'           => $this->EE->config->item('site_id')
);

// Create group
$group_id = $this->EE->template_model->create_group( $grp_data );



// Adding callback template
$tpl_data = array(
    'group_id'              => $group_id,
    'template_name'         => 'tpl_name',
    'template_type'         => 'webpage',
    'template_data'         => $template_content,
    'edit_date'             => $this->EE->localize->now,
    'save_template_file'    => 'n', // DO not save template to the file for product site
    'allow_php'             => 'y', // allow PHP code
    'last_author_id'        => '1', // assume a super admin
    'site_id'               => $this->EE->config->item('site_id') // use current site_id
);

// Create group
$this->EE->template_model->create_template($tpl_data);

// Clear db cache
$this->EE->functions->clear_caching('db');

Unfortunately there are no methods to remove template groups from the DB, so I used DB request to remove group.

Code to remove templates from DB

// Find and remove template
$template_id = null;
$template_find = $this->EE->template_model->get_templates( null, null, array( 'template_name' => 'tpl_name' ) );

if( $template_find->num_rows ) {
    $template_tmp = $template_find->_fetch_assoc();
    $template_id = (int) $template_tmp['template_id'];
}

// Remove template if ID was found
if( $template_id ) {

    // Try to remove file as well
    if( !$this->EE->template_model->delete_template( $template_id, true ) ) {
        // If fiel removing has failed -- remove only DB content
        $this->EE->template_model->delete_template( $template_id );
    }
}


// Clear IPN callback template group
$group_id = null;
$groups = $this->EE->template_model->get_template_groups();

if( $groups->num_rows > 0 ) {
    while( $group_tmp = $groups->_fetch_assoc() ) {
        if( $group_tmp['group_name'] == 'tpl_group_name'  ) {
            $group_id = (int) $group_tmp['group_id'];
        }
    }
}

// Remove group record if ID was found
if( $group_id ) {
    $this->EE->db->delete( 'template_groups', array( 'group_id' => $group_id ) );
}
$this->EE->functions->clear_caching('db');

If you have any advices — I'm glad to read.

Thanks.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.