php 在ccavenue中实现状态API

rks48beu  于 2023-09-29  发布在  PHP
关注(0)|答案(3)|浏览(97)

我已经完成ccavenue网关集成其银行审核网关银行要求实现状态API,下面是示例代码。如何实现此功能以共享响应和请求状态?

<?php
error_reporting(0);
$working_key = ''; //Shared by CCAVENUES
$access_code = '';

$merchant_json_data =
array(
    'order_no' => '',
    'reference_no' =>''
);

$merchant_data = json_encode($merchant_json_data);
$encrypted_data = encrypt($merchant_data, $working_key);
$final_data = 'enc_request='.$encrypted_data.'&access_code='.$access_code.'&command=orderStatusTracker&request_type=JSON&response_type=JSON';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://apitest.ccavenue.com/apis/servlet/DoWebTrans");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER,'Content-Type: application/json') ;
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $final_data);
// Get server response ...
$result = curl_exec($ch);
curl_close($ch);
$status = '';
$information = explode('&', $result);

$dataSize = sizeof($information);
for ($i = 0; $i < $dataSize; $i++) {
    $info_value = explode('=', $information[$i]);
    if ($info_value[0] == 'enc_response') {
        $status = decrypt(trim($info_value[1]), $working_key);
        
    }
}

echo 'Status revert is: ' . $status.'<pre>';
$obj = json_decode($status);
print_r($obj);
?>

<?php
//ADD NEW ENCRYPT Function

function encrypt($plainText,$key)
{
    $key = hextobin(md5($key));
    $initVector = pack("C*", 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
    $openMode = openssl_encrypt($plainText, 'AES-128-CBC', $key, OPENSSL_RAW_DATA, $initVector);
    $encryptedText = bin2hex($openMode);
    return $encryptedText;
}

function decrypt($encryptedText,$key)
{
    $key = hextobin(md5($key));
    $initVector = pack("C*", 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
    $encryptedText = hextobin($encryptedText);
    $decryptedText = openssl_decrypt($encryptedText, 'AES-128-CBC', $key, OPENSSL_RAW_DATA, $initVector);
    return $decryptedText;
}
    //*********** Padding Function *********************

function pkcs5_pad ($plainText, $blockSize)
{
    $pad = $blockSize - (strlen($plainText) % $blockSize);
    return $plainText . str_repeat(chr($pad), $pad);
}

    //********** Hexadecimal to Binary function for php 4.0 version ********

function hextobin($hexString) 
{ 
    $length = strlen($hexString); 
    $binString="";   
    $count=0; 
    while($count<$length) 
    {       
        $subString =substr($hexString,$count,2);           
        $packedString = pack("H*",$subString); 
        if ($count==0)
        {
            $binString=$packedString;
        } 
        
        else 
        {
            $binString.=$packedString;
        } 
        
        $count+=2; 
    } 
    return $binString; 
} 
?>

我希望状态API应该给予这种类型的响应:
{“reference_no”:“309001234567”,“order_no”:“abc123”,“order_currncy”:“INR”、“order_amt”:1049.0,“order_date_time”:2020-10-20 17:十四:18.42”,“order_bill_name”:“",“order_bill_address”:“”,“order_bill_zip”:“”,“order_bill_tel”:“”,“order_bill_email”:“",“order_bill_country”:“”,“order_ship_name”:“yang”,“order_ship_address”:“fthfyggg”,“order_ship_country”:“India”,“order_ship_tel”:“185”

yduiuuwa

yduiuuwa1#

非常简单,只需执行statusAPI.php文件(由CCAvenue共享),并输入所需参数。

$working_key = '*****'; //Shared by CCAVENUES
$access_code = '*****'; //Shared by CCAVENUES

$merchant_json_data = array(
   'order_no' => '', //your app's internal order no
   'reference_no' =>'' //ccavenue tracking no
);
4c8rllxm

4c8rllxm2#

你可以使用下面的简单函数和必要的参数。

function processStatusCallback() {
    // Read the callback data from the input stream
    $callbackData = file_get_contents("php://input");
    
    // Parse the JSON data
    $data = json_decode($callbackData, true);
    
    // Verify the authenticity of the callback (assuming secure hash validation)
    $secureHash = '########'; // Replace with your actual secure hash
    
    // Generate hash from the received data
    $generatedHash = hash('sha256', implode('|', $data) . '|' . $secureHash);
    
    // Compare generated hash with received hash
    if ($data['secure_hash'] === $generatedHash) {
        // Hash validation successful, process the payment status
        $orderId = $data['order_id'];
        $paymentStatus = $data['payment_status'];
                
        // Send a response back to CCAvenue
        echo json_encode(['status' => 'success']);
    } else {        
        // Send an error response back to CCAvenue
        echo json_encode(['status' => 'error', 'message' => 'Invalid secure hash']);
    }
}
sg2wtvxw

sg2wtvxw3#

在.net C#中获取ccavenue付款状态非常简单,使用生产环境的live url,使用测试环境的test url。
谢谢
public void CCAvenueStatus(string OrderNo,string TrackingId)
{

string decResponse = "";
        try
        {
            var LiveUrl = "https://api.ccavenue.com/apis/servlet/DoWebTrans?command=orderStatusTracker&version=1.2&request_type=JSON&response_type=JSON&access_code=";

            var TestUrl = "https://apitest.ccavenue.com/apis/servlet/DoWebTrans?command=orderStatusTracker&version=1.2&request_type=JSON&response_type=JSON&access_code=";

            var Key = "Keyxxxxxxxxx";
            var Code = "Codexxxxxxxxx";

            var reqParam = new StatusEncRequest();
            reqParam.order_no = OrderNo;
            reqParam.reference_no = TrackingId;

            string ccaRequest = JsonConvert.SerializeObject(reqParam);
            CCACrypto ccaCrypto = new CCACrypto();
            string strEncRequest = (ccaCrypto.Encrypt(ccaRequest, Key));

            var curl = TestUrl + Code + "&enc_request=" + strEncRequest;
            var client = new RestClient(curl);
            var request = new RestRequest(curl, Method.Post);
            RestResponse restResponse = client.Execute(request);
            var response = restResponse.Content;

            if (!string.IsNullOrEmpty(response))
            {
                NameValueCollection Params = new NameValueCollection();

                string[] segments = response.Split('&');
                foreach (string seg in segments)
                {
                    string[] parts = seg.Split('=');
                    if (parts.Length > 0)
                    {
                        string KeyParam = parts[0].Trim();
                        string Value = parts[1].Trim();
                        Params.Add(KeyParam, Value);
                    }
                }

                string status = string.IsNullOrEmpty(Params["status"]) ? "" : Params["status"];
                string enc_response = string.IsNullOrEmpty(Params["enc_response"]) ? "" : Params["enc_response"];
                string enc_error_code = string.IsNullOrEmpty(Params["enc_error_code"]) ? "" : Params["enc_error_code"];

                if (!string.IsNullOrEmpty(status) && status == "0")
                {
                    decResponse = ccaCrypto.Decrypt(enc_response, Key);
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }

相关问题