1

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";
  • There are a few ways you can do this. It would be helpful to see your existing code and what you have tried so far in order to recommend a solution that would best fit your setup. – TheGentleman Oct 11 '17 at 18:18
  • Hi @GentlemanMax currently hardcoding it, but I am looking for a way to sort it using a function like javascript equivalent [`Object.keys({object}).sort()`] I updated question with my current implementation. – temiloluwa adesina Oct 11 '17 at 18:33

1 Answers1

1

ksort() in PHP will do something similar to Object.keys({object}).sort() in js. The most notable difference is that ksort() does an "in place" sort of the array.

Assuming that the fields in your code are the only ones you are using, this will do what you are looking for:

$options = array(
    'PBFPubKey' => $pb_key,
    'amount' => $amount_in_naira,
    'customer_email' => $customer_email,
    'customer_firstname' => $customer_firstname,
    'txref' => $txref,
);
ksort($options);
$hashedPayload = '';
foreach($options as $option){
    $hashedPayload += $option;
}

$hashedPayload now contains a string (unhashed) in the correct order for hashing. It looks like you just need to do a sha256 hash of the string at this point. Let me know if you need help with that part.

TheGentleman
  • 2,324
  • 13
  • 17