Last active
September 25, 2024 07:19
-
-
Save nigelheap/47215d6b0c3b0a950c91bcb01a391b35 to your computer and use it in GitHub Desktop.
fingerprint-example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** Template Name: Payment template */ | |
if (! defined('ABSPATH')) { | |
exit; // Exit if accessed directly. | |
} | |
function generateNABTransactFingerprint($vendorName, $paymentReference, $paymentAlert, $products, $merchantTransactionPassword) | |
{ | |
// Sort products alphabetically by name | |
usort($products, function ($a, $b) { | |
return strcmp($a['name'], $b['name']); | |
}); | |
// Prepare unit_names and unit_prices | |
$unitNames = []; | |
$unitPrices = []; | |
foreach ($products as $product) { | |
$unitNames[] = $product['name']; | |
//$unitPrices[] = number_format($product['price'], 2, '.', ''); | |
$unitPrices[] = $product['price']; | |
} | |
// Join unit_names and unit_prices | |
$unitNamesString = implode(',', $unitNames); | |
$unitPricesString = implode(',', $unitPrices); | |
// Prepare the fingerprint text | |
$fingerprintText = implode('|', [ | |
$vendorName, | |
$paymentReference, | |
$paymentAlert, | |
$unitNamesString, | |
$unitPricesString, | |
$merchantTransactionPassword | |
]); | |
// echo $fingerprintText; | |
// Generate the HMAC-SHA256 hash | |
$fingerprint = hash_hmac('sha256', $fingerprintText, $merchantTransactionPassword); | |
return $fingerprint; | |
} | |
$vendorName = 'CQL0010'; // Real: CQL0010 // Finger print test: XYZ0110 | |
$paymentReference = isset($_POST['Parent_Account_Number']) ? $_POST['Parent_Account_Number'] : ''; | |
$total = isset($_POST['Payment_of_fees']) ? $_POST['Payment_of_fees'] : ''; | |
$title = isset($_POST['Title']) && $_POST['Title'] ? $_POST['Title'] : ''; | |
$lastName = isset($_POST['Last_Name']) ? $_POST['Last_Name'] : ''; | |
$firstName = isset($_POST['First_Name']) ? $_POST['First_Name'] : ''; | |
$mobilePhone = isset($_POST['Mobile_Phone']) ? $_POST['Mobile_Phone'] : ''; | |
$emailAddress = isset($_POST['Email_Address']) ? $_POST['Email_Address'] : ''; | |
$paymentAlert = '[email protected]'; | |
$test = false; | |
$merchantTransactionPassword = $test ? 'abcd1234' : 'REPLACEME'; | |
$endpoint = $test ? 'https://demo.transact.nab.com.au/live/hpp/payment' : 'https://transact.nab.com.au/live/hpp/payment'; | |
$action = ''; | |
$products = [ | |
['name' => 'Payment of fees', 'price' => $total], | |
]; | |
$fingerprint = generateNABTransactFingerprint($vendorName, $paymentReference, $paymentAlert, $products, $merchantTransactionPassword); | |
if(!empty($total)){ | |
$action = $endpoint; | |
} | |
get_header(); | |
while (have_posts()) : | |
the_post(); | |
?> | |
<main id="content" <?php post_class('site-main'); ?>> | |
<div class="payment-page-wrapper"> | |
<div class="payment-page-inner"> | |
<form method="post" | |
id="frm-fees" | |
name="frm-fees" | |
action="<?php echo $action; ?>" | |
method="post" | |
style="display:<?php echo !empty($action) ? 'none' : 'block'; ?>"> | |
<h4>Payments – School Fees</h4> | |
<?php //print_r($_POST); ?> | |
<br> | |
<br> | |
<p> | |
<em>Required fields are marked with an asterisk *</em> | |
</p> | |
<p> | |
<label for="payer-title">*Title</label> | |
<select id="payer-title" name="Title"> | |
<option value="Mr" <?php echo $title === 'Mr' ? 'selected' : ''; ?>>Mr</option> | |
<option value="Mrs" <?php echo $title === 'Mrs' ? 'selected' : ''; ?>>Mrs</option> | |
<option value="Ms" <?php echo $title === 'Ms' ? 'selected' : ''; ?>>Ms</option> | |
<option value="Dr" <?php echo $title === 'Dr' ? 'selected' : ''; ?>>Dr</option> | |
</select> | |
<input type="hidden" name="information_fields" value="Title" /> | |
</p> | |
<p> | |
<label for="payer-surname">*Last Name</label> | |
<input type="text" | |
id="payer-surname" | |
name="Last Name" | |
maxlength="128" | |
value="<?php echo $lastName; ?>" | |
class="text required" /> | |
<input type="hidden" name="information_fields" value="Last Name" /> | |
</p> | |
<p> | |
<label for="payer-firstname">*First Name</label> | |
<input type="text" | |
id="payer-firstname" | |
name="First Name" | |
maxlength="128" | |
s class="text required" | |
value="<?php echo $firstName; ?>" /> | |
<input type="hidden" name="information_fields" value="First Name" /> | |
</p> | |
<p> | |
<label for="payer-mobile">*Mobile Phone Number</label> | |
<input type="text" | |
id="payer-mobile" | |
name="Mobile Phone" | |
maxlength="64" | |
value="<?php echo $mobilePhone; ?>" | |
class="text required" /> | |
<input type="hidden" name="information_fields" value="Mobile Phone" /> | |
</p> | |
<p> | |
<label for="payer-email">*Email Address</label> | |
<input type="text" | |
id="payer-email" | |
name="Email Address" | |
maxlength="128" | |
value="<?php echo $emailAddress; ?>" | |
class="text required email" /> | |
<input type="hidden" name="information_fields" value="Email Address" /> | |
</p> | |
<p> | |
<label>*Parent Account Number</label> | |
<input type="text" | |
id="parent-acc-num" | |
name="Parent Account Number" | |
maxlength="6" | |
minlength="6" | |
value="<?php echo $paymentReference; ?>" | |
class="text required minlength maxlength" /> | |
<input type="hidden" name="information_fields" value="Parent Account Number" /> | |
</p> | |
<br><br> | |
<h4>Amount</h4> | |
<p> | |
<label for="total">*Total Amount</label> | |
<input type="text" id="total" name="Payment of fees" maxlength="16" value="<?php echo $total; ?>" class="text required dollar" /> | |
</p> | |
<p class="submit"> | |
<input type="submit" id="btnSubmit" name="btnSubmit" value="Submit" class="btn-submit" /> | |
</p> | |
<input type="hidden" name="payment_reference" value="<?php echo $paymentReference; ?>"> | |
<input type="hidden" name="vendor_name" value="<?php echo $vendorName; ?>"> | |
<input type="hidden" name="payment_alert" value="<?php echo $paymentAlert; ?>"> | |
<input type="hidden" name="print_zero_qty" value="FALSE"> | |
<input type="hidden" name="fingerprint" value="<?php echo $fingerprint; ?>"> | |
</form> | |
<?php if(!empty($action)): ?> | |
Please wait, redirecting you to the payment page... | |
<script> | |
document.forms['frm-fees'].submit(); | |
</script> | |
<?php endif; ?> | |
</div> | |
</div> | |
</main> | |
<?php | |
endwhile; | |
get_footer(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment