Here is an example - paste this code into a snippet and call it from a page.
<?php
#******************************************************************************
#* Name : PxPay_Sample_Curl.php
#* Description : Direct Payment Solutions Payment Express PxPay PHP cURL Sample
#* Copyright : Direct Payment Solutions 2009(c)
#* Date : 2009-10-21
#* References : http://www.paymentexpress.com/technical_resources/ecommerce_hosted/pxpay.html
#*@version : 1.0
#* Author : Thomas Treadwell
#******************************************************************************
# This file is a sample demonstrating integration with the PxPay interface using PHP with the cURL extension installed.
#Inlcude PxPay objects
include "PxPay_Curl.inc.php";
$PxPay_Url = "https://sec.paymentexpress.com/pxpay/pxaccess.aspx";
$PxPay_Userid = "UserId"; #Important! Update with your UserId
$PxPay_Key = "Key"; #Important! Update with your Key
#
# MAIN
#
$pxpay = new PxPay_Curl( $PxPay_Url, $PxPay_Userid, $PxPay_Key );
if (isset($_REQUEST["result"]))
{
# this is a redirection from the payments page.
print_result();
}
elseif (isset($_REQUEST["Submit"]))
{
# this is a post back -- redirect to payments page.
redirect_form();
}
else
{
# this is a fresh request -- display the purchase form.
print_form();
}
#******************************************************************************
# This function receives information back from the payments page,
# and displays it to the user.
#******************************************************************************
function print_result()
{
global $pxpay;
$enc_hex = $_REQUEST["result"];
#getResponse method in PxPay object returns PxPayResponse object
#which encapsulates all the response data
$rsp = $pxpay->getResponse($enc_hex);
# the following are the fields available in the PxPayResponse object
$Success = $rsp->getSuccess(); # =1 when request succeeds
$AmountSettlement = $rsp->getAmountSettlement();
$AuthCode = $rsp->getAuthCode(); # from bank
$CardName = $rsp->getCardName(); # e.g. "Visa"
$CardNumber = $rsp->getCardNumber(); # Truncated card number
$DateExpiry = $rsp->getDateExpiry(); # in mmyy format
$DpsBillingId = $rsp->getDpsBillingId();
$BillingId = $rsp->getBillingId();
$CardHolderName = $rsp->getCardHolderName();
$DpsTxnRef = $rsp->getDpsTxnRef();
$TxnType = $rsp->getTxnType();
$TxnData1 = $rsp->getTxnData1();
$TxnData2 = $rsp->getTxnData2();
$TxnData3 = $rsp->getTxnData3();
$CurrencySettlement= $rsp->getCurrencySettlement();
$ClientInfo = $rsp->getClientInfo(); # The IP address of the user who submitted the transaction
$TxnId = $rsp->getTxnId();
$CurrencyInput = $rsp->getCurrencyInput();
$EmailAddress = $rsp->getEmailAddress();
$MerchantReference = $rsp->getMerchantReference();
$ResponseText = $rsp->getResponseText();
$TxnMac = $rsp->getTxnMac(); # An indication as to the uniqueness of a card used in relation to others
if ($rsp->getSuccess() == "1")
{
$result = "The transaction was approved.";
# Sending invoices/updating order status within database etc.
if (!isProcessed($TxnId))
{
# Send emails, generate invoices, update order status etc.
}
}
else
{
$result = "The transaction was declined.";
}
print <<<HTMLEOF
<html>
<head>
<title>Direct Payment Solutions PxPay transaction result</title>
</head>
<body>
<h1>Direct Payment Solutions PxPay transaction result</h1>
<p>$result</p>
<table border=1>
<tr><th>Name</th> <th>Value</th> </tr>
<tr><td>Success</td> <td>$Success</td></tr>
<tr><td>TxnType</td> <td>$TxnType</td></tr>
<tr><td>CurrencyInput</td> <td>$CurrencyInput</td></tr>
<tr><td>MerchantReference</td> <td>$MerchantReference</td></tr>
<tr><td>TxnData1</td> <td>$TxnData1</td></tr>
<tr><td>TxnData2</td> <td>$TxnData2</td></tr>
<tr><td>TxnData3</td> <td>$TxnData3</td></tr>
<tr><td>AuthCode</td> <td>$AuthCode</td></tr>
<tr><td>CardName</td> <td>$CardName</td></tr>
<tr><td>CardHolderName</td> <td>$CardHolderName</td></tr>
<tr><td>CardNumber</td> <td>$CardNumber</td></tr>
<tr><td>DateExpiry</td> <td>$DateExpiry</td></tr>
<tr><td>CardHolderName</td> <td>$CardHolderName</td></tr>
<tr><td>ClientInfo</td> <td>$ClientInfo</td></tr>
<tr><td>TxnId</td> <td>$TxnId</td></tr>
<tr><td>EmailAddress</td> <td>$EmailAddress</td></tr>
<tr><td>DpsTxnRef</td> <td>$DpsTxnRef</td></tr>
<tr><td>BillingId</td> <td>$BillingId</td></tr>
<tr><td>DpsBillingId</td> <td>$DpsBillingId</td></tr>
<tr><td>AmountSettlement</td> <td>$AmountSettlement</td></tr>
<tr><td>CurrencySettlement</td> <td>$CurrencySettlement</td></tr>
<tr><td>TxnMac</td> <td>$TxnMac</td></tr>
<tr><td>ResponseText</td> <td>$ResponseText</td></tr>
</table>
</body>
</html>
HTMLEOF;
}
#******************************************************************************
# Database lookup to check the status of the order or shopping cart
#******************************************************************************
function isProcessed($TxnId)
{
# Check database if order relating to TxnId has alread been processed
return false;
}
#******************************************************************************
# This function prints a blank purchase form.
#******************************************************************************
function print_form()
{
print <<<HTMLEOF
<html>
<head>
<title>Direct Payment Solutions PxPay transaction sample</title>
</head>
<body>
<h1>Direct Payment Solutions PxPay transaction result</h1>
<p>
You have indicated you would like to buy some widgets.
</p>
<p>
Please enter the number of widgets below, and enter your
shipping details.
</p>
<form method="post">
<table>
<tr>
<td>Quantity:</td>
<td><input name="Quantity" type="text"/></td>
<td>@ $19.95 ea</td>
</tr>
<tr>
<td>Reference:</td>
<td><input name="Reference" type="text"/></td>
</tr>
<tr>
<td>Ship to</td>
<td></td>
</tr>
<tr>
<td>Address line 1:</td>
<td><input name="Address1" type="text"/></td>
</tr>
<tr>
<td>Address line 2</td>
<td><input name="Address2" type="text"/></td>
</tr>
<tr>
<td>Address line 3</td>
<td><input name="Address3" type="text"/></td>
</tr>
</table>
<input name="Submit" type="submit" value="Submit"/>
Click submit to go to the secure payment page.
</form>
</body>
</html>
HTMLEOF;
}
#******************************************************************************
# This function formats data into a request and redirects to the
# Payments Page.
#******************************************************************************
function redirect_form()
{
global $pxpay;
$request = new PxPayRequest();
$http_host = getenv("HTTP_HOST");
$request_uri = getenv("SCRIPT_NAME");
$server_url = "http://$http_host";
#$script_url = "$server_url/$request_uri"; //using this code before PHP version 4.3.4
#$script_url = "$server_url$request_uri"; //Using this code after PHP version 4.3.4
$script_url = (version_compare(PHP_VERSION, "4.3.4", ">=")) ?"$server_url$request_uri" : "$server_url/$request_uri";
# the following variables are read from the form
$Quantity = $_REQUEST["Quantity"];
$MerchantReference = $_REQUEST["Reference"];
$Address1 = $_REQUEST["Address1"];
$Address2 = $_REQUEST["Address2"];
$Address3 = $_REQUEST["Address3"];
#Calculate AmountInput
$AmountInput = 19.95 * $Quantity;
#Generate a unique identifier for the transaction
$TxnId = uniqid("ID");
#Set PxPay properties
$request->setMerchantReference($MerchantReference);
$request->setAmountInput($AmountInput);
$request->setTxnData1($Address1);
$request->setTxnData2($Address2);
$request->setTxnData3($Address3);
$request->setTxnType("Purchase");
$request->setCurrencyInput("NZD");
$request->setEmailAddress("[email protected]");
$request->setUrlFail($script_url); # can be a dedicated failure page
$request->setUrlSuccess($script_url); # can be a dedicated success page
$request->setTxnId($TxnId);
#The following properties are not used in this case
# $request->setEnableAddBillCard($EnableAddBillCard);
# $request->setBillingId($BillingId);
# $request->setOpt($Opt);
#Call makeRequest function to obtain input XML
$request_string = $pxpay->makeRequest($request);
#Obtain output XML
$response = new MifMessage($request_string);
#Parse output XML
$url = $response->get_element_text("URI");
$valid = $response->get_attribute("valid");
#Redirect to payment page
header("Location: ".$url);
}
?>
Then save this code below to a php file and upload it to your server.
Then on the code/snippet above change the path on line 14 to this files path.
<?php
#******************************************************************************
#* Name : PxPay_Curl.inc.php
#* Description : Classes used interact with the PxPay interface using PHP with the cURL extension installed
#* Copyright : Direct Payment Solutions 2009(c)
#* Date : 2009-04-10
#* Version : 1.0
#* Author : Thomas Treadwell
#******************************************************************************
# Use this class to parse an XML document
class MifMessage
{
var $xml_;
var $xml_index_;
var $xml_value_;
# Constructor:
# Create a MifMessage with the specified XML text.
# The constructor returns a null object if there is a parsing error.
function MifMessage($xml)
{
$p = xml_parser_create();
xml_parser_set_option($p,XML_OPTION_CASE_FOLDING,0);
$ok = xml_parse_into_struct($p, $xml, $value, $index);
xml_parser_free($p);
if ($ok)
{
$this->xml_ = $xml;
$this->xml_value_ = $value;
$this->xml_index_ = $index;
}
}
# Return the value of the specified top-level attribute.
# This method can only return attributes of the root element.
# If the attribute is not found, return "".
function get_attribute($attribute)
{
$attributes = $this->xml_value_[0]["attributes"];
return $attributes[$attribute];
}
# Return the text of the specified element.
# The element is given as a simplified XPath-like name.
# For example, "Link/ServerOk" refers to the ServerOk element
# nested in the Link element (nested in the root element).
# If the element is not found, return "".
function get_element_text($element)
{
$index = $this->get_element_index($element, 0);
if ($index == 0)
{
return "";
}
else
{
#When element existent but empty
$elementObj = $this->xml_value_[$index];
if (! array_key_exists("value", $elementObj))
return "";
return $this->xml_value_[$index]["value"];
}
}
# (internal method)
# Return the index of the specified element,
# relative to some given root element index.
#
function get_element_index($element, $rootindex = 0)
{
#$element = strtoupper($element);
$pos = strpos($element, "/");
if ($pos !== false)
{
# element contains '/': find first part
$start_path = substr($element,0,$pos);
$remain_path = substr($element,$pos+1);
$index = $this->get_element_index($start_path, $rootindex);
if ($index == 0)
{
# couldn't find first part give up.
return 0;
}
# recursively find rest
return $this->get_element_index($remain_path, $index);
}
else
{
# search from the parent across all its children
# i.e. until we get the parent's close tag.
$level = $this->xml_value_[$rootindex]["level"];
if ($this->xml_value_[$rootindex]["type"] == "complete")
{
return 0; # no children
}
$index = $rootindex+1;
while ($index<count($this->xml_value_) &&
!($this->xml_value_[$index]["level"]==$level &&
$this->xml_value_[$index]["type"]=="close"))
{
# if one below parent and tag matches, bingo
if ($this->xml_value_[$index]["level"] == $level+1 &&
$this->xml_value_[$index]["tag"] == $element)
{
return $index;
}
$index++;
}
return 0;
}
}
}
class PxPay_Curl
{
var $PxPay_Key;
var $PxPay_Url;
var $PxPay_Userid;
function PxPay_Curl($Url, $UserId, $Key){
error_reporting(E_ERROR);
$this->PxPay_Key = $Key;
$this->PxPay_Url = $Url;
$this->PxPay_Userid = $UserId;
}
#******************************************************************************
# Create a request for the PxPay interface
#******************************************************************************
function makeRequest($request)
{
#Validate the Request
if($request->validData() == false) return "" ;
$request->setUserId($this->PxPay_Userid);
$request->setKey($this->PxPay_Key);
$xml = $request->toXml();
$result = $this->submitXml($xml);
return $result;
}
#******************************************************************************
# Return the transaction outcome details
#******************************************************************************
function getResponse($result){
$inputXml = "<ProcessResponse><PxPayUserId>".$this->PxPay_Userid."</PxPayUserId><PxPayKey>".$this->PxPay_Key.
"</PxPayKey><Response>".$result."</Response></ProcessResponse>";
$outputXml = $this->submitXml($inputXml);
$pxresp = new PxPayResponse($outputXml);
return $pxresp;
}
#******************************************************************************
# Actual submission of XML using cURL. Returns output XML
#******************************************************************************
function submitXml($inputXml){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->PxPay_Url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$inputXml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
#set up proxy, this may change depending on ISP, please contact your ISP to get the correct cURL settings
#curl_setopt($ch,CURLOPT_PROXY , "proxy:8080");
#curl_setopt($ch,CURLOPT_PROXYUSERPWD,"username:password");
$outputXml = curl_exec ($ch);
curl_close ($ch);
return $outputXml;
}
}
#******************************************************************************
# Class for PxPay request messages.
#******************************************************************************
class PxPayRequest extends PxPayMessage
{
var $UrlFail,$UrlSuccess;
var $AmountInput;
var $EnableAddBillCard;
var $PxPayUserId;
var $PxPayKey;
var $Opt;
#Constructor
function PxPayRequest(){
$this->PxPayMessage();
}
function setEnableAddBillCard($EnableBillAddCard){
$this->EnableAddBillCard = $EnableBillAddCard;
}
function setUrlFail($UrlFail){
$this->UrlFail = $UrlFail;
}
function setUrlSuccess($UrlSuccess){
$this->UrlSuccess = $UrlSuccess;
}
function setAmountInput($AmountInput){
$this->AmountInput = sprintf("%9.2f",$AmountInput);
}
function setUserId($UserId){
$this->PxPayUserId = $UserId;
}
function setKey($Key){
$this->PxPayKey = $Key;
}
function setOpt($Opt){
$this->Opt = $Opt;
}
#******************************************************************
#Data validation
#******************************************************************
function validData(){
$msg = "";
if($this->TxnType != "Purchase")
if($this->TxnType != "Auth")
$msg = "Invalid TxnType[$this->TxnType]<br>";
if(strlen($this->MerchantReference) > 64)
$msg = "Invalid MerchantReference [$this->MerchantReference]<br>";
if(strlen($this->TxnId) > 16)
$msg = "Invalid TxnId [$this->TxnId]<br>";
if(strlen($this->TxnData1) > 255)
$msg = "Invalid TxnData1 [$this->TxnData1]<br>";
if(strlen($this->TxnData2) > 255)
$msg = "Invalid TxnData2 [$this->TxnData2]<br>";
if(strlen($this->TxnData3) > 255)
$msg = "Invalid TxnData3 [$this->TxnData3]<br>";
if(strlen($this->EmailAddress) > 255)
$msg = "Invalid EmailAddress [$this->EmailAddress]<br>";
if(strlen($this->UrlFail) > 255)
$msg = "Invalid UrlFail [$this->UrlFail]<br>";
if(strlen($this->UrlSuccess) > 255)
$msg = "Invalid UrlSuccess [$this->UrlSuccess]<br>";
if(strlen($this->BillingId) > 32)
$msg = "Invalid BillingId [$this->BillingId]<br>";
if ($msg != "") {
trigger_error($msg,E_USER_ERROR);
return false;
}
return true;
}
}
#******************************************************************************
# Abstract base class for PxPay messages.
# These are messages with certain defined elements, which can be serialized to XML.
#******************************************************************************
class PxPayMessage {
var $TxnType;
var $CurrencyInput;
var $TxnData1;
var $TxnData2;
var $TxnData3;
var $MerchantReference;
var $EmailAddress;
var $BillingId;
var $TxnId;
function PxPayMessage(){
}
function setBillingId($BillingId){
$this->BillingId = $BillingId;
}
function getBillingId(){
return $this->BillingId;
}
function setTxnType($TxnType){
$this->TxnType = $TxnType;
}
function getTxnType(){
return $this->TxnType;
}
function setCurrencyInput($CurrencyInput){
$this->CurrencyInput = $CurrencyInput;
}
function getCurrencyInput(){
return $this->CurrencyInput;
}
function setMerchantReference($MerchantReference){
$this->MerchantReference = $MerchantReference;
}
function getMerchantReference(){
return $this->MerchantReference;
}
function setEmailAddress($EmailAddress){
$this->EmailAddress = $EmailAddress;
}
function getEmailAddress(){
return $this->EmailAddress;
}
function setTxnData1($TxnData1){
$this->TxnData1 = $TxnData1;
}
function getTxnData1(){
return $this->TxnData1;
}
function setTxnData2($TxnData2){
$this->TxnData2 = $TxnData2;
}
function getTxnData2(){
return $this->TxnData2;
}
function getTxnData3(){
return $this->TxnData3;
}
function setTxnData3($TxnData3){
$this->TxnData3 = $TxnData3;
}
function setTxnId( $TxnId)
{
$this->TxnId = $TxnId;
}
function getTxnId(){
return $this->TxnId;
}
function toXml(){
$arr = get_object_vars($this);
$xml = "<GenerateRequest>";
while (list($prop, $val) = each($arr))
$xml .= "<$prop>$val</$prop>" ;
$xml .= "</GenerateRequest>";
return $xml;
}
}
#******************************************************************************
# Class for PxPay response messages.
#******************************************************************************
class PxPayResponse extends PxPayMessage
{
var $Success;
var $AuthCode;
var $CardName;
var $CardHolderName;
var $CardNumber;
var $DateExpiry;
var $ClientInfo;
var $DpsTxnRef;
var $DpsBillingId;
var $AmountSettlement;
var $CurrencySettlement;
var $TxnMac;
var $ResponseText;
function PxPayResponse($xml){
$msg = new MifMessage($xml);
$this->PxPayMessage();
$this->Success = $msg->get_element_text("Success");
$this->setTxnType($msg->get_element_text("TxnType"));
$this->CurrencyInput = $msg->get_element_text("CurrencyInput");
$this->setMerchantReference($msg->get_element_text("MerchantReference"));
$this->setTxnData1($msg->get_element_text("TxnData1"));
$this->setTxnData2($msg->get_element_text("TxnData2"));
$this->setTxnData3($msg->get_element_text("TxnData3"));
$this->AuthCode = $msg->get_element_text("AuthCode");
$this->CardName = $msg->get_element_text("CardName");
$this->CardHolderName = $msg->get_element_text("CardHolderName");
$this->CardNumber = $msg->get_element_text("CardNumber");
$this->DateExpiry = $msg->get_element_text("DateExpiry");
$this->ClientInfo = $msg->get_element_text("ClientInfo");
$this->TxnId = $msg->get_element_text("TxnId");
$this->setEmailAddress($msg->get_element_text("EmailAddress"));
$this->DpsTxnRef = $msg->get_element_text("DpsTxnRef");
$this->BillingId = $msg->get_element_text("BillingId");
$this->DpsBillingId = $msg->get_element_text("DpsBillingId");
$this->AmountSettlement = $msg->get_element_text("AmountSettlement");
$this->CurrencySettlement = $msg->get_element_text("CurrencySettlement");
$this->TxnMac = $msg->get_element_text("TxnMac");
$this->ResponseText = $msg->get_element_text("ResponseText");
}
function getSuccess(){
return $this->Success;
}
function getAuthCode(){
return $this->AuthCode;
}
function getCardName(){
return $this->CardName;
}
function getCardHolderName(){
return $this->CardHolderName;
}
function getCardNumber(){
return $this->CardNumber;
}
function getDateExpiry(){
return $this->DateExpiry;
}
function getClientInfo(){
return $this->ClientInfo;
}
function getDpsTxnRef(){
return $this->DpsTxnRef;
}
function getDpsBillingId(){
return $this->DpsBillingId;
}
function getAmountSettlement(){
return $this->AmountSettlement;
}
function getCurrencySettlement(){
$this->CurrencySettlement;
}
function getTxnMac(){
return $this->TxnMac;
}
function getResponseText(){
return $this->ResponseText;
}
}
?>
Go to the live modx page with the snippet on it and click the pay button and you will see the same error.
Any ideas?