function onPaymentNotification(&$statuses){
$pluginsClass = hikashop_get('class.plugins');
$elements = $pluginsClass->getMethods('payment','paypal');
if(empty($elements)) return false;
$element = reset($elements);
if(!$element->payment_params->notification){
return false;
}
$vars = array();
$data = array();
$filter = JFilterInput::getInstance();
foreach($_REQUEST as $key => $value){
$key = $filter->clean($key);
if(preg_match("#^[0-9a-z_-]{1,30}$#i",$key)&&!preg_match("#^cmd$#i",$key)){
$value = JRequest::getString($key);
$vars[$key]=$value;
$data[]=$key.'='.urlencode($value);
}
}
$data = implode('&',$data).'&cmd=_notify-validate';
if($element->payment_params->debug){
echo print_r($vars,true)."\n\n\n";
}
$orderClass = hikashop_get('class.order');
$dbOrder = $orderClass->get((int)@$vars['invoice']);
if(empty($dbOrder)){
echo "Could not load any order for your notification ".@$vars['invoice'];
return false;
}
$order = new stdClass();
$order->order_id = $dbOrder->order_id;
$order->old_status->order_status=$dbOrder->order_status;
$url = HIKASHOP_LIVE.'administrator/index.php?option=com_hikashop&ctrl=order&task=edit&order_id='.$order->order_id;
$order_text = "\r\n".JText::sprintf('NOTIFICATION_OF_ORDER_ON_WEBSITE',$dbOrder->order_number,HIKASHOP_LIVE);
$order_text .= "\r\n".str_replace('<br/>',"\r\n",JText::sprintf('ACCESS_ORDER_WITH_LINK',$url));
if($element->payment_params->debug){
echo print_r($dbOrder,true)."\n\n\n";
}
$mailer = JFactory::getMailer();
$config =& hikashop_config();
$sender = array(
$config->get('from_email'),
$config->get('from_name') );
$mailer->setSender($sender);
$mailer->addRecipient(explode(',',$config->get('payment_notification_email')));
if(!empty($element->payment_params->ips)){
$ip = hikashop_getIP();
$ips = str_replace(array('.','*',','),array('\.','[0-9]+','|'),$element->payment_params->ips);
if(!preg_match('#('.implode('|',$ips).')#',$ip)){
$mailer->setSubject(JText::sprintf('NOTIFICATION_REFUSED_FOR_THE_ORDER','Paypal').' '.JText::sprintf('IP_NOT_VALID',$dbOrder->order_number));
$body = str_replace('<br/>',"\r\n",JText::sprintf('NOTIFICATION_REFUSED_FROM_IP','Paypal',$ip,implode("\r\n",$element->payment_params->ips)))."\r\n\r\n".JText::sprintf('CHECK_DOCUMENTATION',HIKASHOP_HELPURL.'payment-paypal-error#ip').$order_text;
$mailer->setBody($body);
$mailer->Send();
JError::raiseError( 403, JText::_( 'Access Forbidden' ));
return false;
}
}
if(empty($element->payment_params->url))$element->payment_params->url='https://www.paypal.com/cgi-bin/webscr';
$url = parse_url($element->payment_params->url);
if(!isset($url['query'])){
$url['query'] = '';
}
if(!isset($url['port'])){
if(!empty($url['scheme'])&&in_array($url['scheme'],array('https','ssl'))){
$url['port'] = 443;
}else{
$url['port'] = 80;
}
}
if(!empty($url['scheme'])&&in_array($url['scheme'],array('https','ssl'))){
$url['host_socket'] = 'ssl://'.$url['host'];
}else{
$url['host_socket'] = $url['host'];
}
if($element->payment_params->debug){
echo print_r($url,true)."\n\n\n";
}
$fp = fsockopen ( $url['host_socket'], $url['port'], $errno, $errstr, 30);
if (!$fp) {
$mailer->setSubject(JText::sprintf('NOTIFICATION_REFUSED_FOR_THE_ORDER','Paypal').' '.JText::sprintf('PAYPAL_CONNECTION_FAILED',$dbOrder->order_number));
$body = str_replace('<br/>',"\r\n",JText::sprintf('NOTIFICATION_REFUSED_NO_CONNECTION','Paypal'))."\r\n\r\n".JText::sprintf('CHECK_DOCUMENTATION',HIKASHOP_HELPURL.'payment-paypal-error#connection').$order_text;
$mailer->setBody($body);
$mailer->Send();
JError::raiseError( 403, JText::_( 'Access Forbidden' ));
return false;
}
$uri = $url['path'].($url['query']!='' ? '?' . $url['query'] : '');
$header = "POST $uri HTTP/1.0\r\n".
"User-Agent: PHP/".phpversion()."\r\n".
"Referer: ".hikashop_currentURL()."\r\n".
"Server: ".$_SERVER['SERVER_SOFTWARE']."\r\n".
"Host: ".$url['host'].":".$url['port']."\r\n".
"Content-Type: application/x-www-form-urlencoded\r\n".
"Content-Length: ".strlen($data)."\r\n".
"Accept: */"."*\r\n\r\n";
fwrite($fp, $header . $data);
$response = '';
while (!feof($fp)) {
$response .= fgets ($fp, 1024);
}
fclose ($fp);
if($element->payment_params->debug){
echo print_r($header,true)."\n\n\n";
echo print_r($data,true)."\n\n\n";
echo print_r($response,true)."\n\n\n";
}
$response = substr($response, strpos($response, "\r\n\r\n") + strlen("\r\n\r\n"));
$verified = preg_match( "#VERIFIED#i", $response);
if(!$verified){
if(preg_match("#INVALID#i", $response)){
$mailer->setSubject(JText::sprintf('NOTIFICATION_REFUSED_FOR_THE_ORDER','Paypal').'invalid transaction');
$body = JText::sprintf("Hello,\r\n A paypal notification was refused because it could not be verified by the paypal server")."\r\n\r\n".JText::sprintf('CHECK_DOCUMENTATION',HIKASHOP_HELPURL.'payment-paypal-error#invalidtnx').$order_text;
$mailer->setBody($body);
$mailer->Send();
if($element->payment_params->debug){
echo 'invalid transaction'."\n\n\n";
}
}else{
$mailer->setSubject(JText::sprintf('NOTIFICATION_REFUSED_FOR_THE_ORDER','Paypal').'invalid response');
$body = JText::sprintf("Hello,\r\n A paypal notification was refused because the response from the paypal server was invalid")."\r\n\r\n".JText::sprintf('CHECK_DOCUMENTATION',HIKASHOP_HELPURL.'payment-paypal-error#invalidresponse').$order_text;
$mailer->setBody($body);
$mailer->Send();
if($element->payment_params->debug){
echo 'invalid response'."\n\n\n";
}
}
return false;
}
$completed = preg_match("#Completed#i", $vars['payment_status']);
$pending = preg_match("#Pending#i", $vars['payment_status']);
if (!$completed && !$pending) {
$body = str_replace('<br/>',"\r\n",JText::sprintf('PAYMENT_NOTIFICATION_STATUS','Paypal',$vars['payment_status'])).' '.JText::_('STATUS_NOT_CHANGED')."\r\n\r\n".JText::sprintf('CHECK_DOCUMENTATION',HIKASHOP_HELPURL.'payment-paypal-error#status').$order_text;
$mailer->setSubject(JText::sprintf('PAYMENT_NOTIFICATION_FOR_ORDER','Paypal',$vars['payment_status'],$dbOrder->order_number));
$mailer->setBody($body);
$mailer->Send();
if($element->payment_params->debug){
echo 'payment '.$vars['payment_status']."\n\n\n";
}
return false;
}
echo 'PayPal transaction id: '.$vars['txn_id']."\n\n\n";
$order->history->history_reason=JText::sprintf('AUTOMATIC_PAYMENT_NOTIFICATION');
$order->history->history_notified=0;
$order->history->history_amount=@$vars['mc_gross'].@$vars['mc_currency'];
$order->history->history_payment_id = $element->payment_id;
$order->history->history_payment_method =$element->payment_type;
$order->history->history_data = ob_get_clean();
$order->history->history_type = 'payment';
$currencyClass = hikashop_get('class.currency');
$currencies=null;
$currencies = $currencyClass->getCurrencies($dbOrder->order_currency_id,$currencies);
$currency=$currencies[$dbOrder->order_currency_id];
$price_check = round($dbOrder->order_full_price, (int)$currency->currency_locale['int_frac_digits'] );
if($price_check != @$vars['mc_gross'] || $currency->currency_code != @$vars['mc_currency']){
$order->order_status = $element->payment_params->invalid_status;
$orderClass->save($order);
$mailer->setSubject(JText::sprintf('NOTIFICATION_REFUSED_FOR_THE_ORDER','Paypal').JText::_('INVALID_AMOUNT'));
$body = str_replace('<br/>',"\r\n",JText::sprintf('AMOUNT_RECEIVED_DIFFERENT_FROM_ORDER','Paypal',$order->history->history_amount,$price_check.$currency->currency_code))."\r\n\r\n".JText::sprintf('CHECK_DOCUMENTATION',HIKASHOP_HELPURL.'payment-paypal-error#amount').$order_text;
$mailer->setBody($body);
$mailer->Send();
return false;
}
if($completed){
$order->order_status = $element->payment_params->verified_status;
}else{
$order->order_status = $element->payment_params->pending_status;
$order_text = JText::sprintf('CHECK_DOCUMENTATION',HIKASHOP_HELPURL.'payment-paypal-error#pending')."\r\n\r\n".$order_text;
}
if($dbOrder->order_status == $order->order_status) return true;
$config =& hikashop_config();
if($config->get('order_confirmed_status','confirmed')==$order->order_status){
$order->history->history_notified=1;
}
$order->mail_status=$statuses[$order->order_status];
$mailer->setSubject(JText::sprintf('PAYMENT_NOTIFICATION_FOR_ORDER','Paypal',$vars['payment_status'],$dbOrder->order_number));
$body = str_replace('<br/>',"\r\n",JText::sprintf('PAYMENT_NOTIFICATION_STATUS','Paypal',$vars['payment_status'])).' '.JText::sprintf('ORDER_STATUS_CHANGED',$order->mail_status)."\r\n\r\n".$order_text;
$mailer->setBody($body);
$mailer->Send();
$orderClass->save($order);
return true;
}
I want use this method in my payment method for update order status automatically at database if payment is ok.