Skip to content

Instantly share code, notes, and snippets.

@aabir
Created October 28, 2015 08:59
Show Gist options
  • Save aabir/9c7c2a8d9e231660a342 to your computer and use it in GitHub Desktop.
Save aabir/9c7c2a8d9e231660a342 to your computer and use it in GitHub Desktop.
SecurePay XML API working with PHP
<!DOCTYPE html>
<html>
<head>
<title> Test Payment </title>
</head>
<style>
#form-success {border: 1px solid #ccc; background: #f8f8f8; display: none; padding: 10px; width: 320px; }
</style>
<body>
<h3> Payment Form </h3>
<div id="form-success"></div>
<form id="payment_info" method="post">
<input type="hidden" name="purchaseOrderNo" value="ODR1">
<p> <label> Name on Card: </label>
<input type="text" name="name" value="" required>
</p>
<p> <label> Select card type: </label>
<select name="cardDescription" required>
<option value=" "> Select... </option>
<option value="1"> Visa </option>
<option value="2"> Master </option>
</select>
</p>
<p> <label> Amount: </label>
<input type="text" name="amount" value="" required>
</p>
<p> <label> Card No: </label>
<input type="text" name="cardNumber" value="" required>
</p>
<p> <label> CVN (or CVV): </label>
<input type="text" name="cvv" value="" required>
</p>
<p> <label> Expires: </label>
<select name="expire_month" required>
<option value=" "> Select... </option>
<option value="01"> 1 - January </option>
<option value="02"> 2 - February </option>
<option value="03"> 3 - March </option>
<option value="04"> 4 - April </option>
<option value="05"> 5 - May </option>
<option value="06"> 6 - June </option>
<option value="07"> 7 - July </option>
<option value="08"> 8 - August </option>
<option value="09"> 9 - September </option>
<option value="10"> 10 - October </option>
<option value="11"> 11 - November </option>
<option value="12"> 12 - December </option>
</select>
<select name="expire_year" required>
<option value=" "> Select... </option>
<?php $year = date('Y');
for($i=0; $i <= 7; $i++){ ?>
<option value="<?php echo ($year + $i); ?>"> <?php echo ($year + $i); ?> </option>
<?php } ?>
?>
</select>
</p>
<p><input type="submit" value="Submit"> </p>
</form>
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script>
$(function(){
var form = $('#payment_info');
form.on('submit', function(e){
e.preventDefault();
data = form.serialize();
$.ajax({
url: "./process.php",
type: "POST",
dataType: "json",
data: data,
success: function(rt){
if(rt && rt.success == true) {
$("#form-success").text(rt.msg[1][0]).css('display', 'block');
if(rt.msg[1][0] == "Approved"){
$("#payment_info")[0].reset();
}
} else {
$("#form-success").html("Some thing went wrong! ").css('color', 'red');
}
}
});
})
});
</script>
</body>
</html>
<?php
extract($_POST);
$purchaseOrderNo = $_POST['purchaseOrderNo'];
$name = $_POST['name'];
$cardDescription = $_POST['cardDescription'];
$amount = $_POST['amount'];
$cardNumber = $_POST['cardNumber'];
$cvv = $_POST['cvv'];
$expire_month = $_POST['expire_month'];
$expire_year = $_POST['expire_year'];
$expire_year = substr($expire_year, 2); // removing first 2 letter from string
$expiryDate = $expire_month."/".$expire_year;
$cardDescription = ($cardDescription == 1 ? "Visa": "Master");
$amount = $amount.'00';
if(isset($_POST['cardNumber'])) {
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<SecurePayMessage>
<MessageInfo>
<messageID>8af793f9af34bea0cf40f5fb5c630c</messageID>
<messageTimestamp>20041803161306527000+660</messageTimestamp>
<timeoutValue>60</timeoutValue>
<apiVersion>xml-4.2</apiVersion>
</MessageInfo>
<MerchantInfo>
<merchantID>abc0001</merchantID>
<password>abc123</password>
</MerchantInfo>
<RequestType>Payment</RequestType>
<Payment>
<TxnList count="1">
<Txn ID="1">
<txnType>0</txnType>
<txnSource>0</txnSource>
<amount>'.$amount.'</amount>
<currency>AUD</currency>
<purchaseOrderNo>'.$purchaseOrderNo.'</purchaseOrderNo>
<CreditCardInfo>
<cardNumber>'.$cardNumber.'</cardNumber>
<cvv>'.$cvv.'</cvv>
<expiryDate>'.$expiryDate.'</expiryDate>
<cardDescription>'.$cardDescription.'</cardDescription>
</CreditCardInfo>
<DirectEntryInfo>
<accountName>'.$name.'</accountName>
</DirectEntryInfo>
</Txn>
</TxnList>
</Payment>
</SecurePayMessage>';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://test.securepay.com.au/xmlapi/payment");
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE); // Follow redirects
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, $xml);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); // Return the HTTP response from the curl_exec function
$response = curl_exec($curl);
curl_close($curl);
//header('Content-type: text/xml');
//echo $response;
$oXML = new SimpleXMLElement($response);
$responseText[1] = $oXML->Payment[0]->TxnList[0]->Txn[0]->responseText;
$responseText[2] = $oXML->Payment[0]->TxnList[0]->Txn[0]->purchaseOrderNo;
$response = array(
'success' => true,
'msg' => $responseText
);
exit (json_encode($response));
}
@Ineke99
Copy link

Ineke99 commented Feb 14, 2021

Hi there,
I know, 5 years ago you wrote this, but to me it looks like the best example I've come across so far.
I thought I'd try it out and this code crashes on the $(function line with an Error 405 from IIS on the server that needs to run this code.
I have IT remove the WebDAV from the Windows program features. (Googled that that could be a possible cause).
I am obviously lacking something in the IIS handler mappings. Or there is a typo in the code????
Can someone help me out here. SecurePay support just keep on referring me back to the User manual.
Much appreciated if someone could help me out here.

@aabir
Copy link
Author

aabir commented Feb 17, 2021

Hi Ineke99,
I'm not sure if the documentation has changed over this period of time. This is a PHP script, not sure if you can run PHP on IIS server. "$(function()" line is JavaScript Jquery I believe this isn't causing an error. The error maybe from other part of the code. Right now can't help you much without knowing details. Let me know if there is a way to help you out. Thanks.

@Ineke99
Copy link

Ineke99 commented Feb 17, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment