• Hi,

    I’m wondering if anyone can help me. I’m working on a custom gateway IPN for WorldPay. I’ve got it setup and taking payments perfectly well, but the instant payment notification (callback) is proving to be a little more tricky.

    I’m passing all the data through WorldPay fine, but it’s just not functioning properly.

    The code I’m using so far is as follows, so if anybody can give me any advice at all, I’d be very grateful. Unfortunately, neither EMP or WorldPay will help with custom work.

    <?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>";
    ?>

    [No bumping. If it’s so urgent that you cannot wait longer than 3 hours, consider hiring someone.]

  • The topic ‘Events Manager Pro – WorldPay Custom Gateway’ is closed to new replies.