I am trying to integrate Paypal with my website. So i got some script from my friend but whenever i try to test notify_url its always returning this error. "We Could not send an IPN due to an HTTP error: 500: Internal Server Error."
this is my scripts which i used for integration, PaypalObject.php
<?php
require_once '../Functions/Database.php' ;
$sendPayData = array(
"METHOD" => "BMCreateButton" ,
"VERSION" => "65.2" ,
"USER" => "uttam2_api1.shop.com" ,
"PWD" => "PNCWS3M7K5MVF4E7" ,
"SIGNATURE" => "AFcWxV21C7fd0v3bYYYRCpSSRl31AVPqL7gWOG.x2DZiTRtu-E.xAFPJ" ,
"BUTTONCODE" => "ENCRYPTED" ,
"BUTTONTYPE" => "BUYNOW" ,
"BUTTONSUBTYPE" => "SERVICES",
"BUTTONCOUNTRY" => "IN",
"L_BUTTONVAR1" => "item_name=D-Points",
"L_BUTTONVAR2" => "currency_code=USD" ,
"L_BUTTONVAR3" => "no_shipping=2" ,
"L_BUTTONVAR4" => "no_note=1" ,
"L_BUTTONVAR5" => "notify_url=http://wrathoftitans.net/Paypal/DonateListener.php");
$db = GetDatabaseConnection ( 'db_misc' ) ;
$points = $db->query ( 'SELECT * FROM `t_point_sale` ORDER BY `points`' )->fetchAll ( PDO::FETCH_ASSOC ) ;
$sendPayData [ "OPTION0NAME" ] = "D-Points" ;
foreach ( $points as $i => $point ) {
$sendPayData [ sprintf ( 'L_OPTION0SELECT%d' , $i ) ] = sprintf ( '%d D-Points' , $point [ 'points' ] ) ;
$sendPayData [ sprintf ( 'L_OPTION0PRICE%d' , $i ) ] = $point [ 'price' ] ;
}
$sendPayData [ "L_TEXTBOX0" ] = "Account" ;
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'content' => http_build_query($sendPayData),
'timeout' => 10,
),
));
$response = file_get_contents("https://api-3t.sandbox.paypal.com/nvp/",true,$context);
parse_str($response, $output);
$button = str_replace("\\","",$output["WEBSITECODE"]);
print $button;
?>
this is Listener script DonateListener.php
<?php
require_once 'DonateIPNHandler.php' ;
require_once 'InstantPaymentNotification.php' ;
$ipn = new InstantPaymentNotification ( true ) ;
$ipn->setIPNHandler ( new DonateIPNHandler ( ) ) ;
$ipn->listen ( ) ;
?>
this is InstantPaymentNotification.php
<?php
class InstantPaymentNotification {
private $endpoint = 'https://www.paypal.com' ;
private $ipnHandler ;
public function __construct ( $sandbox = true ) {
if ( $sandbox === true ) {
$this->endpoint = 'https://www.sandbox.paypal.com' ;
}
$this->endpoint .= '/cgi-bin/webscr?cmd=_notify-validate' ;
}
public function listen () {
if ( $this->ipnHandler !== null ) {
if ( $_SERVER[ 'REQUEST_METHOD' ] == 'POST' ) {
if ( filter_input ( INPUT_POST , 'receiver_email' , FILTER_VALIDATE_EMAIL ) ) {
$curl = curl_init ( ) ;
curl_setopt ( $curl , CURLOPT_URL , $this->endpoint ) ;
curl_setopt ( $curl , CURLOPT_SSL_VERIFYPEER , false ) ;
curl_setopt ( $curl , CURLOPT_RETURNTRANSFER , 1 ) ;
curl_setopt ( $curl , CURLOPT_POST , 1 ) ;
curl_setopt ( $curl , CURLOPT_POSTFIELDS , http_build_query ( $_POST ) ) ;
$response = curl_exec ( $curl ) ;
$error = curl_error ( $curl ) ;
$errno = curl_errno ( $curl ) ;
curl_close ( $curl ) ;
if ( empty ( $error ) && $errno == 0 ) {
$this->ipnHandler->handle ( $response == 'VERIFIED' , $_POST ) ;
}
}
}
} else {
throw new BadMethodCallException ( 'Nenhum manipulador de mensagem ou email foi definido' ) ;
}
}
public function setIPNHandler ( IPNHandler $ipnHandler ) {
$this->ipnHandler = $ipnHandler ;
}
}
?>
This is DonateIPNHandler.php
<?php
require_once 'IPNHandler.php' ;
require_once '../Functions/Database.php' ;
require_once 'PaypalPayment.php' ;
class DonateIPNHandler implements IPNHandler {
private $db , $query ;
public function handle ( $isVerified , array $message ) {
if ( $isVerified ) {
if ( $message [ 'receiver_email' ] == 'uttam2@shop.com' ) {
$data = array (
'first_name' => $message [ 'first_name' ] ,
'last_name' => $message [ 'last_name' ] ,
'address_country' => $message [ 'address_country' ] ,
'address_city' => $message [ 'address_city' ] ,
'quantity' => ( int ) $message [ 'quantity' ] ,
'payment_status' => $message [ 'payment_status' ] ,
'item_name' => $message [ 'item_name' ] ,
'mc_currency' => $message [ 'mc_currency' ] ,
'gross' => $message [ 'payment_gross' ] ,
'acct' => array (
'account' => $message [ 'option_selection2' ] ,
'dpoints' => preg_replace ( '/[^\d]/' , null , $message [ 'option_selection1' ] )
) ,
'check' => $message [ 'option_selection1' ] ,
'email' => $message [ 'payer_email' ] ,
'ID' => $message [ 'ipn_track_id' ]
) ;
$paypalPayment = new PaypalPayment ( GetDatabaseConnection ( 'db_misc' ) ) ;
$_validate = $paypalPayment->isValid ( $data ) ;
if ( $_validate [ "isValid" ] === true ) {
$paypalPayment->storePayment ( $data )->dispatch ( ) ;
}
}
}
}
}
?>
This is IPNHandler.php
<?php
interface IPNHandler {
public function handle ( $isVerified , array $message ) ;
}
?>
This is PaypalPayment.php
<?php
class PaypalPayment {
private $db ;
private $query ;
private $isStored = false ;
private $_toDispatch = array ( ) ;
public function __construct ( PDO $dbConnection ) {
$this->db = $dbConnection ;
$this->db->setAttribute ( PDO::ATTR_ERRMODE , PDO::ERRMODE_EXCEPTION ) ;
$this->db->setAttribute ( PDO::ATTR_TIMEOUT , 5 ) ;
$this->db->setAttribute ( PDO::ATTR_PERSISTENT , false ) ;
}
public function isValid ( array $data ) {
$item = isset ( $data [ "item_name" ] ) ? $data [ "item_name" ] : null ;
$check = isset ( $data [ "check" ] ) ? $data [ "check" ] : null ;
if ( ! is_null ( $item ) && ! is_null ( $check ) ) {
$currency = isset ( $data [ "mc_currency" ] ) ? $data [ "mc_currency" ] : null ;
if ( ! is_null ( $currency ) && ! empty ( $currency ) ) {
$pt = isset ( $data [ "acct" ] [ "dpoints" ] ) ? (int) $data [ "acct" ] [ "dpoints" ] : 0 ;
if ( is_numeric ( $pt ) and ( $pt > 0 ) ) {
$gross = isset ( $data [ "gross" ] ) ? $data [ "gross" ] : null ;
$fields = array ( 'itemName' , 'checkName' , 'currency' , 'amount' , 'points' ) ;
$this->query = $this->db->prepare ( 'SELECT * FROM `t_payment_validate` WHERE `' . implode ( '` = ? AND `' , $fields ) . '` = ?' ) ;
$this->query->bindValue ( 1 , $item , PDO::PARAM_STR ) ;
$this->query->bindValue ( 2 , $check , PDO::PARAM_STR ) ;
$this->query->bindValue ( 3 , $currency , PDO::PARAM_STR ) ;
$this->query->bindValue ( 4 , $gross , PDO::PARAM_STR ) ;
$this->query->bindValue ( 5 , $pt , PDO::PARAM_INT ) ;
$this->query->execute ( ) ;
$_objectFetched = $this->query->fetch ( PDO::FETCH_ASSOC ) ;
return array (
'objectID' => $_objectFetched [ 'vid' ] ,
'isValid' => ( $this->query->rowCount ( ) === 1 && $this->canHandlePaymentID ( $data [ "ID" ] ) ) ? true : false
) ;
}
}
}
return false ;
}
public function storePayment ( array $data ) {
$_validate = $this->isValid ( $data ) ;
if ( $_validate [ "isValid" ] === true ) {
$name = sprintf ( '%s %s' , $data [ "first_name" ] , $data [ "last_name" ] ) ;
$fields = array ( 'name' , 'country' , 'city' , 'paymentStatus' , 'account' , 'email' , 'points' , 'amount' , 'currency' , 'status' , 'paymentID' ) ;
$this->query = $this->db->prepare ( 'INSERT INTO `t_payments` ( `'. implode ( '`, `' , $fields ) .'` ) VALUES ( ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? )' ) ;
$this->query->bindValue ( 1 , $name , PDO::PARAM_STR ) ;
$this->query->bindValue ( 2 , $data [ "address_country" ] , PDO::PARAM_STR ) ;
$this->query->bindValue ( 3 , $data [ "address_city" ] , PDO::PARAM_STR ) ;
$this->query->bindValue ( 4 , $data [ "payment_status" ] , PDO::PARAM_STR ) ;
$this->query->bindValue ( 5 , $data [ "acct" ] [ "account" ] , PDO::PARAM_STR ) ;
$this->query->bindValue ( 6 , $data [ "email" ] , PDO::PARAM_STR ) ;
$this->query->bindValue ( 7 , $data [ "acct" ] [ "dpoints" ] , PDO::PARAM_INT ) ;
$this->query->bindValue ( 8 , $data [ "gross" ] , PDO::PARAM_STR ) ;
$this->query->bindValue ( 9 , $data [ "mc_currency" ] , PDO::PARAM_STR ) ;
$this->query->bindValue ( 10 , "Stored" , PDO::PARAM_STR ) ;
$this->query->bindValue ( 11 , $data [ "ID" ] , PDO::PARAM_STR ) ;
$this->query->execute ( ) ;
$this->_toDispatch = array (
'account' => $data [ "acct" ] [ "account" ] ,
'password' => $data [ "acct" ] [ "password" ] ,
'points' => intval ( $data [ "acct" ] [ "dpoints" ] ) ,
'_objectID' => $this->db->lastInsertId ( )
) ;
$this->isStored = true ;
}
return $this ;
}
public function canHandlePaymentID ( $paymentID ) {
$this->query = $this->db->prepare ( 'SELECT COUNT(*) AS `count` FROM `db_misc`.`t_payments` WHERE `paymentID` = :paymentID' ) ;
$this->query->bindParam ( ':paymentID' , $paymentID , PDO::PARAM_STR ) ;
$this->query->execute ( ) ;
$data = $this->query->fetch ( PDO::FETCH_ASSOC ) ;
return ( $data [ 'count' ] >= 1 ? false : true ) ;
}
public function dispatch ( ) {
if ( $this->isStored === true ) {
$this->query = $this->db->prepare ( 'SELECT * FROM `db_account`.`t_account` WHERE `name` = :name' ) ;
$this->query->bindParam ( ':name' , $this->_toDispatch [ "account" ] , PDO::PARAM_STR ) ;
$this->query->execute ( ) ;
if ( $this->query->rowCount ( ) >= 1 ) {
$data = $this->query->fetch ( PDO::FETCH_OBJ ) ;
$this->query = $this->db->prepare ( 'UPDATE `db_account`.`t_account` SET `dp` = ( `dp` + :points ) WHERE `name` = :name' ) ;
$this->query->bindParam ( ':name' , $this->_toDispatch [ "account" ] , PDO::PARAM_STR ) ;
$this->query->bindParam ( ':points' , $this->_toDispatch [ "points" ] , PDO::PARAM_INT ) ;
$this->query->execute ( ) ;
$this->db->query ( 'UPDATE `t_payments` SET `status` = "Dispatched" WHERE `pid` = "'. $this->_toDispatch [ "_objectID" ] .'"' ) ;
$this->query = $this->db->query ( 'SELECT * FROM `db_game`.`t_user` WHERE `accountid` = "'. $data->accountid .'"' ) ;
foreach ( $this->query->fetchALl ( PDO::FETCH_OBJ ) as $character ) {
$userName = $character [ 'name' ] ;
$points = $this->_toDispatch [ "points" ] ;
$message = sprintf ( 'Hello %s, You Have Recived %d D-Point%s' , $userName , $points , ( $points > 1 ) ? 's' : null ) ;
$this->query = $this->db->prepare ( 'INSERT INTO `t_game_notification` ( `userName` , `message` ) VALUES ( :user , :message )' ) ;
$this->query->bindParam ( ':user' , $userName , PDO::PARAM_STR ) ;
$this->query->bindParam ( ':message' , $message , PDO::PARAM_STR ) ;
$this->query->execute ( ) ;
}
$this->isStored = false ;
$this->_toDispatch = array ( ) ;
return true ;
} else {
$this->db->query ( 'UPDATE `t_payments` SET `status` = "Invalid Account" WHERE `pid` = "'. $this->_toDispatch [ "_objectID" ] .'"' ) ;
return false ;
}
} else {
return false ;
}
}
}
?>
This scripts should work like if buyer make successful payment then he will directly receive one message that you have purchase item(whatever he buy). and Buyer will receive the item for what he paid. and also this script should make some Entry in my Database. can someone help me to find whats the trouble in this all?