如何在 php 中获取 rav 参数的 ascii 键排序

How to get the ascii key sort of rave parameters in php

Ravepay 文档 (https://flutterwavedevelopers.readme.io/docs/checksum) 展示了如何使用 Nodejs 散列值,但我在生成 getpaidSetup 键的正确排序顺序时遇到问题,我如何在 php 中做到这一点。

目前这是我的实现:

// 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);
    },
}

更新:

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";

ksort() in PHP 会做类似于 js 中的 Object.keys({object}).sort() 的事情。最显着的区别是 ksort() 对数组进行 "in place" 排序。

假设您的代码中的字段是您正在使用的唯一字段,这将满足您的要求:

$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 现在以正确的散列顺序包含一个字符串(未散列)。看起来你此时只需要对字符串进行 sha256 哈希。如果您需要这部分的帮助,请告诉我。