Ravepay documentation (https://flutterwavedevelopers.readme.io/docs/checksum) shows how to hash values using Nodejs, but I am having issues generating the correct sort order of my getpaidSetup keys, how can I do that in php.
Currently this is my implementation:
// In php
$amount_in_naira = $_POST['total'] * 400;
$customer_email = $_POST['email'];
$customer_firstname = $_POST['fullname'];
$txref = 'EX' . '_' . get_current_user_id() . '_' . time();
$integity_str = $pb_key . $amount_in_naira . $customer_email . $txref . $sc_key;
$hash = hash( 'sha256', $integity_str );
// JS
var options = {
PBFPubKey: '<?php echo $pb_key; ?>',
amount: <?php echo $amount_in_naira; ?>,
customer_email: '<?php echo $customer_email; ?>',
customer_firstname: '<?php echo $customer_firstname; ?>',
txref: '<?php echo $txref; ?>',
integity_hash: '<?php echo hash( 'sha256', $integity_str ); ?>',
meta: [{ metaname: 'merchant_details', metavalue: '<?php echo $metadata; ?>' }],
onclose: function() {},
callback: function(res) {
console.log(res);
},
}
Update:
From GentlemanMax's answer on ksort, I ran a ksort on an array and compared it to the Javascript equivalent result from
object.keys({payload}).sort()
and results match, see a sample script below showing how to sort by ASCII using Ksort.
$pb_key = "FLWPUBK-7adb6177bd71dd43c2efa3f1229e3b7f-X";
$amount_in_naira = 900;
$customer_email = "user@example.com";
$customer_firstname = "user";
$customer_lastname = "example";
$txref = "MV-1838383-JH";
$pmethod = "both";
$options = array(
"PBFPubKey" => $pb_key,
"amount" => $amount_in_naira,
"customer_email" => $customer_email,
"customer_firstname" => $customer_firstname,
"txref" => $txref,
"payment_method" => $pmethod,
"customer_lastname" => $customer_lastname
);
ksort($options);
var_dump($options);
$hashedPayload = '';
foreach($options as $key => $value){
$hashedPayload .= $value;
}
$hash = hash('sha256', $hashedPayload);
echo "$hashedPayload\n";
echo "$hash";