Jump to content

Wordpress - Events Manager Pro - WorldPay IPN - Code issues?


CatCalledJames

Recommended Posts

Hi,

 

I'm trying to create an IPN (callback) for WorldPay to work with Events Manager Pro (a Wordpress plugin).

 

I've spoken to both WorldPay and EMP, but neither offer support with custom code.

 

I don't expect you guys to work it all out for me, but I was wondering if anyone could tell me at least if there's any errors with the following code?

 

I can't get it to work but can't see what I'm missing either.

 

This is the callback script I'm using, which should, in theory, update the database to say that the payment has been received.

 

If you need further information to answer me properly, please ask and I'll be happy to provide it.

 

I really appreciate any help you guys can give me!

<?php 

// class definition
	class worldpay_response {
		public $transaction_id = null;
		public $transStatus = null;
		public $transTime = null;
		public $rawAuthCode = null;
		public $authAmount = null;
		public $cartId = null;
		public $authMode = null;
		public $MC_custom = null;
		public $MC_custom_values = null;
		public $MC_booking_id = null;
		public $MC_event_id = null;
		public $MC_EM_Booking = null;
		
			public function __construct() {
				$this->transaction_id = $_POST['transId'];
				$this->transStatus = $_POST['transStatus'];
				$this->transTime = $_POST['transTime'];
				$this->rawAuthCode = $_POST['rawAuthCode'];
				$this->authAmount = $_POST['authAmount'];
				$this->cartId = $_POST['cartId'];
				$this->authMode = $_POST['authMode'];
				$this->EM_Booking = $_POST['MC_EM_Booking'];
				$this->MC_custom = $_POST['MC_custom'];
				$this->MC_custom_values = $_POST['MC_custom_values'];
				$this->MC_booking_id = $_POST['MC_booking_id'];
				$this->MC_event_id = $_POST['MC_event_id'];
				$this->MC_EM_Booking = $_POST['MC_EM_Booking'];			
	}

	/**
	 * Runs when WorldPay sends IPNs to the return URL provided during bookings and EM setup. Bookings are updated and transactions are recorded accordingly. 
	 */

	function handle_payment_return() {
		// WorldPay IPN handling code
		if ((isset($_POST['transStatus']) || isset($_POST['MC_custom']))) {
			
		    //Verify IPN request
			if (get_option( 'em_'. $this->gateway . "_status" ) == 'live') {
				$domain = 'https://secure.worldpay.com/wcc/purchase';
			} else {
				$domain = 'https://secure-test.worldpay.com/wcc/purchase';
			}

			$req = 'cmd=_notify-validate';
			if (!isset($_POST)) $_POST = $HTTP_POST_VARS;
			foreach ($_POST as $k => $v) {
				if (get_magic_quotes_gpc()) $v = stripslashes($v);
				$req .= '&' . $k . '=' . urlencode($v);
			}
			
			@set_time_limit(60);

			//add a CA certificate so that SSL requests always go through
			add_action('http_api_curl','EM_Gateway_Worldpay::payment_return_local_ca_curl',10,1);
				//log ipn request if needed, then move on
				EM_Pro::log( $_POST['payment_status']." successfully received for {$_POST['authAmount']} {$_POST['currency']} (TXN ID {$_POST['txn_id']}) - Custom Info: {$_POST['MC_custom']}", 'worldpay');
			}else{
			    //log error if needed, send error header and exit
				EM_Pro::log( array('IPN Verification Error', 'WP_Error'=> $ipn_verification_result, '$_POST'=> $_POST), 'worldpay' );
			    header('HTTP/1.0 502 Bad Gateway');
			    exit;
			}
			//if we get past this, then the IPN went ok
			
			// handle cases that the system must ignore
			$new_status = false;
			//Common variables
			$amount = $_POST['authAmount'];
			$currency = $_POST['currency'];
			$transTime = date('Y-m-d H:i:s', strtotime($_POST['payment_date']));
			$MC_custom_values = explode(':',$_POST['MC_custom']);
			$MC_booking_id = $MC_custom_values[0];
			$MC_event_id = !empty($MC_custom_values[1]) ? $MC_custom_values[1]:0;
			$MC_EM_Booking = em_get_booking($MC_booking_id);
			if( !empty($MC_EM_Booking->booking_id) && count($MC_custom_values) == 2 ){
				//booking exists
				$MC_EM_Booking->manage_override = true; //since we're overriding the booking ourselves.
				$user_id = $MC_EM_Booking->person_id;
			
				// process WorldPay response
				if(isset($_POST['transStatus']) ) 
					$transStatus = "Y"; 
					 {	
						// case: successful payment
						$this->record_transaction($MC_EM_Booking, $amount, $currency, $transTime, $_POST['transId'], $_POST['transStatus'], '');
								$MC_EM_Booking->approve(true, true); //approve and ignore spaces
					 }
				}else{
							//TODO do something if pp payment not enough
							$MC_EM_Booking->set_status(0); //Set back to normal "pending"
						}
						do_action('em_payment_processed', $MC_EM_Booking, $this);
						break;
	
				}
	}
	
	echo "<pre>";
	print_r($GLOBALS);
	echo "</pre>";
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.